@@ -1,7 +1,22 @@
1−-- This library is a way to package up dynamic behavior. It makes it easier to
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
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
3+notes](/blog/announce/version-0.5.0.elm) on this library to get a feel for how
4−-- it can be used.
4+it can be used.
6+# Running and stepping forward Automatons
9+# Putting Automatons together
13+@docs combine, pure, state, hiddenState
15+# Measuring Automatons
5module Automaton where
20module Automaton where
7import open Basics
22import open Basics
@@ -11,59 +26,66 @@ import Maybe (Just, Nothing)
12data Automaton a b = Step (a -> (Automaton a b, b))
27data Automaton a b = Step (a -> (Automaton a b, b))
14−-- Run an automaton on a given signal. The automaton steps forward
29+{-| Run an automaton on a given signal. The automaton steps forward whenever the
15−-- whenever the input signal updates.
30+input signal updates.
16run : Automaton a b -> b -> Signal a -> Signal b
32run : Automaton a b -> b -> Signal a -> Signal b
17run auto base inputs =
33run auto base inputs =
18 let step a (Step f, _) = f a
34 let step a (Step f, _) = f a
19 in lift (\(x,y) -> y) (foldp step (auto,base) inputs)
35 in lift (\(x,y) -> y) (foldp step (auto,base) inputs)
21−-- Step an automaton forward once with a given input.
37+{-| Step an automaton forward once with a given input. -}
22step : a -> Automaton a b -> (Automaton a b, b)
38step : a -> Automaton a b -> (Automaton a b, b)
23step a (Step f) = f a
39step a (Step f) = f a
25−-- Compose two automatons, chaining them together.
41+{-| Compose two automatons, chaining them together. -}
26(>>>) : Automaton a b -> Automaton b c -> Automaton a c
42(>>>) : Automaton a b -> Automaton b c -> Automaton a c
28 Step (\a -> let (f', b) = step a f
44 Step (\a -> let (f', b) = step a f
29 (g', c) = step b g
45 (g', c) = step b g
30 in (f' >>> g', c))
46 in (f' >>> g', c))
32−-- Compose two automatons, chaining them together.
48+{-| Compose two automatons, chaining them together. -}
33(<<<) : Automaton b c -> Automaton a b -> Automaton a c
49(<<<) : Automaton b c -> Automaton a b -> Automaton a c
34g <<< f = f >>> g
50g <<< f = f >>> g
36−-- Combine a list of automatons into a single automaton that produces a list.
52+{-| Combine a list of automatons into a single automaton that produces a
37combine : [Automaton a b] -> Automaton a [b]
55combine : [Automaton a b] -> Automaton a [b]
38combine autos =
56combine autos =
39 Step (\a -> let (autos', bs) = unzip (map (step a) autos)
57 Step (\a -> let (autos', bs) = unzip (map (step a) autos)
40 in (combine autos', bs))
58 in (combine autos', bs))
42−-- Create an automaton with no memory. It just applies the given function to
60+{-| Create an automaton with no memory. It just applies the given function to
43−-- every input.
61+every input.
44pure : (a -> b) -> Automaton a b
63pure : (a -> b) -> Automaton a b
45pure f = Step (\x -> (pure f, f x))
64pure f = Step (\x -> (pure f, f x))
47−-- Create an automaton with state. Requires an initial state and a step
67+{-| Create an automaton with state. Requires an initial state and a step
48−-- function to step the state forward. For example, an automaton that counted
68+function to step the state forward. For example, an automaton that counted
49−-- how many steps it has taken would look like this:
69+how many steps it has taken would look like this:
51−-- count = Automaton a Int
71+ count = Automaton a Int
52−-- count = state 0 (\\_ c -> c+1)
72+ count = state 0 (\\_ c -> c+1)
54−-- It is a stateful automaton. The initial state is zero, and the step function
74+It is a stateful automaton. The initial state is zero, and the step function
55−-- increments the state on every step.
75+increments the state on every step.
56state : b -> (a -> b -> b) -> Automaton a b
77state : b -> (a -> b -> b) -> Automaton a b
57state s f = Step (\x -> let s' = f x s
78state s f = Step (\x -> let s' = f x s
58 in (state s' f, s'))
79 in (state s' f, s'))
60−-- Create an automaton with hidden state. Requires an initial state and a
81+{-| Create an automaton with hidden state. Requires an initial state and a
61−-- step function to step the state forward and produce an output.
82+step function to step the state forward and produce an output.
62hiddenState : s -> (a -> s -> (s,b)) -> Automaton a b
84hiddenState : s -> (a -> s -> (s,b)) -> Automaton a b
63hiddenState s f = Step (\x -> let (s',out) = f x s
85hiddenState s f = Step (\x -> let (s',out) = f x s
64 in (hiddenState s' f, out))
86 in (hiddenState s' f, out))
66−-- Count the number of steps taken.
88+{-| Count the number of steps taken. -}
67count : Automaton a Int
89count : Automaton a Int
68count = state 0 (\_ c -> c + 1)
90count = state 0 (\_ c -> c + 1)
@@ -75,7 +97,7 @@ dequeue q = case q of
75 (en,[]) -> dequeue ([], reverse en)
97 (en,[]) -> dequeue ([], reverse en)
76 (en,hd::tl) -> Just (hd, (en,tl))
98 (en,hd::tl) -> Just (hd, (en,tl))
78−-- Computes the running average of the last `n` inputs.
100+{-| Computes the running average of the last `n` inputs. -}
79average : Int -> Automaton Float Float
101average : Int -> Automaton Float Float
80average k =
102average k =
81 let step n (ns,len,sum) =
103 let step n (ns,len,sum) =
@@ -98,4 +120,3 @@ library faster and better:
98Speeding things up is a really low priority. Language features and
120Speeding things up is a really low priority. Language features and
99libraries with nice APIs and are way more important!
121libraries with nice APIs and are way more important!