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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
open Pp
open CErrors
open Util
open Names
open Constr
open EConstr
open Declarations
open Tactypes
open Proofview
open Proofview.Notations
open Tacmach
module RelDecl = Context.Rel.Declaration
module NamedDecl = Context.Named.Declaration
exception FailError of int * Pp.t Lazy.t
let catch_failerror (e, info) =
match e with
| FailError (lvl,s) when lvl > 0 ->
tclZERO ~info (FailError (lvl - 1, s))
| e ->
Control.check_for_interrupt ();
tclUNIT ()
let fix_empty_or_and_pattern nv l =
match l with
| IntroOrPattern [[]] -> IntroOrPattern (List.make nv [])
| _ -> l
let check_or_and_pattern_size ?loc check_and names branchsigns =
let n = Array.length branchsigns in
let msg p1 p2 = strbrk "a conjunctive pattern made of " ++ int p1 ++ (if p1 == p2 then mt () else str " or " ++ int p2) ++ str " patterns" in
let err1 p1 p2 =
user_err ?loc (str "Expects " ++ msg p1 p2 ++ str ".") in
let errn n =
user_err ?loc (str "Expects a disjunctive pattern with " ++ int n
++ str " branches.") in
let err1' p1 p2 =
user_err ?loc (strbrk "Expects a disjunctive pattern with 1 branch or " ++ msg p1 p2 ++ str ".") in
let errforthcoming ?loc =
user_err ?loc (strbrk "Unexpected non atomic pattern.") in
match names with
| IntroAndPattern l ->
if not (Int.equal n 1) then errn n;
let l' = List.filter CAst.(function {v=IntroForthcoming _} -> true | {v=IntroNaming _} | {v=IntroAction _} -> false) l in
if l' != [] then errforthcoming ?loc:(List.hd l').CAst.loc;
if check_and then
let p1 = List.count (fun x -> x) branchsigns.(0) in
let p2 = List.length branchsigns.(0) in
let p = List.length l in
if not (Int.equal p p1 || Int.equal p p2) then err1 p1 p2;
if Int.equal p p1 then
IntroAndPattern
(List.extend branchsigns.(0) (CAst.make @@ IntroNaming Namegen.IntroAnonymous) l)
else
names
else
names
| IntroOrPattern ll ->
if not (Int.equal n (List.length ll)) then
if Int.equal n 1 then
let p1 = List.count (fun x -> x) branchsigns.(0) in
let p2 = List.length branchsigns.(0) in
err1' p1 p2 else errn n;
names
let get_and_check_or_and_pattern_gen ?loc check_and names branchsigns =
let names = check_or_and_pattern_size ?loc check_and names branchsigns in
match names with
| IntroAndPattern l -> [|l|]
| IntroOrPattern l -> Array.of_list l
let get_and_check_or_and_pattern ?loc = get_and_check_or_and_pattern_gen ?loc true
let compute_induction_names check_and branchletsigns = function
| None ->
Array.make (Array.length branchletsigns) []
| Some {CAst.loc;v=names} ->
let names = fix_empty_or_and_pattern (Array.length branchletsigns) names in
get_and_check_or_and_pattern_gen check_and ?loc names branchletsigns
let is_recursive_argument env self recarg = match Declareops.dest_recarg recarg with
| Norec | Mrec (RecArgPrim _) -> false
| Mrec (RecArgInd ind) -> Environ.QInd.equal env self ind
let compute_constructor_signatures env ~rec_flag ((_,k as ity),u) =
let rec analrec c recargs =
match c, recargs with
| RelDecl.LocalAssum _ :: c, recarg::rest ->
let rest = analrec c rest in
if rec_flag && is_recursive_argument env ity recarg then true :: true :: rest
else true :: rest
| RelDecl.LocalDef _ :: c, rest -> false :: analrec c rest
| [], [] -> []
| _ -> anomaly (Pp.str "compute_constructor_signatures.")
in
let (mib,mip) = Inductive.lookup_mind_specif env ity in
let map (ctx, _) = List.skipn (Context.Rel.length mib.mind_params_ctxt) (List.rev ctx) in
let lc = Array.map map mip.mind_nf_lc in
let lrecargs = Declareops.dest_subterms mip.mind_recargs in
Array.map2 analrec lc lrecargs
let tclIDTAC = tclUNIT ()
let tclTHEN t1 t2 =
t1 <*> t2
let tclFAILn ?info lvl msg =
let info = match info with
| None -> Exninfo.reify ()
| Some info -> info
in
tclZERO ~info (FailError (lvl,lazy msg))
let tclFAIL ?info msg = tclFAILn ?info 0 msg
let tclZEROMSG ?info ?loc msg =
let info = match info with
| None -> Exninfo.reify ()
| Some info -> info
in
let info = match loc with
| None -> info
| Some loc -> Loc.add_loc info loc
in
let err = UserError msg in
tclZERO ~info err
let tclOR t1 t2 =
tclINDEPENDENT begin
Proofview.tclOR
t1
begin fun e ->
catch_failerror e <*> t2
end
end
let tclORD t1 t2 =
tclINDEPENDENT begin
Proofview.tclOR
t1
begin fun e ->
catch_failerror e <*> t2 ()
end
end
let tclONCE = Proofview.tclONCE
let tclEXACTLY_ONCE t = Proofview.tclEXACTLY_ONCE (FailError(0,lazy (assert false))) t
let tclIFCATCH t tt te =
tclINDEPENDENT begin
Proofview.tclIFCATCH t
tt
(fun e -> catch_failerror e <*> te ())
end
let tclORELSE0 t1 t2 =
tclINDEPENDENT begin
tclORELSE
t1
begin fun e ->
catch_failerror e <*> t2
end
end
let tclORELSE0L t1 t2 =
tclINDEPENDENTL begin
tclORELSE
t1
begin fun e ->
catch_failerror e <*> t2
end
end
let tclORELSE t1 t2 =
tclORELSE0 (tclPROGRESS t1) t2
let tclTHENS3PARTS t1 l1 repeat l2 =
tclINDEPENDENT begin
t1 <*>
Proofview.tclORELSE
begin tclEXTEND (Array.to_list l1) repeat (Array.to_list l2) end
begin function (e, info) -> match e with
| SizeMismatch (i,_)->
let errmsg =
str"Incorrect number of goals" ++ spc() ++
str"(expected "++int i++str(String.plural i " tactic") ++ str")"
in
tclFAIL errmsg
| reraise -> tclZERO ~info reraise
end
end
let tclTHENSFIRSTn t1 l repeat =
tclTHENS3PARTS t1 l repeat [||]
let tclTHENFIRSTn t1 l =
tclTHENSFIRSTn t1 l (tclUNIT())
let tclTHENFIRST t1 t2 =
tclTHENFIRSTn t1 [|t2|]
let tclBINDFIRST t1 t2 =
t1 >>= fun ans ->
Proofview.Unsafe.tclGETGOALS >>= fun gls ->
match gls with
| [] -> tclFAIL (str "Expect at least one goal.")
| hd::tl ->
Proofview.Unsafe.tclSETGOALS [hd] <*> t2 ans >>= fun ans ->
Proofview.Unsafe.tclNEWGOALS tl <*>
Proofview.tclUNIT ans
let tclTHENSLASTn t1 repeat l =
tclTHENS3PARTS t1 [||] repeat l
let tclTHENLASTn t1 l =
tclTHENS3PARTS t1 [||] (tclUNIT()) l
let tclTHENLAST t1 t2 = tclTHENLASTn t1 [|t2|]
let option_of_failure f x = try Some (f x) with Failure _ -> None
let tclBINDLAST t1 t2 =
t1 >>= fun ans ->
Proofview.Unsafe.tclGETGOALS >>= fun gls ->
match option_of_failure List.sep_last gls with
| None -> tclFAIL (str "Expect at least one goal.")
| Some (last,firstn) ->
Proofview.Unsafe.tclSETGOALS [last] <*> t2 ans >>= fun ans ->
Proofview.Unsafe.tclGETGOALS >>= fun newgls ->
tclEVARMAP >>= fun sigma ->
let firstn = Proofview.Unsafe.undefined sigma firstn in
Proofview.Unsafe.tclSETGOALS (firstn@newgls) <*>
Proofview.tclUNIT ans
let tclTHENS t l =
tclINDEPENDENT begin
t <*>Proofview.tclORELSE
begin tclDISPATCH l end
begin function (e, info) -> match e with
| SizeMismatch (i,_)->
let errmsg =
str"Incorrect number of goals" ++ spc() ++
str"(expected "++int i++str(String.plural i " tactic") ++ str")"
in
tclFAIL errmsg
| reraise -> tclZERO ~info reraise
end
end
let tclTHENLIST l =
List.fold_left tclTHEN (tclUNIT()) l
let tclMAP tacfun l =
List.fold_right (fun x -> (tclTHEN (tacfun x))) l (tclUNIT())
let tclTRY t =
tclORELSE0 t (tclUNIT ())
let tclTRYb t =
tclORELSE0L (t <*> tclUNIT true) (tclUNIT false)
let tclIFTHENELSE t1 t2 t3 =
tclINDEPENDENT begin
Proofview.tclIFCATCH t1
(fun () -> t2)
(fun (e, info) -> Proofview.tclORELSE t3 (fun e' -> tclZERO ~info e))
end
let tclIFTHENSVELSE t1 a t3 =
Proofview.tclIFCATCH t1
(fun () -> tclDISPATCH (Array.to_list a))
(fun _ -> t3)
let tclIFTHENFIRSTELSE t1 t2 t3 =
Proofview.tclIFCATCH t1
(fun () -> tclEXTEND [t2] (tclUNIT ()) [])
(fun _ -> t3)
let tclIFTHENTRYELSEMUST t1 t2 =
tclIFTHENELSE t1 (tclTRY t2) t2
let tclIFTHENFIRSTTRYELSEMUST t1 t2 =
tclIFTHENFIRSTELSE t1 (tclTRY t2) t2
let rec tclFIRST = function
| [] ->
let info = Exninfo.reify () in
tclZEROMSG ~info (str"No applicable tactic.")
| t::rest -> tclORELSE0 t (tclFIRST rest)
let rec tclFIRST_PROGRESS_ON tac = function
| [] -> tclFAIL (str "No applicable tactic")
| [a] -> tac a
| a::tl -> tclORELSE (tac a) (tclFIRST_PROGRESS_ON tac tl)
let rec tclDO n t =
if n < 0 then
let info = Exninfo.reify () in
tclZEROMSG ~info (str"Wrong argument : Do needs a positive integer.")
else if n = 0 then tclUNIT ()
else if n = 1 then t
else
tclTHEN t (tclUNIT () >>= fun () -> (tclDO (n-1) t))
let rec tclREPEAT0 t =
tclINDEPENDENT begin
Proofview.tclIFCATCH t
(fun () -> tclCHECKINTERRUPT <*> tclREPEAT0 t)
(fun e -> catch_failerror e <*> tclUNIT ())
end
let tclREPEAT t =
tclREPEAT0 (tclPROGRESS t)
let rec tclREPEAT_MAIN0 t =
Proofview.tclIFCATCH t
(fun () -> tclTRYFOCUS 1 1 (tclREPEAT_MAIN0 t))
(fun e -> catch_failerror e <*> tclUNIT ())
let tclREPEAT_MAIN t =
tclREPEAT_MAIN0 (tclPROGRESS t)
let tclCOMPLETE t =
t >>= fun res ->
(tclINDEPENDENT
(let info = Exninfo.reify () in
tclZEROMSG ~info (str"Proof is not complete."))
) <*>
tclUNIT res
let tclSOLVE tacl = tclFIRST (List.map tclCOMPLETE tacl)
let tclPROGRESS t =
Proofview.tclINDEPENDENT (Proofview.tclPROGRESS t)
let check_evar_list env sigma evars origsigma =
let reachable = lazy (Evarutil.reachable_from_evars sigma
(Evar.Map.domain (Evd.undefined_map origsigma))) in
let rec is_undefined_up_to_restriction sigma evk =
if Evd.mem origsigma evk then None else
let EvarInfo evi = Evd.find sigma evk in
match Evd.evar_body evi with
| Evd.Evar_empty -> Some (evk, Evd.EvarInfo evi)
| Evd.Evar_defined c -> match Constr.kind (EConstr.Unsafe.to_constr c) with
| Evar (evk,l) -> is_undefined_up_to_restriction sigma evk
| _ ->
None in
Evar.Set.fold (fun evk acc ->
match is_undefined_up_to_restriction sigma evk with
| Some (evk',evi) ->
if Evar.Set.mem evk (Lazy.force reachable) then acc
else evk'::acc
| _ -> acc)
evars []
let check_evars env sigma extsigma origsigma =
if Evd.undefined_map extsigma != Evd.undefined_map origsigma then
match check_evar_list env sigma (Evar.Map.domain (Evd.undefined_map extsigma)) origsigma with
| [] -> ()
| evk :: _ ->
let EvarInfo evi = Evd.find sigma evk in
let (loc,_) = Evd.evar_source evi in
Pretype_errors.error_unsolvable_implicit ?loc env sigma evk None
let tclMAPDELAYEDWITHHOLES accept_unresolved_holes l tac =
let rec aux = function
| [] -> tclUNIT ()
| x :: l ->
Proofview.Goal.enter begin fun gl ->
let env = Proofview.Goal.env gl in
let sigma_initial = Proofview.Goal.sigma gl in
let (sigma, x) = x env sigma_initial in
Proofview.Unsafe.tclEVARS sigma <*> tac x >>= fun () -> aux l >>= fun () ->
if accept_unresolved_holes then
tclUNIT ()
else
tclEVARMAP >>= fun sigma_final ->
try
let () = check_evars env sigma_final sigma sigma_initial in
tclUNIT ()
with e when CErrors.noncritical e ->
let e, info = Exninfo.capture e in
tclZERO ~info e
end in
aux l
let with_holes_check ~sigma_initial ~sigma =
tclEVARMAP >>= fun sigma_final ->
tclENV >>= fun env ->
try
let () = check_evars env sigma_final sigma sigma_initial in
tclUNIT ()
with e when CErrors.noncritical e ->
let e, info = Exninfo.capture e in
tclZERO ~info e
let tclRUNWITHHOLES accept_unresolved_holes tac0 tac =
if accept_unresolved_holes then tac0 >>= tac
else
tclEVARMAP >>= fun sigma_initial ->
tac0 >>= fun v ->
tclEVARMAP >>= fun sigma ->
if sigma == sigma_initial then tac v
else
tac v >>= fun v ->
with_holes_check ~sigma_initial ~sigma <*>
tclUNIT v
let tclWITHHOLES accept_unresolved_holes tac sigma =
tclEVARMAP >>= fun sigma_initial ->
if sigma == sigma_initial then tac
else
Proofview.Unsafe.tclEVARS sigma <*>
tac >>= fun v ->
(if accept_unresolved_holes then tclUNIT () else with_holes_check ~sigma_initial ~sigma) <*>
tclUNIT v
let tactic_of_delayed d =
Proofview.Goal.enter_one ~__LOC__ @@ fun gl ->
let sigma, v = pf_apply d gl in
Proofview.Unsafe.tclEVARS sigma <*>
tclUNIT v
let tclDELAYEDWITHHOLES check x tac =
Proofview.Goal.enter begin fun gl ->
let env = Proofview.Goal.env gl in
let sigma = Proofview.Goal.sigma gl in
let (sigma, x) = x env sigma in
tclWITHHOLES check (tac x) sigma
end
let tclTIMEOUT n t =
Proofview.tclOR
(Proofview.tclTIMEOUT n t)
begin function (e, info) -> match e with
| Logic_monad.Tac_Timeout as e ->
let info = Exninfo.reify () in
Proofview.tclZERO ~info (FailError (0,lazy (CErrors.print e)))
| e -> Proofview.tclZERO ~info e
end
let tclTIME s t =
Proofview.tclTIME s t
let nthDecl m gl =
let hyps = Proofview.Goal.hyps gl in
try
List.nth hyps (m-1)
with Failure _ -> CErrors.user_err Pp.(str "No such assumption.")
let nLastDecls gl n =
try List.firstn n (Proofview.Goal.hyps gl)
with Failure _ -> CErrors.user_err Pp.(str "Not enough hypotheses in the goal.")
let nthHypId m gl =
nthDecl m gl |> NamedDecl.get_id
let nthHyp m gl =
mkVar (nthHypId m gl)
let onNthHypId m tac =
Proofview.Goal.enter begin fun gl -> tac (nthHypId m gl) end
let onNthHyp m tac =
Proofview.Goal.enter begin fun gl -> tac (nthHyp m gl) end
let onLastHypId = onNthHypId 1
let onLastHyp = onNthHyp 1
let onNthDecl m tac =
Proofview.Goal.enter begin fun gl ->
Proofview.tclUNIT (nthDecl m gl) >>= tac
end
let onLastDecl = onNthDecl 1
let nLastHypsId gl n = List.map (NamedDecl.get_id) (nLastDecls gl n)
let nLastHyps gl n = List.map mkVar (nLastHypsId gl n)
let ifOnHyp pred tac1 tac2 id =
Proofview.Goal.enter begin fun gl ->
let typ = Tacmach.pf_get_hyp_typ id gl in
if pf_apply pred gl (id,typ) then
tac1 id
else
tac2 id
end
let onHyps find tac = Proofview.Goal.enter begin fun gl -> tac (find gl) end
let onNLastDecls n tac = onHyps (fun gl -> nLastDecls gl n) tac
let onNLastHypsId n tac = onHyps (fun gl -> nLastHypsId gl n) tac
let onNLastHyps n tac = onHyps (fun gl -> nLastHyps gl n) tac
let afterHyp id tac =
Proofview.Goal.enter begin fun gl ->
let hyps = Proofview.Goal.hyps gl in
let rem, _ = List.split_when (NamedDecl.get_id %> Id.equal id) hyps in
tac rem
end
let fullGoal gl =
let hyps = Tacmach.pf_ids_of_hyps gl in
None :: List.map Option.make hyps
let tryAllHyps tac =
Proofview.Goal.enter begin fun gl ->
let hyps = Tacmach.pf_ids_of_hyps gl in
tclFIRST_PROGRESS_ON tac hyps
end
let tryAllHypsAndConcl tac =
Proofview.Goal.enter begin fun gl ->
tclFIRST_PROGRESS_ON tac (fullGoal gl)
end
let onClause tac cl =
Proofview.Goal.enter begin fun gl ->
let hyps = Tacmach.pf_ids_of_hyps gl in
tclMAP tac (Locusops.simple_clause_of (fun () -> hyps) cl)
end
let fullGoal gl = None :: List.map Option.make (Tacmach.pf_ids_of_hyps gl)
let onAllHyps tac =
Proofview.Goal.enter begin fun gl ->
tclMAP tac (Tacmach.pf_ids_of_hyps gl)
end
let onAllHypsAndConcl tac =
Proofview.Goal.enter begin fun gl ->
tclMAP tac (fullGoal gl)
end
let elimination_sort_of_goal gl =
let c = Proofview.Goal.concl gl in
pf_apply Retyping.get_sort_family_of gl c
let elimination_sort_of_hyp id gl =
let c = pf_get_hyp_typ id gl in
pf_apply Retyping.get_sort_family_of gl c
let elimination_sort_of_clause id gl = match id with
| None -> elimination_sort_of_goal gl
| Some id -> elimination_sort_of_hyp id gl
let pf_constr_of_global ref =
Proofview.tclEVARMAP >>= fun sigma ->
Proofview.tclENV >>= fun env ->
let (sigma, c) = Evd.fresh_global env sigma ref in
Proofview.Unsafe.tclEVARS sigma <*> Proofview.tclUNIT c
let tclTYPEOFTHEN ?refresh c tac =
Proofview.Goal.enter (fun gl ->
let env = Proofview.Goal.env gl in
let sigma = Proofview.Goal.sigma gl in
let (sigma, t) = Typing.type_of ?refresh env sigma c in
Proofview.Unsafe.tclEVARS sigma <*> tac sigma t)
let tclSELECT = Goal_select.tclSELECT