-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.lean
304 lines (268 loc) · 7.62 KB
/
cfg.lean
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
import context_sensitive.csg
section cfg_definitions
/-- Context-free grammar that generates words over the alphabet `termi` (a type of terminals). -/
structure CF_grammar (termi : Type) :=
(nt : Type) -- type of nonterminals
(initial : nt) -- initial symbol
(rules : list (nt × (list (symbol termi nt)))) -- rewriting rules
variables {T : Type} (g : CF_grammar T)
/-- One step of context-free transformation. -/
def CF_transforms (oldWord newWord : list (symbol T g.nt)) : Prop :=
∃ r ∈ g.rules, ∃ v w : list (symbol T g.nt),
oldWord = v ++ [symbol.nonterminal (prod.fst r)] ++ w ∧ newWord = v ++ (prod.snd r) ++ w
/-- Any number of steps of context-free transformation; reflexive+transitive closure of `CF_transforms`. -/
def CF_derives : list (symbol T g.nt) → list (symbol T g.nt) → Prop :=
relation.refl_trans_gen (CF_transforms g)
/-- Accepts a string (a list of symbols) iff it can be derived from the initial nonterminal. -/
def CF_generates_str (str : list (symbol T g.nt)) : Prop :=
CF_derives g [symbol.nonterminal g.initial] str
/-- Accepts a word (a list of terminals) iff it can be derived from the initial nonterminal. -/
def CF_generates (word : list T) : Prop :=
CF_generates_str g (list.map symbol.terminal word)
/-- Context-free language; just a wrapper around `CF_generates`. -/
def CF_language : language T :=
CF_generates g
end cfg_definitions
/-- Predicate "is context-free"; defined by an existence of a context-free grammar for given language. -/
def is_CF {T : Type} (L : language T) : Prop :=
∃ g : CF_grammar T, CF_language g = L
section cfg_utilities
variables {T : Type} {g : CF_grammar T}
lemma CF_deri_of_tran {v w : list (symbol T g.nt)} :
CF_transforms g v w → CF_derives g v w :=
relation.refl_trans_gen.single
/-- The relation `CF_derives` is reflexive. -/
lemma CF_deri_self {w : list (symbol T g.nt)} :
CF_derives g w w :=
relation.refl_trans_gen.refl
/-- The relation `CF_derives` is transitive. -/
lemma CF_deri_of_deri_deri
{u v w : list (symbol T g.nt)}
(huv : CF_derives g u v)
(hvw : CF_derives g v w) :
CF_derives g u w :=
relation.refl_trans_gen.trans huv hvw
lemma CF_deri_of_deri_tran
{u v w : list (symbol T g.nt)}
(huv : CF_derives g u v)
(hvw : CF_transforms g v w) :
CF_derives g u w :=
CF_deri_of_deri_deri huv (CF_deri_of_tran hvw)
lemma CF_deri_of_tran_deri
{u v w : list (symbol T g.nt)}
(huv : CF_transforms g u v)
(hvw : CF_derives g v w) :
CF_derives g u w :=
CF_deri_of_deri_deri (CF_deri_of_tran huv) hvw
lemma CF_tran_or_id_of_deri {u w : list (symbol T g.nt)} (h : CF_derives g u w) :
(u = w) ∨
(∃ v : list (symbol T g.nt), (CF_transforms g u v) ∧ (CF_derives g v w)) :=
relation.refl_trans_gen.cases_head h
lemma CF_derives_with_prefix
{oldWord newWord : list (symbol T g.nt)}
(prefi : list (symbol T g.nt))
(h : CF_derives g oldWord newWord) :
CF_derives g (prefi ++ oldWord) (prefi ++ newWord) :=
begin
induction h with a b irr hyp ih,
{
apply CF_deri_self,
},
apply CF_deri_of_deri_tran,
{
exact ih,
},
rcases hyp with ⟨ rule, rule_in, v, w, h_bef, h_aft ⟩,
use rule,
split,
{
exact rule_in,
},
use prefi ++ v,
use w,
rw h_bef,
rw h_aft,
split;
simp only [list.append_assoc],
end
lemma CF_derives_with_postfix
{oldWord newWord : list (symbol T g.nt)}
(posfi : list (symbol T g.nt))
(h : CF_derives g oldWord newWord) :
CF_derives g (oldWord ++ posfi) (newWord ++ posfi) :=
begin
induction h with a b irr hyp ih,
{
apply CF_deri_self,
},
apply CF_deri_of_deri_tran,
{
exact ih,
},
rcases hyp with ⟨ rule, rule_in, v, w, h_bef, h_aft ⟩,
use rule,
split,
{
exact rule_in,
},
use v,
use w ++ posfi,
rw h_bef,
rw h_aft,
split;
simp only [list.append_assoc],
end
lemma CF_derives_with_prefix_and_postfix
{oldWord newWord : list (symbol T g.nt)}
(prefi posfi : list (symbol T g.nt))
(h : CF_derives g oldWord newWord) :
CF_derives g (prefi ++ oldWord ++ posfi) (prefi ++ newWord ++ posfi) :=
begin
apply CF_derives_with_postfix,
apply CF_derives_with_prefix,
exact h,
end
end cfg_utilities
section cfg_conversion
variable {T : Type}
def csg_of_cfg (g : CF_grammar T) : CS_grammar T :=
CS_grammar.mk g.nt g.initial (list.map (λ r : g.nt × (list (symbol T g.nt)),
csrule.mk [] r.fst [] r.snd) g.rules)
def grammar_of_cfg (g : CF_grammar T) : grammar T :=
grammar.mk g.nt g.initial (list.map (λ r : g.nt × (list (symbol T g.nt)),
grule.mk ([], r.fst, []) r.snd) g.rules)
lemma grammar_of_cfg_well_defined (g : CF_grammar T) :
grammar_of_csg (csg_of_cfg g) = grammar_of_cfg g :=
begin
unfold grammar_of_cfg,
delta csg_of_cfg,
delta grammar_of_csg,
simp,
ext1,
simp,
apply congr_fun,
dsimp,
ext1,
cases x,
{
refl,
},
-- option.some
apply congr_arg option.some,
dsimp,
rw list.append_nil,
end
lemma grammar_of_csg_of_cfg :
grammar_of_csg ∘ csg_of_cfg = @grammar_of_cfg T :=
begin
ext,
apply grammar_of_cfg_well_defined,
end
lemma CF_language_eq_CS_language (g : CF_grammar T) :
CF_language g = CS_language (csg_of_cfg g) :=
begin
unfold CF_language,
unfold CS_language,
ext1 w,
change
CF_derives g [symbol.nonterminal g.initial] (list.map symbol.terminal w) =
CS_derives (csg_of_cfg g) [symbol.nonterminal (csg_of_cfg g).initial] (list.map symbol.terminal w),
rw eq_iff_iff,
split,
{
have indu :
∀ v : list (symbol T g.nt),
CF_derives g [symbol.nonterminal g.initial] v →
CS_derives (csg_of_cfg g) [symbol.nonterminal (csg_of_cfg g).initial] v,
{
clear w,
intros v h,
induction h with x y trash hyp ih,
{
apply CS_deri_self,
},
apply CS_deri_of_deri_tran,
{
exact ih,
},
unfold CF_transforms at hyp,
unfold CS_transforms,
delta csg_of_cfg,
dsimp,
rcases hyp with ⟨ r, rin, u, w, bef, aft ⟩,
use csrule.mk [] r.fst [] r.snd,
split,
{
finish,
},
use u,
use w,
split;
dsimp;
rw list.append_nil;
rw list.append_nil;
assumption,
},
exact indu (list.map symbol.terminal w),
},
{
have indu :
∀ v : list (symbol T g.nt),
CS_derives (csg_of_cfg g) [symbol.nonterminal g.initial] v →
CF_derives g [symbol.nonterminal (csg_of_cfg g).initial] v,
{
clear w,
intros v h,
induction h with x y trash hyp ih,
{
apply CF_deri_self,
},
apply CF_deri_of_deri_tran,
{
exact ih,
},
unfold CS_transforms at hyp,
unfold CF_transforms,
delta csg_of_cfg at hyp,
dsimp at hyp,
rcases hyp with ⟨ r, rin, u, w, bef, aft ⟩,
use (r.input_nonterminal, r.output_string),
split,
{
finish,
},
use u,
use w,
have cl_empty : r.context_left = list.nil,
{
finish,
},
have cr_empty : r.context_right = list.nil,
{
finish,
},
rw [cl_empty, cr_empty] at *,
repeat { rw list.append_nil at * },
split;
dsimp;
assumption,
},
exact indu (list.map symbol.terminal w),
},
end
lemma CF_language_eq_grammar_language (g : CF_grammar T) :
CF_language g = grammar_language (grammar_of_cfg g) :=
begin
rw ← grammar_of_cfg_well_defined,
rw CF_language_eq_CS_language,
rw CS_language_eq_grammar_language,
end
theorem CF_subclass_CS (L : language T) :
is_CF L → is_CS L :=
begin
rintro ⟨ g, h ⟩,
use csg_of_cfg g,
rw ← h,
rw CF_language_eq_CS_language,
end
end cfg_conversion