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