Add 01->boolean/state (replacing booleanize-state).
This commit is contained in:
parent
aea472acb2
commit
458ba10ab5
2 changed files with 24 additions and 14 deletions
27
networks.rkt
27
networks.rkt
|
@ -9,8 +9,9 @@
|
||||||
|
|
||||||
(provide
|
(provide
|
||||||
State UpdateFunction Domain DomainMapping
|
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-same-domains make-boolean-domains make-boolean-network
|
||||||
make-01-domains make-01-network update)
|
make-01-domains make-01-network update)
|
||||||
|
|
||||||
|
@ -19,6 +20,16 @@
|
||||||
(define-type (Domain a) (Listof a))
|
(define-type (Domain a) (Listof a))
|
||||||
(define-type (DomainMapping a) (VariableMapping (Domain 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))]
|
(struct (a) network ([functions : (VariableMapping (UpdateFunction a))]
|
||||||
[domains : (DomainMapping a)])
|
[domains : (DomainMapping a)])
|
||||||
#:transparent
|
#:transparent
|
||||||
|
@ -122,8 +133,7 @@
|
||||||
[struct network-form ([forms variable-mapping?]
|
[struct network-form ([forms variable-mapping?]
|
||||||
[domains domain-mapping/c])])
|
[domains domain-mapping/c])])
|
||||||
;; Functions
|
;; Functions
|
||||||
(contract-out [booleanize-state (-> state? state?)]
|
(contract-out [update-function-form->update-function (-> update-function-form? update-function/c)]
|
||||||
[update-function-form->update-function (-> update-function-form? update-function/c)]
|
|
||||||
[network-form->network (-> network-form? network?)]
|
[network-form->network (-> network-form? network?)]
|
||||||
[make-boolean-network-form (-> variable-mapping? network-form?)]
|
[make-boolean-network-form (-> variable-mapping? network-form?)]
|
||||||
[forms->boolean-network (-> variable-mapping? network?)]
|
[forms->boolean-network (-> variable-mapping? network?)]
|
||||||
|
@ -266,17 +276,6 @@
|
||||||
;;; values in their domains.
|
;;; values in their domains.
|
||||||
(define domain-mapping/c (hash/c variable? list?))
|
(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
|
;;; Syntactic description of networks
|
||||||
|
|
|
@ -97,6 +97,17 @@ two variables @racket[a] and @racket[b] whose values are in @tt{{0,1}}:
|
||||||
(U Zero One)))
|
(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}
|
@section{Networks}
|
||||||
|
|
||||||
@defstruct*[network ([functions (VariableMapping (UpdateFunction a))]
|
@defstruct*[network ([functions (VariableMapping (UpdateFunction a))]
|
||||||
|
|
Loading…
Reference in a new issue