Typed random-boolean-function/list.

This commit is contained in:
Sergiu Ivanov 2022-04-21 11:23:36 +02:00
parent ee487af157
commit 212440add1
2 changed files with 24 additions and 18 deletions

View file

@ -25,7 +25,7 @@
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 random-boolean-function) random-boolean-table random-boolean-function random-boolean-function/list)
(module+ test (module+ test
(require typed/rackunit)) (require typed/rackunit))
@ -341,6 +341,19 @@
(check-false (random-bool-f #t #f)) (check-false (random-bool-f #t #f))
(check-true (random-bool-f #t #t)))) (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 untyped racket
(module+ test (module+ test
(require rackunit)) (require rackunit))
@ -425,7 +438,7 @@
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 random-boolean-function) random-boolean-table random-boolean-function random-boolean-function/list)
(require (rename-in (submod 'typed untyped) (require (rename-in (submod 'typed untyped)
[tabulate tabulate/untyped] [tabulate tabulate/untyped]
@ -437,7 +450,6 @@
[struct tbf ((weights (vectorof number?)) (threshold number?))]) [struct tbf ((weights (vectorof number?)) (threshold number?))])
;; Functions ;; Functions
(contract-out (contract-out
[random-boolean-function/list (-> number? procedure?)]
[tbf-w (-> tbf? (vectorof number?))] [tbf-w (-> tbf? (vectorof number?))]
[tbf-θ (-> tbf? number?)] [tbf-θ (-> tbf? number?)]
[vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))] [vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))]
@ -460,21 +472,6 @@
(require rackunit)) (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 ;;; Threshold Boolean functions
;;; =========================== ;;; ===========================

View file

@ -357,6 +357,15 @@ Generates a random Boolean function of arity @racket[n].
(random-bool-f #t #f) (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} @section{Threshold Boolean functions}