From 212440add128cb1114fcc12f9f820377498a4b5c Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 21 Apr 2022 11:23:36 +0200 Subject: [PATCH] Typed random-boolean-function/list. --- functions.rkt | 33 +++++++++++++++------------------ scribblings/functions.scrbl | 9 +++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/functions.rkt b/functions.rkt index b9cac0f..64313b3 100644 --- a/functions.rkt +++ b/functions.rkt @@ -25,7 +25,7 @@ table->function/list table->function table->function/pv enumerate-boolean-tables enumerate-boolean-functions enumerate-boolean-functions/pv enumerate-boolean-functions/list - random-boolean-table random-boolean-function) + random-boolean-table random-boolean-function random-boolean-function/list) (module+ test (require typed/rackunit)) @@ -341,6 +341,19 @@ (check-false (random-bool-f #t #f)) (check-true (random-bool-f #t #t)))) + (: random-boolean-function/list (-> Positive-Integer (-> (Listof Boolean) Boolean))) + (define (random-boolean-function/list n) + (table->function/list (random-boolean-table n))) + + (module+ test + (test-case "random-boolean-function/list" + (random-seed 1) + (define random-bool-f/list (random-boolean-function/list 2)) + (check-true (random-bool-f/list '(#f #f))) + (check-true (random-bool-f/list '(#f #t))) + (check-false (random-bool-f/list '(#t #f))) + (check-true (random-bool-f/list '(#t #t))))) + (module untyped racket (module+ test (require rackunit)) @@ -425,7 +438,7 @@ table->function/list table->function table->function/pv enumerate-boolean-tables enumerate-boolean-functions enumerate-boolean-functions/pv enumerate-boolean-functions/list - random-boolean-table random-boolean-function) + random-boolean-table random-boolean-function random-boolean-function/list) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] @@ -437,7 +450,6 @@ [struct tbf ((weights (vectorof number?)) (threshold number?))]) ;; Functions (contract-out - [random-boolean-function/list (-> number? procedure?)] [tbf-w (-> tbf? (vectorof number?))] [tbf-θ (-> tbf? number?)] [vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))] @@ -460,21 +472,6 @@ (require rackunit)) -;;; ================ -;;; Random functions -;;; ================ - -;;; Like random-boolean-function, but the constructed function takes a -;;; list of arguments. -(define random-boolean-function/list (compose table->function/list random-boolean-table)) - -(module+ test - (test-case "random-boolean-function/list" - (define f (random-boolean-function/list 2)) - (check-false (f '(#f #f))) (check-true (f '(#f #t))) - (check-true (f '(#t #f))) (check-false (f '(#t #t))))) - - ;;; =========================== ;;; Threshold Boolean functions ;;; =========================== diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 8121393..c593e1c 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -357,6 +357,15 @@ Generates a random Boolean function of arity @racket[n]. (random-bool-f #t #f) ]} +@defproc[(random-boolean-function/list [n Positive-Integer]) (-> (Listof Boolean) Boolean)]{ + +Like @racket[random-boolean-function], but the constructed function takes +a list of arguments. + +@examples[#:eval functions-evaluator +(define random-bool-f/list (random-boolean-function/list 2)) +(random-bool-f/list '(#t #f)) +]} @section{Threshold Boolean functions}