From 0e9a974965f4dddb5eede9dfa30c445a0ac47e88 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Fri, 22 Apr 2022 16:20:25 +0200 Subject: [PATCH] Add tabulate*/list/boolean tabulate/list/boolean tabulate*/list/01 tabulate/list/01. --- functions.rkt | 49 +++++++++++++++++++++++++++++++++++++ scribblings/functions.scrbl | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/functions.rkt b/functions.rkt index 24d5f01..8fad63b 100644 --- a/functions.rkt +++ b/functions.rkt @@ -23,6 +23,7 @@ tabulate* tabulate*/strict tabulate*/pv tabulate tabulate/strict tabulate/pv tabulate*/pv/boolean tabulate/pv/boolean tabulate*/pv/01 tabulate/pv/01 tabulate*/list tabulate/list + tabulate*/list/boolean tabulate/list/boolean tabulate*/list/01 tabulate/list/01 table->function/list table->function table->function/pv enumerate-boolean-tables enumerate-boolean-functions enumerate-boolean-functions/pv enumerate-boolean-functions/list @@ -229,6 +230,53 @@ ,(pvλ (x y) (cast (max x y) (U Zero One))))) '((0 0 0 0) (0 1 0 1) (1 0 0 1) (1 1 1 1))))) + (: tabulate/list/boolean (-> Positive-Integer (-> (Listof Boolean) Boolean) + (Listof (Listof Boolean)))) + (define (tabulate/list/boolean arity func) + (tabulate/list func (make-list arity '(#f #t)))) + + (module+ test + (test-case "tabulate/list/boolean" + (check-equal? (tabulate/list/boolean 2 (λ (xs) (and (car xs) (cadr xs)))) + '((#f #f #f) (#f #t #f) (#t #f #f) (#t #t #t))))) + + (: tabulate*/list/boolean (-> Positive-Integer (Listof (-> (Listof Boolean) Boolean)) + (Listof (Listof Boolean)))) + (define (tabulate*/list/boolean arity funcs) + (tabulate*/list funcs (make-list arity '(#f #t)))) + + (module+ test + (test-case "tabulate*/list/boolean" + (check-equal? + (tabulate*/list/boolean 2 (list (λ (xs) (and (car xs) (cadr xs))) + (λ (xs) (or (car xs) (cadr xs))))) + '((#f #f #f #f) (#f #t #f #t) (#t #f #f #t) (#t #t #t #t))))) + + (: tabulate/list/01 (-> Positive-Integer (-> (Listof (U Zero One)) (U Zero One)) + (Listof (Listof (U Zero One))))) + (define (tabulate/list/01 arity func) + (tabulate/list func (make-list arity '(0 1)))) + + (module+ test + (test-case "tabulate/list/01" + (check-equal? + (tabulate/list/01 2 (λ (xs) + (cast (modulo (+ (car xs) (cadr xs)) 2) (U Zero One)))) + '((0 0 0) (0 1 1) (1 0 1) (1 1 0))))) + + (: tabulate*/list/01 (-> Positive-Integer (Listof (-> (Listof (U Zero One)) (U Zero One))) + (Listof (Listof (U Zero One))))) + (define (tabulate*/list/01 arity funcs) + (tabulate*/list funcs (make-list arity '(0 1)))) + + (module+ test + (test-case "tabulate*/list/01" + (check-equal? (tabulate*/list/01 + 2 + `(,(λ (xs) (cast (min (car xs) (cadr xs)) (U Zero One))) + ,(λ (xs) (cast (max (car xs) (cadr xs)) (U Zero One))))) + '((0 0 0 0) (0 1 0 1) (1 0 0 1) (1 1 1 1))))) + (: table->function/list (All (a) (-> (Listof (Listof a)) (-> (Listof a) a)))) (define (table->function/list table) @@ -535,6 +583,7 @@ tabulate*/pv/boolean tabulate/pv/boolean tabulate*/pv/01 tabulate/pv/01 tabulate*/list tabulate/list + tabulate*/list/boolean tabulate/list/boolean tabulate*/list/01 tabulate/list/01 table->function/list table->function table->function/pv enumerate-boolean-tables enumerate-boolean-functions enumerate-boolean-functions/pv enumerate-boolean-functions/list diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 0c26fae..018f26b 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -266,6 +266,55 @@ Like @racket[tabulate/pv/01], but takes a list of functions of the same arity. ,(pvλ (x y) (cast (max x y) (U Zero One))))) ]} +@defproc[(tabulate/list/boolean [arity Positive-Integer] + [func (-> (Listof Boolean) Boolean)]) + (Listof (Listof Boolean))]{ + +Like @racket[tabulate/list], but assumes the domains of all variables of the +function are Boolean. + +@examples[#:eval functions-evaluator +(tabulate/list/boolean 2 (λ (xs) (and (car xs) (cadr xs)))) +]} + +@defproc[(tabulate*/list/boolean [arity Positive-Integer] + [funcs (Listof (-> (Listof Boolean) Boolean))]) + (Listof (Listof Boolean))]{ + +Like @racket[tabulate*/list], but assumes the domains of all variables of the +function are Boolean. + +@examples[#:eval functions-evaluator +(tabulate*/list/boolean 2 (list (λ (xs) (and (car xs) (cadr xs))) + (λ (xs) (or (car xs) (cadr xs))))) +]} + +@defproc[(tabulate/list/01 [arity Positive-Integer] + [func (-> (Listof (U Zero One)) (U Zero One))]) + (Listof (Listof (U Zero One)))]{ + +Like @racket[tabulate/list], but assumes the domains of all variables of the +function are @tt{{0,1}}. + +@examples[#:eval functions-evaluator +(tabulate/list/01 2 (λ (xs) + (cast (modulo (+ (car xs) (cadr xs)) 2) (U Zero One)))) +]} + +@defproc[(tabulate*/list/01 [arity Positive-Integer] + [funcs (Listof (-> (Listof (U Zero One)) (U Zero One)))]) + (Listof (Listof (U Zero One)))]{ + +Like @racket[tabulate*/list], but assumes the domains of all variables of the +function are @tt{{0,1}}. + +@examples[#:eval functions-evaluator +(tabulate*/list/01 + 2 + `(,(λ (xs) (cast (min (car xs) (cadr xs)) (U Zero One))) + ,(λ (xs) (cast (max (car xs) (cadr xs)) (U Zero One))))) +]} + @section{Constructing functions} @defproc[(table->function/list [table (Listof (Listof a))])