2020-02-20 00:56:30 +01:00
|
|
|
|
#lang racket
|
|
|
|
|
|
|
|
|
|
;;; dds/networks
|
|
|
|
|
|
|
|
|
|
;;; This module provides some quick definitions for and analysing
|
|
|
|
|
;;; network models. A network is a set of variables which are updated
|
|
|
|
|
;;; according to their corresponding update functions. The variables
|
|
|
|
|
;;; to be updated at each step are given by the mode.
|
|
|
|
|
;;;
|
|
|
|
|
;;; This model can generalise Boolean networks, TBANs, multivalued
|
|
|
|
|
;;; networks, etc.
|
|
|
|
|
|
2020-02-23 12:10:27 +01:00
|
|
|
|
(require "utils.rkt" "generic.rkt" graph)
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
(provide
|
2020-02-23 11:10:18 +01:00
|
|
|
|
;; Structures
|
|
|
|
|
(struct-out dynamics)
|
2020-02-20 14:13:48 +01:00
|
|
|
|
;; Functions
|
2020-02-23 12:10:27 +01:00
|
|
|
|
(contract-out [update (-> network? state? (set/c variable? #:kind 'dont-care) state?)]
|
2020-02-20 00:56:30 +01:00
|
|
|
|
[make-state (-> (listof (cons/c symbol? any/c)) state?)]
|
2020-02-26 15:51:25 +01:00
|
|
|
|
[make-state-booleanize (-> (listof (cons/c symbol? (or/c 0 1))) state?)]
|
2020-02-26 20:47:38 +01:00
|
|
|
|
[booleanize-state (-> state? state?)]
|
2020-02-20 14:13:48 +01:00
|
|
|
|
[make-network-from-functions (-> (listof (cons/c symbol? update-function/c)) network?)]
|
|
|
|
|
[update-function-form->update-function (-> update-function-form? update-function/c)]
|
2020-02-20 00:56:30 +01:00
|
|
|
|
[network-form->network (-> network-form? network?)]
|
|
|
|
|
[make-network-from-forms (-> (listof (cons/c symbol? update-function-form?))
|
2020-02-20 15:17:32 +01:00
|
|
|
|
network?)]
|
2020-02-20 15:56:48 +01:00
|
|
|
|
[list-interactions (-> network-form? variable? (listof variable?))]
|
2020-02-22 22:27:40 +01:00
|
|
|
|
[build-interaction-graph (-> network-form? graph?)]
|
2020-02-23 09:12:01 +01:00
|
|
|
|
[build-all-states (-> domain-mapping/c (listof state?))]
|
2020-02-23 11:51:59 +01:00
|
|
|
|
[make-same-domains (-> (listof variable?) generic-set? domain-mapping/c)]
|
2020-02-23 00:15:18 +01:00
|
|
|
|
[make-boolean-domains (-> (listof variable?) (hash/c variable? (list/c #f #t)))]
|
2020-02-23 20:13:37 +01:00
|
|
|
|
[build-all-boolean-states (-> (listof variable?) (listof state?))]
|
2020-02-23 09:12:01 +01:00
|
|
|
|
[get-interaction-sign (-> network-form? domain-mapping/c variable? variable? (or/c '+ '- '0))]
|
2020-02-23 10:00:48 +01:00
|
|
|
|
[build-signed-interaction-graph (-> network-form? domain-mapping/c graph?)]
|
2020-02-23 11:25:19 +01:00
|
|
|
|
[build-boolean-signed-interaction-graph (-> network-form? graph?)]
|
|
|
|
|
[make-asyn (-> (listof variable?) mode?)]
|
2020-02-23 11:42:01 +01:00
|
|
|
|
[make-syn (-> (listof variable?) mode?)]
|
|
|
|
|
[make-dynamics-from-func (-> network? (-> (listof variable?) mode?) dynamics?)]
|
|
|
|
|
[make-asyn-dynamics (-> network? dynamics?)]
|
2020-02-23 12:19:47 +01:00
|
|
|
|
[make-syn-dynamics (-> network? dynamics?)]
|
2020-03-04 18:54:36 +01:00
|
|
|
|
[read-org-network-make-asyn (-> string? dynamics?)]
|
|
|
|
|
[read-org-network-make-syn (-> string? dynamics?)]
|
2020-02-23 13:28:51 +01:00
|
|
|
|
[dds-step-one (-> dynamics? state? (set/c state?))]
|
2020-02-23 14:11:55 +01:00
|
|
|
|
[dds-step-one-annotated (-> dynamics? state? (set/c (cons/c modality? state?)))]
|
2020-02-23 18:51:57 +01:00
|
|
|
|
[dds-step (-> dynamics? (set/c state? #:kind 'dont-care) (set/c state?))]
|
|
|
|
|
[dds-build-state-graph (-> dynamics? (set/c state? #:kind 'dont-care) graph?)]
|
2020-02-23 19:24:53 +01:00
|
|
|
|
[dds-build-n-step-state-graph (-> dynamics? (set/c state? #:kind 'dont-care) number? graph?)]
|
2020-02-28 21:46:25 +01:00
|
|
|
|
[dds-build-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) graph?)]
|
|
|
|
|
[dds-build-n-step-state-graph-annotated (-> dynamics? (set/c state? #:kind 'dont-care) number? graph?)]
|
2020-02-23 19:44:55 +01:00
|
|
|
|
[pretty-print-state (-> state? string?)]
|
|
|
|
|
[any->boolean (-> any/c boolean?)]
|
|
|
|
|
[pretty-print-boolean-state (-> state? string?)]
|
|
|
|
|
[pretty-print-state-graph-with (-> graph? (-> state? string?) graph?)]
|
|
|
|
|
[pretty-print-state-graph (-> graph? graph?)]
|
2020-02-23 20:13:37 +01:00
|
|
|
|
[pretty-print-boolean-state-graph (-> graph? graph?)]
|
2020-02-28 21:56:42 +01:00
|
|
|
|
[build-full-boolean-state-graph (-> dynamics? graph?)]
|
2020-03-15 16:12:35 +01:00
|
|
|
|
[build-full-boolean-state-graph-annotated (-> dynamics? graph?)]
|
|
|
|
|
[tabulate/domain-list (-> procedure? (listof generic-set?) (listof list?))]
|
|
|
|
|
[tabulate (->* (procedure?) () #:rest (listof generic-set?) (listof list?))]
|
2020-03-18 21:40:09 +01:00
|
|
|
|
[tabulate/boolean (-> procedure-fixed-arity? (listof (listof boolean?)))]
|
|
|
|
|
[table->function (-> (listof (*list/c any/c any/c)) procedure?)]
|
2020-03-19 23:40:08 +01:00
|
|
|
|
[table->function/list (-> (listof (*list/c any/c any/c)) procedure?)]
|
2020-03-20 16:38:32 +01:00
|
|
|
|
[boolean-power-n (-> number? (listof (listof boolean?)))]
|
2020-03-20 16:40:15 +01:00
|
|
|
|
[boolean-power-n/stream (-> number? (stream/c (listof boolean?)))]
|
2020-03-20 00:15:51 +01:00
|
|
|
|
[enumerate-boolean-tables (-> number? (stream/c (listof (*list/c any/c any/c))))]
|
|
|
|
|
[enumerate-boolean-functions (-> number? (stream/c procedure?))]
|
|
|
|
|
[enumerate-boolean-functions/list (-> number? (stream/c procedure?))])
|
2020-02-20 14:13:48 +01:00
|
|
|
|
;; Predicates
|
|
|
|
|
(contract-out [variable? (-> any/c boolean?)]
|
|
|
|
|
[state? (-> any/c boolean?)]
|
|
|
|
|
[update-function-form? (-> any/c boolean?)]
|
2020-02-23 11:10:18 +01:00
|
|
|
|
[network-form? (-> any/c boolean?)]
|
2020-02-23 13:53:02 +01:00
|
|
|
|
[modality? (-> any/c boolean?)]
|
2020-02-23 11:10:18 +01:00
|
|
|
|
[mode? (-> any/c boolean?)])
|
2020-02-20 00:56:30 +01:00
|
|
|
|
;; Contracts
|
2020-02-22 10:37:37 +01:00
|
|
|
|
(contract-out [state/c contract?]
|
|
|
|
|
[network/c contract?]
|
2020-02-23 09:12:01 +01:00
|
|
|
|
[update-function/c contract?]
|
|
|
|
|
[domain-mapping/c contract?])
|
2020-02-20 00:56:30 +01:00
|
|
|
|
;; Syntax
|
2020-03-04 18:54:36 +01:00
|
|
|
|
st stb nn ppsg ppsgb unorg-syn unorg-asyn)
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; =================
|
|
|
|
|
;;; Basic definitions
|
|
|
|
|
;;; =================
|
|
|
|
|
|
|
|
|
|
(define variable? symbol?)
|
|
|
|
|
|
|
|
|
|
;;; A state of a network is a mapping from the variables of the
|
|
|
|
|
;;; network to their values.
|
|
|
|
|
(define state? variable-mapping?)
|
|
|
|
|
(define state/c (flat-named-contract 'state state?))
|
|
|
|
|
|
|
|
|
|
;;; An update function is a function computing a value from the given
|
|
|
|
|
;;; state.
|
2020-02-20 14:13:48 +01:00
|
|
|
|
(define update-function/c (-> state? any/c))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
;;; A network is a mapping from its variables to its update functions.
|
|
|
|
|
(define network? variable-mapping?)
|
|
|
|
|
(define network/c (flat-named-contract 'network network?))
|
|
|
|
|
|
|
|
|
|
;;; Given a state s updates all the variables from xs. This
|
|
|
|
|
;;; corresponds to a parallel mode.
|
2020-02-23 09:19:45 +01:00
|
|
|
|
(define (update network s xs)
|
|
|
|
|
(for/fold ([new-s s])
|
|
|
|
|
([x xs])
|
|
|
|
|
(let ([f (hash-ref network x)])
|
|
|
|
|
(hash-set new-s x (f s)))))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
2020-02-23 08:47:16 +01:00
|
|
|
|
;;; A version of make-immutable-hash restricted to creating network
|
|
|
|
|
;;; states (see contract).
|
2020-02-22 23:20:10 +01:00
|
|
|
|
(define (make-state mappings) (make-immutable-hash mappings))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
;;; A shortcut for make-state.
|
|
|
|
|
(define-syntax-rule (st mappings) (make-state mappings))
|
|
|
|
|
|
2020-02-26 15:51:25 +01:00
|
|
|
|
;;; Makes a new Boolean states from a state with numerical values 0
|
|
|
|
|
;;; and 1.
|
|
|
|
|
(define (make-state-booleanize mappings)
|
|
|
|
|
(make-state (for/list ([mp mappings])
|
|
|
|
|
(match mp
|
|
|
|
|
[(cons var 0) (cons var #f)]
|
|
|
|
|
[(cons var 1) (cons var #t)]))))
|
|
|
|
|
|
2020-02-26 20:47:38 +01:00
|
|
|
|
;;; 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)])))
|
|
|
|
|
|
2020-02-26 15:51:25 +01:00
|
|
|
|
;;; A shortcut for make-state-booleanize.
|
2020-02-26 20:47:38 +01:00
|
|
|
|
(define-syntax-rule (stb s) (booleanize-state s))
|
2020-02-26 15:51:25 +01:00
|
|
|
|
|
2020-02-23 08:47:16 +01:00
|
|
|
|
;;; A version of make-immutable-hash restricted to creating networks.
|
2020-02-22 23:22:43 +01:00
|
|
|
|
(define (make-network-from-functions funcs) (make-immutable-hash funcs))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; =================================
|
|
|
|
|
;;; Syntactic description of networks
|
|
|
|
|
;;; =================================
|
|
|
|
|
|
|
|
|
|
;;; An update function form is any form which can appear as a body of
|
|
|
|
|
;;; a function and which can be evaluated with eval. For example,
|
|
|
|
|
;;; '(and x y (not z)) or '(+ 1 a (- b 10)).
|
|
|
|
|
(define update-function-form? any/c)
|
|
|
|
|
|
|
|
|
|
;;; A Boolean network form is a mapping from its variables to the
|
|
|
|
|
;;; forms of their update functions.
|
|
|
|
|
(define network-form? variable-mapping?)
|
|
|
|
|
|
|
|
|
|
;;; Build an update function from an update function form.
|
|
|
|
|
(define (update-function-form->update-function form)
|
2020-02-20 15:36:29 +01:00
|
|
|
|
(λ (s) (eval-with s form)))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
;;; Build a network from a network form.
|
|
|
|
|
(define (network-form->network bnf)
|
2020-02-23 08:57:50 +01:00
|
|
|
|
(for/hash ([(x form) bnf])
|
|
|
|
|
(values x (update-function-form->update-function form))))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
;;; Build a network from a list of pairs of forms of update functions.
|
|
|
|
|
(define (make-network-from-forms forms)
|
2020-02-22 23:22:43 +01:00
|
|
|
|
(network-form->network (make-immutable-hash forms)))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
2020-02-23 20:28:11 +01:00
|
|
|
|
;;; A shortcut for network-form->network.
|
|
|
|
|
(define-syntax-rule (nn forms) (network-form->network forms))
|
2020-02-20 00:56:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ============================
|
|
|
|
|
;;; Inferring interaction graphs
|
|
|
|
|
;;; ============================
|
|
|
|
|
|
|
|
|
|
;;; I allow any syntactic forms in definitions of Boolean functions.
|
|
|
|
|
;;; I can still find out which Boolean variables appear in those
|
|
|
|
|
;;; syntactic form, but I have no reliable syntactic means of finding
|
|
|
|
|
;;; out what kind of action do they have (inhibition or activation)
|
|
|
|
|
;;; since I cannot do Boolean minimisation (e.g., I cannot rely on not
|
|
|
|
|
;;; appearing before a variable, since (not (not a)) is equivalent
|
|
|
|
|
;;; to a). On the other hand, going through all Boolean states is
|
|
|
|
|
;;; quite resource-consuming and thus not always useful.
|
|
|
|
|
;;;
|
|
|
|
|
;;; In this section I provide inference of both unsigned and signed
|
|
|
|
|
;;; interaction graphs, but since the inference of signed interaction
|
|
|
|
|
;;; graphs is based on analysing the dynamics of the networks, it may
|
|
|
|
|
;;; be quite resource-consuming.
|
2020-02-20 15:17:32 +01:00
|
|
|
|
|
|
|
|
|
;;; Lists the variables of the network form appearing in the update
|
|
|
|
|
;;; function form for x.
|
|
|
|
|
(define (list-interactions nf x)
|
|
|
|
|
(set-intersect
|
|
|
|
|
(extract-symbols (hash-ref nf x))
|
|
|
|
|
(hash-keys nf)))
|
2020-02-20 15:56:48 +01:00
|
|
|
|
|
|
|
|
|
;;; Builds the graph in which the vertices are the variables of a
|
|
|
|
|
;;; given network, and which contains an arrow from a to b whenever a
|
|
|
|
|
;;; appears in (list-interactions a).
|
|
|
|
|
(define (build-interaction-graph n)
|
|
|
|
|
(transpose
|
|
|
|
|
(unweighted-graph/adj
|
2020-02-23 08:57:50 +01:00
|
|
|
|
(for/list ([(var _) n]) (cons var (list-interactions n var))))))
|
2020-02-22 22:27:40 +01:00
|
|
|
|
|
2020-02-23 09:12:01 +01:00
|
|
|
|
;;; A domain mapping is a hash set mapping variables to the lists of
|
|
|
|
|
;;; values in their domains.
|
|
|
|
|
(define domain-mapping/c (hash/c variable? list?))
|
|
|
|
|
|
2020-02-23 09:09:43 +01:00
|
|
|
|
;;; Given a hash-set mapping variables to generic sets of their
|
2020-02-22 22:27:40 +01:00
|
|
|
|
;;; possible values, constructs the list of all possible states.
|
|
|
|
|
(define (build-all-states vars-domains)
|
2020-02-23 09:09:43 +01:00
|
|
|
|
(let* ([var-dom-list (hash->list vars-domains)]
|
|
|
|
|
[vars (map car var-dom-list)]
|
|
|
|
|
[domains (map cdr var-dom-list)])
|
2020-02-22 22:27:40 +01:00
|
|
|
|
(for/list ([s (apply cartesian-product domains)])
|
|
|
|
|
(make-state (for/list ([var vars] [val s])
|
|
|
|
|
(cons var val))))))
|
2020-02-22 22:41:56 +01:00
|
|
|
|
|
2020-02-23 00:13:36 +01:00
|
|
|
|
;;; Makes a hash set mapping all variables to a single domain.
|
2020-02-23 00:15:18 +01:00
|
|
|
|
(define (make-same-domains vars domain)
|
2020-02-23 08:57:50 +01:00
|
|
|
|
(for/hash ([var vars]) (values var domain)))
|
2020-02-23 00:13:36 +01:00
|
|
|
|
|
|
|
|
|
;;; Makes a hash set mapping all variables to the Boolean domain.
|
2020-02-23 00:15:18 +01:00
|
|
|
|
(define (make-boolean-domains vars)
|
|
|
|
|
(make-same-domains vars '(#f #t)))
|
2020-02-23 00:13:36 +01:00
|
|
|
|
|
2020-02-23 20:13:37 +01:00
|
|
|
|
;;; Builds all boolean states possible over a given set of variables.
|
|
|
|
|
(define (build-all-boolean-states vars)
|
|
|
|
|
(build-all-states (make-boolean-domains vars)))
|
|
|
|
|
|
2020-02-23 00:04:19 +01:00
|
|
|
|
;;; Given two interacting variables of a network form and the domains
|
|
|
|
|
;;; of the variables, returns '+ if the interaction is monotonously
|
|
|
|
|
;;; increasing, '- if it is monotonously decreasing, and '0 otherwise.
|
|
|
|
|
;;;
|
|
|
|
|
;;; This function does not check whether the two variables indeed
|
|
|
|
|
;;; interact. Its behaviour is undefined if the variables do not
|
|
|
|
|
;;; interact.
|
|
|
|
|
;;;
|
|
|
|
|
;;; /!\ This function iterates through almost all of the states of the
|
|
|
|
|
;;; network, so its performance decreases very quickly with network
|
|
|
|
|
;;; size.
|
|
|
|
|
(define (get-interaction-sign network-form doms x y)
|
|
|
|
|
(let* ([dom-x (hash-ref doms x)]
|
|
|
|
|
[dom-y (hash-ref doms y)]
|
|
|
|
|
[network (network-form->network network-form)]
|
|
|
|
|
;; Replace the domain of x by a dummy singleton.
|
|
|
|
|
[doms-no-x (hash-set doms x '(#f))]
|
|
|
|
|
;; Build all the states, but as if x were not there: since I
|
|
|
|
|
;; replace its domain by a singleton, all states will contain
|
|
|
|
|
;; the same value for x.
|
2020-02-23 09:09:43 +01:00
|
|
|
|
[states-no-x (build-all-states doms-no-x)]
|
2020-02-23 00:04:19 +01:00
|
|
|
|
;; Go through all states, then through all ordered pairs of
|
|
|
|
|
;; values of x, generate pairs of states (s1, s2) such that x
|
|
|
|
|
;; has a smaller value in s1, and check that updating y in s1
|
|
|
|
|
;; yields a smaller value than updating y in s2. I rely on
|
|
|
|
|
;; the fact that the domains are ordered.
|
|
|
|
|
[x-y-interactions (for*/list ([s states-no-x]
|
|
|
|
|
[x1 dom-x] ; ordered pairs of values of x
|
|
|
|
|
[x2 (cdr (member x1 dom-x))])
|
|
|
|
|
(let* ([s1 (hash-set s x x1)] ; s1(x) < s2(x)
|
|
|
|
|
[s2 (hash-set s x x2)]
|
|
|
|
|
[y1 ((hash-ref network y) s1)]
|
|
|
|
|
[y2 ((hash-ref network y) s2)])
|
|
|
|
|
;; y1 <= y2?
|
|
|
|
|
(<= (index-of dom-y y1) (index-of dom-y y2))))])
|
|
|
|
|
(cond
|
|
|
|
|
;; If, in all interactions, y1 <= y2, then we have an
|
|
|
|
|
;; increasing/promoting interaction between x and y.
|
|
|
|
|
[(andmap (λ (x) (eq? x #t)) x-y-interactions) '+]
|
|
|
|
|
;; If, in all interactions, y1 > y2, then we have an
|
|
|
|
|
;; decreasing/inhibiting interaction between x and y.
|
|
|
|
|
[(andmap (λ (x) (eq? x #f)) x-y-interactions) '-]
|
|
|
|
|
;; Otherwise the interaction is neither increasing nor
|
|
|
|
|
;; decreasing.
|
|
|
|
|
[else '0])))
|
2020-02-23 01:00:09 +01:00
|
|
|
|
|
|
|
|
|
;;; Constructs a signed interaction graph of a given network form,
|
|
|
|
|
;;; given the ordered domains of its variables. The order on the
|
|
|
|
|
;;; domains determines the signs which will appear on the interaction
|
|
|
|
|
;;; graph.
|
|
|
|
|
;;;
|
|
|
|
|
;;; /!\ This function iterates through almost all states of the
|
|
|
|
|
;;; network for every arrow in the unsigned interaction graph, so its
|
|
|
|
|
;;; performance decreases very quickly with the size of the network.
|
|
|
|
|
(define (build-signed-interaction-graph network-form doms)
|
|
|
|
|
(let ([ig (build-interaction-graph network-form)])
|
|
|
|
|
(weighted-graph/directed
|
|
|
|
|
(for/list ([e (in-edges ig)])
|
|
|
|
|
(match-let ([(list x y) e])
|
|
|
|
|
(list (match (get-interaction-sign network-form doms x y)
|
|
|
|
|
['+ 1] ['- -1] ['0 0])
|
|
|
|
|
x y))))))
|
2020-02-23 10:00:48 +01:00
|
|
|
|
|
|
|
|
|
;;; Calls build-signed-interaction-graph with the Boolean domain for
|
|
|
|
|
;;; all variable.
|
|
|
|
|
;;;
|
|
|
|
|
;;; /!\ The same performance warning applies as for
|
|
|
|
|
;;; build-signed-interaction-graph.
|
|
|
|
|
(define (build-boolean-signed-interaction-graph network-form)
|
|
|
|
|
(build-signed-interaction-graph network-form
|
|
|
|
|
(make-boolean-domains (hash-keys network-form))))
|
2020-02-23 11:10:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; ====================
|
|
|
|
|
;;; Dynamics of networks
|
|
|
|
|
;;; ====================
|
|
|
|
|
|
|
|
|
|
;;; This section contains definitions for building and analysing the
|
|
|
|
|
;;; dynamics of networks.
|
|
|
|
|
|
2020-02-23 13:53:02 +01:00
|
|
|
|
;;; A modality is a set of variable.
|
|
|
|
|
(define modality? (set/c variable?))
|
|
|
|
|
|
|
|
|
|
;;; A mode is a set of modalities.
|
|
|
|
|
(define mode? (set/c modality?))
|
2020-02-23 11:10:18 +01:00
|
|
|
|
|
|
|
|
|
;;; A network dynamics is a network plus a mode.
|
2020-02-23 12:19:47 +01:00
|
|
|
|
(struct dynamics (network mode)
|
|
|
|
|
#:methods gen:dds
|
2020-03-02 12:12:57 +01:00
|
|
|
|
[;; Annotates each result state with the modality which lead to it.
|
2020-02-23 14:11:55 +01:00
|
|
|
|
(define/match (dds-step-one-annotated dyn s)
|
|
|
|
|
[((dynamics network mode) s)
|
|
|
|
|
(for/set ([m mode]) (cons m (update network s m)))])])
|
2020-02-23 11:25:19 +01:00
|
|
|
|
|
|
|
|
|
;;; Given a list of variables, builds the asynchronous mode (a set of
|
|
|
|
|
;;; singletons).
|
|
|
|
|
(define (make-asyn vars)
|
|
|
|
|
(for/set ([v vars]) (set v)))
|
|
|
|
|
|
|
|
|
|
;;; Given a list of variables, builds the synchronous mode (a set
|
|
|
|
|
;;; containing the set of variables).
|
|
|
|
|
(define (make-syn vars) (set (list->set vars)))
|
2020-02-23 11:42:01 +01:00
|
|
|
|
|
|
|
|
|
;;; Given a network, applies a function for building a mode to its
|
|
|
|
|
;;; variables and returns the corresponding network dynamics.
|
|
|
|
|
(define (make-dynamics-from-func network mode-func)
|
|
|
|
|
(dynamics network (mode-func (hash-keys network))))
|
|
|
|
|
|
|
|
|
|
;;; Creates the asynchronous dynamics for a given network.
|
|
|
|
|
(define (make-asyn-dynamics network)
|
|
|
|
|
(make-dynamics-from-func network make-asyn))
|
|
|
|
|
|
|
|
|
|
;;; Creates the synchronous dynamics for a given network.
|
|
|
|
|
(define (make-syn-dynamics network)
|
|
|
|
|
(make-dynamics-from-func network make-syn))
|
2020-02-23 19:24:53 +01:00
|
|
|
|
|
2020-03-04 18:54:36 +01:00
|
|
|
|
;;; Reads an Org-mode-produced sexp, converts it into a network, and
|
|
|
|
|
;;; builds the asyncronous dynamics out of it.
|
|
|
|
|
(define read-org-network-make-asyn (compose make-asyn-dynamics network-form->network read-org-variable-mapping))
|
|
|
|
|
|
|
|
|
|
;;; A shortcut for read-org-network-make-asyn.
|
|
|
|
|
(define-syntax-rule (unorg-asyn str) (read-org-network-make-asyn str))
|
|
|
|
|
|
|
|
|
|
;;; Reads an Org-mode-produced sexp, converts it into a network, and
|
|
|
|
|
;;; builds the synchronous dynamics out of it.
|
|
|
|
|
(define read-org-network-make-syn (compose make-syn-dynamics network-form->network read-org-variable-mapping))
|
|
|
|
|
|
|
|
|
|
;;; A shortcut for read-org-network-make-syn.
|
|
|
|
|
(define-syntax-rule (unorg-syn str) (read-org-network-make-syn str))
|
|
|
|
|
|
2020-02-23 19:24:53 +01:00
|
|
|
|
;;; Pretty-prints a state of the network.
|
|
|
|
|
(define (pretty-print-state s)
|
2020-02-26 21:27:02 +01:00
|
|
|
|
(string-join (hash-map s (λ (key val) (format "~a:~a" key val)) #t)))
|
2020-02-23 19:44:55 +01:00
|
|
|
|
|
|
|
|
|
;;; Converts any non-#f value to 1 and #f to 0.
|
|
|
|
|
(define (any->boolean x) (if x 1 0))
|
|
|
|
|
|
|
|
|
|
;;; Pretty-prints a state of the network to Boolean values 0 or 1.
|
|
|
|
|
(define (pretty-print-boolean-state s)
|
2020-02-26 21:27:02 +01:00
|
|
|
|
(string-join (hash-map s (λ (key val) (format "~a:~a" key (any->boolean val))) #t)))
|
2020-02-23 19:44:55 +01:00
|
|
|
|
|
|
|
|
|
;;; Given a state graph and a pretty-printer for states build a new
|
2020-03-01 15:20:44 +01:00
|
|
|
|
;;; state graph with pretty-printed vertices and edges.
|
2020-02-23 19:44:55 +01:00
|
|
|
|
(define (pretty-print-state-graph-with gr pprinter)
|
2020-03-02 23:59:37 +01:00
|
|
|
|
(update-graph gr #:v-func pprinter #:e-func pretty-print-set-sets))
|
2020-02-23 19:44:55 +01:00
|
|
|
|
|
|
|
|
|
;;; Pretty prints a state graph with pretty-print-state.
|
|
|
|
|
(define (pretty-print-state-graph gr)
|
|
|
|
|
(pretty-print-state-graph-with gr pretty-print-state))
|
|
|
|
|
|
|
|
|
|
;;; A shortcut for pretty-print-state-graph.
|
|
|
|
|
(define-syntax-rule (ppsg gr) (pretty-print-state-graph gr))
|
|
|
|
|
|
|
|
|
|
;;; Pretty prints a state graph with pretty-print-boolean-state.
|
|
|
|
|
(define (pretty-print-boolean-state-graph gr)
|
|
|
|
|
(pretty-print-state-graph-with gr pretty-print-boolean-state))
|
|
|
|
|
|
|
|
|
|
;;; A shortcut for pretty-print-boolean-state-graph.
|
|
|
|
|
(define-syntax-rule (ppsgb gr) (pretty-print-boolean-state-graph gr))
|
2020-02-23 20:13:37 +01:00
|
|
|
|
|
|
|
|
|
;;; Builds the full state graph of a Boolean network.
|
|
|
|
|
(define (build-full-boolean-state-graph dyn)
|
|
|
|
|
(dds-build-state-graph
|
|
|
|
|
dyn
|
|
|
|
|
(list->set (build-all-boolean-states (hash-keys (dynamics-network dyn))))))
|
2020-02-28 21:56:42 +01:00
|
|
|
|
|
|
|
|
|
;;; Build the full annotated state graph of a Boolean network.
|
|
|
|
|
(define (build-full-boolean-state-graph-annotated dyn)
|
|
|
|
|
(dds-build-state-graph-annotated
|
|
|
|
|
dyn
|
|
|
|
|
(list->set (build-all-boolean-states (hash-keys (dynamics-network dyn))))))
|
2020-03-15 16:12:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; =========
|
|
|
|
|
;;; Functions
|
|
|
|
|
;;; =========
|
|
|
|
|
|
|
|
|
|
;;; Given a function and a list of domains for each of its arguments,
|
|
|
|
|
;;; in order, produces a list of lists giving the values of arguments
|
|
|
|
|
;;; and the value of the functions for these inputs.
|
|
|
|
|
(define (tabulate/domain-list func doms)
|
|
|
|
|
(for/list ([xs (apply cartesian-product doms)])
|
|
|
|
|
(append xs (list (apply func xs)))))
|
|
|
|
|
|
|
|
|
|
;;; Like tabulate, but the domains are given as a rest argument.
|
|
|
|
|
(define (tabulate func . doms) (tabulate/domain-list func doms))
|
|
|
|
|
|
|
|
|
|
;;; Like tabulate, but assumes the domains of all variables of the
|
|
|
|
|
;;; function are Boolean. func must have a fixed arity. It is an
|
|
|
|
|
;;; error to supply a function of variable arity.
|
|
|
|
|
(define (tabulate/boolean func)
|
|
|
|
|
(tabulate/domain-list func (make-list (procedure-arity func) '(#f #t))))
|
2020-03-18 21:40:09 +01:00
|
|
|
|
|
|
|
|
|
;;; Given a table like the one produced by the tabulate functions,
|
|
|
|
|
;;; creates a function which has this behaviour.
|
|
|
|
|
;;;
|
|
|
|
|
;;; More exactly, the input is a list of lists of values. All but the
|
|
|
|
|
;;; last elements of every list give the values of the parameters of
|
|
|
|
|
;;; the function, while the the last element of every list gives the
|
|
|
|
|
;;; value of the function. Thus, every list should have at least two
|
|
|
|
|
;;; elements.
|
|
|
|
|
;;;
|
|
|
|
|
;;; The produced function is implemented via lookups in hash tables,
|
|
|
|
|
;;; meaning that it may be sometimes more expensive to compute than by
|
|
|
|
|
;;; using an direct symbolic implementation.
|
|
|
|
|
(define (table->function table)
|
2020-03-19 23:17:23 +01:00
|
|
|
|
(let ([func (table->function/list table)])
|
|
|
|
|
(λ args (func args))))
|
2020-03-18 21:40:09 +01:00
|
|
|
|
|
|
|
|
|
;;; Like table->function, but the produced function accepts a single
|
|
|
|
|
;;; list of arguments instead of individual arguments.
|
|
|
|
|
(define (table->function/list table)
|
|
|
|
|
((curry hash-ref)
|
|
|
|
|
(for/hash ([line table])
|
|
|
|
|
(let-values ([(x fx) (split-at-right line 1)])
|
|
|
|
|
(values x (car fx))))))
|
2020-03-19 23:40:08 +01:00
|
|
|
|
|
|
|
|
|
;;; Returns the n-th Cartesian power of the Boolean domain: {0,1}^n.
|
|
|
|
|
(define (boolean-power-n n) (apply cartesian-product (make-list n '(#f #t))))
|
|
|
|
|
|
2020-03-20 16:40:15 +01:00
|
|
|
|
;;; Like boolean-power-n, but returns a stream whose elements the
|
|
|
|
|
;;; elements of the Cartesian power.
|
|
|
|
|
(define (boolean-power-n/stream n) (apply cartesian-product/stream (make-list n '(#f #t))))
|
|
|
|
|
|
2020-03-20 00:15:51 +01:00
|
|
|
|
;;; Returns the stream of the truth tables of all Boolean functions of
|
|
|
|
|
;;; a given arity.
|
2020-03-19 23:40:08 +01:00
|
|
|
|
;;;
|
2020-03-20 00:15:51 +01:00
|
|
|
|
;;; There are 2^(2^n) Boolean functions of arity n.
|
2020-03-19 23:40:08 +01:00
|
|
|
|
(define (enumerate-boolean-tables n)
|
|
|
|
|
(let ([inputs (boolean-power-n n)]
|
|
|
|
|
[outputs (boolean-power-n (expt 2 n))])
|
2020-03-20 00:15:51 +01:00
|
|
|
|
(for/stream ([out outputs])
|
2020-03-19 23:40:08 +01:00
|
|
|
|
(for/list ([in inputs] [o out])
|
|
|
|
|
(append in (list o))))))
|
|
|
|
|
|
2020-03-20 00:15:51 +01:00
|
|
|
|
;;; Returns the stream of all Boolean functions of a given arity.
|
2020-03-19 23:40:08 +01:00
|
|
|
|
;;;
|
2020-03-20 00:15:51 +01:00
|
|
|
|
;;; There are 2^(2^n) Boolean functions of arity n.
|
2020-03-19 23:40:08 +01:00
|
|
|
|
(define (enumerate-boolean-functions n)
|
2020-03-20 00:15:51 +01:00
|
|
|
|
(stream-map table->function (enumerate-boolean-tables n)))
|
2020-03-19 23:40:08 +01:00
|
|
|
|
|
2020-03-20 00:15:51 +01:00
|
|
|
|
;;; Returns the stream of all Boolean functions of a given arity. As
|
|
|
|
|
;;; different from the functions returned by
|
|
|
|
|
;;; enumerate-boolean-functions, the functions take lists of arguments
|
|
|
|
|
;;; instead of n arguments.
|
2020-03-19 23:40:08 +01:00
|
|
|
|
;;;
|
2020-03-20 00:15:51 +01:00
|
|
|
|
;;; There are 2^(2^n) Boolean functions of arity n.
|
2020-03-19 23:40:08 +01:00
|
|
|
|
(define (enumerate-boolean-functions/list n)
|
2020-03-20 00:15:51 +01:00
|
|
|
|
(stream-map table->function/list (enumerate-boolean-tables n)))
|