From 1b22ba4a7eb54b6e82c741dc9d01f294c8795fde Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 9 Apr 2022 02:03:51 +0200 Subject: [PATCH] Add tabulate/pv/boolean and tabulate*/pv/boolean. --- functions.rkt | 26 ++++++++++++++++++++-- scribblings/functions.scrbl | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/functions.rkt b/functions.rkt index e4ab726..4dabc28 100644 --- a/functions.rkt +++ b/functions.rkt @@ -17,7 +17,8 @@ (provide pseudovariadic-lambda pvλ pseudovariadic-define pvdefine - tabulate* tabulate*/strict tabulate*/pv tabulate tabulate/strict tabulate/pv) + tabulate* tabulate*/strict tabulate*/pv tabulate tabulate/strict tabulate/pv + tabulate*/pv/boolean tabulate/pv/boolean) (module+ test (require typed/rackunit)) @@ -147,6 +148,26 @@ (check-equal? (tabulate/pv (pvλ (x y) (and x y)) '((#f #t) (#f #t))) '((#f #f #f) (#f #t #f) (#t #f #f) (#t #t #t))))) + (: tabulate/pv/boolean (-> Integer (-> Boolean * Boolean) (Listof (Listof Boolean)))) + (define (tabulate/pv/boolean arity func) + (tabulate/pv func (make-list arity '(#f #t)))) + + (module+ test + (test-case "tabulate/pv/boolean" + (check-equal? (tabulate/pv/boolean 2 (pvλ (x y) (and x y))) + '((#f #f #f) (#f #t #f) (#t #f #f) (#t #t #t))))) + + (: tabulate*/pv/boolean (-> Integer (Listof (-> Boolean * Boolean)) + (Listof (Listof Boolean)))) + (define (tabulate*/pv/boolean arity funcs) + (tabulate*/pv funcs (make-list arity '(#f #t)))) + + (module+ test + (test-case "tabulate*/pv/boolean" + (check-equal? (tabulate*/pv/boolean 2 (list (pvλ (x y) (and x y)) + (pvλ (x y) (or x y)))) + '((#f #f #f #f) (#f #t #f #t) (#t #f #f #t) (#t #t #t #t))))) + (module untyped racket (module+ test (require rackunit)) @@ -207,7 +228,8 @@ (provide pseudovariadic-lambda pvλ pseudovariadic-define pvdefine tabulate* tabulate*/strict tabulate*/pv - tabulate tabulate/strict tabulate/pv) + tabulate tabulate/strict tabulate/pv + tabulate*/pv/boolean tabulate/pv/boolean) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 4f4b750..aeb8636 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -166,6 +166,49 @@ are @seclink["pseudovariadic"]{pseudovariadic}. '((#f #t) (#f #t))) ]} +@defproc[(tabulate/pv/boolean [arity Integer] [func (-> Boolean * Boolean)]) + (Listof (Listof Boolean))]{ + +Like @racket[tabulate/pv], but assumes the domains of all variables of the +function are Boolean. The arity of @racket[func] must be explicitly supplied. + +@examples[#:eval functions-evaluator +(tabulate/pv/boolean 2 (pvλ (x y) (and x y))) +] + +Explicitly supplying the arity is necessary because the actual arity of +a pseudovariadic function cannot be determined programmatically. Note that +@racket[tabulate] can be applied directly to a function, but the type of +@racket[tabulate] requires a cast is required the domains argument +@racket[doms]. + +@examples[#:eval functions-evaluator +(tabulate (λ (x y) (and x y)) + (cast (make-list 2 '(#f #t)) + (List (Listof Boolean) (Listof Boolean)))) +] + +This cast is what makes it necessary to resort to pseudovariadic functions and +explicit @racket[arity] to be able to write a type for +@racket[tabulate/pv/boolean]. + +See also @secref{fuctions/untyped} for simpler, but untyped version of +this function. + +} + +@defproc[(tabulate*/pv/boolean [arity Integer] + [func (Listof (-> Boolean * Boolean))]) + (Listof (Listof Boolean))]{ + +Like @racket[tabulate/pv/boolean], but takes a list of functions of the +same arity. + +@examples[#:eval functions-evaluator +(tabulate*/pv/boolean 2 (list (pvλ (x y) (and x y)) + (pvλ (x y) (or x y)))) +]} + @section{Constructing functions} @section{Random functions}