From f0a20646ef3954e48c5012aa2425cd0d12b8f262 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Fri, 14 Jul 2023 16:21:20 +0200 Subject: [PATCH] Type normalized-tbn?. --- scribblings/tbn.scrbl | 15 +++++++++++++++ tbn.rkt | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/scribblings/tbn.scrbl b/scribblings/tbn.scrbl index ed93a42..cfc3eb8 100644 --- a/scribblings/tbn.scrbl +++ b/scribblings/tbn.scrbl @@ -453,6 +453,21 @@ and pretty-prints the node labels. 'b (tbf/state (hash 'a -1 'b 1) 1)))) ]} +@defproc[(normalized-tbn? [tbn TBN]) Boolean]{ + +Checks whether @racket[tbn] is normalized: whether all of the +functions have the same inputs, and whether these inputs are exactly +the variables of @racket[tbn]. + +@ex[ +(normalized-tbn? + (hash 'x (tbf/state (hash 'x 0 'y -1) -1) + 'y (tbf/state (hash 'x -1 'y 0) -1))) +(normalized-tbn? + (hash 'x (tbf/state (hash 'x 0 ) -1) + 'y (tbf/state (hash 'y 0) -1))) +]} + @section{Miscellaneous utilities} @defproc[(group-truth-table-by-nai [tt (Listof (Listof Integer))]) diff --git a/tbn.rkt b/tbn.rkt index 4348251..53d3a91 100644 --- a/tbn.rkt +++ b/tbn.rkt @@ -38,7 +38,7 @@ TBN sbn? tbn->network parse-org-tbn read-org-tbn read-org-sbn - build-tbn-state-graph + build-tbn-state-graph normalized-tbn? ) (: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One))) @@ -514,6 +514,21 @@ (hash 'a (tbf/state (hash 'a -1 'b 1) 0) 'b (tbf/state (hash 'a -1 'b 1) 1)))) "digraph G {\n\tnode0 [label=\"a:0 b:0\"];\n\tnode1 [label=\"a:1 b:1\"];\n\tnode2 [label=\"a:0 b:1\"];\n\tnode3 [label=\"a:1 b:0\"];\n\tsubgraph U {\n\t\tedge [dir=none];\n\t\tnode0 -> node0 [];\n\t}\n\tsubgraph D {\n\t\tnode1 -> node0 [];\n\t\tnode2 -> node3 [];\n\t\tnode3 -> node0 [];\n\t}\n}\n"))) + + (: normalized-tbn? (-> TBN Boolean)) + (define (normalized-tbn? tbn) + (define tbn-vars (hash-keys tbn)) + (for/and ([tbf (in-list (hash-values tbn))]) + (set=? tbn-vars (hash-keys (tbf/state-w tbf))))) + + (module+ test + (test-case "normalized-tbn?" + (check-true (normalized-tbn? + (hash 'x (tbf/state (hash 'x 0 'y -1) -1) + 'y (tbf/state (hash 'x -1 'y 0) -1)))) + (check-false (normalized-tbn? + (hash 'x (tbf/state (hash 'x 0 ) -1) + 'y (tbf/state (hash 'y 0) -1)))))) ) (module+ test