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