2020-02-15 13:32:54 +01:00
|
|
|
#lang typed/racket
|
|
|
|
|
|
|
|
;;; dds/bn
|
|
|
|
|
|
|
|
;;; This module provides some quick definitions for running Boolean
|
|
|
|
;;; networks. A Boolean network is a set of Boolean variables which
|
|
|
|
;;; are updated according to their corresponding update functions.
|
|
|
|
;;; The variables to be updated at each step are given by the mode.
|
|
|
|
|
2020-02-15 20:30:46 +01:00
|
|
|
(provide Variable State UpdateFunc Network
|
2020-02-15 21:00:20 +01:00
|
|
|
update new-state make-bn)
|
2020-02-15 15:19:53 +01:00
|
|
|
|
|
|
|
(define-type Variable Symbol)
|
2020-02-15 13:32:54 +01:00
|
|
|
|
2020-02-15 15:16:20 +01:00
|
|
|
;;; A state of a Boolean network is a mapping from the variables of the
|
|
|
|
;;; network to their Boolean values.
|
2020-02-15 15:19:53 +01:00
|
|
|
(define-type State (HashTable Variable Boolean))
|
2020-02-15 13:32:54 +01:00
|
|
|
|
2020-02-15 15:16:20 +01:00
|
|
|
;;; An update function is a Boolean function computing a Boolean value
|
|
|
|
;;; from the given state.
|
2020-02-15 13:32:54 +01:00
|
|
|
(define-type UpdateFunc (-> State Boolean))
|
|
|
|
|
2020-02-15 15:16:20 +01:00
|
|
|
;;; A Boolean network is a mapping from its variables to its update
|
|
|
|
;;; functions.
|
2020-02-15 15:19:53 +01:00
|
|
|
(define-type Network (HashTable Variable UpdateFunc))
|
2020-02-15 20:30:46 +01:00
|
|
|
|
|
|
|
;;; Given a state s updates all the variables from xs. This
|
|
|
|
;;; corresponds to a parallel mode.
|
|
|
|
(define (update [bn : Network] ; the Boolean network
|
|
|
|
[s : State] ; the state to operate on
|
|
|
|
[xs : (Listof Variable)]) ; the variables to update
|
|
|
|
(let ([new-s : State (hash-copy s)])
|
|
|
|
(for ([x xs])
|
|
|
|
(let ([f (hash-ref bn x)])
|
|
|
|
(hash-set! new-s x (f s))))
|
|
|
|
new-s))
|
2020-02-15 20:51:52 +01:00
|
|
|
|
|
|
|
;;; A version of make-hash restricted to creating Boolean states.
|
|
|
|
(define (new-state [mappings : (Listof (Pairof Variable Boolean))])
|
|
|
|
(make-hash mappings))
|
2020-02-15 21:00:20 +01:00
|
|
|
|
|
|
|
;;; A version of make-hash restricted to creating Boolean networks.
|
|
|
|
(define (make-bn [funcs : (Listof (Pairof Variable UpdateFunc))])
|
|
|
|
(make-hash funcs))
|