Add 01->boolean/state (replacing booleanize-state).

This commit is contained in:
Sergiu Ivanov 2022-05-01 01:05:35 +02:00
parent aea472acb2
commit 458ba10ab5
2 changed files with 24 additions and 14 deletions

View file

@ -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

View file

@ -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))]