From f09f24d2dd482767a7242a72d2e7e2e73a6daf80 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Fri, 20 Mar 2020 00:15:51 +0100 Subject: [PATCH] networks: Use streams for enumerating Boolean functions. --- networks-tests.rkt | 4 ++-- networks.rkt | 37 ++++++++++++++++--------------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/networks-tests.rkt b/networks-tests.rkt index 187a073..bec5fbe 100644 --- a/networks-tests.rkt +++ b/networks-tests.rkt @@ -219,7 +219,7 @@ [negation/list (table->function/list '((#t #f) (#f #t)))]) (check-true (negation #f)) (check-false (negation #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))]) + (let ([f1 (stream-first (enumerate-boolean-functions 1))] + [f1/list (stream-first (enumerate-boolean-functions/list 1))]) (check-false (f1 #f)) (check-false (f1 #t)) (check-false (f1/list '(#f))) (check-false (f1/list '(#t))))) diff --git a/networks.rkt b/networks.rkt index 535c4e5..faea2d3 100644 --- a/networks.rkt +++ b/networks.rkt @@ -61,9 +61,9 @@ [tabulate/boolean (-> procedure-fixed-arity? (listof (listof boolean?)))] [table->function (-> (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?))]) + [enumerate-boolean-tables (-> number? (stream/c (listof (*list/c any/c any/c))))] + [enumerate-boolean-functions (-> number? (stream/c procedure?))] + [enumerate-boolean-functions/list (-> number? (stream/c procedure?))]) ;; Predicates (contract-out [variable? (-> any/c boolean?)] [state? (-> any/c boolean?)] @@ -442,33 +442,28 @@ ;;; 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. +;;; Returns the stream of 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. +;;; There are 2^(2^n) Boolean functions of arity n. (define (enumerate-boolean-tables n) (let ([inputs (boolean-power-n n)] [outputs (boolean-power-n (expt 2 n))]) - (for/list ([out outputs]) + (for/stream ([out outputs]) (for/list ([in inputs] [o out]) (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. -;;; -;;; TODO: Think about making this function lazy. +;;; There are 2^(2^n) Boolean functions of arity 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 -;;; from the functions returned by enumerate-boolean-functions, the -;;; functions take lists of arguments instead of n arguments. +;;; Returns the stream of 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. +;;; There are 2^(2^n) Boolean functions of arity n. (define (enumerate-boolean-functions/list n) - (map table->function/list (enumerate-boolean-tables n))) + (stream-map table->function/list (enumerate-boolean-tables n)))