From 001a12d166ab0afeb771c2bd89d7bd34fac4f193 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Mon, 22 May 2023 15:34:27 +0200 Subject: [PATCH] Type group-truth-table-by-nai. --- scribblings/tbn.scrbl | 20 ++++++++++++++++++++ tbn.rkt | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/scribblings/tbn.scrbl b/scribblings/tbn.scrbl index 1e575d6..a6654ff 100644 --- a/scribblings/tbn.scrbl +++ b/scribblings/tbn.scrbl @@ -276,3 +276,23 @@ Like @racket[tabulate-tbfs/state] and (tabulate-tbf/state (tbf/state (hash 'a 1 'b 2) 2)) (tabulate-tbf/state+headers (tbf/state (hash 'a 1 'b 2) 2)) ]} + +@section{Miscellaneous utilities} + +@defproc[(group-truth-table-by-nai [tt (Listof (Listof Integer))]) + (Listof (Listof (Listof Integer)))]{ + +Given the truth table @racket[tt] of a Boolean function, groups the +lines by the @italic{N}umber of @italic{A}ctivated @italic{I}nputs—the +number of inputs which are 1 in the input vector. + +@ex[ +(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))) +]} diff --git a/tbn.rkt b/tbn.rkt index f8a782f..286b69b 100644 --- a/tbn.rkt +++ b/tbn.rkt @@ -32,6 +32,8 @@ tabulate-tbfs/state tabulate-tbfs/state+headers tabulate-tbf/state tabulate-tbf/state+headers + + group-truth-table-by-nai ) (: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One))) @@ -289,6 +291,32 @@ (0 1 0) (1 0 0) (1 1 1))))) + + (: group-truth-table-by-nai (-> (Listof (Listof Integer)) + (Listof (Listof (Listof Integer))))) + (define (group-truth-table-by-nai tt) + (: sum (-> (Listof Integer) Integer)) + (define (sum xs) (foldl + 0 xs)) + (group-by (λ ([row : (Listof Integer)]) + (drop-right row 1)) + tt + (λ ([in1 : (Listof Integer)] [in2 : (Listof Integer)]) + (= (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)))))) ) (module+ test