]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/tactics/reductionTactics.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / components / tactics / reductionTactics.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 open ProofEngineTypes
29
30 (* Note: this code is almost identical to change_tac and
31 *  it could be unified by making the change function a callback *)
32 let reduction_tac ~reduction ~pattern (proof,goal) =
33   let curi,metasenv,subst,pbo,pty, attrs = proof in
34   let (metano,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
35   let change subst where terms metasenv ugraph =
36    if terms = [] then where, metasenv, ugraph
37    else
38      let pairs, metasenv, ugraph =
39        List.fold_left
40         (fun (pairs, metasenv, ugraph) (context, t) ->
41           let reduction, metasenv, ugraph = reduction context metasenv ugraph in
42           ((t, reduction context t) :: pairs), metasenv, ugraph)
43         ([], metasenv, ugraph)
44         terms
45      in
46      let terms, terms' = List.split pairs in
47      let where' =
48        ProofEngineReduction.replace ~equality:(==) ~what:terms ~with_what:terms'
49         ~where:where
50      in
51      CicMetaSubst.apply_subst subst where', metasenv, ugraph
52   in
53   let (subst,metasenv,ugraph,selected_context,selected_ty) =
54     ProofEngineHelpers.select ~subst ~metasenv ~ugraph:CicUniv.oblivion_ugraph
55       ~conjecture ~pattern
56   in
57   let ty', metasenv, ugraph = change subst ty selected_ty metasenv ugraph in
58   let context', metasenv, ugraph =
59    List.fold_right2
60     (fun entry selected_entry (context', metasenv, ugraph) ->
61       match entry,selected_entry with
62          None,None -> None::context', metasenv, ugraph
63        | Some (name,Cic.Decl ty),Some (`Decl selected_ty) ->
64           let ty', metasenv, ugraph =
65             change subst ty selected_ty metasenv ugraph
66           in
67           Some (name,Cic.Decl ty')::context', metasenv, ugraph
68        | Some (name,Cic.Def (bo,ty)),Some (`Def (selected_bo,selected_ty)) ->
69           let bo', metasenv, ugraph =
70             change subst bo selected_bo metasenv ugraph in
71           let ty', metasenv, ugraph =
72             change subst ty selected_ty metasenv ugraph
73           in
74            (Some (name,Cic.Def (bo',ty'))::context'), metasenv, ugraph
75        | _,_ -> assert false
76     ) context selected_context ([], metasenv, ugraph) in
77   let metasenv' = 
78     List.map (function 
79       | (n,_,_) when n = metano -> (metano,context',ty')
80       | _ as t -> t
81     ) metasenv
82   in
83   (curi,metasenv',subst,pbo,pty, attrs), [metano]
84 ;;
85
86 let simpl_tac ~pattern =
87  mk_tactic (reduction_tac
88   ~reduction:(const_lazy_reduction ProofEngineReduction.simpl) ~pattern)
89
90 let unfold_tac what ~pattern =
91   let reduction =
92     match what with
93     | None -> const_lazy_reduction (ProofEngineReduction.unfold ?what:None)
94     | Some lazy_term ->
95         (fun context metasenv ugraph ->
96           let what, metasenv, ugraph = lazy_term context metasenv ugraph in
97           let unfold ctx t =
98            try
99             ProofEngineReduction.unfold ~what ctx t
100            with
101             (* Not what we would like to have; however, this is required
102                right now for the case of a definition in the context:
103                if it works only in the body (or only in the type), that should
104                be accepted *)
105             ProofEngineTypes.Fail _ -> t
106           in
107           unfold, metasenv, ugraph)
108   in
109   mk_tactic (reduction_tac ~reduction ~pattern)
110   
111 let whd_tac ~pattern =
112  mk_tactic (reduction_tac
113   ~reduction:(const_lazy_reduction CicReduction.whd) ~pattern)
114
115 let normalize_tac ~pattern =
116  mk_tactic (reduction_tac
117   ~reduction:(const_lazy_reduction CicReduction.normalize) ~pattern)
118
119 let head_beta_reduce_tac ?delta ?upto ~pattern =
120  mk_tactic (reduction_tac
121   ~reduction:
122     (const_lazy_reduction
123       (fun _context -> CicReduction.head_beta_reduce ?delta ?upto)) ~pattern)
124
125 exception NotConvertible
126
127 (* Note: this code is almost identical to reduction_tac and
128 *  it could be unified by making the change function a callback *)
129 (* CSC: with_what is parsed in the context of the goal, but it should replace
130         something that lives in a completely different context. Thus we
131         perform a delift + lift phase to move it in the right context. However,
132         in this way the tactic is less powerful than expected: with_what cannot
133         reference variables that are local to the term that is going to be
134         replaced. To fix this we should parse with_what in the context of the
135         term(s) to be replaced. *)
136 let change_tac ?(with_cast=false) ~pattern with_what  =
137  let change_tac ~pattern ~with_what (proof, goal) =
138   let curi,metasenv,subst,pbo,pty, attrs = proof in
139   let (metano,context,ty) as conjecture = CicUtil.lookup_meta goal metasenv in
140   let change subst where terms metasenv ugraph =
141    if terms = [] then where, metasenv, ugraph
142    else
143     let pairs, metasenv, ugraph =
144       List.fold_left
145         (fun (pairs, metasenv, ugraph) (context_of_t, t) ->
146           let with_what, metasenv, ugraph =
147             with_what context_of_t metasenv ugraph
148           in
149           let _,u =
150             CicTypeChecker.type_of_aux' 
151               metasenv ~subst context_of_t with_what ugraph
152           in
153           let b,_ =
154            CicReduction.are_convertible 
155              ~metasenv ~subst context_of_t t with_what u
156           in
157           if b then
158            ((t, with_what) :: pairs), metasenv, ugraph
159           else
160            raise NotConvertible)
161         ([], metasenv, ugraph)
162         terms
163     in
164     let terms, terms' = List.split pairs in
165      let where' =
166       ProofEngineReduction.replace ~equality:(==) ~what:terms ~with_what:terms'
167        ~where:where
168      in
169       CicMetaSubst.apply_subst subst where', metasenv, ugraph
170   in
171   let (subst,metasenv,ugraph,selected_context,selected_ty) =
172    ProofEngineHelpers.select 
173      ~metasenv ~subst ~ugraph:CicUniv.oblivion_ugraph ~conjecture ~pattern 
174   in
175   let ty', metasenv, ugraph = change subst ty selected_ty metasenv ugraph in
176   let context', metasenv, ugraph =
177    List.fold_right2
178     (fun entry selected_entry (context', metasenv, ugraph) ->
179      match entry,selected_entry with
180          None,None -> (None::context'), metasenv, ugraph
181        | Some (name,Cic.Decl ty),Some (`Decl selected_ty) ->
182           let ty', metasenv, ugraph =
183             change subst ty selected_ty metasenv ugraph
184           in
185            (Some (name,Cic.Decl ty')::context'), metasenv, ugraph
186        | Some (name,Cic.Def (bo,ty)),Some (`Def (selected_bo,selected_ty)) ->
187           let bo', metasenv, ugraph =
188             change subst bo selected_bo metasenv ugraph in
189           let ty', metasenv, ugraph =
190             change subst ty selected_ty metasenv ugraph
191           in
192            (Some (name,Cic.Def (bo',ty'))::context'), metasenv, ugraph
193        | _,_ -> assert false
194     ) context selected_context ([], metasenv, ugraph) in
195   let metasenv' = 
196     List.map
197       (function 
198         | (n,_,_) when n = metano -> (metano,context',ty')
199         | _ as t -> t)
200       metasenv
201   in
202    let proof,goal = (curi,metasenv',subst,pbo,pty, attrs), metano in
203     if with_cast then
204      let metano' = ProofEngineHelpers.new_meta_of_proof ~proof in
205      let (newproof,_) =
206        let irl= CicMkImplicit.identity_relocation_list_for_metavariable context'
207        in
208         ProofEngineHelpers.subst_meta_in_proof
209          proof metano
210           (Cic.Cast (Cic.Meta(metano',irl),ty')) [metano',context',ty']
211      in
212       newproof, [metano']
213     else
214      proof,[goal]
215   in
216     mk_tactic (change_tac ~pattern ~with_what)
217 ;;
218
219 let fold_tac ~reduction ~term ~pattern =
220  let fold_tac ~reduction ~term ~pattern:(wanted,hyps_pat,concl_pat) status =
221   assert (wanted = None); (* this should be checked syntactically *)
222   let reduced_term =
223     (fun context metasenv ugraph ->
224       let term, metasenv, ugraph = term context metasenv ugraph in
225       let reduction, metasenv, ugraph = reduction context metasenv ugraph in
226       reduction context term, metasenv, ugraph)
227   in
228    apply_tactic
229     (change_tac ~pattern:(Some reduced_term,hyps_pat,concl_pat) term) status
230  in
231   mk_tactic (fold_tac ~reduction ~term ~pattern)
232 ;;
233