From 35abded48632a4dbd7f7d82a83f22d6d0a74cc8f Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Tue, 18 Feb 2020 12:07:38 +0100 Subject: [PATCH] bn: Add bn-form->bn. --- bn-tests.rkt | 6 +++++- bn.rkt | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bn-tests.rkt b/bn-tests.rkt index 8dec81e..6012f95 100644 --- a/bn-tests.rkt +++ b/bn-tests.rkt @@ -35,4 +35,8 @@ (test-case "Constructing networks from forms" (let ([s (make-state '((x . #t) (y . #f)))] [f (update-func-form->update-func '(and x y))]) - (check-equal? (f s) #f)))))) + (check-equal? (f s) #f)) + (let ([bn (bn-form->bn (make-hash '((a . (and a b)) (b . (not b)))))] + [s (make-state '((a . #t) (b . #t)))]) + (check-equal? ((hash-ref bn 'a) s) + #t)))))) diff --git a/bn.rkt b/bn.rkt index 9d3fe20..0aff9fa 100644 --- a/bn.rkt +++ b/bn.rkt @@ -10,7 +10,8 @@ (require "utils.rkt") (provide Variable State UpdateFunc Network - update make-state make-bn update-func-form->update-func) + update make-state make-bn update-func-form->update-func + bn-form->bn) ;;; ================= @@ -72,3 +73,10 @@ (define (update-func-form->update-func form) (lambda ([s : State]) (cast (eval-with1 (cast s (HashTable Variable Any)) form) Boolean))) + +;;; Build a Network from a Network form. +(: bn-form->bn (-> NetworkForm Network)) +(define (bn-form->bn bnf) + (make-hash + (hash-map bnf (lambda ([x : Variable] [form : UpdateFuncForm]) + (cons x (update-func-form->update-func form))))))