]> matita.cs.unibo.it Git - helm.git/blob - weblib/tutorial/chapter4.ma
New chapter 4
[helm.git] / weblib / tutorial / chapter4.ma
1
2
3 (**************************************************************************)
4 (*       ___                                                              *)
5 (*      ||M||                                                             *)
6 (*      ||A||       A project by Andrea Asperti                           *)
7 (*      ||T||                                                             *)
8 (*      ||I||       Developers:                                           *)
9 (*      ||T||         The HELM team.                                      *)
10 (*      ||A||         http://helm.cs.unibo.it                             *)
11 (*      \   /                                                             *)
12 (*       \ /        This file is distributed under the terms of the       *)
13 (*        v         GNU General Public License Version 2                  *)
14 (*                                                                        *)
15 (**************************************************************************)
16
17 include "arithmetics/nat.ma".
18 include "basics/lists/list.ma".
19 include "basics/sets.ma".
20
21 definition word ≝ λS:DeqSet.list S.
22
23 notation "ϵ" non associative with precedence 90 for @{ 'epsilon }.
24 interpretation "epsilon" 'epsilon = (nil ?).
25
26 (* concatenation *)
27 definition cat : ∀S,l1,l2,w.Prop ≝ 
28   λS.λl1,l2.λw:word S.∃w1,w2.w1 @ w2 = w ∧ l1 w1 ∧ l2 w2.
29 notation "a · b" non associative with precedence 60 for @{ 'middot $a $b}.
30 interpretation "cat lang" 'middot a b = (cat ? a b).
31
32 let rec flatten (S : DeqSet) (l : list (word S)) on l : word S ≝ 
33 match l with [ nil ⇒ [ ] | cons w tl ⇒ w @ flatten ? tl ].
34
35 let rec conjunct (S : DeqSet) (l : list (word S)) (r : word S → Prop) on l: Prop ≝
36 match l with [ nil ⇒ True | cons w tl ⇒ r w ∧ conjunct ? tl r ]. 
37
38 (* kleene's star *)
39 definition star ≝ λS.λl.λw:word S.∃lw.flatten ? lw = w ∧ conjunct ? lw l. 
40 notation "a ^ *" non associative with precedence 90 for @{ 'star $a}.
41 interpretation "star lang" 'star l = (star ? l).
42
43 lemma cat_ext_l: ∀S.∀A,B,C:word S →Prop. 
44   A =1 C  → A · B =1 C · B.
45 #S #A #B #C #H #w % * #w1 * #w2 * * #eqw #inw1 #inw2
46 cases (H w1) /6/ 
47 qed.
48
49 lemma cat_ext_r: ∀S.∀A,B,C:word S →Prop. 
50   B =1 C → A · B =1 A · C.
51 #S #A #B #C #H #w % * #w1 * #w2 * * #eqw #inw1 #inw2
52 cases (H w2) /6/ 
53 qed.
54   
55 lemma distr_cat_r: ∀S.∀A,B,C:word S →Prop.
56   (A ∪ B) · C =1  A · C ∪ B · C. 
57 #S #A #B #C #w %
58   [* #w1 * #w2 * * #eqw * /6/ |* * #w1 * #w2 * * /6/] 
59 qed.
60
61 lemma espilon_in_star: ∀S.∀A:word S → Prop.
62   A^* ϵ.
63 #S #A @(ex_intro … [ ]) normalize /2/
64 qed.
65
66 lemma cat_to_star:∀S.∀A:word S → Prop.
67   ∀w1,w2. A w1 → A^* w2 → A^* (w1@w2).
68 #S #A #w1 #w2 #Aw * #l * #H #H1 @(ex_intro … (w1::l)) 
69 % normalize /2/
70 qed.
71
72 lemma fix_star: ∀S.∀A:word S → Prop. 
73   A^* =1 A · A^* ∪ {ϵ}.
74 #S #A #w %
75   [* #l generalize in match w; -w cases l [normalize #w * /2/]
76    #w1 #tl #w * whd in ⊢ ((??%?)→?); #eqw whd in ⊢ (%→?); *
77    #w1A #cw1 %1 @(ex_intro … w1) @(ex_intro … (flatten S tl))
78    % /2/ whd @(ex_intro … tl) /2/
79   |* [2: whd in ⊢ (%→?); #eqw <eqw //]
80    * #w1 * #w2 * * #eqw <eqw @cat_to_star 
81   ]
82 qed.
83
84 lemma star_fix_eps : ∀S.∀A:word S → Prop.
85   A^* =1 (A - {ϵ}) · A^* ∪ {ϵ}.  
86 #S #A #w %
87   [* #l elim l 
88     [* whd in ⊢ ((??%?)→?); #eqw #_ %2 <eqw // 
89     |* [#tl #Hind * #H * #_ #H2 @Hind % [@H | //]
90        |#a #w1 #tl #Hind * whd in ⊢ ((??%?)→?); #H1 * #H2 #H3 %1 
91         @(ex_intro … (a::w1)) @(ex_intro … (flatten S tl)) %
92          [% [@H1 | normalize % /2/] |whd @(ex_intro … tl) /2/]
93        ]
94     ]
95   |* [* #w1 * #w2 * * #eqw * #H1 #_ <eqw @cat_to_star //
96      | whd in ⊢ (%→?); #H <H //
97      ]
98   ]
99 qed. 
100      
101 lemma star_epsilon: ∀S:DeqSet.∀A:word S → Prop.
102   A^* ∪ {ϵ} =1 A^*.
103 #S #A #w % /2/ * // 
104 qed.
105   
106 lemma epsilon_cat_r: ∀S.∀A:word S →Prop.
107    A · {ϵ} =1  A. 
108 #S #A #w %
109   [* #w1 * #w2 * * #eqw #inw1 normalize #eqw2 <eqw //
110   |#inA @(ex_intro … w) @(ex_intro … [ ]) /3/
111   ]
112 qed.
113
114 lemma epsilon_cat_l: ∀S.∀A:word S →Prop.
115    {ϵ} · A =1  A. 
116 #S #A #w %
117   [* #w1 * #w2 * * #eqw normalize #eqw2 <eqw <eqw2 //
118   |#inA @(ex_intro … ϵ) @(ex_intro … w) /3/
119   ]
120 qed.
121
122 lemma distr_cat_r_eps: ∀S.∀A,C:word S →Prop.
123   (A ∪ {ϵ}) · C =1  A · C ∪ C. 
124 #S #A #C @eqP_trans [|@distr_cat_r |@eqP_union_l @epsilon_cat_l]
125 qed.
126