Type random-boolean-function.

This commit is contained in:
Sergiu Ivanov 2022-04-21 11:03:57 +02:00
parent a7a25f92fe
commit 242ea9d31d
2 changed files with 26 additions and 12 deletions

View file

@ -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))

View file

@ -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}