-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategies.hs
52 lines (38 loc) · 1.33 KB
/
Strategies.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Strategies where
import Elevate
-- Naive Strategies
id' :: Strategy p
id' = Strategy (\p -> success p id' p) "id'"
fail' :: Strategy p
fail' = Strategy (\p -> Failure fail') "fail'"
-- Basic Combinators
seq' :: Strategy p -> Strategy p -> Strategy p
seq' f s = Strategy (\p -> flatMapSuccess s (f $$ p)) "seq'"
-- seq' Alternativ Syntax
(~>>) :: Strategy p -> Strategy p -> Strategy p
(~>>) f s = seq' f s
lChoice' :: Strategy p -> Strategy p -> Strategy p
lChoice' f s = Strategy (\p -> flatMapFailure (\_ -> (s $$ p)) (f $$ p)) "lChoice'"
-- lChoice' Alternativ Syntax
(<+) :: Strategy p -> Strategy p -> Strategy p
(<+) f s = lChoice' f s
-- Basic Strategies
try' :: Strategy p -> Strategy p
try' s = s <+ id'
repeat' :: Strategy p -> Strategy p
repeat' s = try' (s ~>> (repeat' s))
-- Traversable
class Traversable' p where
all' :: Strategy p -> Strategy p
one' :: Strategy p -> Strategy p
-- some' :: Strategy p -> Strategy p
-- Complete Traversals
oncetd :: Traversable' p => Strategy p -> Strategy p
oncetd s = s <+ (one' (oncetd s))
oncebu :: Traversable' p => Strategy p -> Strategy p
oncebu s = (one' (oncebu s)) <+ s
topdown :: Traversable' p => Strategy p -> Strategy p
topdown s = s ~>> (all' (topdown s))
-- Normalize
normalize :: Traversable' p => Strategy p -> Strategy p
normalize s = repeat' (oncetd s)