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
|
(***********************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * INRIA-Rocquencourt & LRI-CNRS-Orsay *)
(* \VV/ *************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(***********************************************************************)
(* $Id$ *)
open Pp
open Util
open Names
open Term
open Termops
open Sign
open Declarations
open Inductive
open Reduction
open Environ
open Libnames
open Declare
open Refiner
open Tacmach
open Clenv
open Pattern
open Evar_refiner
open Wcclausenv
(******************************************)
(* Basic Tacticals *)
(******************************************)
(*************************************************)
(* Tacticals re-exported from the Refiner module.*)
(*************************************************)
let tclIDTAC = tclIDTAC
let tclORELSE = tclORELSE
let tclTHEN = tclTHEN
let tclTHENLIST = tclTHENLIST
let tclTHEN_i = tclTHEN_i
let tclTHENFIRST = tclTHENFIRST
let tclTHENLAST = tclTHENLAST
let tclTHENS = tclTHENS
let tclTHENSV = Refiner.tclTHENSV
let tclTHENSFIRSTn = Refiner.tclTHENSFIRSTn
let tclTHENSLASTn = Refiner.tclTHENSLASTn
let tclTHENFIRSTn = Refiner.tclTHENFIRSTn
let tclTHENLASTn = Refiner.tclTHENLASTn
let tclREPEAT = Refiner.tclREPEAT
let tclREPEAT_MAIN = tclREPEAT_MAIN
let tclFIRST = Refiner.tclFIRST
let tclSOLVE = Refiner.tclSOLVE
let tclTRY = Refiner.tclTRY
let tclINFO = Refiner.tclINFO
let tclCOMPLETE = Refiner.tclCOMPLETE
let tclAT_LEAST_ONCE = Refiner.tclAT_LEAST_ONCE
let tclFAIL = Refiner.tclFAIL
let tclDO = Refiner.tclDO
let tclPROGRESS = Refiner.tclPROGRESS
let tclWEAK_PROGRESS = Refiner.tclWEAK_PROGRESS
let tclNOTSAMEGOAL = Refiner.tclNOTSAMEGOAL
let tclTHENTRY = tclTHENTRY
let unTAC = unTAC
(* [rclTHENSEQ [t1;..;tn] is equivalent to t1;..;tn *)
let tclTHENSEQ = List.fold_left tclTHEN tclIDTAC
(* map_tactical f [x1..xn] = (f x1);(f x2);...(f xn) *)
(* tclMAP f [x1..xn] = (f x1);(f x2);...(f xn) *)
let tclMAP tacfun l =
List.fold_right (fun x -> (tclTHEN (tacfun x))) l tclIDTAC
(* apply a tactic to the nth element of the signature *)
let tclNTH_HYP m (tac : constr->tactic) gl =
tac (try mkVar(let (id,_,_) = List.nth (pf_hyps gl) (m-1) in id)
with Failure _ -> error "No such assumption") gl
(* apply a tactic to the last element of the signature *)
let tclLAST_HYP = tclNTH_HYP 1
let tclTRY_sign (tac : constr->tactic) sign gl =
let rec arec = function
| [] -> tclFAIL 0
| [s] -> tac (mkVar s) (*added in order to get useful error messages *)
| (s::sl) -> tclORELSE (tac (mkVar s)) (arec sl)
in
arec (ids_of_named_context sign) gl
let tclTRY_HYPS (tac : constr->tactic) gl =
tclTRY_sign tac (pf_hyps gl) gl
(* OR-branch *)
let tryClauses tac =
let rec firstrec = function
| [] -> tclFAIL 0
| [cls] -> tac cls (* added in order to get a useful error message *)
| cls::tl -> (tclORELSE (tac cls) (firstrec tl))
in
firstrec
(***************************************)
(* Clause Tacticals *)
(***************************************)
(* The following functions introduce several tactic combinators and
functions useful for working with clauses. A clause is either None
or (Some id), where id is an identifier. This type is useful for
defining tactics that may be used either to transform the
conclusion (None) or to transform a hypothesis id (Some id). --
--Eduardo (8/8/97)
*)
(* The type of clauses *)
type clause = identifier option
(* A clause corresponding to the |n|-th hypothesis or None *)
let nth_clause n gl =
if n = 0 then
None
else if n < 0 then
let id = List.nth (pf_ids_of_hyps gl) (-n-1) in
Some id
else
let id = List.nth (pf_ids_of_hyps gl) (n-1) in
Some id
(* Gets the conclusion or the type of a given hypothesis *)
let clause_type cls gl =
match cls with
| None -> pf_concl gl
| Some id -> pf_get_hyp_typ gl id
(* Functions concerning matching of clausal environments *)
let pf_is_matching gls pat n =
let (wc,_) = startWalk gls in
is_matching_conv (w_env wc) (w_Underlying wc) pat n
let pf_matches gls pat n =
matches_conv (pf_env gls) (project gls) pat n
(* [OnCL clausefinder clausetac]
* executes the clausefinder to find the clauses, and then executes the
* clausetac on the list so obtained. *)
let onCL cfind cltac gl = cltac (cfind gl) gl
(* [OnHyps hypsfinder hypstac]
* idem [OnCL] but only for hypotheses, not for conclusion *)
let onHyps find tac gl = tac (find gl) gl
(* Create a clause list with all the hypotheses from the context *)
let allHyps gl = pf_ids_of_hyps gl
(* Create a clause list with all the hypotheses from the context, occuring
after id *)
let afterHyp id gl =
fst (list_splitby (fun (hyp,_,_) -> hyp = id) (pf_hyps gl))
(* Create a singleton clause list with the last hypothesis from then context *)
let lastHyp gl = List.hd (pf_ids_of_hyps gl)
(* Create a clause list with the n last hypothesis from then context *)
let nLastHyps n gl =
try list_firstn n (pf_hyps gl)
with Failure "firstn" -> error "Not enough hypotheses in the goal"
(* A clause list with the conclusion and all the hypotheses *)
let allClauses gl =
let ids = pf_ids_of_hyps gl in
(None::(List.map in_some ids))
let onClause t cls gl = t cls gl
let tryAllClauses tac = onCL allClauses (tryClauses tac)
let onAllClauses tac = onCL allClauses (tclMAP tac)
let onAllClausesLR tac = onCL (compose List.rev allClauses) (tclMAP tac)
let onNthLastHyp n tac gls = tac (nth_clause n gls) gls
let tryAllHyps tac gls = tryClauses tac (allHyps gls) gls
let onNLastHyps n tac = onHyps (nLastHyps n) (tclMAP tac)
let onLastHyp tac gls = tac (lastHyp gls) gls
let clauseTacThen tac continuation =
(fun cls -> (tclTHEN (tac cls) continuation))
let if_tac pred tac1 tac2 gl =
if pred gl then tac1 gl else tac2 gl
let ifOnClause pred tac1 tac2 cls gl =
if pred (cls,clause_type cls gl) then
tac1 cls gl
else
tac2 cls gl
let ifOnHyp pred tac1 tac2 id gl =
if pred (id,pf_get_hyp_typ gl id) then
tac1 id gl
else
tac2 id gl
(***************************************)
(* Elimination Tacticals *)
(***************************************)
(* The following tacticals allow to apply a tactic to the
branches generated by the application of an elimination
tactic.
Two auxiliary types --branch_args and branch_assumptions-- are
used to keep track of some information about the ``branches'' of
the elimination. *)
type branch_args = {
ity : inductive; (* the type we were eliminating on *)
largs : constr list; (* its arguments *)
branchnum : int; (* the branch number *)
pred : constr; (* the predicate we used *)
nassums : int; (* the number of assumptions to be introduced *)
branchsign : bool list} (* the signature of the branch.
true=recursive argument, false=constant *)
type branch_assumptions = {
ba : branch_args; (* the branch args *)
assums : named_context; (* the list of assumptions introduced *)
cargs : identifier list; (* the constructor arguments *)
constargs : identifier list; (* the CONSTANT constructor arguments *)
recargs : identifier list; (* the RECURSIVE constructor arguments *)
indargs : identifier list} (* the inductive arguments *)
let compute_construtor_signatures isrec (_,k as ity) =
let rec analrec c recargs =
match kind_of_term c, recargs with
| Prod (_,_,c), recarg::rest ->
(match dest_recarg recarg with
| Norec -> false :: (analrec c rest)
| Imbr _ -> false :: (analrec c rest)
| Mrec j -> (isrec & j=k) :: (analrec c rest))
| LetIn (_,_,_,c), rest -> false :: (analrec c rest)
| _, [] -> []
| _ -> anomaly "compute_construtor_signatures"
in
let (mib,mip) = Global.lookup_inductive ity in
let n = mip.mind_nparams in
let lc =
Array.map (fun c -> snd (decompose_prod_n_assum n c)) mip.mind_nf_lc in
let lrecargs = dest_subterms mip.mind_recargs in
array_map2 analrec lc lrecargs
let case_sign = compute_construtor_signatures false
let elim_sign = compute_construtor_signatures true
let elimination_sort_of_goal gl =
match kind_of_term (hnf_type_of gl (pf_concl gl)) with
| Sort s ->
(match s with
| Prop Null -> InProp
| Prop Pos -> InSet
| Type _ -> InType)
| _ -> anomaly "goal should be a type"
let elimination_sort_of_hyp id gl =
match kind_of_term (hnf_type_of gl (pf_get_hyp_typ gl id)) with
| Sort s ->
(match s with
| Prop Null -> InProp
| Prop Pos -> InSet
| Type _ -> InType)
| _ -> anomaly "goal should be a type"
(* Find the right elimination suffix corresponding to the sort of the goal *)
(* c should be of type A1->.. An->B with B an inductive definition *)
let last_arg c = match kind_of_term c with
| App (f,cl) -> array_last cl
| _ -> anomaly "last_arg"
let general_elim_then_using
elim elim_sign_fun tac predicate (indbindings,elimbindings) c gl =
let (ity,t) = pf_reduce_to_quantified_ind gl (pf_type_of gl c) in
(* applying elimination_scheme just a little modified *)
let (wc,kONT) = startWalk gl in
let indclause = mk_clenv_from wc (c,t) in
let indclause' = clenv_constrain_with_bindings indbindings indclause in
let elimclause = mk_clenv_from () (elim,w_type_of wc elim) in
let indmv =
match kind_of_term (last_arg (clenv_template elimclause).rebus) with
| Meta mv -> mv
| _ -> error "elimination"
in
let pmv =
let p, _ = decompose_app (clenv_template_type elimclause).rebus in
match kind_of_term p with
| Meta p -> p
| _ ->
let name_elim =
match kind_of_term elim with
| Const kn -> string_of_kn kn
| Var id -> string_of_id id
| _ -> "\b"
in
error ("The elimination combinator " ^ name_elim ^ " is not known")
in
let elimclause' = clenv_fchain indmv elimclause indclause' in
let elimclause' = clenv_constrain_with_bindings elimbindings elimclause' in
let branchsigns = elim_sign_fun ity in
let after_tac ce i gl =
let (hd,largs) = decompose_app (clenv_template_type ce).rebus in
let ba = { branchsign = branchsigns.(i);
nassums =
List.fold_left
(fun acc b -> if b then acc+2 else acc+1)
0 branchsigns.(i);
branchnum = i+1;
ity = ity;
largs = List.map (clenv_instance_term ce) largs;
pred = clenv_instance_term ce hd }
in
tac ba gl
in
let branchtacs ce = Array.init (Array.length branchsigns) (after_tac ce) in
let elimclause' =
match predicate with
| None -> elimclause'
| Some p -> clenv_assign pmv p elimclause'
in
elim_res_pf_THEN_i kONT elimclause' branchtacs gl
let elimination_then_using tac predicate (indbindings,elimbindings) c gl =
let (ind,t) = pf_reduce_to_quantified_ind gl (pf_type_of gl c) in
let elim =
Indrec.lookup_eliminator ind (elimination_sort_of_goal gl) in
general_elim_then_using
elim elim_sign tac predicate (indbindings,elimbindings) c gl
let elimination_then tac = elimination_then_using tac None
let simple_elimination_then tac = elimination_then tac ([],[])
let case_then_using tac predicate (indbindings,elimbindings) c gl =
(* finding the case combinator *)
let (ity,t) = pf_reduce_to_quantified_ind gl (pf_type_of gl c) in
let sigma = project gl in
let sort = elimination_sort_of_goal gl in
let elim = Indrec.make_case_dep (pf_env gl) sigma ity sort in
general_elim_then_using
elim case_sign tac predicate (indbindings,elimbindings) c gl
let case_nodep_then_using tac predicate (indbindings,elimbindings) c gl =
(* finding the case combinator *)
let (ity,t) = pf_reduce_to_quantified_ind gl (pf_type_of gl c) in
let sigma = project gl in
let sort = elimination_sort_of_goal gl in
let elim = Indrec.make_case_nodep (pf_env gl) sigma ity sort in
general_elim_then_using
elim case_sign tac predicate (indbindings,elimbindings) c gl
let make_elim_branch_assumptions ba gl =
let rec makerec (assums,cargs,constargs,recargs,indargs) lb lc =
match lb,lc with
| ([], _) ->
{ ba = ba;
assums = assums;
cargs = cargs;
constargs = constargs;
recargs = recargs;
indargs = indargs}
| ((true::tl), ((idrec,_,_ as recarg)::(idind,_,_ as indarg)::idtl)) ->
makerec (recarg::indarg::assums,
idrec::cargs,
idrec::recargs,
constargs,
idind::indargs) tl idtl
| ((false::tl), ((id,_,_ as constarg)::idtl)) ->
makerec (constarg::assums,
id::cargs,
id::constargs,
recargs,
indargs) tl idtl
| (_, _) -> error "make_elim_branch_assumptions"
in
makerec ([],[],[],[],[]) ba.branchsign
(try list_firstn ba.nassums (pf_hyps gl)
with Failure _ -> anomaly "make_elim_branch_assumptions")
let elim_on_ba tac ba gl = tac (make_elim_branch_assumptions ba gl) gl
let make_case_branch_assumptions ba gl =
let rec makerec (assums,cargs,constargs,recargs) p_0 p_1 =
match p_0,p_1 with
| ([], _) ->
{ ba = ba;
assums = assums;
cargs = cargs;
constargs = constargs;
recargs = recargs;
indargs = []}
| ((true::tl), ((idrec,_,_ as recarg)::idtl)) ->
makerec (recarg::assums,
idrec::cargs,
idrec::recargs,
constargs) tl idtl
| ((false::tl), ((id,_,_ as constarg)::idtl)) ->
makerec (constarg::assums,
id::cargs,
recargs,
id::constargs) tl idtl
| (_, _) -> error "make_case_branch_assumptions"
in
makerec ([],[],[],[]) ba.branchsign
(try list_firstn ba.nassums (pf_hyps gl)
with Failure _ -> anomaly "make_case_branch_assumptions")
let case_on_ba tac ba gl = tac (make_case_branch_assumptions ba gl) gl
|