From 2b60b23bf172b235fbca655c13c10068200cccda Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 8 Nov 2020 23:31:11 +0100 Subject: [PATCH] networks: Add group-truth-table-by-nai. --- networks.rkt | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/networks.rkt b/networks.rkt index 14ab1b2..3d49619 100644 --- a/networks.rkt +++ b/networks.rkt @@ -106,6 +106,7 @@ (listof (listof (or/c symbol? number?))))] [tbf/state-tabulate (->* (tbf/state?) (#:headers boolean?) (listof (listof (or/c symbol? number?))))] + [group-truth-table-by-nai (-> (listof (listof (or/c 0 1))) (listof (listof (listof (or/c 0 1)))))] [make-tbn (-> (listof (cons/c variable? tbf/state?)) tbn?)] [tbn->network (-> tbn? network?)] [make-sbn (-> (listof (cons/c variable? tbf/state?)) sbn?)] @@ -1298,6 +1299,32 @@ (1 0 0) (1 1 0))))) +;;; Given a truth table of a Boolean function, groups the lines by the +;;; "number of activated inputs"—the number of inputs which are 1 in +;;; the input vector. +;;; +;;; The truth table must not include the header line. +(define (group-truth-table-by-nai tt) + (define sum (((curry foldl) +) 0)) + (group-by (λ (row) (drop-right row 1)) + tt + (λ (in1 in2) (= (sum in1) (sum in2))))) + +(module+ test + (test-case "group-truth-table-by-nai" + (check-equal? (group-truth-table-by-nai '((0 0 0 1) + (0 0 1 1) + (0 1 0 0) + (0 1 1 1) + (1 0 0 0) + (1 0 1 0) + (1 1 0 1) + (1 1 1 0))) + '(((0 0 0 1)) + ((0 0 1 1) (0 1 0 0) (1 0 0 0)) + ((0 1 1 1) (1 0 1 0) (1 1 0 1)) + ((1 1 1 0)))))) + ;;; A TBN is a network form mapping variables to tbf/state. ;;; ;;; The tbf/state must only reference variables appearing in the