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 let premise_pattern what = None, [what, C.Implicit (Some `Hole)], None
58 let get_inductive_def uri =
59 match E.get_obj Un.empty_ugraph uri with
60 | C.InductiveDefinition (tys, _, lpsno, _), _ ->
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)
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
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 ()))
92 let right_sort = List.mem sort sorts in
93 if not_relation && inductive && not_recursive && right_sort then
95 HLog.warn (Printf.sprintf "Decomposing %s %u" (UriManager.string_of_uri uri) (succ tyno));
99 | C.Appl (hd :: tl) -> check_type sorts metasenv context hd
102 (* unexported tactics *******************************************************)
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
111 match H.get_name context index with
112 | _ when index <= 0 -> (proof, [goal])
113 | None -> aux (pred index)
115 let tac = T.then_ ~start:(tactic ~what)
116 ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
118 try PET.apply_tactic tac status
119 with PET.Fail _ -> aux (pred index)
120 in aux (index + context_length - old_context_length)
122 PET.mk_tactic scan_tac
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 pattern = PET.conclusion_pattern (Some (C.Rel index)) in
132 if check_type sorts metasenv context (S.lift index ty) then
133 T.then_ ~start:(P.elim_intros_tac ~mk_fresh_name_callback pattern)
134 ~continuation:(PESR.clear [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
151 let elim_type_tac status =
152 let pattern = PET.conclusion_pattern (Some (C.Rel 1)) in
154 T.thens ~start: (P.cut_tac what) ~continuations:[elim pattern; T.id_tac]
156 PET.apply_tactic tac status
158 PET.mk_tactic elim_type_tac
160 (* decompose ****************************************************************)
162 (* robaglia --------------------------------------------------------------- *)
164 (** perform debugging output? *)
166 let debug_print = fun _ -> ()
168 (** debugging print *)
169 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
171 (* roba seria ------------------------------------------------------------- *)
173 let decompose_tac ?(sorts=[CicPp.ppsort C.Prop; CicPp.ppsort C.CProp])
174 ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) () =
175 let decompose_tac status =
176 let (proof, goal) = status in
177 let _, metasenv,_,_, _ = proof in
178 let _, context, _ = CicUtil.lookup_meta goal metasenv in
179 let tactic = elim_clear_unfold_tac ~sorts ~mk_fresh_name_callback in
180 let old_context_length = List.length context in
181 let tac = scan_tac ~old_context_length ~index:old_context_length ~tactic
183 PET.apply_tactic tac status
185 PET.mk_tactic decompose_tac