on 104faf1 help sign in
elk/libraries/Automaton.elm
111 lines 4.0 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
7data Automaton a b = Step (a -> (Automaton a b, b))
9-- Run an automaton on a given signal. The automaton steps forward
10-- whenever the input signal updates.
11run : Automaton a b -> b -> Signal a -> Signal b
12run (Step f) base inputs =
13 let step a (Step f, _) = f a
14 in lift snd $ foldp step base inputs
16-- Step an automaton forward once with a given input.
17step : a -> Automaton a b -> (Automaton a b, b)
18step a (Step f) = f a
20-- Compose two automatons, chaining them together.
21(>>>) : Automaton a b -> Automaton b c -> Automaton a c
22f >>> g =
23 Step (\a -> let (f', b) = step a f
24 (g', c) = step b g
25 in (f' >>> g', c))
27-- Compose two automatons, chaining them together.
28(<<<) : Automaton b c -> Automaton a b -> Automaton a c
29g <<< f = f >>> g
31-- Combine a list of automatons into a single automaton that produces a list.
32combine : [Automaton a b] -> Automaton a [b]
33combine autos =
34 Step (\a -> let (autos', bs) = unzip $ map (step a) autos
35 in (combine autos', bs))
37-- Create an automaton with no memory. It just applies the given function to
38-- every input.
39pure : (a -> b) -> Automaton a b
40pure f = Step (\x -> (pure f, f x))
42-- Create an automaton with state. Requires an initial state and a step
43-- function to step the state forward.
44init : b -> (a -> b -> b) -> Automaton a b
45init s f = Step (\x -> let s' = f x s
46 in (init s' f, s'))
48-- Create an automaton with hidden state. Requires an initial state and a
49-- step function to step the state forward and produce an output.
50init' : s -> (a -> s -> (s,b)) -> Automaton a b
51init' s f = Step (\x -> let (s',out) = f x s
52 in (init' s' f, out))
54-- Count the number of steps taken.
55count : Automaton a Int
56count = init 0 (\_ c -> c + 1)
58type Queue t = ([t],[t])
59empty = ([],[])
60enqueue x (en,de) = (x::en, de)
61dequeue q = case q of
62 ([],[]) -> Nothing
63 (en,[]) -> enqueue ([], reverse en)
64 (en,hd::tl) -> Just (hd, (en,tl))
66-- Computes the running average of the last `n` inputs.
67average : Int -> Automaton (Number a) (Number a)
68average k =
69 let step n (ns,len,sum) =
70 if len == k then stepFull n (ns,len,sum)
71 else ((enqueue n ns, len+1, sum+n), (sum+n) / (len+1))
72 stepFull n (ns,len,sum) =
73 case dequeue ns of
74 Nothing -> ((ns,len,sum), 0)
75 Just (m,ns') -> let sum' = sum + n - m
76 in ((enqueue n ns', len, sum'), sum' / len)
77 in init' (empty,0,0) step
79{-- TODO(evancz): move this code to the Form library so people can find it.
81data DragState = Listen | Ignore | DragFrom (Int,Int)
83vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
85stepDrag (press,pos) (ds,form) =
86 let wrap ds' = (form, (ds',form)) in
87 case ds of
88 Listen -> wrap (if | not press -> Listen
89 | pos `isWithin` form -> DragFrom pos
90 | otherwise -> Ignore)
91 Ignore -> wrap (if press then Ignore else Listen)
92 DragFrom p0 ->
93 if press then (uncurry move (vecSub pos p0) form, (DragFrom p0, form))
94 else (let form' = uncurry move (vecSub pos p0) form in
95 (form', (Listen,form')))
97-- Create a draggable form that can be dynamically created and added to a scene.
98draggable : Form -> Automaton (Bool,(Int,Int)) Form
99draggable form = init' (Listen,form) stepDrag
100--}
102{-- TODO(evancz): See the following papers for ideas on how to make this
103library faster and better:
105- Functional Reactive Programming, Continued
106- Causal commutative arrows and their optimization
108Speeding things up is a really low priority. Language features and
109libraries with nice APIs and are way more important!
110--}