Type random-boolean-table.

This commit is contained in:
Sergiu Ivanov 2022-04-21 10:46:21 +02:00
parent 52bf1b2f58
commit a7a25f92fe
2 changed files with 28 additions and 16 deletions

View file

@ -24,7 +24,8 @@
tabulate*/pv/boolean tabulate/pv/boolean tabulate*/pv/01 tabulate/pv/01 tabulate*/pv/boolean tabulate/pv/boolean tabulate*/pv/01 tabulate/pv/01
table->function/list table->function table->function/pv table->function/list table->function table->function/pv
enumerate-boolean-tables enumerate-boolean-functions enumerate-boolean-tables enumerate-boolean-functions
enumerate-boolean-functions/pv enumerate-boolean-functions/list) enumerate-boolean-functions/pv enumerate-boolean-functions/list
random-boolean-table)
(module+ test (module+ test
(require typed/rackunit)) (require typed/rackunit))
@ -311,6 +312,22 @@
(check-false (bool-f1/list '(#f))) (check-false (bool-f1/list '(#f)))
(check-false (bool-f1/list '(#t))))) (check-false (bool-f1/list '(#t)))))
(: random-boolean-table (-> Integer (Listof (Listof Boolean))))
(define (random-boolean-table n)
(define ins (boolean-power n))
(define outs (stream-take (in-random 2) (cast (expt 2 n) Nonnegative-Integer)))
(for/list ([i ins] [o outs])
(append i (list (if (= o 1) #t #f)))))
(module+ test
(test-case "random-boolean-table"
(random-seed 1)
(check-equal? (random-boolean-table 2)
'((#f #f #t)
(#f #t #t)
(#t #f #f)
(#t #t #t)))))
(module untyped racket (module untyped racket
(module+ test (module+ test
(require rackunit)) (require rackunit))
@ -394,7 +411,8 @@
tabulate*/pv/01 tabulate/pv/01 tabulate*/pv/01 tabulate/pv/01
table->function/list table->function table->function/pv table->function/list table->function table->function/pv
enumerate-boolean-tables enumerate-boolean-functions enumerate-boolean-tables enumerate-boolean-functions
enumerate-boolean-functions/pv enumerate-boolean-functions/list) enumerate-boolean-functions/pv enumerate-boolean-functions/list
random-boolean-table)
(require (rename-in (submod 'typed untyped) (require (rename-in (submod 'typed untyped)
[tabulate tabulate/untyped] [tabulate tabulate/untyped]
@ -406,7 +424,6 @@
[struct tbf ((weights (vectorof number?)) (threshold number?))]) [struct tbf ((weights (vectorof number?)) (threshold number?))])
;; Functions ;; Functions
(contract-out (contract-out
[random-boolean-table (-> number? (listof (*list/c boolean? boolean?)))]
[random-boolean-function (-> number? procedure?)] [random-boolean-function (-> number? procedure?)]
[random-boolean-function/list (-> number? procedure?)] [random-boolean-function/list (-> number? procedure?)]
[tbf-w (-> tbf? (vectorof number?))] [tbf-w (-> tbf? (vectorof number?))]
@ -435,19 +452,6 @@
;;; Random functions ;;; Random functions
;;; ================ ;;; ================
;;; Generates a random truth table for a Boolean function of arity n.
(define (random-boolean-table n)
(define/match (num->bool x) [(0) #f] [(1) #t])
(define inputs (boolean-power n))
(define outputs (stream-take (in-random 2) (expt 2 n)))
(for/list ([i inputs] [o outputs])
(append i (list (num->bool o)))))
(module+ test
(test-case "random-boolean-table"
(random-seed 0)
(check-equal? (random-boolean-table 2) '((#f #f #t) (#f #t #t) (#t #f #f) (#t #t #f)))))
;;; Generates a random Boolean function of arity n. ;;; Generates a random Boolean function of arity n.
(define random-boolean-function (compose table->function random-boolean-table)) (define random-boolean-function (compose table->function random-boolean-table))

View file

@ -340,6 +340,14 @@ their arguments as a single list.
@section{Random functions} @section{Random functions}
@defproc[(random-boolean-table [n Integer]) (Listof (Listof Boolean))]{
Generates a random truth table for a Boolean function of arity @racket[n].
@examples[#:eval functions-evaluator
(random-boolean-table 2)
]}
@section{Threshold Boolean functions} @section{Threshold Boolean functions}
@section[#:tag "fuctions/untyped"]{Untyped definitions} @section[#:tag "fuctions/untyped"]{Untyped definitions}