on 294fdfd help sign in
elk/libraries/Automaton.elm
91 lines 3.3 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)
59{-- TODO(evancz): move this code to the Form library so people can find it.
61data DragState = Listen | Ignore | DragFrom (Int,Int)
63vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
65stepDrag (press,pos) (ds,form) =
66 let wrap ds' = (form, (ds',form)) in
67 case ds of
68 Listen -> wrap (if | not press -> Listen
69 | pos `isWithin` form -> DragFrom pos
70 | otherwise -> Ignore)
71 Ignore -> wrap (if press then Ignore else Listen)
72 DragFrom p0 ->
73 if press then (uncurry move (vecSub pos p0) form, (DragFrom p0, form))
74 else (let form' = uncurry move (vecSub pos p0) form in
75 (form', (Listen,form')))
77-- Create a draggable form that can be dynamically created and added to a scene.
78draggable : Form -> Automaton (Bool,(Int,Int)) Form
79draggable form = init' (Listen,form) stepDrag
80--}
82{-- TODO(evancz): See the following papers for ideas on how to make this
83library faster and better:
85- Functional Reactive Programming, Continued
86- Causal commutative arrows and their optimization
88Speeding things up is a really low priority. Language features and
89libraries with nice APIs and are way more important!
90--}