]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tactics/eliminationTactics.ml
c98f020d7139a522be67d9b0c97f26a71a76f18e
[helm.git] / helm / software / components / tactics / eliminationTactics.ml
1 (* Copyright (C) 2002, HELM Team.
2  *
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  *
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  *
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* $Id$ *)
27
28 module C    = Cic
29 module P    = PrimitiveTactics
30 module T    = Tacticals
31 module S    = ProofEngineStructuralRules
32 module F    = FreshNamesGenerator
33 module PET  = ProofEngineTypes
34 module H    = ProofEngineHelpers
35 module RT   = ReductionTactics
36 module E    = CicEnvironment
37 module R    = CicReduction
38 module Un   = CicUniv
39
40 (* from ProceduralClasify ***************************************************)
41
42 let split c t =
43    let add s v c = Some (s, C.Decl v) :: c in
44    let rec aux whd a n c = function
45       | C.Prod (s, v, t)  -> aux false (v :: a) (succ n) (add s v c) t
46       | v when whd        -> v :: a, n
47       | v                 -> aux true a n c (R.whd ~delta:true c v)
48     in
49     aux false [] 0 c t
50
51 (****************************************************************************)
52
53 type type_class = Other
54                 | Ind
55                 | Con of C.lazy_term
56
57 let premise_pattern what = None, [what, C.Implicit (Some `Hole)], None 
58
59 let get_inductive_type uri tyno =
60    match E.get_obj Un.empty_ugraph uri with
61       | C.InductiveDefinition (tys, _, lpsno, _), _ -> 
62          let _, inductive, arity, _ = List.nth tys tyno in
63          lpsno, inductive, arity
64       | _                                           -> assert false
65
66 let rec check_type = function 
67    | C.MutInd (uri, tyno, _) -> 
68       let lpsno, inductive, arity = get_inductive_type uri tyno in
69       let _, psno = split [] arity in
70       if lpsno <> psno && inductive then Other else Ind 
71 (*   | C.Const (uri, _) as t     -> 
72       if List.mem (uri, None) types then Con (PET.const_lazy_term t) else Other
73 *)   | C.Appl (hd :: tl)         -> check_type hd
74    | _                         -> Other
75
76 (* unexported tactics *******************************************************)
77
78 let rec scan_tac ~old_context_length ~index ~tactic =
79    let scan_tac status =
80       let (proof, goal) = status in
81       let _, metasenv, _, _, _ = proof in
82       let _, context, _ = CicUtil.lookup_meta goal metasenv in
83       let context_length = List.length context in
84       let rec aux index =
85          match H.get_name context index with 
86             | _ when index <= 0 -> (proof, [goal])
87             | None              -> aux (pred index)
88             | Some what         -> 
89                let tac = T.then_ ~start:(tactic ~what)
90                                  ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
91                in
92                try PET.apply_tactic tac status 
93                with PET.Fail _ -> aux (pred index)
94       in aux (index + context_length - old_context_length)
95    in
96    PET.mk_tactic scan_tac
97
98 let elim_clear_unfold_tac ~mk_fresh_name_callback ~what =
99    let elim_clear_unfold_tac status =
100       let (proof, goal) = status in
101       let _, metasenv, _, _, _ = proof in
102       let _, context, _ = CicUtil.lookup_meta goal metasenv in
103       let index, ty = H.lookup_type metasenv context what in
104       let tac = match check_type ty with 
105          | Ind   -> T.then_ ~start:(P.elim_intros_tac ~mk_fresh_name_callback (C.Rel index))
106                             ~continuation:(S.clear [what])
107          | Con t -> RT.unfold_tac (Some t) ~pattern:(premise_pattern what)
108          | Other -> 
109             let msg = "unexported elim_clear: not an decomposable type" in
110             raise (PET.Fail (lazy msg))
111       in
112       PET.apply_tactic tac status
113    in
114    PET.mk_tactic elim_clear_unfold_tac
115
116 (* elim type ****************************************************************)
117
118 let elim_type_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) ?depth
119   ?using what
120 =
121   let elim what =
122     P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what
123   in
124   let elim_type_tac status =
125     let tac =
126       T.thens ~start: (P.cut_tac what) ~continuations:[elim (C.Rel 1); T.id_tac]
127     in
128     PET.apply_tactic tac status
129   in
130   PET.mk_tactic elim_type_tac
131
132 (* decompose ****************************************************************)
133
134 (* robaglia --------------------------------------------------------------- *)
135
136   (** perform debugging output? *)
137 let debug = false
138 let debug_print = fun _ -> ()
139
140   (** debugging print *)
141 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
142
143 (* roba seria ------------------------------------------------------------- *)
144
145 let decompose_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) () =
146    let decompose_tac status =
147       let (proof, goal) = status in
148       let _, metasenv,_,_, _ = proof in
149       let _, context, _ = CicUtil.lookup_meta goal metasenv in
150       let tactic = elim_clear_unfold_tac ~mk_fresh_name_callback in
151       let old_context_length = List.length context in      
152       let tac = scan_tac ~old_context_length ~index:old_context_length ~tactic
153       in
154       PET.apply_tactic tac status
155    in
156    PET.mk_tactic decompose_tac