@@ -13,36 +13,42 @@ def universal {α: Type} : Lang α :=
13
13
fun _ => True
14
14
15
15
def emptystr {α: Type } : Lang α :=
16
- fun w => w = []
16
+ fun xs => xs = []
17
17
18
- def char {α: Type } (a : α): Lang α :=
19
- fun w => w = [a]
18
+ def char {α: Type } (x : α): Lang α :=
19
+ fun xs => xs = [x]
20
+
21
+ def pred {α: Type } (p : α -> Prop ): Lang α :=
22
+ fun xs => ∃ x, xs = [x] /\ p x
20
23
21
24
-- onlyif is used as an and to make derive char not require an if statement
22
25
-- (derive (char c) a) w <-> (onlyif (a = c) emptystr)
23
- def onlyif {α: Type } (cond : Prop ) (P : Lang α) : Lang α :=
24
- fun w => cond /\ P w
26
+ def onlyif {α: Type } (cond : Prop ) (R : Lang α) : Lang α :=
27
+ fun xs => cond /\ R xs
25
28
26
29
def or {α: Type } (P : Lang α) (Q : Lang α) : Lang α :=
27
- fun w => P w \/ Q w
30
+ fun xs => P xs \/ Q xs
28
31
29
32
def and {α: Type } (P : Lang α) (Q : Lang α) : Lang α :=
30
- fun w => P w /\ Q w
33
+ fun xs => P xs /\ Q xs
31
34
32
35
def concat {α: Type } (P : Lang α) (Q : Lang α) : Lang α :=
33
36
fun (xs : List α) =>
34
- ∃ (ys : List α) (zs : List α), P ys /\ Q zs /\ xs = (ys ++ zs )
37
+ ∃ (xs1 : List α) (xs2 : List α), P xs1 /\ Q xs2 /\ xs = (xs1 ++ xs2 )
35
38
36
- inductive All {α: Type } (P : α -> Prop ) : (List α -> Prop ) where
37
- | nil : All P []
38
- | cons : ∀ {x xs} (_px : P x) (_pxs : All P xs), All P (x :: xs)
39
+ inductive star {α: Type } (R: Lang α): Lang α where
40
+ | zero: star R []
41
+ | more: ∀ (x: α) (xs1 xs2 xs: List α),
42
+ xs = (x::xs1) ++ xs2
43
+ -> R (x::xs1)
44
+ -> star R xs2
45
+ -> star R xs
39
46
40
- def star {α: Type } (R : Lang α) : Lang α :=
41
- fun (xs : List α) =>
42
- ∃ (xss : List (List α)), All R xss /\ xs = (List.flatten xss)
47
+ def not {α: Type } (R: Lang α): Lang α :=
48
+ fun xs => (Not (R xs))
43
49
44
50
-- attribute [ simp ] allows these definitions to be unfolded when using the simp tactic.
45
- attribute [simp] universal emptyset emptystr char onlyif or and concat star
51
+ attribute [simp] universal emptyset emptystr char onlyif or and concat
46
52
47
53
example : Lang α := universal
48
54
example : Lang α := emptystr
@@ -154,6 +160,18 @@ def null_char {α: Type} {c: α}:
154
160
null (char c) = False := by
155
161
rw [null_iff_char]
156
162
163
+ def null_iff_pred {α: Type } {p: α -> Prop }:
164
+ null (pred p) <-> False :=
165
+ Iff.intro nofun nofun
166
+
167
+ def not_null_if_pred {α: Type } {p: α -> Prop }:
168
+ null (pred p) -> False :=
169
+ nofun
170
+
171
+ def null_pred {α: Type } {p: α -> Prop }:
172
+ null (pred p) = False := by
173
+ rw [null_iff_pred]
174
+
157
175
def null_or {α: Type } {P Q: Lang α}:
158
176
null (or P Q) = ((null P) \/ (null Q)) :=
159
177
rfl
@@ -181,22 +199,27 @@ def null_concat {α: Type} {P Q: Lang α}:
181
199
null (concat P Q) = ((null P) /\ (null Q)) := by
182
200
rw [null_iff_concat]
183
201
184
- def null_star {α: Type } {P: Lang α}:
185
- null (star P) = True := by
186
- simp
187
- exists []
188
- apply And.intro
189
- · exact All.nil
190
- · intro l hl
191
- cases hl
202
+ def null_if_star {α: Type } {R: Lang α}:
203
+ null (star R) :=
204
+ star.zero
192
205
193
- def null_iff_star {α: Type } {P: Lang α}:
194
- null (star P) <-> True := by
195
- rw [null_star]
206
+ def null_iff_star {α: Type } {R: Lang α}:
207
+ null (star R) <-> True :=
208
+ Iff.intro
209
+ (fun _ => True.intro)
210
+ (fun _ => star.zero)
211
+
212
+ def null_star {α: Type } {R: Lang α}:
213
+ null (star R) = True := by
214
+ rw [null_iff_star]
215
+
216
+ def null_not {α: Type } {R: Lang α}:
217
+ null (not R) = null (Not ∘ R) :=
218
+ rfl
196
219
197
- def null_if_star {α: Type } {P : Lang α}:
198
- null (star P) :=
199
- null_iff_star.mpr True.intro
220
+ def null_iff_not {α: Type } {R : Lang α}:
221
+ null (not R) <-> null (Not ∘ R) := by
222
+ rw [null_not]
200
223
201
224
def derive_emptyset {α: Type } {a: α}:
202
225
(derive emptyset a) = emptyset :=
@@ -233,6 +256,32 @@ def derive_char {α: Type} [DecidableEq α] {a: α} {c: α}:
233
256
funext
234
257
rw [derive_iff_char]
235
258
259
+ def derive_iff_pred {α: Type } {p: α -> Prop } [dp: DecidablePred p] {x: α} {xs: List α}:
260
+ (derive (pred p) x) xs <-> (onlyif (p x) emptystr) xs := by
261
+ simp only [derive, derives, singleton_append]
262
+ simp only [onlyif, emptystr]
263
+ refine Iff.intro ?toFun ?invFun
264
+ case toFun =>
265
+ intro D
266
+ match D with
267
+ | Exists.intro x' D =>
268
+ simp only [cons.injEq] at D
269
+ match D with
270
+ | And.intro (And.intro hxx' hxs) hpx =>
271
+ rw [<- hxx'] at hpx
272
+ exact And.intro hpx hxs
273
+ case invFun =>
274
+ intro ⟨ hpx , hxs ⟩
275
+ unfold pred
276
+ exists x
277
+ simp only [cons.injEq, true_and]
278
+ exact And.intro hxs hpx
279
+
280
+ def derive_pred {α: Type } {p: α -> Prop } [DecidablePred p] {x: α}:
281
+ (derive (pred p) x) = (onlyif (p x) emptystr) := by
282
+ funext
283
+ rw [derive_iff_pred]
284
+
236
285
def derive_or {α: Type } {a: α} {P Q: Lang α}:
237
286
(derive (or P Q) a) = (or (derive P a) (derive Q a)) :=
238
287
rfl
@@ -313,147 +362,6 @@ def derive_concat {α: Type} {x: α} {P Q: Lang α}:
313
362
funext
314
363
rw [derive_iff_concat]
315
364
316
- inductive starplus {α: Type } (R: Lang α): Lang α where
317
- | zero: starplus R []
318
- | more: ∀ (ps qs w: List α),
319
- w = ps ++ qs
320
- -> R ps
321
- -> starplus R qs
322
- -> starplus R w
323
-
324
- theorem star_is_starplus : ∀ {α: Type } (R: Lang α) (xs: List α),
325
- star R xs -> starplus R xs := by
326
- intro α R xs
327
- unfold star
328
- intro s
329
- match s with
330
- | Exists.intro ws (And.intro sl sr) =>
331
- clear s
332
- rw [sr]
333
- clear sr
334
- clear xs
335
- induction ws with
336
- | nil =>
337
- simp
338
- exact starplus.zero
339
- | cons w' ws' ih =>
340
- cases sl with
341
- | cons rw allrw =>
342
- have hr := ih allrw
343
- refine starplus.more ?ps ?qs ?w ?psqs ?rps ?rqs
344
- · exact w'
345
- · exact ws'.flatten
346
- · simp
347
- · assumption
348
- · assumption
349
-
350
- theorem starplus_is_star : ∀ {α: Type } (R: Lang α) (xs: List α),
351
- starplus R xs -> star R xs := by
352
- intro α R xs
353
- intro hs
354
- induction xs with
355
- | nil =>
356
- simp
357
- exists []
358
- apply And.intro ?_ (by simp)
359
- apply All.nil
360
- | cons x' xs' ih =>
361
- cases hs with
362
- | more ps qs _ xxspsqs Rps sRqs =>
363
- unfold star
364
- have hxs := list_split_cons (x' :: xs')
365
- cases hxs with
366
- | inl h =>
367
- contradiction
368
- | inr h =>
369
- cases h with
370
- | intro x h =>
371
- cases h with
372
- | intro xs h =>
373
- cases h with
374
- | intro ys h =>
375
- rw [h]
376
- have hys := list_split_flatten ys
377
- cases hys with
378
- | intro yss hyss =>
379
- sorry
380
-
381
- inductive starcons {α: Type } (R: Lang α): Lang α where
382
- | zero: starcons R []
383
- | more: ∀ (p: α) (ps qs w: List α),
384
- w = (p::ps) ++ qs
385
- -> R (p::ps)
386
- -> starcons R qs
387
- -> starcons R w
388
-
389
- def stara {α: Type } (R : Lang α) (ws : List (List α)) (allr: All R ws): star R ws.flatten := by
390
- unfold star
391
- exists ws
392
-
393
- theorem star_is_starcons : ∀ {α: Type } (R: Lang α) (xs: List α),
394
- star R xs <-> starcons R xs := by
395
- intro α R xs
396
- refine Iff.intro ?toFun ?invFun
397
- case toFun =>
398
- intro s
399
- match s with
400
- | Exists.intro ws (And.intro sl sr) =>
401
- cases sl with
402
- | nil =>
403
- rw [sr]
404
- exact starcons.zero
405
- | cons rx1 rx2 =>
406
- rename_i xs1 xs2
407
- cases xs with
408
- | nil =>
409
- exact starcons.zero
410
- | cons x' xs' =>
411
- cases xs1 with
412
- | nil =>
413
- sorry
414
- | cons xs1' xss1' =>
415
- refine starcons.more x' ?ps ?qs ?ws' ?hw ?hr ?hsr
416
- · exact xss1'
417
- · exact xs2.flatten
418
- · sorry
419
- · sorry
420
- · sorry
421
- case invFun =>
422
- intro hs
423
- induction hs with
424
- | zero =>
425
- unfold star
426
- exists []
427
- simp
428
- exact All.nil
429
- | more x' ys' zs' xs hsplit hr hrs ih =>
430
- have hxs := list_split_cons xs
431
- cases hxs with
432
- | inl h =>
433
- rw [h]
434
- simp
435
- exists []
436
- simp
437
- apply All.nil
438
- | inr h =>
439
- cases h with
440
- | intro x h =>
441
- cases h with
442
- | intro ys h =>
443
- cases h with
444
- | intro zs h =>
445
- rw [hsplit] at h
446
- rw [hsplit]
447
- unfold star
448
- exists [x' :: ys', zs']
449
- refine And.intro ?_ (by simp)
450
- refine All.cons hr ?_
451
- cases ih with
452
- | intro ws h2 =>
453
- cases h2 with
454
- | intro h2l h2r =>
455
- sorry
456
-
457
365
def derive_iff_star {α: Type } {x: α} {R: Lang α} {xs: List α}:
458
366
(derive (star R) x) xs <-> (concat (derive R x) (star R)) xs := by
459
367
refine Iff.intro ?toFun ?invFun
@@ -462,7 +370,16 @@ def derive_iff_star {α: Type} {x: α} {R: Lang α} {xs: List α}:
462
370
case invFun =>
463
371
sorry
464
372
465
- def derive_star {α: Type } {a : α} {P : Lang α}:
466
- (derive (star P) a ) = (concat (derive P a ) (star P )) := by
373
+ def derive_star {α: Type } {x : α} {R : Lang α}:
374
+ (derive (star R) x ) = (concat (derive R x ) (star R )) := by
467
375
funext
468
376
rw [derive_iff_star]
377
+
378
+ def derive_not {α: Type } {x: α} {R: Lang α}:
379
+ (derive (not R) x) = Not ∘ (derive R x) :=
380
+ rfl
381
+
382
+ def derive_iff_not {α: Type } {x: α} {R: Lang α} {xs: List α}:
383
+ (derive (not R) x) xs <-> Not ((derive R x) xs) := by
384
+ rw [derive_not]
385
+ rfl
0 commit comments