on master help sign in
Removed after and renamed before to andThen.
8544bb8Jeff Smits
libraries/Automaton.elm+9−14
@@ -9,10 +9,8 @@ import Signal (lift,foldp,Signal)
9import open List
9import open List
10import Maybe (Just, Nothing)
10import Maybe (Just, Nothing)
11
11
12−data Automaton input output =
12+data Automaton input output = Pure (input -> output)
13− Pure (input -> output)
14− | Stateful state (input -> state -> (output,state))
13+ | Stateful state (input -> state -> (output,state))
15−
16
14
17-- The basics
15-- The basics
18
16
@@ -21,8 +19,8 @@ pure: (i -> o) -> Automaton i o
21pure = Pure
19pure = Pure
22
20
23-- AFRP name: >>>
21-- AFRP name: >>>
24−before: Automaton i inter -> Automaton inter o -> Automaton i o
22+andThen: Automaton i inter -> Automaton inter o -> Automaton i o
25−before first second =
23+andThen first second =
26 case first of
24 case first of
27 Pure f -> case second of -- f is the function from first
25 Pure f -> case second of -- f is the function from first
28 Pure s -> Pure (s . f) -- s is the function from second
26 Pure s -> Pure (s . f) -- s is the function from second
@@ -84,26 +82,23 @@ hiddenState base fun = loop base (pure (\(i,s) ->
84 let (o,s') = fun i s
82 let (o,s') = fun i s
85 in (s',o)))
83 in (s',o)))
86
84
87−after : Automaton inter o -> Automaton i inter -> Automaton i o
88−after g f = f `before` g
89−
90-- AFRP name: second
85-- AFRP name: second
91extendUp: Automaton i o -> Automaton (extra,i) (extra,o)
86extendUp: Automaton i o -> Automaton (extra,i) (extra,o)
92extendUp auto =
87extendUp auto =
93 let swap (a, b) = (b, a)
88 let swap (a, b) = (b, a)
94− in pure swap `before` extendDown auto `before` pure swap
89+ in pure swap `andThen` extendDown auto `andThen` pure swap
95
90
96-- (parallel composition)
91-- (parallel composition)
97pair: Automaton i1 o1 -> Automaton i2 o2 -> Automaton (i1,i2) (o1,o2)
92pair: Automaton i1 o1 -> Automaton i2 o2 -> Automaton (i1,i2) (o1,o2)
98−pair f g = extendDown f `before` extendUp g
93+pair f g = extendDown f `andThen` extendUp g
99
94
100branch : Automaton i o1 -> Automaton i o2 -> Automaton i (o1,o2)
95branch : Automaton i o1 -> Automaton i o2 -> Automaton i (o1,o2)
101branch f g =
96branch f g =
102 let double = pure (\i -> (i,i))
97 let double = pure (\i -> (i,i))
103− in double `before` pair f g
98+ in double `andThen` pair f g
104
99
105combi: Automaton i o -> Automaton i [o] -> Automaton i [o]
100combi: Automaton i o -> Automaton i [o] -> Automaton i [o]
106−combi a1 a2 = (a1 `branch` a2) `before` pure (uncurry (::))
101+combi a1 a2 = (a1 `branch` a2) `andThen` pure (uncurry (::))
107
102
108-- Combine a list of automatons into a single automaton that produces a list.
103-- Combine a list of automatons into a single automaton that produces a list.
109combine : [Automaton i o] -> Automaton i [o]
104combine : [Automaton i o] -> Automaton i [o]
@@ -111,7 +106,7 @@ combine autos =
111 let l = length autos
106 let l = length autos
112 in if l == 0
107 in if l == 0
113 then pure (\_ -> [])
108 then pure (\_ -> [])
114− else foldr combi (last autos `before` pure (\a -> [a])) (take (l-1) autos)
109+ else foldr combi (last autos `andThen` pure (\a -> [a])) (take (l-1) autos)
115
110
116
111
117-- Examples of automata
112-- Examples of automata