From b795be0a3916e5bddd532563068cb420cc3c65d2 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Tue, 3 May 2022 22:01:13 +0200 Subject: [PATCH] Type network-form->network as network-form->network/any. --- networks.rkt | 49 +++++++++++++++++++------------------- scribblings/networks.scrbl | 12 ++++++++++ 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/networks.rkt b/networks.rkt index edf9a51..464db3e 100644 --- a/networks.rkt +++ b/networks.rkt @@ -19,6 +19,7 @@ update-function-form->update-function/any update-function-form->update-function/boolean update-function-form->update-function/01 + network-form->network/any ) (define-type (State a) (VariableMapping a)) @@ -156,6 +157,24 @@ (define s (hash 'x 0 'y 1)) (define f (update-function-form->update-function/01 '(max x y))) (check-equal? (f s) 1))) + + (: network-form->network/any (-> (NetworkForm Any) (Network Any))) + (define (network-form->network/any nf) + (network + (for/hash ([(x form) (in-hash (network-form-forms nf))]) + : (VariableMapping (UpdateFunction Any)) + (values x (update-function-form->update-function/any form))) + (network-form-domains nf))) + + (module+ test + (test-case "network-form->network/any" + (define bn (network-form->network/any + (network-form (hash 'a '(and a b) + 'b '(not b)) + (hash 'a '(#f #t) + 'b '(#f #t))))) + (define s (hash 'a #t 'b #t)) + (check-equal? ((hash-ref (network-functions bn) 'a) s) #t))) ) (require 'typed) @@ -172,8 +191,7 @@ [struct dynamics ([network network?] [mode mode?])]) ;; Functions - (contract-out [network-form->network (-> network-form? network?)] - [make-boolean-network-form (-> variable-mapping? network-form?)] + (contract-out [make-boolean-network-form (-> variable-mapping? network-form?)] [forms->boolean-network (-> variable-mapping? network?)] [list-syntactic-interactions (-> network-form? variable? (listof variable?))] [build-syntactic-interaction-graph (-> network-form? graph?)] @@ -321,23 +339,6 @@ (define update-function-form? any/c) -;;; Build a network from a network form. -(define (network-form->network nf) - (network - (for/hash ([(x form) (in-hash (network-form-forms nf))]) - (values x (update-function-form->update-function/any form))) - (network-form-domains nf))) - -(module+ test - (test-case "network-form->network" - (define bn (network-form->network - (network-form (hash 'a '(and a b) - 'b '(not b)) - (hash 'a '(#f #t) - 'b '(#f #t))))) - (define s (hash 'a #t 'b #t)) - (check-equal? ((hash-ref (network-functions bn) 'a) s) #t))) - ;;; Build a Boolean network form from a given mapping assigning forms ;;; to variables. (define (make-boolean-network-form forms) @@ -354,7 +355,7 @@ ;;; Build a Boolean network from a given mapping assigning forms ;;; to variables. (define forms->boolean-network - (compose network-form->network make-boolean-network-form)) + (compose network-form->network/any make-boolean-network-form)) (module+ test (test-case "forms->boolean-network" @@ -508,7 +509,7 @@ (define n-multi (hash 'x '(max (+ y 1) 2) 'y '(min (- y 1) 0))) (define 123-doms (make-same-domains '(x y) '(0 1 2))) - (define n2 (network-form->network (network-form n-multi 123-doms))) + (define n2 (network-form->network/any (network-form n-multi 123-doms))) (check-false (interaction? n2 'x 'y)) (check-true (interaction? n2 'y 'x)))) @@ -573,7 +574,7 @@ 'z '(- 2 y) 't '(abs (- y 1)))) (define 123-doms (make-same-domains '(x y z t) '(0 1 2))) - (define n2 (network-form->network (network-form n-multi 123-doms))) + (define n2 (network-form->network/any (network-form n-multi 123-doms))) (check-false (get-interaction-sign n2 'x 'y)) (check-equal? (get-interaction-sign n2 'y 'x) 1) (check-equal? (get-interaction-sign n2 'y 'z) -1) @@ -594,7 +595,7 @@ ;;; Like build-interaction-graph, but accepts a network form and ;;; converts it a to a network. (define build-interaction-graph/form - (compose build-interaction-graph network-form->network)) + (compose build-interaction-graph network-form->network/any)) (module+ test (test-case "build-interaction-graph" @@ -635,7 +636,7 @@ ;;; Like build-signed-interaction-graph, but takes a network form and ;;; converts it a to a network. (define build-signed-interaction-graph/form - (compose build-signed-interaction-graph network-form->network)) + (compose build-signed-interaction-graph network-form->network/any)) (module+ test (test-case "build-signed-interaction-graph" diff --git a/scribblings/networks.scrbl b/scribblings/networks.scrbl index a412542..03dbd06 100644 --- a/scribblings/networks.scrbl +++ b/scribblings/networks.scrbl @@ -273,6 +273,18 @@ function operates on Boolean states. (and-from-form/01 (hash 'x 1 'y 1)) ]} +@defproc[(network-form->network/any [nf (NetworkForm Any)]) (Network Any)]{ + +Builds a network from a network form. + +@ex[ +(network-form->network/any + (network-form (hash 'a '(and a b) + 'b '(not b)) + (hash 'a '(#f #t) + 'b '(#f #t)))) +]} + @section{Inferring interaction graphs} This section provides inference of both unsigned and signed interaction graphs.