diff --git a/functions.rkt b/functions.rkt index 97dcdeb..56f3110 100644 --- a/functions.rkt +++ b/functions.rkt @@ -28,7 +28,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 apply-tbf/boolean + (struct-out tbf) TBF tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean list->tbf lists->tbfs read-org-tbfs tbf-tabulate* tbf-tabulate tbf-tabulate*/boolean sbf? sbf list->sbf read-org-sbfs) @@ -434,7 +434,8 @@ (check-false (random-bool-f/list '(#t #f))) (check-true (random-bool-f/list '(#t #t))))) -(struct tbf ([weights : (Vectorof Real)] [threshold : Real]) #:transparent) +(struct tbf ([weights : (Vectorof Real)] [threshold : Real]) + #:transparent #:type-name TBF) (define tbf-w tbf-weights) (define tbf-θ tbf-threshold) @@ -446,7 +447,7 @@ (test-case "boolean->01/vector" (check-equal? (boolean->01/vector #(#t #f #f)) #(1 0 0)))) -(: apply-tbf (-> tbf (Vectorof (U Zero One)) (U Zero One))) +(: apply-tbf (-> TBF (Vectorof (U Zero One)) (U Zero One))) (define (apply-tbf tbf inputs) (any->01 (> @@ -462,7 +463,7 @@ (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)) +(: apply-tbf/boolean (-> TBF (Vectorof Boolean) Boolean)) (define (apply-tbf/boolean tbf inputs) (01->boolean (apply-tbf tbf (boolean->01/vector inputs)))) @@ -472,7 +473,7 @@ (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))))) -(: list->tbf (-> (Listof Real) tbf)) +(: list->tbf (-> (Listof Real) TBF)) (define (list->tbf lst) (define-values (w θ) (split-at-right lst 1)) (tbf (list->vector w) (car θ))) @@ -481,7 +482,7 @@ (test-case "list->tbf" (check-equal? (list->tbf '(1 2 3)) (tbf #(1 2) 3)))) -(: lists->tbfs (-> (Listof (Listof Real)) (Listof tbf))) +(: lists->tbfs (-> (Listof (Listof Real)) (Listof TBF))) (define (lists->tbfs lsts) (map list->tbf lsts)) @@ -490,7 +491,7 @@ (check-equal? (lists->tbfs '((1 2 3) (2 3 4))) (list (tbf '#(1 2) 3) (tbf '#(2 3) 4))))) -(: read-org-tbfs (->* (String) (#:headers Boolean) (Listof tbf))) +(: read-org-tbfs (->* (String) (#:headers Boolean) (Listof TBF))) (define (read-org-tbfs str #:headers [headers #f]) (define sexp (assert-type (read-org-sexp str) (Listof Any))) (define sexp-clean (cond [headers (cdr sexp)] [else sexp])) @@ -501,7 +502,7 @@ (check-equal? (read-org-tbfs "((1 2 1) (1 0 1))") (list (tbf '#(1 2) 1) (tbf '#(1 0) 1))))) -(: tbf-tabulate* (-> (Listof tbf) (Listof (Listof (U Zero One))))) +(: tbf-tabulate* (-> (Listof TBF) (Listof (Listof (U Zero One))))) (define (tbf-tabulate* tbfs) (define funcs (for/list ([tbf tbfs]) : (Listof (-> (Listof (U Zero One)) (U Zero One))) @@ -515,7 +516,7 @@ (check-equal? (tbf-tabulate* (list (tbf #(2 2) 1) (tbf #(1 1) 1))) '((0 0 0 0) (0 1 1 0) (1 0 1 0) (1 1 1 1))))) -(: tbf-tabulate (-> tbf (Listof (Listof (U Zero One))))) +(: tbf-tabulate (-> TBF (Listof (Listof (U Zero One))))) (define (tbf-tabulate t) (tbf-tabulate* (list t))) @@ -524,7 +525,7 @@ (check-equal? (tbf-tabulate (tbf #(1 2) 1)) '((0 0 0) (0 1 1) (1 0 0) (1 1 1))))) -(: tbf-tabulate*/boolean (-> (Listof tbf) (Listof (Listof Boolean)))) +(: tbf-tabulate*/boolean (-> (Listof TBF) (Listof (Listof Boolean)))) (define (tbf-tabulate*/boolean tbfs) (define funcs (for/list ([tbf tbfs]) : (Listof (-> (Listof Boolean) Boolean)) @@ -538,7 +539,7 @@ (check-equal? (tbf-tabulate*/boolean (list (tbf #(1 2) 1))) '((#f #f #f) (#f #t #t) (#t #f #f) (#t #t #t))))) -(: sbf? (-> tbf Boolean)) +(: sbf? (-> TBF Boolean)) (define (sbf? t) (= 0 (tbf-θ t))) @@ -547,7 +548,7 @@ (check-false (sbf? (tbf #(1 2) 3))) (check-true (sbf? (tbf #(1 2) 0))))) -(: sbf (-> (Vectorof Real) tbf)) +(: sbf (-> (Vectorof Real) TBF)) (define (sbf w) (tbf w 0)) @@ -555,14 +556,14 @@ (test-case "sbf" (check-equal? (sbf #(1 -1)) (tbf '#(1 -1) 0)))) -(: list->sbf (-> (Listof Real) tbf)) +(: list->sbf (-> (Listof Real) TBF)) (define (list->sbf lst) (sbf (list->vector lst))) (module+ test (test-case "list->sbf" (check-equal? (list->sbf '(1 -1)) (tbf '#(1 -1) 0)))) -(: read-org-sbfs (->* (String) (#:headers Boolean) (Listof tbf))) +(: read-org-sbfs (->* (String) (#:headers Boolean) (Listof TBF))) (define (read-org-sbfs str #:headers [headers #f]) (define sexp (assert-type (read-org-sexp str) (Listof Any))) (define sexp-clean (cond [headers (cdr sexp)] [else sexp])) diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 84690aa..2478ac8 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -453,10 +453,20 @@ a list of arguments. A threshold Boolean function (TBF) is a pair @tt{(w, θ)}, where @tt{w} is a vector of weights and @tt{θ} is the threshold. +Instances of @racket[tbf] have the type @racket[TBF]. + } -@deftogether[(@defproc[(tbf-w [t tbf]) (Vectorof Real)] - @defproc[(tbf-θ [t tbf]) Real])]{ +@defidform[TBF]{ + +The type of the instances of @racket[tbf]: + +@ex[ +(tbf #(1 2) 3) +]} + +@deftogether[(@defproc[(tbf-w [t TBF]) (Vectorof Real)] + @defproc[(tbf-θ [t TBF]) Real])]{ Shortcuts for @racket[tbf-weights] and @racket[tbf-threshold]. @@ -471,7 +481,7 @@ Converts a Boolean vector to a vector of zeros and ones. (boolean->01/vector #(#t #f #f)) ]} -@defproc[(apply-tbf [t tbf] [inputs (Vectorof (U Zero One))]) (U Zero One)]{ +@defproc[(apply-tbf [t TBF] [inputs (Vectorof (U Zero One))]) (U Zero One)]{ Applies the TBF to its inputs. @@ -484,7 +494,7 @@ 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]{ +@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. @@ -494,7 +504,7 @@ a Boolean value. (tabulate/pv/boolean 2 (pvλ (x y) (apply-tbf/boolean simple-tbf (vector x y)))) ]} -@defproc[(list->tbf [lst (Listof Real)]) tbf]{ +@defproc[(list->tbf [lst (Listof Real)]) TBF]{ 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. @@ -503,7 +513,7 @@ be the threshold, while the other elements are taken to be the weights. (list->tbf '(1 2 3)) ]} -@defproc[(lists->tbfs [lsts (Listof (Listof Real))]) (Listof tbf)]{ +@defproc[(lists->tbfs [lsts (Listof (Listof Real))]) (Listof TBF)]{ Converts multiple lists of numbers to a list of TBFs. @@ -515,7 +525,7 @@ The main use is for reading TBFs from Org-mode tables read by ]} @defproc[(read-org-tbfs [str String] [#:headers headers Boolean #f]) - (Listof tbf)]{ + (Listof TBF)]{ Reads a list of TBF from an Org-mode string containing a sexp, containing a list of lists of numbers. If headers is @racket[#t], drops the first list, @@ -528,7 +538,7 @@ The input is typically what @racket[read-org-sexp] reads. (read-org-tbfs "((x y f) (1 2 1) (1 0 1))" #:headers #t) ]} -@defproc[(tbf-tabulate* [tbfs (Listof tbf)]) +@defproc[(tbf-tabulate* [tbfs (Listof TBF)]) (Listof (Listof (U Zero One)))]{ Tabulates a list of TBFs. @@ -544,7 +554,7 @@ TBF in the list. (tbf-tabulate* (list (tbf #(2 2) 1) (tbf #(1 1) 1))) ]} -@defproc[(tbf-tabulate [t tbf]) +@defproc[(tbf-tabulate [t TBF]) (Listof (Listof (U Zero One)))]{ Tabulates a single TBF. @@ -553,7 +563,7 @@ Tabulates a single TBF. (tbf-tabulate (tbf #(1 2) 1)) ]} -@defproc[(tbf-tabulate*/boolean [tbfs (Listof tbf)]) +@defproc[(tbf-tabulate*/boolean [tbfs (Listof TBF)]) (Listof (Listof Boolean))]{ Tabulates a list of TBFs like @racket[tbf-tabulate*], but uses Boolean values @@ -566,7 +576,7 @@ TBF in the list. (tbf-tabulate*/boolean (list (tbf #(1 2) 1))) ]} -@defproc[(sbf? [t tbf]) Boolean]{ +@defproc[(sbf? [t TBF]) Boolean]{ A sign Boolean function (SBF) is a TBF whose threshold is 0. @@ -575,7 +585,7 @@ A sign Boolean function (SBF) is a TBF whose threshold is 0. (sbf? (tbf #(1 2) 0)) ]} -@defproc[(sbf [w (Vectorof Real)]) tbf]{ +@defproc[(sbf [w (Vectorof Real)]) TBF]{ Creates a TBF which is an SBF from a vector of weights. @@ -584,7 +594,7 @@ Creates a TBF which is an SBF from a vector of weights. ]} @defproc[(list->sbf [lst (Listof Real)]) - tbf]{ + TBF]{ Converts a list of numbers to an SBF. The elements of the list are taken to be the weights of the SBF. @@ -594,7 +604,7 @@ the weights of the SBF. ]} @defproc[(read-org-sbfs [str String] [#:headers headers Boolean #f]) - (Listof tbf)]{ + (Listof TBF)]{ Reads a list of SBF from an Org-mode string containing a sexp, containing a list of lists of numbers. If headers is @racket[#t], drops the first list,