Fix Automaton library to match PLDI paper.
This commit is contained in:
parent
ecaa5bdb29
commit
a66a4b1fab
1 changed files with 35 additions and 29 deletions
|
@ -4,54 +4,60 @@
|
||||||
-- it can be used.
|
-- it can be used.
|
||||||
module Automaton where
|
module Automaton where
|
||||||
|
|
||||||
data Automaton a b = Automaton (a -> (b, Automaton a b))
|
data Automaton a b = Step (a -> (Automaton a b, b))
|
||||||
|
|
||||||
-- Run an automaton on a given signal. The automaton takes in ‘a’ values and returns ‘b’ values. The automaton steps forward whenever the input signal updates.
|
-- Run an automaton on a given signal. The automaton steps forward
|
||||||
run : Automaton a b -> Signal a -> Signal b
|
-- whenever the input signal updates.
|
||||||
run (Automaton m0) input =
|
run : Automaton a b -> b -> Signal a -> Signal b
|
||||||
lift fst $ foldp' (\a (b, Automaton m) -> m a) m0 input
|
run (Step f) base inputs =
|
||||||
|
let step a (Step f, _) = f a
|
||||||
|
in lift snd $ foldp step base inputs
|
||||||
|
|
||||||
-- Step an automaton forward once with a given input.
|
-- Step an automaton forward once with a given input.
|
||||||
step : Automaton a b -> a -> (b, Automaton a b)
|
step : a -> Automaton a b -> (Automaton a b, b)
|
||||||
step (Automaton m) a = m a
|
step a (Step f) = f a
|
||||||
|
|
||||||
|
|
||||||
-- Compose two automatons, chaining them together.
|
-- Compose two automatons, chaining them together.
|
||||||
(>>>) : Automaton a b -> Automaton b c -> Automaton a c
|
(>>>) : Automaton a b -> Automaton b c -> Automaton a c
|
||||||
a1 >>> a2 =
|
f >>> g =
|
||||||
let Automaton m1 = a1
|
Step (\a -> let (f', b) = step a f
|
||||||
Automaton m2 = a2
|
(g', c) = step b g
|
||||||
in Automaton (\a -> let (b,m1') = m1 a
|
in (f' >>> g', c))
|
||||||
(c,m2') = m2 b
|
|
||||||
in (c, m1' >>> m2'))
|
|
||||||
|
|
||||||
-- Compose two automatons, chaining them together.
|
-- Compose two automatons, chaining them together.
|
||||||
(<<<) : Automaton b c -> Automaton a b -> Automaton a c
|
(<<<) : Automaton b c -> Automaton a b -> Automaton a c
|
||||||
a2 <<< a1 = a1 >>> a2
|
g <<< f = f >>> g
|
||||||
|
|
||||||
-- Combine a list of automatons into a single automaton that produces a list.
|
-- Combine a list of automatons into a single automaton that produces a list.
|
||||||
combine : [Automaton a b] -> Automaton a [b]
|
combine : [Automaton a b] -> Automaton a [b]
|
||||||
combine autos =
|
combine autos =
|
||||||
Automaton (\a -> let (bs,autos') = unzip $ map (\(Automaton m) -> m a) autos in
|
Step (\a -> let (autos', bs) = unzip $ map (step a) autos
|
||||||
(bs, combine autos'))
|
in (combine autos', bs))
|
||||||
|
|
||||||
-- Create an automaton with no memory. It just applies the given function to every input.
|
-- Create an automaton with no memory. It just applies the given function to
|
||||||
|
-- every input.
|
||||||
pure : (a -> b) -> Automaton a b
|
pure : (a -> b) -> Automaton a b
|
||||||
pure f = Automaton (\x -> (f x, pure f))
|
pure f = Step (\x -> (pure f, f x))
|
||||||
|
|
||||||
-- Create an automaton with no memory. It just applies the given function to every input.
|
-- 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 : b -> (a -> b -> b) -> Automaton a b
|
||||||
init s step = Automaton (\a -> let s' = step a s in (s', init s' step))
|
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.
|
-- Create an automaton with hidden state. Requires an initial state and a
|
||||||
init' : s -> (a -> s -> (b,s)) -> Automaton a b
|
-- step function to step the state forward and produce an output.
|
||||||
init' s step = Automaton (\a -> let (b,s') = step a s in (b , init' s' step))
|
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))
|
||||||
|
|
||||||
-- Count the number of steps taken.
|
-- Count the number of steps taken.
|
||||||
count : Automaton a Int
|
count : Automaton a Int
|
||||||
count = init 0 (\_ c -> c + 1)
|
count = init 0 (\_ c -> c + 1)
|
||||||
|
|
||||||
|
|
||||||
|
{-- TODO(evancz): move this code to the Form library so people can find it.
|
||||||
|
|
||||||
data DragState = Listen | Ignore | DragFrom (Int,Int)
|
data DragState = Listen | Ignore | DragFrom (Int,Int)
|
||||||
|
|
||||||
vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
|
vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
|
||||||
|
@ -71,15 +77,15 @@ stepDrag (press,pos) (ds,form) =
|
||||||
-- Create a draggable form that can be dynamically created and added to a scene.
|
-- Create a draggable form that can be dynamically created and added to a scene.
|
||||||
draggable : Form -> Automaton (Bool,(Int,Int)) Form
|
draggable : Form -> Automaton (Bool,(Int,Int)) Form
|
||||||
draggable form = init' (Listen,form) stepDrag
|
draggable form = init' (Listen,form) stepDrag
|
||||||
|
--}
|
||||||
|
|
||||||
|
{-- TODO(evancz): See the following papers for ideas on how to make this
|
||||||
{--- See the following papers for ideas on how to make this faster:
|
library faster and better:
|
||||||
|
|
||||||
- Functional Reactive Programming, Continued
|
- Functional Reactive Programming, Continued
|
||||||
- Causal commutative arrows and their optimization
|
- Causal commutative arrows and their optimization
|
||||||
|
|
||||||
Speeding things up is a really low priority. Language features and
|
Speeding things up is a really low priority. Language features and
|
||||||
libraries with nice APIs and are way more important!
|
libraries with nice APIs and are way more important!
|
||||||
|
|
||||||
--}
|
--}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue