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/.
28 (* Note: this code is almost identical to change_tac and
29 * it could be unified by making the change function a callback *)
30 let reduction_tac ~reduction ~pattern (proof,goal) =
31 let curi,metasenv,pbo,pty = proof in
32 let (metano,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
33 let change subst where terms metasenv ugraph =
34 if terms = [] then where, metasenv, ugraph
36 let pairs, metasenv, ugraph =
38 (fun (pairs, metasenv, ugraph) (context, t) ->
39 let reduction, metasenv, ugraph = reduction context metasenv ugraph in
40 ((t, reduction context t) :: pairs), metasenv, ugraph)
41 ([], metasenv, ugraph)
44 let terms, terms' = List.split pairs in
46 ProofEngineReduction.replace ~equality:(==) ~what:terms ~with_what:terms'
49 CicMetaSubst.apply_subst subst where', metasenv, ugraph
51 let (subst,metasenv,ugraph,selected_context,selected_ty) =
52 ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph
55 let ty', metasenv, ugraph = change subst ty selected_ty metasenv ugraph in
56 let context', metasenv, ugraph =
58 (fun entry selected_entry (context', metasenv, ugraph) ->
59 match entry,selected_entry with
60 None,None -> None::context', metasenv, ugraph
61 | Some (name,Cic.Decl ty),Some (`Decl selected_ty) ->
62 let ty', metasenv, ugraph =
63 change subst ty selected_ty metasenv ugraph
65 Some (name,Cic.Decl ty')::context', metasenv, ugraph
66 | Some (name,Cic.Def (bo,ty)),Some (`Def (selected_bo,selected_ty)) ->
67 let bo', metasenv, ugraph =
68 change subst bo selected_bo metasenv ugraph
70 let ty', metasenv, ugraph =
71 match ty,selected_ty with
72 None,None -> None, metasenv, ugraph
73 | Some ty,Some selected_ty ->
74 let ty', metasenv, ugraph =
75 change subst ty selected_ty metasenv ugraph
77 Some ty', metasenv, ugraph
80 (Some (name,Cic.Def (bo',ty'))::context'), metasenv, ugraph
82 ) context selected_context ([], metasenv, ugraph) in
85 | (n,_,_) when n = metano -> (metano,context',ty')
89 (curi,metasenv',pbo,pty), [metano]
92 let simpl_tac ~pattern =
93 mk_tactic (reduction_tac
94 ~reduction:(const_lazy_reduction ProofEngineReduction.simpl) ~pattern)
96 let reduce_tac ~pattern =
97 mk_tactic (reduction_tac
98 ~reduction:(const_lazy_reduction ProofEngineReduction.reduce) ~pattern)
100 let unfold_tac what ~pattern =
103 | None -> const_lazy_reduction (ProofEngineReduction.unfold ?what:None)
105 (fun context metasenv ugraph ->
106 let what, metasenv, ugraph = lazy_term context metasenv ugraph in
107 ProofEngineReduction.unfold ~what, metasenv, ugraph)
109 mk_tactic (reduction_tac ~reduction ~pattern)
111 let whd_tac ~pattern =
112 mk_tactic (reduction_tac
113 ~reduction:(const_lazy_reduction CicReduction.whd) ~pattern)
115 let normalize_tac ~pattern =
116 mk_tactic (reduction_tac
117 ~reduction:(const_lazy_reduction CicReduction.normalize) ~pattern)
119 exception NotConvertible
121 (* Note: this code is almost identical to reduction_tac and
122 * it could be unified by making the change function a callback *)
123 (* CSC: with_what is parsed in the context of the goal, but it should replace
124 something that lives in a completely different context. Thus we
125 perform a delift + lift phase to move it in the right context. However,
126 in this way the tactic is less powerful than expected: with_what cannot
127 reference variables that are local to the term that is going to be
128 replaced. To fix this we should parse with_what in the context of the
129 term(s) to be replaced. *)
130 let change_tac ~pattern with_what =
131 let change_tac ~pattern ~with_what (proof, goal) =
132 let curi,metasenv,pbo,pty = proof in
133 let (metano,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
134 let change subst where terms metasenv ugraph =
135 if terms = [] then where, metasenv, ugraph
137 let pairs, metasenv, ugraph =
139 (fun (pairs, metasenv, ugraph) (context_of_t, t) ->
140 let with_what, metasenv, ugraph =
141 with_what context_of_t metasenv ugraph
144 CicTypeChecker.type_of_aux' metasenv context_of_t with_what ugraph
147 CicReduction.are_convertible ~metasenv context_of_t t with_what u
150 ((t, with_what) :: pairs), metasenv, ugraph
152 raise NotConvertible)
153 ([], metasenv, ugraph)
156 let terms, terms' = List.split pairs in
158 ProofEngineReduction.replace ~equality:(==) ~what:terms ~with_what:terms'
161 CicMetaSubst.apply_subst subst where', metasenv, ugraph
163 let (subst,metasenv,ugraph,selected_context,selected_ty) =
164 ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph ~conjecture
166 let ty', metasenv, ugraph = change subst ty selected_ty metasenv ugraph in
167 let context', metasenv, ugraph =
169 (fun entry selected_entry (context', metasenv, ugraph) ->
170 match entry,selected_entry with
171 None,None -> (None::context'), metasenv, ugraph
172 | Some (name,Cic.Decl ty),Some (`Decl selected_ty) ->
173 let ty', metasenv, ugraph =
174 change subst ty selected_ty metasenv ugraph
176 (Some (name,Cic.Decl ty')::context'), metasenv, ugraph
177 | Some (name,Cic.Def (bo,ty)),Some (`Def (selected_bo,selected_ty)) ->
178 let bo', metasenv, ugraph =
179 change subst bo selected_bo metasenv ugraph
181 let ty', metasenv, ugraph =
182 match ty,selected_ty with
183 None,None -> None, metasenv, ugraph
184 | Some ty,Some selected_ty ->
185 let ty', metasenv, ugraph =
186 change subst ty selected_ty metasenv ugraph
188 Some ty', metasenv, ugraph
189 | _,_ -> assert false
191 (Some (name,Cic.Def (bo',ty'))::context'), metasenv, ugraph
192 | _,_ -> assert false
193 ) context selected_context ([], metasenv, ugraph) in
197 | (n,_,_) when n = metano -> (metano,context',ty')
201 (curi,metasenv',pbo,pty), [metano]
203 mk_tactic (change_tac ~pattern ~with_what)
205 let fold_tac ~reduction ~term ~pattern =
206 let fold_tac ~reduction ~term ~pattern:(wanted,hyps_pat,concl_pat) status =
207 assert (wanted = None); (* this should be checked syntactically *)
209 (fun context metasenv ugraph ->
210 let term, metasenv, ugraph = term context metasenv ugraph in
211 let reduction, metasenv, ugraph = reduction context metasenv ugraph in
212 reduction context term, metasenv, ugraph)
215 (change_tac ~pattern:(Some reduced_term,hyps_pat,concl_pat) term) status
217 mk_tactic (fold_tac ~reduction ~term ~pattern)