aboutsummaryrefslogtreecommitdiffhomepage
path: root/tactics/inv.ml
blob: 4617ba354226acec0dd5ce6ebdf5f5d86783d229 (plain)
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
(***********************************************************************)
(*  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 Nameops
open Term
open Termops
open Global
open Sign
open Environ
open Inductiveops
open Printer
open Reductionops
open Retyping
open Tacmach
open Proof_type
open Evar_refiner
open Clenv
open Tactics
open Wcclausenv
open Tacticals
open Tactics
open Elim
open Equality
open Typing
open Pattern
open Rawterm

let collect_meta_variables c = 
  let rec collrec acc c = match kind_of_term c with
    | Meta mv -> mv::acc
    | _ -> fold_constr collrec acc c
  in 
  collrec [] c

let check_no_metas clenv ccl =
  if occur_meta ccl then
    let metas = List.map (fun n -> Intmap.find n clenv.namenv)
		  (collect_meta_variables ccl) in
    errorlabstrm "inversion" 
      (str ("Cannot find an instantiation for variable"^
	       (if List.length metas = 1 then " " else "s ")) ++
	 prlist_with_sep pr_coma pr_id metas
	 (* ajouter "in " ++ prterm ccl mais il faut le bon contexte *))

let dest_match_eq gls eqn =
  try 
    pf_matches gls (Coqlib.build_coq_eq_pattern ()) eqn
  with PatternMatchingFailure -> 
    (try 
       pf_matches gls (Coqlib.build_coq_eqT_pattern ()) eqn
     with PatternMatchingFailure -> 
       (try 
	  pf_matches gls (Coqlib.build_coq_idT_pattern ()) eqn
        with PatternMatchingFailure ->
	  errorlabstrm "dest_match_eq" 
	    (str "no primitive equality here")))

let var_occurs_in_pf gl id =
  let env = pf_env gl in
  occur_var env id (pf_concl gl) or
  List.exists (occur_var_in_decl env id) (pf_hyps gl)

(* [make_inv_predicate (ity,args) C]
  
   is given the inductive type, its arguments, both the global
   parameters and its local arguments, and is expected to produce a
   predicate P such that if largs is the "local" part of the
   arguments, then (P largs) will be convertible with a conclusion of
   the form:

   <A1>a1=a1-><A2>a2=a2 ... -> C

   Algorithm: suppose length(largs)=n

   (1) Push the entire arity, [xbar:Abar], carrying along largs and
   the conclusion

   (2) Pair up each ai with its respective Rel version: a1==(Rel n),
   a2==(Rel n-1), etc.

   (3) For each pair, ai,Rel j, if the Ai is dependent - that is, the
   type of [Rel j] is an open term, then we construct the iterated
   tuple, [make_iterated_tuple] does it, and use that for our equation

   Otherwise, we just use <Ai>ai=Rel j

 *)

type inversion_status = Dep of constr option | NoDep

let compute_eqn env sigma n i ai =
  (ai,get_type_of env sigma ai),
  (mkRel (n-i),get_type_of env sigma (mkRel (n-i)))

let make_inv_predicate env sigma ind id status concl =
  let indf,realargs = dest_ind_type ind in
  let nrealargs = List.length realargs in
  let (hyps,concl) =
    match status with
      | NoDep ->
	  (* We push the arity and leave concl unchanged *)
	  let hyps_arity,_ = get_arity env indf in
	  (hyps_arity,concl)
      | Dep dflt_concl ->
	  if not (occur_var env id concl) then
	    errorlabstrm "make_inv_predicate"
              (str "Current goal does not depend on " ++ pr_id id);
          (* We abstract the conclusion of goal with respect to
             realargs and c to * be concl in order to rewrite and have
             c also rewritten when the case * will be done *)
	  let pred =
            match dflt_concl with
              | Some concl -> concl (*assumed it's some [x1..xn,H:I(x1..xn)]C*)
              | None ->
		let sort = get_sort_of env sigma concl in
		let p = make_arity env true indf sort in
		abstract_list_all env sigma p concl (realargs@[mkVar id]) in
	  let hyps,bodypred = decompose_lam_n_assum (nrealargs+1) pred in
	  (* We lift to make room for the equations *)
	  (hyps,lift nrealargs bodypred)
  in
  let nhyps = List.length hyps in
  let env' = push_rel_context hyps env in
  let realargs' = List.map (lift nhyps) realargs in
  let pairs = list_map_i (compute_eqn env' sigma nhyps) 0 realargs' in
  (* Now the arity is pushed, and we need to construct the pairs
   * ai,mkRel(n-i+1) *)
  (* Now, we can recurse down this list, for each ai,(mkRel k) whether to
     push <Ai>(mkRel k)=ai (when   Ai is closed).
   In any case, we carry along the rest of pairs *)
  let rec build_concl eqns n = function
    | [] -> (prod_it concl eqns,n)
    | ((ai,ati),(xi,ti))::restlist ->
        let (lhs,eqnty,rhs) =
          if closed0 ti then 
	    (xi,ti,ai)
          else 
	    make_iterated_tuple env' sigma (ai,ati) (xi,ti)
	in
        let type_type_rhs = get_sort_of env sigma (type_of env sigma rhs) in
        let sort = get_sort_of env sigma concl in 
        let eq_term = find_eq_pattern type_type_rhs sort in
        let eqn = applist (eq_term ,[eqnty;lhs;rhs]) in 
	build_concl ((Anonymous,lift n eqn)::eqns) (n+1) restlist
  in
  let (newconcl,neqns) = build_concl [] 0 pairs in
  let predicate = it_mkLambda_or_LetIn_name env newconcl hyps in
  (* OK - this predicate should now be usable by res_elimination_then to
     do elimination on the conclusion. *)
  (predicate,neqns)

(* The result of the elimination is a bunch of goals like:

           |- (cibar:Cibar)Equands->C

   where the cibar are either dependent or not.  We are fed a
   signature, with "true" for every recursive argument, and false for
   every non-recursive one.  So we need to do the
   sign_branch_len(sign) intros, thinning out all recursive
   assumptions.  This leaves us with exactly length(sign) assumptions.

   We save their names, and then do introductions for all the equands
   (there are some number of them, which is the other argument of the
   tactic)

   This gives us the #neqns equations, whose names we get also, and
   the #length(sign) arguments.

   Suppose that #nodep of these arguments are non-dependent.
   Generalize and thin them.

   This gives us #dep = #length(sign)-#nodep arguments which are
   dependent.

   Now, we want to take each of the equations, and do all possible
   injections to get the left-hand-side to be a variable.  At the same
   time, if we find a lhs/rhs pair which are different, we can
   discriminate them to prove false and finish the branch.

   Then, we thin away the equations, and do the introductions for the
   #nodep arguments which we generalized before.
 *)

(* Called after the case-assumptions have been killed off, and all the
   intros have been done.  Given that the clause in question is an
   equality (if it isn't we fail), we are responsible for projecting
   the equality, using Injection and Discriminate, and applying it to
   the concusion *)

(* Computes the subset of hypothesis in the local context whose
   type depends on t (should be of the form (mkVar id)), then
   it generalizes them, applies tac to rewrite all occurrencies of t,
   and introduces generalized hypotheis.
   Precondition: t=(mkVar id) *)
    
let rec dependent_hyps id idlist sign = 
  let rec dep_rec =function
    | [] -> []
    | (id1,_,id1ty as d1)::l -> 
	if occur_var (Global.env()) id (body_of_type id1ty)
        then d1 :: dep_rec l
        else dep_rec l
  in 
  dep_rec idlist 

let split_dep_and_nodep hyps gl =
  List.fold_right 
    (fun (id,_,_ as d) (l1,l2) ->
       if var_occurs_in_pf gl id then (d::l1,l2) else (l1,d::l2))
    hyps ([],[])


let dest_eq gls t =
  match dest_match_eq gls t with
    | [(1,x);(2,y);(3,z)] ->
        (x,pf_whd_betadeltaiota gls y, pf_whd_betadeltaiota gls z)
    | _ -> error "dest_eq: should be an equality"

let generalizeRewriteIntros tac depids id gls = 
  let dids = dependent_hyps id depids (pf_env gls) in
  (tclTHENSEQ
    [bring_hyps dids; tac; intros_replacing (ids_of_named_context dids)])
  gls

(* invariant: ProjectAndApply is responsible for erasing the clause
   which it is given as input
   It simplifies the clause (an equality) to use it as a rewrite rule and then
   erases the result of the simplification. *)
(* invariant: ProjectAndApplyNoThining simplifies the clause (an equality) .
   If it can discriminate then the goal is proved, if not tries to use it as
   a rewrite rule. It erases the clause which is given as input *)

let projectAndApply thin id depids gls =
  let env = pf_env gls in
  let subst_hyp_LR id = tclTRY(hypSubst_LR id None) in
  let subst_hyp_RL id = tclTRY(hypSubst_RL id None) in
  let subst_hyp gls = 
    let orient_rule id = 
      let (t,t1,t2) = dest_eq gls (pf_get_hyp_typ gls id) in
      match (kind_of_term t1, kind_of_term t2) with
        | Var id1, _ ->
            generalizeRewriteIntros (subst_hyp_LR id) depids id1
        | _, Var id2 ->
            generalizeRewriteIntros (subst_hyp_RL id) depids id2
        | _ -> subst_hyp_RL id
    in 
    onLastHyp orient_rule gls 
  in
  let (t,t1,t2) = dest_eq gls (pf_get_hyp_typ gls id)  in
  match (thin, kind_of_term t1, kind_of_term t2) with
    | (true, Var id1,  _) ->
        generalizeRewriteIntros
          (tclTHEN (subst_hyp_LR id) (clear [id])) depids id1 gls
    | (false, Var id1,  _) ->
        generalizeRewriteIntros (subst_hyp_LR id) depids id1 gls
    | (true, _ , Var id2) ->
        generalizeRewriteIntros
          (tclTHEN (subst_hyp_RL id) (clear [id])) depids id2 gls
    | (false, _ , Var id2) ->
        generalizeRewriteIntros (subst_hyp_RL id) depids id2 gls
    | _ ->
        let trailer =
          if thin then onLastHyp (fun id -> clear [id]) else tclIDTAC in
        let deq_trailer neqns =
          tclDO neqns
            (tclTHENSEQ [intro; tclTRY subst_hyp; trailer]) in 
	(tclTHEN (dEqThen deq_trailer (Some (NamedHyp id))) (clear [id])) gls

(* Inversion qui n'introduit pas les hypotheses, afin de pouvoir les nommer
   soi-meme (proposition de Valerie). *)
let rewrite_equations_gene othin neqns ba gl =
  let (depids,nodepids) = split_dep_and_nodep ba.assums gl in
  let rewrite_eqns =
    match othin with
      | Some thin ->
          onLastHyp
            (fun last ->
              tclTHENSEQ
                [tclDO neqns
                     (tclTHEN intro
                        (onLastHyp
                           (fun id ->
                              tclTRY (projectAndApply thin id depids))));
                 onHyps (compose List.rev (afterHyp last)) bring_hyps;
                 onHyps (afterHyp last)
                   (compose clear ids_of_named_context)])
      | None -> tclIDTAC
  in
  (tclTHENSEQ
    [tclDO neqns intro;
     bring_hyps nodepids;
     clear (ids_of_named_context nodepids);
     onHyps (compose List.rev (nLastHyps neqns)) bring_hyps;
     onHyps (nLastHyps neqns) (compose clear ids_of_named_context);
     rewrite_eqns;
     tclMAP (fun (id,_,_ as d) ->
               (tclORELSE (clear [id])
                 (tclTHEN (bring_hyps [d]) (clear [id]))))
       depids])
  gl

(* Introduction of the equations on arguments
   othin: discriminates Simple Inversion, Inversion and Inversion_clear
     None: the equations are introduced, but not rewritten
     Some thin: the equations are rewritten, and cleared if thin is true *)

let rewrite_equations othin neqns ba gl =
  let (depids,nodepids) = split_dep_and_nodep ba.assums gl in
  let rewrite_eqns =
    match othin with
      | Some thin ->
          tclTHENSEQ
            [onHyps (compose List.rev (nLastHyps neqns)) bring_hyps;
             onHyps (nLastHyps neqns) (compose clear ids_of_named_context);
	     tclDO neqns
               (tclTHEN intro
		 (onLastHyp
		   (fun id -> tclTRY (projectAndApply thin id depids))));
	     tclDO (List.length nodepids) intro;
	     tclMAP (fun (id,_,_) -> tclTRY (clear [id])) depids]
      | None -> tclIDTAC
  in
  (tclTHENSEQ
    [tclDO neqns intro;
     bring_hyps nodepids;
     clear (ids_of_named_context nodepids);
     rewrite_eqns])
  gl

let rewrite_equations_tac (gene, othin) id neqns ba =
  let tac =
    if gene then rewrite_equations_gene othin neqns ba
    else rewrite_equations othin neqns ba in
  if othin = Some true (* if Inversion_clear, clear the hypothesis *) then 
    tclTHEN tac (tclTRY (clear [id]))
  else 
    tac


let raw_inversion inv_kind indbinding id status gl =
  let env = pf_env gl and sigma = project gl in
  let c = mkVar id in
  let (wc,kONT) = startWalk gl in
  let t = strong_prodspine (pf_whd_betadeltaiota gl) (pf_type_of gl c) in
  let indclause = mk_clenv_from wc (c,t) in
  let indclause' = clenv_constrain_with_bindings indbinding indclause in
  let newc = clenv_instance_template indclause' in
  let ccl = clenv_instance_template_type indclause' in
  check_no_metas indclause' ccl;
  let (IndType (indf,realargs) as indt) =
    try find_rectype env sigma ccl
    with Not_found ->
      errorlabstrm "raw_inversion"
	(str ("The type of "^(string_of_id id)^" is not inductive")) in
  let (elim_predicate,neqns) =
    make_inv_predicate env sigma indt id status (pf_concl gl) in
  let (cut_concl,case_tac) =
    if status <> NoDep & (dependent c (pf_concl gl)) then 
      Reduction.beta_appvect elim_predicate (Array.of_list (realargs@[c])),
      case_then_using 
    else 
      Reduction.beta_appvect elim_predicate (Array.of_list realargs),
      case_nodep_then_using 
  in
  (tclTHENS
     (true_cut None cut_concl)
     [case_tac (introCaseAssumsThen (rewrite_equations_tac inv_kind id neqns))
        (Some elim_predicate) ([],[]) newc;
      onLastHyp
        (fun id ->
           (tclTHEN
              (applyUsing
                 (applist(mkVar id,
                          list_tabulate
                            (fun _ -> mkMeta(Clenv.new_meta())) neqns)))
              reflexivity))])
  gl

(* Error messages of the inversion tactics *)
let not_found_message ids =
  if List.length ids = 1 then
    (str "the variable" ++ spc () ++ str (string_of_id (List.hd ids)) ++ spc () ++
      str" was not found in the current environment")
  else 
    (str "the variables [" ++ 
      spc () ++ prlist (fun id -> (str (string_of_id id) ++ spc ())) ids ++
      str" ] were not found in the current environment")

let dep_prop_prop_message id =
  errorlabstrm "Inv"
    (str "Inversion on " ++ pr_id id ++
       str " would needs dependent elimination Prop-Prop")
 
let not_inductive_here id =
  errorlabstrm "mind_specif_of_mind"
    (str "Cannot recognize an inductive predicate in " ++ pr_id id ++
       str ". If there is one, may be the structure of the arity or of the type of constructors is hidden by constant definitions.")

(* Noms d'errreurs obsolètes ?? *)
let wrap_inv_error id = function
  | UserError ("Case analysis",s) -> errorlabstrm "Inv needs Nodep Prop Set" s
  | UserError("mind_specif_of_mind",_)  -> not_inductive_here id
  | UserError (a,b)  -> errorlabstrm "Inv" b
  | Invalid_argument (*"it_list2"*) "List.fold_left2" -> dep_prop_prop_message id
  | Not_found  ->  errorlabstrm "Inv" (not_found_message [id]) 
  | e -> raise e

(* The most general inversion tactic *)
let inversion inv_kind status id gls =
  try (raw_inversion inv_kind [] id status) gls
  with e -> wrap_inv_error id e

(* Specializing it... *)

let inv_gen gene thin status = try_intros_until (inversion (gene,thin) status)

open Tacexpr

(*
let hinv_kind = Quoted_string "HalfInversion"
let inv_kind = Quoted_string "Inversion"
let invclr_kind = Quoted_string "InversionClear"

let com_of_id id =
  if id = hinv_kind then None
  else if id = inv_kind then Some false
  else Some true
*)

let inv k id = inv_gen false k NoDep id

let half_inv_tac id = inv None (NamedHyp id)
let inv_tac id = inv (Some false) (NamedHyp id)
let inv_clear_tac id = inv (Some true) (NamedHyp id)

let dinv k c id = inv_gen false k (Dep c) id

let half_dinv_tac id = dinv None None (NamedHyp id)
let dinv_tac id = dinv (Some false) None (NamedHyp id)
let dinv_clear_tac id = dinv (Some true) None (NamedHyp id)

(* InvIn will bring the specified clauses into the conclusion, and then
 * perform inversion on the named hypothesis.  After, it will intro them
 * back to their places in the hyp-list. *)

let invIn com id ids gls =
  let hyps = List.map (pf_get_hyp gls) ids in
  let nb_prod_init = nb_prod (pf_concl gls) in
  let intros_replace_ids gls =
    let nb_of_new_hyp =
      nb_prod (pf_concl gls) - (List.length hyps + nb_prod_init)
    in 
    if nb_of_new_hyp < 1 then 
      intros_replacing ids gls
    else 
      tclTHEN (tclDO nb_of_new_hyp intro) (intros_replacing ids) gls
  in
  try 
    (tclTHENSEQ
      [bring_hyps hyps; inversion (false, com) NoDep id; intros_replace_ids])
    gls
  with e -> wrap_inv_error id e

let invIn_gen com id idl = try_intros_until (fun id -> invIn com id idl) id