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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
module RegExpEff
( Reg, eps, literal, alt, conc, rep, pl, anyL, bracket, negBracket
, start, end, times, captureG
, match, grep, grepPos, grepShow, grepShowUnique, capture
)
where
import Data.List
data Reg c s = Reg {emptyRe :: s, final :: s, cg :: [Int], reg :: Re c s}
data Re c s = Eps
| Sym ((c, [Int]) -> s)
| Alt (Reg c s) (Reg c s)
| Seq (Reg c s) (Reg c s)
| Rep (Reg c s)
| Start (Reg c s)
| End (Reg c s)
| Times (Int,Int) (Reg c s)
data Leftmost = NoLeft | Leftmost Startl
data Startl = NoStartl | Startl Int
data LeftLong = NoLeftLong | LeftLong Range
data Range = NoRange | Range Int Int
data All = NoAll | All List
data List = NoList | List [Int]
data AllRange = NoAllRange | AllRange Ranges
data Ranges = NoRanges | Ranges [(Int, Int)]
data CaptureGroups = NoCaptureGroups | CaptureGroups Groups
data Groups = NoGroups | Groups ([(Int, [(Int, Int)])], [Int])
instance Eq Leftmost where
NoLeft == NoLeft = True
Leftmost _ == Leftmost _ = True
NoLeft == Leftmost _ = False
Leftmost _ == NoLeft = False
instance Eq LeftLong where
NoLeftLong == NoLeftLong = True
LeftLong _ == LeftLong _ = True
NoLeftLong == LeftLong _ = False
LeftLong _ == NoLeftLong = False
instance Eq All where
NoAll == NoAll = True
All _ == All _ = True
NoAll == All _ = False
All _ == NoAll = False
instance Eq AllRange where
NoAllRange == NoAllRange = True
AllRange _ == AllRange _ = True
NoAllRange == AllRange _ = False
AllRange _ == NoAllRange = False
instance Eq CaptureGroups where
NoCaptureGroups == NoCaptureGroups = True
CaptureGroups _ == CaptureGroups _ = True
NoCaptureGroups == CaptureGroups _ = False
CaptureGroups _ == NoCaptureGroups = False
class Semiring s where
zero, one :: s
(+) :: s -> s -> s
(*) :: s -> s -> s
instance Semiring Bool where
zero = False
one = True
(+) = (||)
(*) = (&&)
instance Semiring Int where
zero = 0
one = 1
(+) = (Prelude.+)
(*) = (Prelude.*)
instance Semiring Leftmost where
zero = NoLeft
one = Leftmost NoStartl
NoLeft + NoLeft = NoLeft
Leftmost x + NoLeft = Leftmost x
NoLeft + Leftmost x = Leftmost x
Leftmost x + Leftmost y = Leftmost (leftmost x y)
where leftmost NoStartl NoStartl = NoStartl
leftmost NoStartl (Startl i) = Startl i
leftmost (Startl i) NoStartl = Startl i
leftmost (Startl i) (Startl j) = Startl (min i j)
NoLeft * NoLeft = NoLeft
NoLeft * Leftmost _ = NoLeft
Leftmost _ * NoLeft = NoLeft
Leftmost x * Leftmost y = Leftmost (startl x y)
where startl NoStartl s = s
startl (Startl i) _ = Startl i
instance Semiring LeftLong where
zero = NoLeftLong
one = LeftLong NoRange
NoLeftLong + NoLeftLong = NoLeftLong
NoLeftLong + LeftLong x = LeftLong x
LeftLong x + NoLeftLong = LeftLong x
LeftLong x + LeftLong y = LeftLong (leftlong x y)
where leftlong NoRange NoRange = NoRange
leftlong NoRange (Range i j) = Range i j
leftlong (Range i j) NoRange = Range i j
leftlong (Range i j) (Range k l) | i < k || i == k && j >= l = Range i j
| otherwise = Range k l
NoLeftLong * NoLeftLong = NoLeftLong
LeftLong _ * NoLeftLong = NoLeftLong
NoLeftLong * LeftLong _ = NoLeftLong
LeftLong x * LeftLong y = LeftLong (range x y)
where range NoRange NoRange = NoRange
range (Range i j) NoRange = Range i j
range NoRange (Range i j) = Range i j
range (Range i _) (Range _ j) = Range i j
instance Semiring All where
zero = NoAll
one = All NoList
NoAll + NoAll = NoAll
NoAll + All l = All l
All l + NoAll = All l
All x + All y = All (allList x y)
where allList NoList NoList = NoList
allList NoList (List l) = List l
allList (List l) NoList = List l
allList (List i) (List j) = List (j ++ i)
NoAll * NoAll = NoAll
NoAll * All _ = NoAll
All _ * NoAll = NoAll
All x * All y = All (list0 x y)
where list0 NoList NoList = NoList
list0 (List i) NoList = List i
list0 NoList (List i) = List i
list0 (List i) (List _) = List i
instance Semiring AllRange where
zero = NoAllRange
one = AllRange NoRanges
NoAllRange + NoAllRange = NoAllRange
NoAllRange + AllRange l = AllRange l
AllRange l + NoAllRange = AllRange l
AllRange x + AllRange y = AllRange (allrange x y)
where allrange NoRanges NoRanges = NoRanges
allrange NoRanges (Ranges l) = Ranges l
allrange (Ranges l) NoRanges = Ranges l
allrange (Ranges i) (Ranges j) = Ranges (j++i)
NoAllRange * NoAllRange = NoAllRange
NoAllRange * AllRange _ = NoAllRange
AllRange _ * NoAllRange = NoAllRange
AllRange x * AllRange y = AllRange (allr x y)
where
allr NoRanges NoRanges = NoRanges
allr (Ranges l) NoRanges = Ranges l
allr NoRanges (Ranges l) = Ranges l
allr (Ranges i) (Ranges j) = case i of
[] -> Ranges j
list1 -> case j of
((_,d) : js) -> Ranges (take ((length list1)-1) i ++
[(fst (last list1),d)] ++ js)
[] -> Ranges list1
instance Semiring CaptureGroups where
zero = NoCaptureGroups
one = CaptureGroups NoGroups
NoCaptureGroups + NoCaptureGroups = NoCaptureGroups
NoCaptureGroups + CaptureGroups l = CaptureGroups l
CaptureGroups l + NoCaptureGroups = CaptureGroups l
CaptureGroups x + CaptureGroups y = CaptureGroups (cgs x y)
where
cgs NoGroups NoGroups = NoGroups
cgs (Groups l) NoGroups = Groups l
cgs NoGroups (Groups l) = Groups l
cgs (Groups (i, l1)) (Groups (j, l2)) = Groups ((conca i j),nub (l1++ l2))
conca i j = case i of
[] -> j
((n, s) : xs) -> case filter (\(nj, _) -> n == nj) j of
[] -> (n, s) : conca xs j
((_, sj) : _) -> (n, s ++ sj) : conca xs
(filter (\(nj, _) -> not (n == nj)) j)
NoCaptureGroups * NoCaptureGroups = NoCaptureGroups
NoCaptureGroups * CaptureGroups _ = NoCaptureGroups
CaptureGroups _ * NoCaptureGroups = NoCaptureGroups
CaptureGroups x * CaptureGroups y = CaptureGroups (cgsm x y)
where cgsm NoGroups NoGroups = NoGroups
cgsm (Groups l) NoGroups = Groups l
cgsm NoGroups (Groups l) = Groups l
cgsm (Groups (i, l1)) (Groups (j, _)) = Groups ((comp i j l1), l1)
comp i j l = case i of
[] -> j
((n,s) : xs) -> case filter (\(nj,_) -> n == nj) j of
[] -> (n,s) : comp xs j l
((_, sj) : _) -> (n, if elem n l then comb s sj else s ++ sj) :
comp xs (filter (\(nj, _) -> not (n == nj)) j) l
comb _ [] = []
comb s ((_, b) : xs) = map (\(a, _) -> (a, b)) s ++ comb s xs
class Semiring s => Semiringi s where
index :: Int -> s
instance Semiringi Leftmost where
index = Leftmost . Startl
instance Semiringi LeftLong where
index i = LeftLong (Range i i)
instance Semiringi All where
index i = All (List [i])
instance Semiringi AllRange where
index i = AllRange (Ranges [(i,i)])
class Semiring s => Semiringc s where
list :: Int -> [Int] -> s
instance Semiringc CaptureGroups where
list i l = CaptureGroups (Groups ((map (\n -> (n, [(i, i)])) l), l))
instance Semiringc All where
list i _ = All (List [i])
instance Semiringc AllRange where
list i _ = AllRange (Ranges [(i,i)])
class CGFunction s where
newcg :: s -> [Int] -> s
justr :: s -> [Int] -> s
instance CGFunction Bool where
newcg s _ = s
justr = newcg
instance CGFunction Int where
newcg s _ = s
justr = newcg
instance CGFunction Leftmost where
newcg s _ = s
justr = newcg
instance CGFunction LeftLong where
newcg s _ = s
justr = newcg
instance CGFunction All where
newcg s _ = s
justr = newcg
instance CGFunction AllRange where
newcg s _ = s
justr = newcg
instance CGFunction CaptureGroups where
newcg (NoCaptureGroups) _ = NoCaptureGroups
newcg (CaptureGroups (NoGroups)) _ = CaptureGroups (NoGroups)
newcg (CaptureGroups (Groups (g, _))) l = CaptureGroups (Groups (g, l))
justr (NoCaptureGroups) _ = NoCaptureGroups
justr (CaptureGroups (NoGroups)) _ = CaptureGroups (NoGroups)
justr (CaptureGroups (Groups (g, _))) l = CaptureGroups (Groups
(map (\(n, nl) -> if elem n l then (n, nl) else ((-abs(n)), nl)) g, l))
eps :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Reg c s
eps l = Reg {emptyRe = one, final = zero, cg = l, reg = Eps}
literal :: (Semiringc s, CGFunction s, Eq s) => [Int] -> Char
-> Reg (Int, Char) s
literal l a = symc l (==a)
sym :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> ((c, [Int]) -> s)
-> Reg c s
sym l f = Reg {emptyRe = zero, final = zero, cg = l, reg = Sym f}
symi :: (Semiringi s, CGFunction s, Eq s)=> [Int] -> Char -> Reg (Int, Char) s
symi l c = sym l weight
where weight ((pos,x), _) | x == c = index pos
| otherwise = zero
symc :: (Semiringc s, CGFunction s, Eq s)=> [Int] -> (Char -> Bool)
-> Reg (Int, Char) s
symc l f = sym l captureList
where captureList ((pos, x), li) | f x = list pos li
| otherwise = zero
alt :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Reg c s -> Reg c s
-> Reg c s
alt l p q = Reg {emptyRe = newcg (emptyRe p + emptyRe q) l,
final = newcg (final p + final q) l, cg = l, reg = Alt p q}
conc :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Reg c s -> Reg c s
-> Reg c s
conc l p q = Reg {emptyRe = emptyRe p * emptyRe q,
final = final p * emptyRe q + final q, cg = l, reg = Seq p q}
rep :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Reg c s -> Reg c s
rep l r = Reg {emptyRe = one, final = newcg (final r) l, cg = l,
reg = Rep r}
pl :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Reg c s -> Reg c s
pl l r = conc l r (rep l r)
anyL :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Reg c s
anyL l = sym l (\_ -> one)
bracket :: (Semiringc s, CGFunction s, Eq s) => [Int] ->
[Either Char (Char, Char)] -> Reg (Int, Char) s
bracket li l = let l' = map lift l
f = foldl (\g h -> (\c -> (g c) || (h c))) (head l')
(tail l')
in symc li (\c -> f c)
negBracket :: (Semiringc s, CGFunction s, Eq s) => [Int] ->
[Either Char (Char, Char)] -> Reg (Int, Char) s
negBracket li l = let l' = map negLift l
f = foldl (\g h -> (\c -> (g c) && (h c)))(head l')
(tail l')
in symc li (\c -> f c)
start :: (Semiringc s, CGFunction s, Eq s) => [Int] -> Reg (Int, Char) s -> Bool
-> Reg (Int, Char) s
start l r True = Reg {emptyRe = emptyRe r, final = newcg (final r) l, cg = l,
reg = Start r}
start l r False = conc l (Reg {emptyRe = emptyRe r, final = newcg (final r) l,
cg = l, reg = Start r}) (rep l (symc l (\_ -> True)))
end :: (Semiringc s, CGFunction s, Eq s) => [Int] -> Reg (Int, Char) s -> Bool
-> Reg (Int, Char) s
end l r True = Reg {emptyRe = emptyRe r, final = newcg (final r) l, cg = l,
reg = End r}
end l r False = conc l (rep l (symc l (\_ -> True))) (Reg {emptyRe = emptyRe r,
final = newcg (final r) l, cg = l, reg = End r})
times :: (Semiring s, CGFunction s, Ord c, Eq s) => [Int] -> Int -> Int ->
Reg c s -> Reg c s
times l n m r =
if m < n || n < 0
then Reg {emptyRe = zero, final = zero, cg = l, reg = Times (n,m) r}
else if n == 0
then Reg {emptyRe = one, final = final r, cg = l, reg =
Times (n,m) r}
else Reg {emptyRe = emptyRe r, final = final r * time n, cg = l,
reg = Times (n,m) r}
where time k = if k == 0 then one else zero
captureG :: (Semiring s, CGFunction s, Ord c, Eq s) => Int -> Reg c s -> Reg c s
captureG _ r = r
lift :: Either Char (Char,Char) -> Char -> Bool
lift (Left a) = \c -> c == a
lift (Right (a,b)) = \c -> (c >= a && c <= b)
negLift :: Either Char (Char,Char) -> Char -> Bool
negLift a c = not (lift a c)
matchEff :: (Semiring s, CGFunction s, Ord c, Eq s) => Reg c s -> [c] -> s
matchEff r [] = emptyRe r
matchEff r (c:cs) = final (foldl (shift zero) (shift one r c) cs)
shift :: (Semiring s, CGFunction s, Ord c, Eq s) => s -> Reg c s -> c -> Reg c s
shift m r c = case reg r of
Eps -> eps (cg r)
(Sym f) -> (sym (cg r) f){final = (newcg (m * (f (c, cg r))) (cg r))}
(Alt p q) -> alt (cg r) (shift m p c) (shift m q c)
(Seq p q) -> conc (cg r) (shift m p c) (shift (m * emptyRe p + final p)
q c)
(Rep re) -> rep (cg r) (shift (newcg (m + justr (final re) (cg r))
(cg r)) re c)
(Start re) -> shift m re c
(End re) -> shift m re c
(Times (i,j) re) -> let nre = (shift (newcg (m + justr (final re) (cg r))
(cg r)) re c)
in times (cg r) (max (if (final nre == one) then i-1
else i) 0) (if (final nre == one) then j-1 else j) nre
submatch :: (Semiring s, CGFunction s, Ord c, Eq s) => Reg (Int, c) s -> [c]
-> s
submatch r s = matchEff (conc [] arb (conc [] r arb)) (zip [0..] s)
where arb = rep [] (sym [] (\_ -> one))
grep :: Ord a => Reg (Int, a) All -> [a] -> [Int]
grep re s = case submatch re s of
All (List l) -> l
_ -> []
grepPos :: Ord a => Reg (Int, a) All -> [a] -> Int
grepPos re s = case grep re s of
[] -> -1
pos -> head pos
grepShow :: Ord a => Reg (Int, a) AllRange -> [a] -> [[a]]
grepShow re s = case submatch re s of
AllRange (Ranges list0) -> map (\(i,j) -> drop i (take (j Prelude.+1)s)) list0
_ -> []
grepShowUnique :: Ord a => Reg (Int, a) AllRange -> [a] -> [[a]]
grepShowUnique re s = nub (grepShow re s)
capture :: Ord c => Reg (Int, c) CaptureGroups -> [c] -> [(Int, [[c]])]
capture r s = case (matchEff r (zip [0.. ] s)) of
CaptureGroups (Groups (l, _)) -> combine $ map (\(n, li) -> (n, (map (\(i, j)
-> drop i (take (j Prelude.+ 1) s)) li))) l
_ -> []
combine :: Ord c => [(Int, [[c]])] -> [(Int, [[c]])]
combine [] = []
combine ((n1, s1) : ns) = case filter (\(i, _) -> abs(i) == abs(n1)) ns of
[] -> (abs(n1), s1) : combine ns
li -> (abs(n1), foldr (++) [] $ map (\(_, s2) -> s2) ((n1, s1) : li)):
combine (filter (\(i, _) -> not $ abs(i) == abs(n1)) ns)
match :: Ord c => Reg (Int, c) CaptureGroups -> [c] -> Bool
match r s = let l = capture r s
in if l == []
then False
else if (snd $ head l) == []
then False
else (head $ snd $ head l) == s
|