on a3801ac help sign in
elk/libraries/Automaton.elm
98 lines 3.5 kB history raw
1-- This library is a way to package up dynamic behavior. It makes it easier to
2-- dynamically create dynamic components. See the [original release
3-- notes](/blog/announce/version-0.5.0.elm) on this library to get a feel for how
4-- it can be used.
5module Automaton where
7import Signal (lift,foldp)
9data Automaton a b = Step (a -> (Automaton a b, b))
11-- Run an automaton on a given signal. The automaton steps forward
12-- whenever the input signal updates.
13run : Automaton a b -> b -> Signal a -> Signal b
14run auto base inputs =
15 let step a (Step f, _) = f a
16 in lift (\(x,y) -> y) <| foldp step (auto,base) inputs
18-- Step an automaton forward once with a given input.
19step : a -> Automaton a b -> (Automaton a b, b)
20step a (Step f) = f a
22-- Compose two automatons, chaining them together.
23(>>>) : Automaton a b -> Automaton b c -> Automaton a c
24f >>> g =
25 Step (\a -> let (f', b) = step a f
26 (g', c) = step b g
27 in (f' >>> g', c))
29-- Compose two automatons, chaining them together.
30(<<<) : Automaton b c -> Automaton a b -> Automaton a c
31g <<< f = f >>> g
33-- Combine a list of automatons into a single automaton that produces a list.
34combine : [Automaton a b] -> Automaton a [b]
35combine autos =
36 Step (\a -> let (autos', bs) = unzip $ map (step a) autos
37 in (combine autos', bs))
39-- Create an automaton with no memory. It just applies the given function to
40-- every input.
41pure : (a -> b) -> Automaton a b
42pure f = Step (\x -> (pure f, f x))
44-- Create an automaton with state. Requires an initial state and a step
45-- function to step the state forward. For example, an automaton that counted
46-- how many steps it has taken would look like this:
47--
48-- count = Automaton a Int
49-- count = state 0 (\\_ c -> c+1)
50--
51-- It is a stateful automaton. The initial state is zero, and the step function
52-- increments the state on every step.
53state : b -> (a -> b -> b) -> Automaton a b
54state s f = Step (\x -> let s' = f x s
55 in (state s' f, s'))
57-- Create an automaton with hidden state. Requires an initial state and a
58-- step function to step the state forward and produce an output.
59hiddenState : s -> (a -> s -> (s,b)) -> Automaton a b
60hiddenState s f = Step (\x -> let (s',out) = f x s
61 in (hiddenState s' f, out))
63-- Count the number of steps taken.
64count : Automaton a Int
65count = state 0 (\_ c -> c + 1)
67type Queue t = ([t],[t])
68empty = ([],[])
69enqueue x (en,de) = (x::en, de)
70dequeue q = case q of
71 ([],[]) -> Nothing
72 (en,[]) -> enqueue ([], reverse en)
73 (en,hd::tl) -> Just (hd, (en,tl))
75-- Computes the running average of the last `n` inputs.
76average : Int -> Automaton Float Float
77average k =
78 let step n (ns,len,sum) =
79 if len == k then stepFull n (ns,len,sum)
80 else ((enqueue n ns, len+1, sum+n), (sum+n) / (len+1))
81 stepFull n (ns,len,sum) =
82 case dequeue ns of
83 Nothing -> ((ns,len,sum), 0)
84 Just (m,ns') -> let sum' = sum + n - m
85 in ((enqueue n ns', len, sum'), sum' / len)
86 in hiddenState (empty,0,0) step
89{-- TODO(evancz): See the following papers for ideas on how to make this
90library faster and better:
92- Functional Reactive Programming, Continued
93- Causal commutative arrows and their optimization
95Speeding things up is a really low priority. Language features and
96libraries with nice APIs and are way more important!
97--}