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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
open Util
open Pp
open CErrors
open Names
open Vars
open Constr
open Context
type sorts_family = Sorts.family = InSProp | InProp | InSet | InType | InQSort
[@@ocaml.deprecated "Alias for Sorts.family"]
type sorts = Sorts.t = private
| SProp | Prop | Set
| Type of Univ.Universe.t (** Type *)
| QSort of Sorts.QVar.t * Univ.Universe.t
[@@ocaml.deprecated "Alias for Sorts.t"]
let name_annot = map_annot Name.mk_name
let mkNamedProd id typ c = mkProd (name_annot id, typ, subst_var id.binder_name c)
let mkNamedLambda id typ c = mkLambda (name_annot id, typ, subst_var id.binder_name c)
let mkNamedLetIn id c1 t c2 = mkLetIn (name_annot id, c1, t, subst_var id.binder_name c2)
let mkProd_or_LetIn decl c =
let open Context.Rel.Declaration in
match decl with
| LocalAssum (na,t) -> mkProd (na, t, c)
| LocalDef (na,b,t) -> mkLetIn (na, b, t, c)
let mkNamedProd_or_LetIn decl c =
let open Context.Named.Declaration in
match decl with
| LocalAssum (id,t) -> mkNamedProd id t c
| LocalDef (id,b,t) -> mkNamedLetIn id b t c
let mkProd_wo_LetIn decl c =
let open Context.Rel.Declaration in
match decl with
| LocalAssum (na,t) -> mkProd (na, t, c)
| LocalDef (_na,b,_t) -> subst1 b c
let mkNamedProd_wo_LetIn decl c =
let open Context.Named.Declaration in
match decl with
| LocalAssum (id,t) -> mkNamedProd id t c
| LocalDef (id,b,_) -> subst1 b (subst_var id.binder_name c)
let mkArrow t1 r t2 = mkProd (make_annot Anonymous r, t1, t2)
let mkArrowR t1 t2 = mkArrow t1 Sorts.Relevant t2
let mkLambda_or_LetIn decl c =
let open Context.Rel.Declaration in
match decl with
| LocalAssum (na,t) -> mkLambda (na, t, c)
| LocalDef (na,b,t) -> mkLetIn (na, b, t, c)
let mkNamedLambda_or_LetIn decl c =
let open Context.Named.Declaration in
match decl with
| LocalAssum (id,t) -> mkNamedLambda id t c
| LocalDef (id,b,t) -> mkNamedLetIn id b t c
let prodn n env b =
let rec prodrec = function
| (0, _env, b) -> b
| (n, ((v,t)::l), b) -> prodrec (n-1, l, mkProd (v,t,b))
| _ -> assert false
in
prodrec (n,env,b)
let compose_prod l b = prodn (List.length l) l b
let lamn n env b =
let rec lamrec = function
| (0, _env, b) -> b
| (n, ((v,t)::l), b) -> lamrec (n-1, l, mkLambda (v,t,b))
| _ -> assert false
in
lamrec (n,env,b)
let compose_lam l b = lamn (List.length l) l b
let applist (f,l) = mkApp (f, Array.of_list l)
let applistc f l = mkApp (f, Array.of_list l)
let appvect = mkApp
let appvectc f l = mkApp (f,l)
let rec to_lambda n prod =
if Int.equal n 0 then
prod
else
match kind prod with
| Prod (na,ty,bd) -> mkLambda (na,ty,to_lambda (n-1) bd)
| Cast (c,_,_) -> to_lambda n c
| _ -> anomaly Pp.(str "Not enough lambda's.")
let rec to_prod n lam =
if Int.equal n 0 then
lam
else
match kind lam with
| Lambda (na,ty,bd) -> mkProd (na,ty,to_prod (n-1) bd)
| Cast (c,_,_) -> to_prod n c
| _ -> anomaly Pp.(str "Not enough prod's.")
let it_mkProd_or_LetIn = List.fold_left (fun c d -> mkProd_or_LetIn d c)
let it_mkProd_wo_LetIn = List.fold_left (fun c d -> mkProd_wo_LetIn d c)
let it_mkLambda_or_LetIn = List.fold_left (fun c d -> mkLambda_or_LetIn d c)
let lambda_applist c l =
let rec app subst c l =
match kind c, l with
| Lambda(_,_,c), arg::l -> app (arg::subst) c l
| _, [] -> substl subst c
| _ -> anomaly (Pp.str "Not enough lambda's.") in
app [] c l
let lambda_appvect c v = lambda_applist c (Array.to_list v)
let lambda_applist_decls n c l =
let rec app n subst t l =
if Int.equal n 0 then
if l == [] then substl subst t
else anomaly (Pp.str "Too many arguments.")
else match kind t, l with
| Lambda(_,_,c), arg::l -> app (n-1) (arg::subst) c l
| LetIn(_,b,_,c), _ -> app (n-1) (substl subst b::subst) c l
| _, [] -> anomaly (Pp.str "Not enough arguments.")
| _ -> anomaly (Pp.str "Not enough lambda/let's.") in
app n [] c l
let lambda_appvect_decls n c v = lambda_applist_decls n c (Array.to_list v)
let prod_applist c l =
let rec app subst c l =
match kind c, l with
| Prod(_,_,c), arg::l -> app (arg::subst) c l
| _, [] -> substl subst c
| _ -> anomaly (Pp.str "Not enough prod's.") in
app [] c l
let prod_appvect c v = prod_applist c (Array.to_list v)
let prod_applist_decls n c l =
let rec app n subst t l =
if Int.equal n 0 then
if l == [] then substl subst t
else anomaly (Pp.str "Too many arguments.")
else match kind t, l with
| Prod(_,_,c), arg::l -> app (n-1) (arg::subst) c l
| LetIn(_,b,_,c), _ -> app (n-1) (substl subst b::subst) c l
| _, [] -> anomaly (Pp.str "Not enough arguments.")
| _ -> anomaly (Pp.str "Not enough prod/let's.") in
app n [] c l
let prod_appvect_decls n c v = prod_applist_decls n c (Array.to_list v)
let decompose_prod =
let rec prodec_rec l c = match kind c with
| Prod (x,t,c) -> prodec_rec ((x,t)::l) c
| Cast (c,_,_) -> prodec_rec l c
| _ -> l,c
in
prodec_rec []
let decompose_lambda =
let rec lamdec_rec l c = match kind c with
| Lambda (x,t,c) -> lamdec_rec ((x,t)::l) c
| Cast (c,_,_) -> lamdec_rec l c
| _ -> l,c
in
lamdec_rec []
let decompose_prod_n n =
if n < 0 then anomaly (str "decompose_prod_n: integer parameter must be positive.");
let rec prodec_rec l n c =
if Int.equal n 0 then l,c
else match kind c with
| Prod (x,t,c) -> prodec_rec ((x,t)::l) (n-1) c
| Cast (c,_,_) -> prodec_rec l n c
| _ -> anomaly (str "decompose_prod_n: not enough products.")
in
prodec_rec [] n
let decompose_lambda_n n =
if n < 0 then anomaly (str "decompose_lambda_n: integer parameter must be positive.");
let rec lamdec_rec l n c =
if Int.equal n 0 then l,c
else match kind c with
| Lambda (x,t,c) -> lamdec_rec ((x,t)::l) (n-1) c
| Cast (c,_,_) -> lamdec_rec l n c
| _ -> anomaly (str "decompose_lambda_n: not enough abstractions.")
in
lamdec_rec [] n
let decompose_prod_decls =
let open Context.Rel.Declaration in
let rec prodec_rec l c =
match kind c with
| Prod (x,t,c) -> prodec_rec (Context.Rel.add (LocalAssum (x,t)) l) c
| LetIn (x,b,t,c) -> prodec_rec (Context.Rel.add (LocalDef (x,b,t)) l) c
| Cast (c,_,_) -> prodec_rec l c
| _ -> l,c
in
prodec_rec Context.Rel.empty
let decompose_lambda_decls =
let rec lamdec_rec l c =
let open Context.Rel.Declaration in
match kind c with
| Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) c
| LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) c
| Cast (c,_,_) -> lamdec_rec l c
| _ -> l,c
in
lamdec_rec Context.Rel.empty
let decompose_prod_n_decls n =
if n < 0 then
anomaly (str "decompose_prod_n_decls: integer parameter must be positive.");
let rec prodec_rec l n c =
if Int.equal n 0 then l,c
else
let open Context.Rel.Declaration in
match kind c with
| Prod (x,t,c) -> prodec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
| LetIn (x,b,t,c) -> prodec_rec (Context.Rel.add (LocalDef (x,b,t)) l) (n-1) c
| Cast (c,_,_) -> prodec_rec l n c
| _ -> anomaly (str "decompose_prod_n_decls: not enough declarations.")
in
prodec_rec Context.Rel.empty n
(** Given a positive integer n, decompose a lambda term [fun
(x1:T1)..(xn:Tn) => T] into the pair of the abstracted
context [(xn,None,Tn);...;(x1,None,T1)] and of the inner body [T]. *)
let decompose_lambda_n_assum n =
if n < 0 then
anomaly (str "decompose_lambda_n_assum: integer parameter must be positive.");
let rec lamdec_rec l n c =
if Int.equal n 0 then l,c
else
let open Context.Rel.Declaration in
match kind c with
| Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
| Cast (c,_,_) -> lamdec_rec l n c
| _c -> anomaly (str "decompose_lambda_n_assum: not enough abstractions.")
in
lamdec_rec Context.Rel.empty n
let decompose_lambda_n_decls n =
if n < 0 then
anomaly (str "decompose_lambda_n_decls: integer parameter must be positive.");
let rec lamdec_rec l n c =
if Int.equal n 0 then l,c
else
let open Context.Rel.Declaration in
match kind c with
| Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
| LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) (n-1) c
| Cast (c,_,_) -> lamdec_rec l n c
| _ -> anomaly (str "decompose_lambda_n_decls: not enough declarations.")
in
lamdec_rec Context.Rel.empty n
let prod_decls t = fst (decompose_prod_decls t)
let prod_n_decls n t = fst (decompose_prod_n_decls n t)
let strip_prod_decls t = snd (decompose_prod_decls t)
let strip_prod t = snd (decompose_prod t)
let strip_prod_n n t = snd (decompose_prod_n n t)
let lambda_decls t = fst (decompose_lambda_decls t)
let lam_n_assum n t = fst (decompose_lambda_n_assum n t)
let strip_lambda_decls t = snd (decompose_lambda_decls t)
let strip_lam t = snd (decompose_lambda t)
let strip_lam_n n t = snd (decompose_lambda_n n t)
type arity = Constr.rel_context * Sorts.t
let destArity =
let open Context.Rel.Declaration in
let rec prodec_rec l c =
match kind c with
| Prod (x,t,c) -> prodec_rec (LocalAssum (x,t) :: l) c
| LetIn (x,b,t,c) -> prodec_rec (LocalDef (x,b,t) :: l) c
| Cast (c,_,_) -> prodec_rec l c
| Sort s -> l,s
| _ -> anomaly ~label:"destArity" (Pp.str "not an arity.")
in
prodec_rec []
let mkArity (sign,s) = it_mkProd_or_LetIn (mkSort s) sign
let rec isArity c =
match kind c with
| Prod (_,_,c) -> isArity c
| LetIn (_,b,_,c) -> isArity (subst1 b c)
| Cast (c,_,_) -> isArity c
| Sort _ -> true
| _ -> false
let decompose_prod_assum = decompose_prod_decls
let decompose_lam_assum = decompose_lambda_decls
let decompose_prod_n_assum = decompose_prod_n_decls
let prod_assum = prod_decls
let lam_assum = lambda_decls
let prod_n_assum = prod_n_decls
let strip_prod_assum = strip_prod_decls
let strip_lam_assum = strip_lambda_decls
let decompose_lam = decompose_lambda
let decompose_lam_n = decompose_lambda_n
let decompose_lam_n_assum = decompose_lambda_n_assum
let decompose_lam_n_decls = decompose_lambda_n_decls