-
Notifications
You must be signed in to change notification settings - Fork 4
/
terms.v
296 lines (240 loc) · 8.4 KB
/
terms.v
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
Require Import bin_rels.
Require Import eq_rel.
Require Import universe.
Require Import LibTactics.
Require Import tactics.
Require Import Coq.Bool.Bool.
Require Import Coq.Program.Tactics.
Require Import Omega.
Require Import Coq.Program.Basics.
Require Import Coq.Lists.List.
Require Import Coq.Init.Notations.
Require Import UsefulTypes.
Require Import Coq.Classes.DecidableClass.
Require Import Coq.Classes.Morphisms.
Require Import list.
Require Import Recdef.
Require Import Eqdep_dec.
Require Import varInterface.
Class GenericTermSig (Opid : Type) : Type :=
{
(** the collection of operators in the language. For example, in lambda calculus, App and Lam are operators *)
(** Arities for each operator. An [Opid] [o] takes
[length (OpBindings o)] arguments. The number of bound variables in the ith argument is
specified by the ith member of [(OpBindings o)].
For example this signature for Lambda is [[1]]. And it is [[0,0]] for App.
A lambda takes one argument and that argument binds 1 variable. An application takes 2 arguments,
each of which bind 0 variables.*)
OpBindings : Opid -> list nat;
}.
Generalizable Variable Opid.
Inductive NTerm {NVar Opid} : Type :=
| vterm: NVar -> NTerm
| oterm: Opid -> list BTerm -> NTerm
with BTerm {NVar Opid} : Type :=
| bterm: (list NVar) -> NTerm -> BTerm.
Section terms.
Context {NVar VarClass} `{VarType NVar VarClass} `{Deq Opid} {gts : GenericTermSig Opid}.
Notation NTerm := (@NTerm NVar Opid).
Notation BTerm := (@BTerm NVar Opid).
(*
The [Opid] type contains one element corresponding to every operator
of the language, e.g. lambda abstraction, funtion application,
dependent function type constructor. As a more concrete example,
the [NLambda] is the element of [Opid] that represents lambda
abstractions.
To construct a bound term([BTerm]), we need a list of variables and
an [NTerm] (see the [bterm] constructor). As a concrete example,
$ \lambda x.y$ is represented in this type
as [(oterm NLambda (bterm [x] (vterm y)))].
*)
(**
It is a mutually inductive definition that simultaneously defines terms
and bound terms. As mentioned before, the [vterm] constructor
takes an [NVar] and constructs an [NTerm]. The other constructor([oterm])
takes an [Opid] and a list of bound terms ([BTerm]s) and constructs an [NTerm].
Note that not all members of [NTerm] are meaningful(well-formed).
For example, the [Opid] corresponding to lambda abstractions must be
provided with exactly one bound term as argument. Moreover, that
bound term must have exactly one bound variable. So, we have a function
[OpBindings] in type [Opid -> list nat] that specifies both the
number of arguments and the number of bound variables in each argument([BTerm]).
We will use it soon to define the subcollection of well-formed terms.
*)
(* begin hide *)
(*
Definition term_rel := NTerm -> NTerm -> Type.
*)
Definition isvar (t : NTerm) :=
match t with
| vterm _ => true
| _ => false
end.
Definition isvariable (t : NTerm) :=
match t with
| vterm _ => True
| _ => False
end.
Definition getOpid (n: NTerm) : option Opid :=
match n with
| vterm _ => None
| oterm o _ => Some o
end.
(*Notation "x # b" := (bterm [x] b) (at level 70, right associativity).
(*Check [[ btermO (vterm(nvar 0))]] *)
(* Notation "< N >" := (btermO N). *)
Notation "\\ f" :=
(oterm (Can NLambda) [[f]]) (at level 70, right associativity).
*)
(* ------ CONSTRUCTORS ------ *)
(* --- primitives --- *)
Definition mk_var (nv : NVar) : NTerm := vterm nv.
(* end hide *)
(** %\noindent% Whenever we talk about the [NTerm] of a [BTerm], this is
what we would mean:
*)
Definition get_nt (bt: BTerm ) : NTerm :=
match bt with
| bterm lv nt => nt
end.
Definition get_vars (bt: BTerm ) : list NVar :=
match bt with
| bterm lv nt => lv
end.
Definition num_bvars (bt : BTerm) := length (get_vars bt).
(** % \noindent \\* % We define
a predicate [nt_wf] on [NTerm] such that
[nt_wf nt] asserts that [nt] is a well-formed term. %\\* %
*)
Inductive nt_wf: NTerm -> [univ] :=
| wfvt: forall nv : NVar, nt_wf (vterm nv)
| wfot: forall (o: Opid) (lnt: list BTerm),
(forall l, LIn l lnt -> bt_wf l)
-> map (num_bvars) lnt
= OpBindings o
-> nt_wf (oterm o lnt)
with bt_wf : BTerm -> [univ] :=
| wfbt : forall (lnv : list NVar) (nt: NTerm),
nt_wf nt -> bt_wf (bterm lnv nt).
End terms.
(* closing the section because there is a problem with
simpl and sections and mutual fixpoints
https://coq.inria.fr/bugs/show_bug.cgi?id=3343
*)
(* --- variables --- *)
(** Just decidability of equality on variables suffices for these definitions.
The full [VarType] may not be needed until [ssubst]*)
Fixpoint free_vars {NVar} `{Deq NVar} {Opid:Type}
(t:NTerm) {struct t}: list NVar :=
match t with
| vterm v => [v]
| oterm op bts => flat_map (@free_vars_bterm NVar _ Opid ) bts
end
with free_vars_bterm {NVar} `{Deq NVar} {Opid:Type}
(bt : @BTerm NVar Opid)
{struct bt} : list NVar :=
match bt with
| bterm lv nt => remove_nvars lv (@free_vars NVar _ Opid nt)
end.
Fixpoint allVars {NVar} {Opid:Type}
(t:NTerm) {struct t}: list NVar :=
match t with
| vterm v => [v]
| oterm op bts => flat_map (@allVars_bterm NVar Opid ) bts
end
with allVars_bterm {NVar} {Opid:Type}
(bt : @BTerm NVar Opid)
{struct bt} : list NVar :=
match bt with
| bterm lv nt => lv ++ (@allVars NVar Opid nt)
end.
Fixpoint bound_vars {NVar} `{Deq NVar} {Opid:Type} (t : NTerm) : list NVar :=
match t with
| vterm v => []
| oterm op bts => flat_map (@bound_vars_bterm NVar _ Opid) bts
end
with bound_vars_bterm {NVar} `{Deq NVar} {Opid:Type} (bt : @BTerm NVar Opid)
:list NVar :=
match bt with
| bterm lv nt => lv ++ bound_vars nt
end.
Section termsCont.
Context {NVar VarClass} `{VarType NVar VarClass} `{Deq Opid} {gts : GenericTermSig Opid}.
Definition all_vars (t:@NTerm NVar Opid) : list NVar := free_vars t ++ bound_vars t.
Definition closed (t : @NTerm NVar Opid) := free_vars t = [].
(* Howe's T_0(L) *)
Definition isprogram (t : @NTerm NVar Opid) := closed t # nt_wf t.
Definition getVar (t: @NTerm NVar Opid) : option NVar :=
match t with
| vterm v => Some v
| _ => None
end.
End termsCont.
Fixpoint tmap {V1 V2 O1 O2 :Type} (fv: V1 -> V2) (fo : O1 -> O2) (t : @NTerm V1 O1)
: (@NTerm V2 O2) :=
match t with
| vterm v => vterm (fv v)
| oterm o lbt => oterm (fo o) (map (tmap_bterm fv fo) lbt)
end
with
tmap_bterm {V1 V2 O1 O2 :Type} (fv: V1 -> V2) (fo : O1 -> O2) (t : @BTerm V1 O1)
: (@BTerm V2 O2) :=
match t with
| bterm lv nt => bterm (map fv lv) (tmap fv fo nt)
end.
Definition tvmap {V1 V2 O :Type} (fv: V1 -> V2) : (@NTerm V1 O) -> (@NTerm V2 O) :=
tmap fv id.
Require Import String.
Definition flatten (l:list string) : string :=
List.fold_left append l EmptyString.
Fixpoint flattenDelim (d:string) (l:list string) {struct l}: string :=
match l with
| nil => EmptyString
| h::tl => match tl with
| nil => h
| m::tm => flatten [h;d; flattenDelim d tl]
end
end.
(*
Eval vm_compute in (flattenDelim "," []).
Eval vm_compute in (flattenDelim "," ["hello"]).
Eval vm_compute in (flattenDelim "," ["hello"; "how"]).
Eval vm_compute in (flattenDelim "," ["hello"; "how" ; "are"]).
*)
Definition newLineChar : Ascii.ascii := Ascii.ascii_of_nat 10.
Definition newLineString : string := String newLineChar EmptyString.
Fixpoint tprint {V O :Type} (spaces:string) (fv: V -> string) (fo : O -> string) (t : @NTerm V O)
: string :=
match t with
| vterm v => flatten [fv v; newLineString]
| oterm o lbt =>
flatten [(fo o); newLineString; flatten (map (bprint (append " " spaces) fv fo) lbt)]
end
with
bprint {V O :Type} (spaces:string) (fv: V -> string) (fo : O -> string) (t : @BTerm V O)
: string :=
match t with
| bterm lv nt =>
let pv := flattenDelim " " (map fv lv) in
let pt := tprint spaces fv fo nt in
flatten [spaces; pv; "." ; pt]
end.
(* Move. and replace in SquiggleEq.terms*)
Definition btMapNt {O O2 V} (f: @NTerm V O -> @NTerm V O2)
(b: @BTerm V O) : @BTerm V O2 :=
match b with
|bterm lv nt => bterm lv (f nt)
end.
Definition btSkipBinders {O V} (n:nat)
(b: @BTerm O V) : @BTerm O V :=
match b with
|bterm lv nt => bterm (skipn n lv) nt
end.
Definition tvmap_bterm :=
fun {V1 V2 O : Type} (fv : V1-> V2) =>
@tmap_bterm V1 V2 O O fv (@id O).
Definition getFirstBTermVars {V O }(t:list (@BTerm V O)) : list V:=
match t with
| (bterm lv _)::_ => lv
| [] => []
end.