2013-03-24 12:45:56 +00:00
|
|
|
-- This library is a way to package up dynamic behavior. It makes it easier to
|
|
|
|
-- dynamically create dynamic components. See the [original release
|
|
|
|
-- notes](/blog/announce/version-0.5.0.elm) on this library to get a feel for how
|
|
|
|
-- it can be used.
|
2012-10-05 01:13:00 +00:00
|
|
|
module Automaton where
|
|
|
|
|
2013-03-28 01:56:30 +00:00
|
|
|
data Automaton a b = Step (a -> (Automaton a b, b))
|
2012-10-05 01:40:49 +00:00
|
|
|
|
2013-03-28 01:56:30 +00:00
|
|
|
-- Run an automaton on a given signal. The automaton steps forward
|
|
|
|
-- whenever the input signal updates.
|
|
|
|
run : Automaton a b -> b -> Signal a -> Signal b
|
|
|
|
run (Step f) base inputs =
|
|
|
|
let step a (Step f, _) = f a
|
|
|
|
in lift snd $ foldp step base inputs
|
2012-10-05 01:40:49 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Step an automaton forward once with a given input.
|
2013-03-28 01:56:30 +00:00
|
|
|
step : a -> Automaton a b -> (Automaton a b, b)
|
|
|
|
step a (Step f) = f a
|
2012-10-07 06:14:42 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Compose two automatons, chaining them together.
|
2013-02-10 11:58:40 +00:00
|
|
|
(>>>) : Automaton a b -> Automaton b c -> Automaton a c
|
2013-03-28 01:56:30 +00:00
|
|
|
f >>> g =
|
|
|
|
Step (\a -> let (f', b) = step a f
|
|
|
|
(g', c) = step b g
|
|
|
|
in (f' >>> g', c))
|
2012-10-05 21:14:30 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Compose two automatons, chaining them together.
|
2013-02-10 11:58:40 +00:00
|
|
|
(<<<) : Automaton b c -> Automaton a b -> Automaton a c
|
2013-03-28 01:56:30 +00:00
|
|
|
g <<< f = f >>> g
|
2012-10-05 01:40:49 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Combine a list of automatons into a single automaton that produces a list.
|
2013-02-06 06:26:52 +00:00
|
|
|
combine : [Automaton a b] -> Automaton a [b]
|
2012-10-05 01:40:49 +00:00
|
|
|
combine autos =
|
2013-03-28 01:56:30 +00:00
|
|
|
Step (\a -> let (autos', bs) = unzip $ map (step a) autos
|
|
|
|
in (combine autos', bs))
|
|
|
|
|
|
|
|
-- Create an automaton with no memory. It just applies the given function to
|
|
|
|
-- every input.
|
|
|
|
pure : (a -> b) -> Automaton a b
|
|
|
|
pure f = Step (\x -> (pure f, f x))
|
|
|
|
|
|
|
|
-- Create an automaton with state. Requires an initial state and a step
|
|
|
|
-- function to step the state forward.
|
|
|
|
init : b -> (a -> b -> b) -> Automaton a b
|
|
|
|
init s f = Step (\x -> let s' = f x s
|
|
|
|
in (init s' f, s'))
|
|
|
|
|
|
|
|
-- Create an automaton with hidden state. Requires an initial state and a
|
|
|
|
-- step function to step the state forward and produce an output.
|
|
|
|
init' : s -> (a -> s -> (s,b)) -> Automaton a b
|
|
|
|
init' s f = Step (\x -> let (s',out) = f x s
|
|
|
|
in (init' s' f, out))
|
2012-10-05 21:14:30 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Count the number of steps taken.
|
2013-02-06 06:26:52 +00:00
|
|
|
count : Automaton a Int
|
2012-10-05 21:14:30 +00:00
|
|
|
count = init 0 (\_ c -> c + 1)
|
|
|
|
|
2013-04-03 17:15:46 +00:00
|
|
|
type Queue t = ([t],[t])
|
|
|
|
empty = ([],[])
|
2013-04-22 09:36:11 +00:00
|
|
|
enqueue x (en,de) = (x::en, de)
|
2013-04-03 17:15:46 +00:00
|
|
|
dequeue q = case q of
|
|
|
|
([],[]) -> Nothing
|
|
|
|
(en,[]) -> enqueue ([], reverse en)
|
|
|
|
(en,hd::tl) -> Just (hd, (en,tl))
|
|
|
|
|
|
|
|
-- Computes the running average of the last `n` inputs.
|
2013-04-22 09:36:11 +00:00
|
|
|
average : Int -> Automaton (Number a) (Number a)
|
2013-04-03 17:15:46 +00:00
|
|
|
average k =
|
|
|
|
let step n (ns,len,sum) =
|
|
|
|
if len == k then stepFull n (ns,len,sum)
|
|
|
|
else ((enqueue n ns, len+1, sum+n), (sum+n) / (len+1))
|
|
|
|
stepFull n (ns,len,sum) =
|
|
|
|
case dequeue ns of
|
|
|
|
Nothing -> ((ns,len,sum), 0)
|
|
|
|
Just (m,ns') -> let sum' = sum + n - m
|
|
|
|
in ((enqueue n ns', len, sum'), sum' / len)
|
|
|
|
in init' (empty,0,0) step
|
2013-04-22 09:36:11 +00:00
|
|
|
|
2013-03-28 01:56:30 +00:00
|
|
|
{-- TODO(evancz): move this code to the Form library so people can find it.
|
|
|
|
|
2012-10-07 06:14:42 +00:00
|
|
|
data DragState = Listen | Ignore | DragFrom (Int,Int)
|
|
|
|
|
|
|
|
vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
|
|
|
|
|
|
|
|
stepDrag (press,pos) (ds,form) =
|
|
|
|
let wrap ds' = (form, (ds',form)) in
|
|
|
|
case ds of
|
2013-02-10 11:58:40 +00:00
|
|
|
Listen -> wrap (if | not press -> Listen
|
|
|
|
| pos `isWithin` form -> DragFrom pos
|
|
|
|
| otherwise -> Ignore)
|
|
|
|
Ignore -> wrap (if press then Ignore else Listen)
|
|
|
|
DragFrom p0 ->
|
|
|
|
if press then (uncurry move (vecSub pos p0) form, (DragFrom p0, form))
|
|
|
|
else (let form' = uncurry move (vecSub pos p0) form in
|
|
|
|
(form', (Listen,form')))
|
2012-10-07 06:14:42 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Create a draggable form that can be dynamically created and added to a scene.
|
2013-02-06 06:26:52 +00:00
|
|
|
draggable : Form -> Automaton (Bool,(Int,Int)) Form
|
2012-10-19 07:27:12 +00:00
|
|
|
draggable form = init' (Listen,form) stepDrag
|
2013-03-28 01:56:30 +00:00
|
|
|
--}
|
2012-10-07 06:14:42 +00:00
|
|
|
|
2013-03-28 01:56:30 +00:00
|
|
|
{-- TODO(evancz): See the following papers for ideas on how to make this
|
|
|
|
library faster and better:
|
2012-10-05 01:40:49 +00:00
|
|
|
|
2012-10-05 21:14:30 +00:00
|
|
|
- Functional Reactive Programming, Continued
|
|
|
|
- Causal commutative arrows and their optimization
|
2012-10-05 01:40:49 +00:00
|
|
|
|
2012-10-05 21:14:30 +00:00
|
|
|
Speeding things up is a really low priority. Language features and
|
|
|
|
libraries with nice APIs and are way more important!
|
2013-03-24 12:45:56 +00:00
|
|
|
--}
|
|
|
|
|