64 ($$) :: String -> Rexp -> Rexp |
64 ($$) :: String -> Rexp -> Rexp |
65 x $$ r = RECD x r |
65 x $$ r = RECD x r |
66 |
66 |
67 alts :: [Rexp] -> Rexp |
67 alts :: [Rexp] -> Rexp |
68 alts rs = case rs of |
68 alts rs = case rs of |
69 [] -> NULL |
69 [] -> ZERO |
70 [r] -> r |
70 [r] -> r |
71 r:rs -> foldl (ALT) r rs |
71 r:rs -> foldl (ALT) r rs |
72 |
72 |
73 size :: Rexp -> Int |
73 size :: Rexp -> Int |
74 size r = case r of |
74 size r = case r of |
75 NULL -> 1 |
75 ZERO -> 1 |
76 EMPTY -> 1 |
76 ONE -> 1 |
77 CHAR _ -> 1 |
77 CHAR _ -> 1 |
78 ALT r1 r2 -> 1 + (size r1) + (size r2) |
78 ALT r1 r2 -> 1 + (size r1) + (size r2) |
79 SEQ r1 r2 -> 1 + (size r1) + (size r2) |
79 SEQ r1 r2 -> 1 + (size r1) + (size r2) |
80 STAR r -> 1 + (size r) |
80 STAR r -> 1 + (size r) |
81 RECD _ r -> 1 + (size r) |
81 RECD _ r -> 1 + (size r) |
82 |
82 |
83 nullable :: Rexp -> Bool |
83 nullable :: Rexp -> Bool |
84 nullable r = case r of |
84 nullable r = case r of |
85 NULL -> False |
85 ZERO -> False |
86 EMPTY -> True |
86 ONE -> True |
87 CHAR _ -> False |
87 CHAR _ -> False |
88 ALT r1 r2 -> nullable(r1) || nullable(r2) |
88 ALT r1 r2 -> nullable(r1) || nullable(r2) |
89 SEQ r1 r2 -> nullable(r1) && nullable(r2) |
89 SEQ r1 r2 -> nullable(r1) && nullable(r2) |
90 STAR _ -> True |
90 STAR _ -> True |
91 RECD _ r -> nullable(r) |
91 RECD _ r -> nullable(r) |
92 |
92 |
93 der :: Char -> Rexp -> Rexp |
93 der :: Char -> Rexp -> Rexp |
94 der c r = case r of |
94 der c r = case r of |
95 NULL -> NULL |
95 ZERO -> ZERO |
96 EMPTY -> NULL |
96 ONE -> ZERO |
97 CHAR d -> if c == d then EMPTY else NULL |
97 CHAR d -> if c == d then ONE else ZERO |
98 ALT r1 r2 -> ALT (der c r1) (der c r2) |
98 ALT r1 r2 -> ALT (der c r1) (der c r2) |
99 SEQ r1 r2 -> |
99 SEQ r1 r2 -> |
100 if nullable r1 then ALT (SEQ (der c r1) r2) (der c r2) |
100 if nullable r1 then ALT (SEQ (der c r1) r2) (der c r2) |
101 else SEQ (der c r1) r2 |
101 else SEQ (der c r1) r2 |
102 STAR r -> SEQ (der c r) (STAR r) |
102 STAR r -> SEQ (der c r) (STAR r) |
186 ALT r1 r2 -> |
186 ALT r1 r2 -> |
187 let (r1s, f1s) = simp r1 |
187 let (r1s, f1s) = simp r1 |
188 (r2s, f2s) = simp r2 |
188 (r2s, f2s) = simp r2 |
189 in |
189 in |
190 (case (r1s, r2s) of |
190 (case (r1s, r2s) of |
191 (NULL, _) -> (r2s, f_right f2s) |
191 (ZERO, _) -> (r2s, f_right f2s) |
192 (_, NULL) -> (r1s, f_left f1s) |
192 (_, ZERO) -> (r1s, f_left f1s) |
193 (_, _) -> if r1s == r2s then (r1s, f_left f1s) |
193 (_, _) -> if r1s == r2s then (r1s, f_left f1s) |
194 else (ALT r1s r2s, f_alt f1s f2s)) |
194 else (ALT r1s r2s, f_alt f1s f2s)) |
195 SEQ r1 r2 -> |
195 SEQ r1 r2 -> |
196 let (r1s, f1s) = simp r1 |
196 let (r1s, f1s) = simp r1 |
197 (r2s, f2s) = simp r2 |
197 (r2s, f2s) = simp r2 |
198 in |
198 in |
199 (case (r1s, r2s) of |
199 (case (r1s, r2s) of |
200 (NULL, _) -> (NULL, f_right f2s) |
200 (ZERO, _) -> (ZERO, f_right f2s) |
201 (_, NULL) -> (NULL, f_left f1s) |
201 (_, ZERO) -> (ZERO, f_left f1s) |
202 (EMPTY, _) -> (r2s, f_seq_void1 f1s f2s) |
202 (ONE, _) -> (r2s, f_seq_void1 f1s f2s) |
203 (_, EMPTY) -> (r1s, f_seq_void2 f1s f2s) |
203 (_, ONE) -> (r1s, f_seq_void2 f1s f2s) |
204 (_, _) -> (SEQ r1s r2s, f_seq f1s f2s)) |
204 (_, _) -> (SEQ r1s r2s, f_seq f1s f2s)) |
205 RECD x r1 -> |
205 RECD x r1 -> |
206 let (r1s, f1s) = simp r1 |
206 let (r1s, f1s) = simp r1 |
207 in |
207 in |
208 (RECD x r1s, f_rec f1s) |
208 (RECD x r1s, f_rec f1s) |
209 r -> (r, f_id) |
209 r -> (r, f_id) |
210 |
210 |
211 der_simp :: Char -> Rexp -> (Rexp, Value -> Value) |
211 der_simp :: Char -> Rexp -> (Rexp, Value -> Value) |
212 der_simp c r = case r of |
212 der_simp c r = case r of |
213 NULL -> (NULL, f_id) |
213 ZERO -> (ZERO, f_id) |
214 EMPTY -> (NULL, f_id) |
214 ONE -> (ZERO, f_id) |
215 CHAR(d) -> ((if c == d then EMPTY else NULL), f_id) |
215 CHAR(d) -> ((if c == d then ONE else ZERO), f_id) |
216 ALT r1 r2 -> |
216 ALT r1 r2 -> |
217 let (r1d, f1d) = der_simp c r1 |
217 let (r1d, f1d) = der_simp c r1 |
218 (r2d, f2d) = der_simp c r2 |
218 (r2d, f2d) = der_simp c r2 |
219 in |
219 in |
220 (case (r1d, r2d) of |
220 (case (r1d, r2d) of |
221 (NULL, _) -> (r2d, f_right f2d) |
221 (ZERO, _) -> (r2d, f_right f2d) |
222 (_, NULL) -> (r1d, f_left f1d) |
222 (_, ZERO) -> (r1d, f_left f1d) |
223 (_, _) -> if r1d == r2d then (r1d, f_left f1d) |
223 (_, _) -> if r1d == r2d then (r1d, f_left f1d) |
224 else (ALT r1d r2d, f_alt f1d f2d)) |
224 else (ALT r1d r2d, f_alt f1d f2d)) |
225 SEQ r1 r2 -> |
225 SEQ r1 r2 -> |
226 if nullable r1 |
226 if nullable r1 |
227 then |
227 then |
228 let (r1d, f1d) = der_simp c r1 |
228 let (r1d, f1d) = der_simp c r1 |
229 (r2d, f2d) = der_simp c r2 |
229 (r2d, f2d) = der_simp c r2 |
230 (r2s, f2s) = simp r2 |
230 (r2s, f2s) = simp r2 |
231 in |
231 in |
232 (case (r1d, r2s, r2d) of |
232 (case (r1d, r2s, r2d) of |
233 (NULL, _, _) -> (r2d, f_right f2d) |
233 (ZERO, _, _) -> (r2d, f_right f2d) |
234 (_, NULL, _) -> (r2d, f_right f2d) |
234 (_, ZERO, _) -> (r2d, f_right f2d) |
235 (_, _, NULL) -> (SEQ r1d r2s, f_left (f_seq f1d f2s)) |
235 (_, _, ZERO) -> (SEQ r1d r2s, f_left (f_seq f1d f2s)) |
236 (EMPTY, _, _) -> (ALT r2s r2d, f_alt (f_seq_void1 f1d f2s) f2d) |
236 (ONE, _, _) -> (ALT r2s r2d, f_alt (f_seq_void1 f1d f2s) f2d) |
237 (_, EMPTY, _) -> (ALT r1d r2d, f_alt (f_seq_void2 f1d f2s) f2d) |
237 (_, ONE, _) -> (ALT r1d r2d, f_alt (f_seq_void2 f1d f2s) f2d) |
238 (_, _, _) -> (ALT (SEQ r1d r2s) r2d, f_alt (f_seq f1d f2s) f2d)) |
238 (_, _, _) -> (ALT (SEQ r1d r2s) r2d, f_alt (f_seq f1d f2s) f2d)) |
239 else |
239 else |
240 let (r1d, f1d) = der_simp c r1 |
240 let (r1d, f1d) = der_simp c r1 |
241 (r2s, f2s) = simp r2 |
241 (r2s, f2s) = simp r2 |
242 in |
242 in |
243 (case (r1d, r2s) of |
243 (case (r1d, r2s) of |
244 (NULL, _) -> (NULL, f_id) |
244 (ZERO, _) -> (ZERO, f_id) |
245 (_, NULL) -> (NULL, f_id) |
245 (_, ZERO) -> (ZERO, f_id) |
246 (EMPTY, _) -> (r2s, f_seq_void1 f1d f2s) |
246 (ONE, _) -> (r2s, f_seq_void1 f1d f2s) |
247 (_, EMPTY) -> (r1d, f_seq_void2 f1d f2s) |
247 (_, ONE) -> (r1d, f_seq_void2 f1d f2s) |
248 (_, _) -> (SEQ r1d r2s, f_seq f1d f2s)) |
248 (_, _) -> (SEQ r1d r2s, f_seq f1d f2s)) |
249 STAR r1 -> |
249 STAR r1 -> |
250 let (r1d, f1d) = der_simp c r1 |
250 let (r1d, f1d) = der_simp c r1 |
251 in |
251 in |
252 (case r1d of |
252 (case r1d of |
253 NULL -> (NULL, f_id) |
253 ZERO -> (ZERO, f_id) |
254 EMPTY -> (STAR r1, f_seq_void1 f1d f_id) |
254 ONE -> (STAR r1, f_seq_void1 f1d f_id) |
255 _ -> (SEQ r1d (STAR r1), f_seq f1d f_id)) |
255 _ -> (SEQ r1d (STAR r1), f_seq f1d f_id)) |
256 RECD x r1 -> der_simp c r1 |
256 RECD x r1 -> der_simp c r1 |
257 |
257 |
258 |
258 |
259 |
259 |