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/.
29 module P = PrimitiveTactics
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
40 (* from ProceduralClasify ***************************************************)
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)
51 (****************************************************************************)
53 type type_class = Other
57 let premise_pattern what = None, [what, C.Implicit (Some `Hole)], None
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
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
76 (* unexported tactics *******************************************************)
78 let rec scan_tac ~old_context_length ~index ~tactic =
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
85 match H.get_name context index with
86 | _ when index <= 0 -> (proof, [goal])
87 | None -> aux (pred index)
89 let tac = T.then_ ~start:(tactic ~what)
90 ~continuation:(scan_tac ~old_context_length:context_length ~index ~tactic)
92 try PET.apply_tactic tac status
93 with PET.Fail _ -> aux (pred index)
94 in aux (index + context_length - old_context_length)
96 PET.mk_tactic scan_tac
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)
109 let msg = "unexported elim_clear: not an decomposable type" in
110 raise (PET.Fail (lazy msg))
112 PET.apply_tactic tac status
114 PET.mk_tactic elim_clear_unfold_tac
116 (* elim type ****************************************************************)
118 let elim_type_tac ?(mk_fresh_name_callback = F.mk_fresh_name ~subst:[]) ?depth
122 P.elim_intros_simpl_tac ?using ?depth ~mk_fresh_name_callback what
124 let elim_type_tac status =
126 T.thens ~start: (P.cut_tac what) ~continuations:[elim (C.Rel 1); T.id_tac]
128 PET.apply_tactic tac status
130 PET.mk_tactic elim_type_tac
132 (* decompose ****************************************************************)
134 (* robaglia --------------------------------------------------------------- *)
136 (** perform debugging output? *)
138 let debug_print = fun _ -> ()
140 (** debugging print *)
141 let warn s = debug_print (lazy ("DECOMPOSE: " ^ (Lazy.force s)))
143 (* roba seria ------------------------------------------------------------- *)
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
154 PET.apply_tactic tac status
156 PET.mk_tactic decompose_tac