From 458ba10ab5e77891281656c8c89abf3805b0e7dc Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 1 May 2022 01:05:35 +0200 Subject: [PATCH] Add 01->boolean/state (replacing booleanize-state). --- networks.rkt | 27 +++++++++++++-------------- scribblings/networks.scrbl | 11 +++++++++++ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/networks.rkt b/networks.rkt index 9e8640b..48640ee 100644 --- a/networks.rkt +++ b/networks.rkt @@ -9,8 +9,9 @@ (provide State UpdateFunction Domain DomainMapping - (struct-out network) Network + 01->boolean/state + (struct-out network) Network make-same-domains make-boolean-domains make-boolean-network make-01-domains make-01-network update) @@ -19,6 +20,16 @@ (define-type (Domain a) (Listof a)) (define-type (DomainMapping a) (VariableMapping (Domain a))) + (: 01->boolean/state (-> (State (U Zero One)) (State Boolean))) + (define (01->boolean/state s) + (for/hash ([(x val) (in-hash s)]) : (State Boolean) + (if (eq? val 1) (values x #t) (values x #f)))) + + (module+ test + (test-case "01->boolean/state" + (check-equal? (01->boolean/state (hash 'a 0 'b 1)) + (hash 'a #f 'b #t)))) + (struct (a) network ([functions : (VariableMapping (UpdateFunction a))] [domains : (DomainMapping a)]) #:transparent @@ -122,8 +133,7 @@ [struct network-form ([forms variable-mapping?] [domains domain-mapping/c])]) ;; Functions - (contract-out [booleanize-state (-> state? state?)] - [update-function-form->update-function (-> update-function-form? update-function/c)] + (contract-out [update-function-form->update-function (-> update-function-form? update-function/c)] [network-form->network (-> network-form? network?)] [make-boolean-network-form (-> variable-mapping? network-form?)] [forms->boolean-network (-> variable-mapping? network?)] @@ -266,17 +276,6 @@ ;;; values in their domains. (define domain-mapping/c (hash/c variable? list?)) -;;; Booleanizes a given state: replaces 0 with #f and 1 with #t. -(define (booleanize-state s) - (for/hash ([(x val) s]) (match val [0 (values x #f)] [1 (values x #t)]))) - -(module+ test - (test-case "make-state, make-state-booleanize, booleanize-state" - (check-equal? (make-state-booleanize '((a . 0) (b . 1))) - (make-state '((a . #f) (b . #t)))) - (check-equal? (booleanize-state (make-state '((a . 0) (b . 1)))) - (make-state '((a . #f) (b . #t)))))) - ;;; ================================= ;;; Syntactic description of networks diff --git a/scribblings/networks.scrbl b/scribblings/networks.scrbl index 40c8f32..ceaa80a 100644 --- a/scribblings/networks.scrbl +++ b/scribblings/networks.scrbl @@ -97,6 +97,17 @@ two variables @racket[a] and @racket[b] whose values are in @tt{{0,1}}: (U Zero One))) ] +@section{Utilities} + +@defproc[(01->boolean/state [s (State (U Zero One))]) (State Boolean)]{ + +Converts the values 0 and 1 in a state to @racket[#f] and +@racket[#t] respectively. + +@ex[ +(01->boolean/state (hash 'a 0 'b 1)) +]} + @section{Networks} @defstruct*[network ([functions (VariableMapping (UpdateFunction a))]