From 2c79ca37813ca58cc903fff18a8b9261f126dc13 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 19 Mar 2020 23:40:08 +0100 Subject: [PATCH] networks: Add enumerate-boolean-tables, enumerate-boolean-functions, and enumerate-boolean-functions/list. --- networks-tests.rkt | 6 +++++- networks.rkt | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/networks-tests.rkt b/networks-tests.rkt index ffc6176..187a073 100644 --- a/networks-tests.rkt +++ b/networks-tests.rkt @@ -218,4 +218,8 @@ (let ([negation (table->function '((#t #f) (#f #t)))] [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))))) + (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))))) diff --git a/networks.rkt b/networks.rkt index 458196e..535c4e5 100644 --- a/networks.rkt +++ b/networks.rkt @@ -60,7 +60,10 @@ [tabulate (->* (procedure?) () #:rest (listof generic-set?) (listof list?))] [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?)]) + [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 (contract-out [variable? (-> any/c boolean?)] [state? (-> any/c boolean?)] @@ -435,3 +438,37 @@ (for/hash ([line table]) (let-values ([(x fx) (split-at-right line 1)]) (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)))