From a7a25f92fe4517d285b24da9f48cc8b0dacb63c8 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 21 Apr 2022 10:46:21 +0200 Subject: [PATCH] Type random-boolean-table. --- functions.rkt | 36 ++++++++++++++++++++---------------- scribblings/functions.scrbl | 8 ++++++++ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/functions.rkt b/functions.rkt index 07385df..7a062c1 100644 --- a/functions.rkt +++ b/functions.rkt @@ -24,7 +24,8 @@ 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-functions - enumerate-boolean-functions/pv enumerate-boolean-functions/list) + enumerate-boolean-functions/pv enumerate-boolean-functions/list + random-boolean-table) (module+ test (require typed/rackunit)) @@ -311,6 +312,22 @@ (check-false (bool-f1/list '(#f))) (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+ test (require rackunit)) @@ -394,7 +411,8 @@ tabulate*/pv/01 tabulate/pv/01 table->function/list table->function table->function/pv 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) [tabulate tabulate/untyped] @@ -406,7 +424,6 @@ [struct tbf ((weights (vectorof number?)) (threshold number?))]) ;; Functions (contract-out - [random-boolean-table (-> number? (listof (*list/c boolean? boolean?)))] [random-boolean-function (-> number? procedure?)] [random-boolean-function/list (-> number? procedure?)] [tbf-w (-> tbf? (vectorof number?))] @@ -435,19 +452,6 @@ ;;; 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. (define random-boolean-function (compose table->function random-boolean-table)) diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index afa64af..cea242e 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -340,6 +340,14 @@ their arguments as a single list. @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[#:tag "fuctions/untyped"]{Untyped definitions}