From 53d7c7cfb800f12b3438cbe2a6b61174d5b4c1ad Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Mon, 30 Dec 2013 17:59:39 +0100 Subject: [PATCH] expose more functions in Automaton --- automaton.ml | 8 ++++++-- automaton.mli | 23 +++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/automaton.ml b/automaton.ml index f35273b6..53748039 100644 --- a/automaton.ml +++ b/automaton.ml @@ -67,7 +67,9 @@ let product f1 f2 (s1, s2) i = module I = struct type 'a t = 'a -> unit - let send x i = i x + let create f = f + + let send x i = x i let comap f i x = i (f x) @@ -153,7 +155,7 @@ module O = struct end let connect o i = - O.on o (fun x -> I.send x i; true) + O.on o (fun x -> I.send i x; true) module Instance = struct type ('s, 'i, 'o) t = { @@ -174,6 +176,8 @@ module Instance = struct let transitions a = a.transitions + let send a i = I.send a.i i + let _q = Queue.create () let _process q = diff --git a/automaton.mli b/automaton.mli index f633deb1..005607aa 100644 --- a/automaton.mli +++ b/automaton.mli @@ -61,17 +61,14 @@ Input sink, that accepts values of a given type. Cofunctor. *) module I : sig type -'a t + val create : ('a -> unit) -> 'a t + val comap : ('a -> 'b) -> 'b t -> 'a t val filter : ('a -> bool) -> 'a t -> 'a t - val send : 'a -> 'a t -> unit - (** [send a i] uses [a]'s transition function to update [a] with the input - event [i]. The output of the transition function (a list of outputs) is - recursively processed. - - This may not terminate, if the automata keep on creating new outputs that - trigger other outputs forever. *) + val send : 'a t -> 'a -> unit + (** [send a i] inputs [i] on the channel [a]. *) end (** {2 Output} @@ -81,6 +78,8 @@ Stream of output values. Functor. *) module O : sig type 'a t + val create : unit -> 'a t + val map : ('a -> 'b) -> 'a t -> 'b t val filter : ('a -> bool) -> 'a t -> 'a t @@ -89,6 +88,8 @@ module O : sig val once : 'a t -> ('a -> unit) -> unit + val send : 'a t -> 'a -> unit + val propagate : 'a t -> 'a t -> unit (** [propagate a b] forwards all elements of [a] into [b]. As long as [a] exists, [b] will not be GC'ed. *) @@ -115,7 +116,13 @@ module Instance : sig val transitions : ('s, 'i, 'o) t -> ('s * 'i * 's * 'o list) O.t + val send : (_, 'i, _) t -> 'i -> unit + (** Shortcut to send an input *) + val create : f:('s, 'i, 'o) automaton -> 's -> ('s, 'i, 'o) t (** [create ~f init] creates an instance of [f] with initial state - [init]. *) + [init]. + + @param f the transition function + @param init the initial state *) end