From 242ea9d31db77a147041854e21cdbf9a1d3e2fd4 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 21 Apr 2022 11:03:57 +0200 Subject: [PATCH] Type random-boolean-function. --- functions.rkt | 28 ++++++++++++++++------------ scribblings/functions.scrbl | 10 ++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/functions.rkt b/functions.rkt index 7a062c1..edbd2f1 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-table random-boolean-function) (module+ test (require typed/rackunit)) @@ -328,6 +328,20 @@ (#t #f #f) (#t #t #t))))) + (: random-boolean-function (-> Integer (-> Boolean * Boolean))) + (define (random-boolean-function n) + (table->function (random-boolean-table n))) + + (module+ test + (test-case "random-boolean-function" + (random-seed 1) + (define random-bool-f (random-boolean-function 2)) + (check-true (random-bool-f #f #f)) + (check-true (random-bool-f #f #t)) + (check-false (random-bool-f #t #f)) + (check-true (random-bool-f #t #t)) + )) + (module untyped racket (module+ test (require rackunit)) @@ -412,7 +426,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-table random-boolean-function) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] @@ -424,7 +438,6 @@ [struct tbf ((weights (vectorof number?)) (threshold number?))]) ;; Functions (contract-out - [random-boolean-function (-> number? procedure?)] [random-boolean-function/list (-> number? procedure?)] [tbf-w (-> tbf? (vectorof number?))] [tbf-θ (-> tbf? number?)] @@ -452,15 +465,6 @@ ;;; Random functions ;;; ================ -;;; Generates a random Boolean function of arity n. -(define random-boolean-function (compose table->function random-boolean-table)) - -(module+ test - (test-case "random-boolean-function" - (define f (random-boolean-function 2)) - (check-true (f #f #f)) (check-false (f #f #t)) - (check-true (f #t #f)) (check-false (f #t #t)))) - ;;; 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)) diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index cea242e..dcd924c 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -348,6 +348,16 @@ Generates a random truth table for a Boolean function of arity @racket[n]. (random-boolean-table 2) ]} +@defproc[(random-boolean-function [n Integer]) (-> Boolean * Boolean)]{ + +Generates a random Boolean function of arity @racket[n]. + +@examples[#:eval functions-evaluator +(define random-bool-f (random-boolean-function 2)) +(random-bool-f #t #f) +]} + + @section{Threshold Boolean functions} @section[#:tag "fuctions/untyped"]{Untyped definitions}