Skip to content

Commit 97cc532

Browse files
new implementation for interleave and atomic prefix combinators (more lazy)
1 parent 2e64e15 commit 97cc532

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Changelog ideas-1.4 => ideas.1.5
88
* requests can fix the random seed
99
* added Term to service types, encoders and decoders
1010
* JSON-Int can be used for decoded environments
11+
* new implementation for interleave and atomic prefix combinators (more lazy)
1112
* bug fix: "no prefix" now handled by json decoder
1213

1314

src/Ideas/Common/Strategy/Derived.hs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Ideas.Common.Strategy.Derived
1818
permute, many, many1, replicate, option, try
1919
, repeat, repeat1, exhaustive
2020
-- * Process-specific combinators
21-
, atomic, (<%>), interleave, concurrent
21+
, atomic, (<%>), interleave
2222
, (<@>), (!*>), inits, filterP, hide
2323
) where
2424

@@ -56,9 +56,11 @@ split2 op1 op2 = withMenu f
5656

5757
-- atomic prefix
5858
(!*>) :: AtomicSymbol a => Process a -> Process a -> Process a
59-
a !*> p = split op (atomic a) p
59+
a !*> p = atomicOpen ~> a .*. withMenu op (single atomicClose) p
6060
where
61-
op b q = atomic (a .*. b) .*. q
61+
op b q
62+
| b == atomicOpen = q
63+
| otherwise = b ~> atomicClose ~> q
6264

6365
filterP :: (a -> Bool) -> Process a -> Process a
6466
filterP cond = fold (\a q -> if cond a then a ~> q else empty) done
@@ -72,23 +74,28 @@ atomic p = atomicOpen ~> (p .*. single atomicClose)
7274
interleave :: (AtomicSymbol a, LabelSymbol a) => [Process a] -> Process a
7375
interleave xs = if null xs then done else foldr1 (<%>) xs
7476

77+
-- interleaving
7578
(<%>) :: (AtomicSymbol a, LabelSymbol a) => Process a -> Process a -> Process a
76-
(<%>) = concurrent (not . isEnterSymbol)
77-
78-
concurrent :: AtomicSymbol a => (a -> Bool) -> Process a -> Process a -> Process a
79-
concurrent switch = normal
79+
p <%> q =
80+
bothAreDone p q .|. ((p %>> q) .|. (q %>> p))
8081
where
81-
normal p q = stepBoth q p .|. (stepRight q p .|. stepRight p q)
82-
83-
stepBoth = withMenu stop2 . withMenu stop2 done
84-
stop2 _ _ = empty
82+
bothAreDone = withMenu stop2 . withMenu stop2 done
83+
stop2 _ _ = empty
8584

86-
stepRight p = split2 op1 op2 empty
85+
-- left-interleaving
86+
(%>>) :: (AtomicSymbol a, LabelSymbol a) => Process a -> Process a -> Process a
87+
p %>> q = rec (0 :: Int) p
88+
where
89+
rec n = withMenu op empty
8790
where
88-
op1 a q2
89-
| switch a = a ~> normal p q2
90-
| otherwise = a ~> stepRight p q2
91-
op2 q1 q2 = q1 .*. normal p q2
91+
op a = a ~> rest
92+
where
93+
next | a == atomicOpen = n+1
94+
| a == atomicClose = n-1
95+
| otherwise = n
96+
rest | isEnterSymbol a = rec next
97+
| next > 0 = rec next
98+
| otherwise = (<%> q)
9299

93100
-- | Allows all permutations of the list
94101
permute :: (Choice a, Sequence a) => [a] -> a

0 commit comments

Comments
 (0)