networks: Add enumerate-boolean-tables, enumerate-boolean-functions, and enumerate-boolean-functions/list.
This commit is contained in:
parent
80ef41eaa4
commit
2c79ca3781
2 changed files with 43 additions and 2 deletions
|
@ -218,4 +218,8 @@
|
||||||
(let ([negation (table->function '((#t #f) (#f #t)))]
|
(let ([negation (table->function '((#t #f) (#f #t)))]
|
||||||
[negation/list (table->function/list '((#t #f) (#f #t)))])
|
[negation/list (table->function/list '((#t #f) (#f #t)))])
|
||||||
(check-true (negation #f)) (check-false (negation #t))
|
(check-true (negation #f)) (check-false (negation #t))
|
||||||
(check-true (negation/list '(#f))) (check-false (negation/list '(#t)))))
|
(check-true (negation/list '(#f))) (check-false (negation/list '(#t))))
|
||||||
|
(let ([f1 (car (enumerate-boolean-functions 1))]
|
||||||
|
[f1/list (car (enumerate-boolean-functions/list 1))])
|
||||||
|
(check-false (f1 #f)) (check-false (f1 #t))
|
||||||
|
(check-false (f1/list '(#f))) (check-false (f1/list '(#t)))))
|
||||||
|
|
39
networks.rkt
39
networks.rkt
|
@ -60,7 +60,10 @@
|
||||||
[tabulate (->* (procedure?) () #:rest (listof generic-set?) (listof list?))]
|
[tabulate (->* (procedure?) () #:rest (listof generic-set?) (listof list?))]
|
||||||
[tabulate/boolean (-> procedure-fixed-arity? (listof (listof boolean?)))]
|
[tabulate/boolean (-> procedure-fixed-arity? (listof (listof boolean?)))]
|
||||||
[table->function (-> (listof (*list/c any/c any/c)) procedure?)]
|
[table->function (-> (listof (*list/c any/c any/c)) procedure?)]
|
||||||
[table->function/list (-> (listof (*list/c any/c any/c)) procedure?)])
|
[table->function/list (-> (listof (*list/c any/c any/c)) procedure?)]
|
||||||
|
[enumerate-boolean-tables (-> number? (listof (listof (*list/c any/c any/c))))]
|
||||||
|
[enumerate-boolean-functions (-> number? (listof procedure?))]
|
||||||
|
[enumerate-boolean-functions/list (-> number? (listof procedure?))])
|
||||||
;; Predicates
|
;; Predicates
|
||||||
(contract-out [variable? (-> any/c boolean?)]
|
(contract-out [variable? (-> any/c boolean?)]
|
||||||
[state? (-> any/c boolean?)]
|
[state? (-> any/c boolean?)]
|
||||||
|
@ -435,3 +438,37 @@
|
||||||
(for/hash ([line table])
|
(for/hash ([line table])
|
||||||
(let-values ([(x fx) (split-at-right line 1)])
|
(let-values ([(x fx) (split-at-right line 1)])
|
||||||
(values x (car fx))))))
|
(values x (car fx))))))
|
||||||
|
|
||||||
|
;;; Returns the n-th Cartesian power of the Boolean domain: {0,1}^n.
|
||||||
|
(define (boolean-power-n n) (apply cartesian-product (make-list n '(#f #t))))
|
||||||
|
|
||||||
|
;;; Enumerates the truth tables of all Boolean functions of a given
|
||||||
|
;;; arity.
|
||||||
|
;;;
|
||||||
|
;;; /!\ There are 2^(2^n) Boolean functions of arity n.
|
||||||
|
;;;
|
||||||
|
;;; TODO: Think about making this function lazy.
|
||||||
|
(define (enumerate-boolean-tables n)
|
||||||
|
(let ([inputs (boolean-power-n n)]
|
||||||
|
[outputs (boolean-power-n (expt 2 n))])
|
||||||
|
(for/list ([out outputs])
|
||||||
|
(for/list ([in inputs] [o out])
|
||||||
|
(append in (list o))))))
|
||||||
|
|
||||||
|
;;; Enumerates all Boolean functions of a given arity.
|
||||||
|
;;;
|
||||||
|
;;; /!\ There are 2^(2^n) Boolean functions of arity n.
|
||||||
|
;;;
|
||||||
|
;;; TODO: Think about making this function lazy.
|
||||||
|
(define (enumerate-boolean-functions n)
|
||||||
|
(map table->function (enumerate-boolean-tables n)))
|
||||||
|
|
||||||
|
;;; Enumerates all Boolean functions of a given arity. As different
|
||||||
|
;;; from the functions returned by enumerate-boolean-functions, the
|
||||||
|
;;; functions take lists of arguments instead of n arguments.
|
||||||
|
;;;
|
||||||
|
;;; /!\ There are 2^(2^n) Boolean functions of arity n.
|
||||||
|
;;;
|
||||||
|
;;; TODO: Think about making this function lazy.
|
||||||
|
(define (enumerate-boolean-functions/list n)
|
||||||
|
(map table->function/list (enumerate-boolean-tables n)))
|
||||||
|
|
Loading…
Reference in a new issue