networks: Add booleanize-state and make stb a shortcut for it.

This commit is contained in:
Sergiu Ivanov 2020-02-26 20:47:38 +01:00
parent 2022e0187b
commit b53639839f
2 changed files with 10 additions and 1 deletions

View file

@ -21,6 +21,8 @@
(check-equal? (make-state-booleanize '((a . 0) (b . 1))) (check-equal? (make-state-booleanize '((a . 0) (b . 1)))
(st '((a . #f) (b . #t)))) (st '((a . #f) (b . #t))))
(check-equal? (stb '((a . 0) (b . 1))) (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))))) (st '((a . #f) (b . #t)))))
(test-case "One-step syncronous update" (test-case "One-step syncronous update"

View file

@ -19,6 +19,7 @@
(contract-out [update (-> network? state? (set/c variable? #:kind 'dont-care) state?)] (contract-out [update (-> network? state? (set/c variable? #:kind 'dont-care) state?)]
[make-state (-> (listof (cons/c symbol? any/c)) state?)] [make-state (-> (listof (cons/c symbol? any/c)) state?)]
[make-state-booleanize (-> (listof (cons/c symbol? (or/c 0 1))) 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?)] [make-network-from-functions (-> (listof (cons/c symbol? update-function/c)) network?)]
[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?)]
@ -108,8 +109,14 @@
[(cons var 0) (cons var #f)] [(cons var 0) (cons var #f)]
[(cons var 1) (cons var #t)])))) [(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. ;;; 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. ;;; A version of make-immutable-hash restricted to creating networks.
(define (make-network-from-functions funcs) (make-immutable-hash funcs)) (define (make-network-from-functions funcs) (make-immutable-hash funcs))