Type enumerate-boolean-functions.

This commit is contained in:
Sergiu Ivanov 2022-04-18 23:26:52 +02:00
parent fa63875022
commit a4a6604ecd
2 changed files with 26 additions and 15 deletions

View file

@ -15,12 +15,15 @@
(only-in typed/racket/unsafe unsafe-provide)
(for-syntax syntax/parse))
(require/typed racket/stream
[stream-map (All (a b) (-> (-> a b) (Sequenceof a) (Sequenceof b)))])
(provide
pseudovariadic-lambda pvλ pseudovariadic-define pvdefine
tabulate* tabulate*/strict tabulate*/pv tabulate tabulate/strict tabulate/pv
tabulate*/pv/boolean tabulate/pv/boolean tabulate*/pv/01 tabulate/pv/01
table->function/list table->function table->function/pv
enumerate-boolean-tables)
enumerate-boolean-tables enumerate-boolean-functions)
(module+ test
(require typed/rackunit))
@ -275,6 +278,16 @@
((#f #f #t) (#f #t #t) (#t #f #t) (#t #t #f))
((#f #f #t) (#f #t #t) (#t #f #t) (#t #t #t))))))
(: enumerate-boolean-functions (-> Integer (Sequenceof (-> Boolean * Boolean))))
(define (enumerate-boolean-functions n)
(stream-map (inst table->function Boolean) (enumerate-boolean-tables n)))
(module+ test
(test-case "enumerate-boolean-functions"
(define bool-f1 (stream-first (enumerate-boolean-functions 1)))
(check-false (bool-f1 #f))
(check-false (bool-f1 #t))))
(module untyped racket
(module+ test
(require rackunit))
@ -357,7 +370,7 @@
tabulate*/pv/boolean tabulate/pv/boolean
tabulate*/pv/01 tabulate/pv/01
table->function/list table->function table->function/pv
enumerate-boolean-tables)
enumerate-boolean-tables enumerate-boolean-functions)
(require (rename-in (submod 'typed untyped)
[tabulate tabulate/untyped]
@ -369,7 +382,6 @@
[struct tbf ((weights (vectorof number?)) (threshold number?))])
;; Functions
(contract-out
[enumerate-boolean-functions (-> number? (stream/c procedure?))]
[enumerate-boolean-functions/list (-> number? (stream/c procedure?))]
[random-boolean-table (-> number? (listof (*list/c boolean? boolean?)))]
[random-boolean-function (-> number? procedure?)]
@ -400,18 +412,6 @@
;;; Constructing functions
;;; ======================
;;; Returns the stream of all Boolean functions of a given arity.
;;;
;;; There are 2^(2^n) Boolean functions of arity n.
(define (enumerate-boolean-functions n)
(stream-map table->function (enumerate-boolean-tables n)))
(module+ test
(test-case "enumerate-boolean-tables"
(define f1 (stream-first (enumerate-boolean-functions 1)))
(check-false (f1 #f))
(check-false (f1 #t))))
;;; Returns the stream of all Boolean functions of a given arity. As
;;; different from the functions returned by
;;; enumerate-boolean-functions, the functions take lists of arguments

View file

@ -303,6 +303,17 @@ There are @tt{2^(2^n)} Boolean functions of arity @racket[n].
(stream->list (enumerate-boolean-tables 1))
]}
@defproc[(enumerate-boolean-functions [n Integer])
(Sequenceof (-> Boolean * Boolean))]{
Returns the stream of all Boolean functions of a given arity @racket[n].
There are @tt{2^(2^n)} Boolean functions of arity @racket[n].
@examples[#:eval functions-evaluator
(length (stream->list (enumerate-boolean-functions 2)))
]}
@section{Random functions}
@section{Threshold Boolean functions}