From cbede999df8edd29c1d9d6c4b5d17eef1a91e866 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 21 Apr 2022 15:00:24 +0200 Subject: [PATCH] Type apply-tbf/boolean. --- functions.rkt | 26 ++++++++++++-------------- scribblings/functions.scrbl | 10 ++++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/functions.rkt b/functions.rkt index 1e7e79c..b4ecc38 100644 --- a/functions.rkt +++ b/functions.rkt @@ -27,7 +27,7 @@ enumerate-boolean-functions/pv enumerate-boolean-functions/list random-boolean-table random-boolean-function random-boolean-function/list - (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf) + (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean) (module+ test (require typed/rackunit)) @@ -384,6 +384,16 @@ (check-equal? (tabulate/pv/01 2 (pvλ (x y) (apply-tbf f1 (vector x y)))) '((0 0 0) (0 1 0) (1 0 1) (1 1 0))))) + (: apply-tbf/boolean (-> tbf (Vectorof Boolean) Boolean)) + (define (apply-tbf/boolean tbf inputs) + (01->boolean (apply-tbf tbf (boolean->01/vector inputs)))) + + (module+ test + (test-case "apply-tbf/boolean" + (define f1 (tbf #(2 -2) 1)) + (check-equal? (tabulate/pv/boolean 2 (pvλ (x y) (apply-tbf/boolean f1 (vector x y)))) + '((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f))))) + (module untyped racket (module+ test (require rackunit)) @@ -470,7 +480,7 @@ enumerate-boolean-functions/pv enumerate-boolean-functions/list random-boolean-table random-boolean-function random-boolean-function/list - (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf) + (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] @@ -479,7 +489,6 @@ (provide ;; Functions (contract-out - [apply-tbf/boolean (-> tbf? (vectorof boolean?) boolean?)] [list->tbf (-> (cons/c number? (cons/c number? (listof number?))) tbf?)] [lists->tbfs (-> (listof (listof number?)) (listof tbf?))] [read-org-tbfs (->* (string?) (#:headers boolean?) (listof tbf?))] @@ -501,17 +510,6 @@ ;;; Threshold Boolean functions ;;; =========================== -;;; Like apply-tbf, but takes Boolean values as inputs and outputs a -;;; boolean value. -(define (apply-tbf/boolean tbf inputs) - (01->boolean (apply-tbf tbf (vector-map any->01 inputs)))) - -(module+ test - (test-case "apply-tbf/boolean" - (define f1 (tbf #(2 -2) 1)) - (check-equal? (tabulate/boolean (λ (x y) (apply-tbf/boolean f1 (vector x y)))) - '((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f))))) - ;;; Converts a list of numbers to a TBF. The last element of the list ;;; is taken to be the threshold, while the other elements are taken ;;; to be the weights. diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 1a11c42..6c0bfa4 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -405,6 +405,16 @@ above the threshold, the function is 1, otherwise it is 0. (tabulate/pv/01 2 (pvλ (x y) (apply-tbf simple-tbf (vector x y)))) ]} +@defproc[(apply-tbf/boolean [t tbf] [inputs (Vectorof Boolean)]) Boolean]{ + +Like @racket[apply-tbf], but takes Boolean values as inputs and outputs +a Boolean value. + +@examples[#:eval functions-evaluator +(define simple-tbf (tbf #(2 -2) 1)) +(tabulate/pv/boolean 2 (pvλ (x y) (apply-tbf/boolean simple-tbf (vector x y)))) +]} + @section[#:tag "fuctions/untyped"]{Untyped definitions} @defmodule[(submod dds/functions typed untyped)]