]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/eliminationTactics.ml
e74886e95e1146235ef0114fd3b350d4360a5e13
[helm.git] / 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 I    = CicInspect
30 module S    = CicSubstitution
31 module TC   = CicTypeChecker 
32 module P    = PrimitiveTactics
33 module T    = Tacticals
34 module PESR = ProofEngineStructuralRules
35 module F    = FreshNamesGenerator
36 module PET  = ProofEngineTypes
37 module H    = ProofEngineHelpers
38 module RT   = ReductionTactics
39 module E    = CicEnvironment
40 module R    = CicReduction
41 module Un   = CicUniv
42
43 (* from ProceduralClasify ***************************************************)
44
45 let split c t =
46    let add s v c = Some (s, C.Decl v) :: c in
47    let rec aux whd a n c = function
48       | C.Prod (s, v, t)  -> aux false (v :: a) (succ n) (add s v c) t
49       | v when whd        -> v :: a, n
50       | v                 -> aux true a n c (R.whd ~delta:true c v)
51     in
52     aux false [] 0 c t
53
54 (****************************************************************************)
55
56 let premise_pattern what = None, [what, C.Implicit (Some `Hole)], None 
57
58 let get_inductive_def uri =
59    match E.get_obj Un.empty_ugraph uri with
60       | C.InductiveDefinition (tys, _, lpsno, _), _ -> 
61          lpsno, tys
62       | _                                           -> assert false
63
64 let is_not_recursive uri tyno tys = 
65    let map mutinds (_, ty) = 
66 (* FG: we can do much better here *)      
67       let map mutinds t = I.S.union mutinds (I.get_mutinds_of_uri uri t) in
68 (**********************************)      
69       let premises, _ = split [] ty in
70       List.fold_left map mutinds (List.tl premises)
71    in
72    let msg = "recursiveness check non implemented for mutually inductive types" in
73    if List.length tys > 1 then raise (PET.Fail (lazy msg)) else
74    let _, _, _, constructors = List.nth tys tyno in
75    let mutinds = List.fold_left map I.S.empty constructors in
76    I.S.is_empty mutinds
77
78 let rec check_type sorts metasenv context t = 
79    match R.whd ~delta:true context t with
80       | C.MutInd (uri, tyno, _) as t -> 
81          let lpsno, tys = get_inductive_def uri in
82          let _, inductive, arity, _ = List.nth tys tyno in
83          let _, psno = split [] arity in
84          let not_relation = (lpsno = psno) in
85          let not_recursive = is_not_recursive uri tyno tys in
86          let ty_ty, _ = TC.type_of_aux' metasenv context t Un.empty_ugraph in
87          let sort = match split context ty_ty with
88             | C.Sort sort ::_ , _ -> CicPp.ppsort sort
89             | C.Meta _ :: _, _    -> CicPp.ppsort (C.Type (Un.fresh ()))
90             | _                   -> assert false
91          in
92          let right_sort = List.mem sort sorts in
93          if not_relation && inductive && not_recursive && right_sort then
94          begin
95             HLog.warn (Printf.sprintf "Decomposing %s %u" (UriManager.string_of_uri uri) (succ tyno));
96             true 
97          end
98          else false 
99       | C.Appl (hd :: tl)         -> check_type sorts metasenv context hd
100       | _                         -> false
101
102 (* unexported tactics *******************************************************)
103
104 let rec scan_tac ~old_context_length ~index ~tactic =
105    let scan_tac status =
106       let (proof, goal) = status in
107       let _, metasenv, _, _, _ = proof in
108       let _, context, _ = CicUtil.lookup_meta goal metasenv in
109       let context_length = List.length context in
110       let rec aux index =
111          match H.get_name context index with 
112             | _ when index <= 0 -> (proof, [goal])
113             | None              -> aux (pred index)
114             | Some what         -> 
115                let tac = T.then_ ~start:(tactic ~what)
116                                  ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
117                in
118                try PET.apply_tactic tac status 
119                with PET.Fail _ -> aux (pred index)
120       in aux (index + context_length - old_context_length)
121    in
122    PET.mk_tactic scan_tac
123
124 let elim_clear_unfold_tac ~sorts ~mk_fresh_name_callback ~what =
125    let elim_clear_unfold_tac status =
126       let (proof, goal) = status in
127       let _, metasenv, _, _, _ = proof in
128       let _, context, _ = CicUtil.lookup_meta goal metasenv in
129       let index, ty = H.lookup_type metasenv context what in
130       let tac = 
131          if check_type sorts metasenv context (S.lift index ty) then 
132             T.then_ ~start:(P.elim_intros_tac ~mk_fresh_name_callback (C.Rel index))
133                     ~continuation:(PESR.clear [what])
134          else 
135             let msg = "unexported elim_clear: not an decomposable type" in
136             raise (PET.Fail (lazy msg))
137       in
138       PET.apply_tactic tac status
139    in
140    PET.mk_tactic elim_clear_unfold_tac
141
142 (* elim type ****************************************************************)
143
144 let elim_type_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) ?depth
145   ?using what
146 =
147   let elim what =
148     P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what
149   in
150   let elim_type_tac status =
151     let tac =
152       T.thens ~start: (P.cut_tac what) ~continuations:[elim (C.Rel 1); T.id_tac]
153     in
154     PET.apply_tactic tac status
155   in
156   PET.mk_tactic elim_type_tac
157
158 (* decompose ****************************************************************)
159
160 (* robaglia --------------------------------------------------------------- *)
161
162   (** perform debugging output? *)
163 let debug = false
164 let debug_print = fun _ -> ()
165
166   (** debugging print *)
167 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
168
169 (* roba seria ------------------------------------------------------------- *)
170
171 let decompose_tac ?(sorts=[CicPp.ppsort C.Prop; CicPp.ppsort C.CProp]) 
172                   ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) () =
173    let decompose_tac status =
174       let (proof, goal) = status in
175       let _, metasenv,_,_, _ = proof in
176       let _, context, _ = CicUtil.lookup_meta goal metasenv in
177       let tactic = elim_clear_unfold_tac ~sorts ~mk_fresh_name_callback in
178       let old_context_length = List.length context in      
179       let tac = scan_tac ~old_context_length ~index:old_context_length ~tactic
180       in
181       PET.apply_tactic tac status
182    in
183    PET.mk_tactic decompose_tac