Type group-truth-table-by-nai.

This commit is contained in:
Sergiu Ivanov 2023-05-22 15:34:27 +02:00
parent b9eb692091
commit 001a12d166
2 changed files with 48 additions and 0 deletions

View file

@ -276,3 +276,23 @@ Like @racket[tabulate-tbfs/state] and
(tabulate-tbf/state (tbf/state (hash 'a 1 'b 2) 2)) (tabulate-tbf/state (tbf/state (hash 'a 1 'b 2) 2))
(tabulate-tbf/state+headers (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)))
]}

28
tbn.rkt
View file

@ -32,6 +32,8 @@
tabulate-tbfs/state tabulate-tbfs/state+headers tabulate-tbfs/state tabulate-tbfs/state+headers
tabulate-tbf/state tabulate-tbf/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))) (: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One)))
@ -289,6 +291,32 @@
(0 1 0) (0 1 0)
(1 0 0) (1 0 0)
(1 1 1))))) (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 (module+ test