1 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
30 module S = CicSubstitution
31 module TC = CicTypeChecker
32 module P = PrimitiveTactics
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
43 (* from ProceduralClasify ***************************************************)
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)
54 (****************************************************************************)
56 type type_class = Other
60 let premise_pattern what = None, [what, C.Implicit (Some `Hole)], None
62 let get_inductive_def uri =
63 match E.get_obj Un.empty_ugraph uri with
64 | C.InductiveDefinition (tys, _, lpsno, _), _ ->
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)
76 let _, _, _, constructors = List.nth tys tyno in
77 let mutinds = List.fold_left map I.S.empty constructors in
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 ()))
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);
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
103 (* unexported tactics *******************************************************)
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
112 match H.get_name context index with
113 | _ when index <= 0 -> (proof, [goal])
114 | None -> aux (pred index)
116 let tac = T.then_ ~start:(tactic ~what)
117 ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
119 try PET.apply_tactic tac status
120 with PET.Fail _ -> aux (pred index)
121 in aux (index + context_length - old_context_length)
123 PET.mk_tactic scan_tac
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)
136 let msg = "unexported elim_clear: not an decomposable type" in
137 raise (PET.Fail (lazy msg))
139 PET.apply_tactic tac status
141 PET.mk_tactic elim_clear_unfold_tac
143 (* elim type ****************************************************************)
145 let elim_type_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) ?depth
149 P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what
151 let elim_type_tac status =
153 T.thens ~start: (P.cut_tac what) ~continuations:[elim (C.Rel 1); T.id_tac]
155 PET.apply_tactic tac status
157 PET.mk_tactic elim_type_tac
159 (* decompose ****************************************************************)
161 (* robaglia --------------------------------------------------------------- *)
163 (** perform debugging output? *)
165 let debug_print = fun _ -> ()
167 (** debugging print *)
168 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
170 (* roba seria ------------------------------------------------------------- *)
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
182 PET.apply_tactic tac status
184 PET.mk_tactic decompose_tac