networks: Use streams for enumerating Boolean functions.
This commit is contained in:
parent
2c79ca3781
commit
f09f24d2dd
2 changed files with 18 additions and 23 deletions
|
@ -219,7 +219,7 @@
|
||||||
[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))]
|
(let ([f1 (stream-first (enumerate-boolean-functions 1))]
|
||||||
[f1/list (car (enumerate-boolean-functions/list 1))])
|
[f1/list (stream-first (enumerate-boolean-functions/list 1))])
|
||||||
(check-false (f1 #f)) (check-false (f1 #t))
|
(check-false (f1 #f)) (check-false (f1 #t))
|
||||||
(check-false (f1/list '(#f))) (check-false (f1/list '(#t)))))
|
(check-false (f1/list '(#f))) (check-false (f1/list '(#t)))))
|
||||||
|
|
37
networks.rkt
37
networks.rkt
|
@ -61,9 +61,9 @@
|
||||||
[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-tables (-> number? (stream/c (listof (*list/c any/c any/c))))]
|
||||||
[enumerate-boolean-functions (-> number? (listof procedure?))]
|
[enumerate-boolean-functions (-> number? (stream/c procedure?))]
|
||||||
[enumerate-boolean-functions/list (-> number? (listof procedure?))])
|
[enumerate-boolean-functions/list (-> number? (stream/c procedure?))])
|
||||||
;; Predicates
|
;; Predicates
|
||||||
(contract-out [variable? (-> any/c boolean?)]
|
(contract-out [variable? (-> any/c boolean?)]
|
||||||
[state? (-> any/c boolean?)]
|
[state? (-> any/c boolean?)]
|
||||||
|
@ -442,33 +442,28 @@
|
||||||
;;; Returns the n-th Cartesian power of the Boolean domain: {0,1}^n.
|
;;; 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))))
|
(define (boolean-power-n n) (apply cartesian-product (make-list n '(#f #t))))
|
||||||
|
|
||||||
;;; Enumerates the truth tables of all Boolean functions of a given
|
;;; Returns the stream of the truth tables of all Boolean functions of
|
||||||
;;; arity.
|
;;; a given arity.
|
||||||
;;;
|
;;;
|
||||||
;;; /!\ There are 2^(2^n) Boolean functions of arity n.
|
;;; There are 2^(2^n) Boolean functions of arity n.
|
||||||
;;;
|
|
||||||
;;; TODO: Think about making this function lazy.
|
|
||||||
(define (enumerate-boolean-tables n)
|
(define (enumerate-boolean-tables n)
|
||||||
(let ([inputs (boolean-power-n n)]
|
(let ([inputs (boolean-power-n n)]
|
||||||
[outputs (boolean-power-n (expt 2 n))])
|
[outputs (boolean-power-n (expt 2 n))])
|
||||||
(for/list ([out outputs])
|
(for/stream ([out outputs])
|
||||||
(for/list ([in inputs] [o out])
|
(for/list ([in inputs] [o out])
|
||||||
(append in (list o))))))
|
(append in (list o))))))
|
||||||
|
|
||||||
;;; Enumerates all Boolean functions of a given arity.
|
;;; Returns the stream of all Boolean functions of a given arity.
|
||||||
;;;
|
;;;
|
||||||
;;; /!\ There are 2^(2^n) Boolean functions of arity n.
|
;;; There are 2^(2^n) Boolean functions of arity n.
|
||||||
;;;
|
|
||||||
;;; TODO: Think about making this function lazy.
|
|
||||||
(define (enumerate-boolean-functions n)
|
(define (enumerate-boolean-functions n)
|
||||||
(map table->function (enumerate-boolean-tables n)))
|
(stream-map table->function (enumerate-boolean-tables n)))
|
||||||
|
|
||||||
;;; Enumerates all Boolean functions of a given arity. As different
|
;;; Returns the stream of all Boolean functions of a given arity. As
|
||||||
;;; from the functions returned by enumerate-boolean-functions, the
|
;;; different from the functions returned by
|
||||||
;;; functions take lists of arguments instead of n arguments.
|
;;; enumerate-boolean-functions, the functions take lists of arguments
|
||||||
|
;;; instead of n arguments.
|
||||||
;;;
|
;;;
|
||||||
;;; /!\ There are 2^(2^n) Boolean functions of arity n.
|
;;; There are 2^(2^n) Boolean functions of arity n.
|
||||||
;;;
|
|
||||||
;;; TODO: Think about making this function lazy.
|
|
||||||
(define (enumerate-boolean-functions/list n)
|
(define (enumerate-boolean-functions/list n)
|
||||||
(map table->function/list (enumerate-boolean-tables n)))
|
(stream-map table->function/list (enumerate-boolean-tables n)))
|
||||||
|
|
Loading…
Reference in a new issue