diff --git a/networks-tests.rkt b/networks-tests.rkt index 17d9205..2d154d7 100644 --- a/networks-tests.rkt +++ b/networks-tests.rkt @@ -21,6 +21,8 @@ (check-equal? (make-state-booleanize '((a . 0) (b . 1))) (st '((a . #f) (b . #t)))) (check-equal? (stb '((a . 0) (b . 1))) + (st '((a . #f) (b . #t)))) + (check-equal? (booleanize-state (st '((a . 0) (b . 1)))) (st '((a . #f) (b . #t))))) (test-case "One-step syncronous update" diff --git a/networks.rkt b/networks.rkt index b1765a1..374e394 100644 --- a/networks.rkt +++ b/networks.rkt @@ -19,6 +19,7 @@ (contract-out [update (-> network? state? (set/c variable? #:kind 'dont-care) state?)] [make-state (-> (listof (cons/c symbol? any/c)) state?)] [make-state-booleanize (-> (listof (cons/c symbol? (or/c 0 1))) state?)] + [booleanize-state (-> state? state?)] [make-network-from-functions (-> (listof (cons/c symbol? update-function/c)) network?)] [update-function-form->update-function (-> update-function-form? update-function/c)] [network-form->network (-> network-form? network?)] @@ -108,8 +109,14 @@ [(cons var 0) (cons var #f)] [(cons var 1) (cons var #t)])))) +;;; 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)]))) + +(booleanize-state (st '((a . 1) (b . 0)))) + ;;; A shortcut for make-state-booleanize. -(define-syntax-rule (stb mappings) (make-state-booleanize mappings)) +(define-syntax-rule (stb s) (booleanize-state s)) ;;; A version of make-immutable-hash restricted to creating networks. (define (make-network-from-functions funcs) (make-immutable-hash funcs))