2012-10-05 01:13:00 +00:00
|
|
|
|
|
|
|
module Automaton where
|
|
|
|
|
2012-10-05 01:40:49 +00:00
|
|
|
data Automaton a b = Automaton (a -> (b, Automaton a b))
|
|
|
|
|
2013-02-06 06:26:52 +00:00
|
|
|
run : Automaton a b -> Signal a -> Signal b
|
2012-10-05 01:40:49 +00:00
|
|
|
run (Automaton m0) input =
|
2012-10-05 21:14:30 +00:00
|
|
|
lift fst $ foldp' (\a (b, Automaton m) -> m a) m0 input
|
2012-10-05 01:40:49 +00:00
|
|
|
|
2013-02-06 06:26:52 +00:00
|
|
|
step : Automaton a b -> a -> (b, Automaton a b)
|
2012-10-19 07:13:28 +00:00
|
|
|
step (Automaton m) a = m a
|
2012-10-07 06:14:42 +00:00
|
|
|
|
|
|
|
|
2013-02-10 11:58:40 +00:00
|
|
|
(>>>) : Automaton a b -> Automaton b c -> Automaton a c
|
2012-11-23 04:31:55 +00:00
|
|
|
a1 >>> a2 =
|
2013-02-06 06:26:52 +00:00
|
|
|
let Automaton m1 = a1
|
|
|
|
Automaton m2 = a2
|
|
|
|
in Automaton (\a -> let (b,m1') = m1 a
|
|
|
|
(c,m2') = m2 b
|
|
|
|
in (c, m1' >>> m2'))
|
2012-10-05 21:14:30 +00:00
|
|
|
|
2013-02-10 11:58:40 +00:00
|
|
|
(<<<) : Automaton b c -> Automaton a b -> Automaton a c
|
2012-11-23 04:31:55 +00:00
|
|
|
a2 <<< a1 = a1 >>> a2
|
2012-10-05 01:40:49 +00:00
|
|
|
|
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 =
|
2012-10-07 06:14:42 +00:00
|
|
|
Automaton (\a -> let (bs,autos') = unzip $ map (\(Automaton m) -> m a) autos in
|
2012-10-05 21:14:30 +00:00
|
|
|
(bs, combine autos'))
|
|
|
|
|
2013-02-06 06:26:52 +00:00
|
|
|
pure : (a -> b) -> Automaton a b
|
2012-10-05 21:14:30 +00:00
|
|
|
pure f = Automaton (\x -> (f x, pure f))
|
2013-02-06 06:26:52 +00:00
|
|
|
|
|
|
|
init : b -> (a -> b -> b) -> Automaton a b
|
2012-10-07 06:14:42 +00:00
|
|
|
init s step = Automaton (\a -> let s' = step a s in (s', init s' step))
|
2013-02-06 06:26:52 +00:00
|
|
|
|
|
|
|
init' : s -> (a -> s -> (b,s)) -> Automaton a b
|
2012-10-07 06:14:42 +00:00
|
|
|
init' s step = Automaton (\a -> let (b,s') = step a s in (b , init' s' step))
|
2012-10-05 21:14:30 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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-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
|
2012-10-07 06:14:42 +00:00
|
|
|
|
2012-10-05 21:14:30 +00:00
|
|
|
|
|
|
|
{--- See the following papers for ideas on how to make this faster:
|
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!
|
2012-10-05 01:55:06 +00:00
|
|
|
|
2012-10-05 21:14:30 +00:00
|
|
|
--}
|