]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/reductionTactics.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / 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 open ProofEngineTypes
27
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
35    else
36      let pairs, metasenv, ugraph =
37        List.fold_left
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)
42         terms
43      in
44      let terms, terms' = List.split pairs in
45      let where' =
46        ProofEngineReduction.replace ~equality:(==) ~what:terms ~with_what:terms'
47         ~where:where
48      in
49      CicMetaSubst.apply_subst subst where', metasenv, ugraph
50   in
51   let (subst,metasenv,ugraph,selected_context,selected_ty) =
52     ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph
53       ~conjecture ~pattern
54   in
55   let ty', metasenv, ugraph = change subst ty selected_ty metasenv ugraph in
56   let context', metasenv, ugraph =
57    List.fold_right2
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
64           in
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
69           in
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
76                 in
77                 Some ty', metasenv, ugraph
78             | _,_ -> assert false
79           in
80            (Some (name,Cic.Def (bo',ty'))::context'), metasenv, ugraph
81        | _,_ -> assert false
82     ) context selected_context ([], metasenv, ugraph) in
83   let metasenv' = 
84     List.map (function 
85       | (n,_,_) when n = metano -> (metano,context',ty')
86       | _ as t -> t
87     ) metasenv
88   in
89   (curi,metasenv',pbo,pty), [metano]
90 ;;
91
92 let simpl_tac ~pattern =
93  mk_tactic (reduction_tac
94   ~reduction:(const_lazy_reduction ProofEngineReduction.simpl) ~pattern)
95
96 let reduce_tac ~pattern =
97  mk_tactic (reduction_tac
98   ~reduction:(const_lazy_reduction ProofEngineReduction.reduce) ~pattern)
99
100 let unfold_tac what ~pattern =
101   let reduction =
102     match what with
103     | None -> const_lazy_reduction (ProofEngineReduction.unfold ?what:None)
104     | Some lazy_term ->
105         (fun context metasenv ugraph ->
106           let what, metasenv, ugraph = lazy_term context metasenv ugraph in
107           ProofEngineReduction.unfold ~what, 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 exception NotConvertible
120
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
136    else
137     let pairs, metasenv, ugraph =
138       List.fold_left
139         (fun (pairs, metasenv, ugraph) (context_of_t, t) ->
140           let with_what, metasenv, ugraph =
141             with_what context_of_t metasenv ugraph
142           in
143           let _,u =
144             CicTypeChecker.type_of_aux' metasenv context_of_t with_what ugraph
145           in
146           let b,_ =
147            CicReduction.are_convertible ~metasenv context_of_t t with_what u
148           in
149           if b then
150            ((t, with_what) :: pairs), metasenv, ugraph
151           else
152            raise NotConvertible)
153         ([], metasenv, ugraph)
154         terms
155     in
156     let terms, terms' = List.split pairs in
157      let where' =
158       ProofEngineReduction.replace ~equality:(==) ~what:terms ~with_what:terms'
159        ~where:where
160      in
161       CicMetaSubst.apply_subst subst where', metasenv, ugraph
162   in
163   let (subst,metasenv,ugraph,selected_context,selected_ty) =
164    ProofEngineHelpers.select ~metasenv ~ugraph:CicUniv.empty_ugraph ~conjecture
165     ~pattern in
166   let ty', metasenv, ugraph = change subst ty selected_ty metasenv ugraph in
167   let context', metasenv, ugraph =
168    List.fold_right2
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
175           in
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
180           in
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
187                 in
188                 Some ty', metasenv, ugraph
189             | _,_ -> assert false
190           in
191            (Some (name,Cic.Def (bo',ty'))::context'), metasenv, ugraph
192        | _,_ -> assert false
193     ) context selected_context ([], metasenv, ugraph) in
194   let metasenv' = 
195     List.map
196       (function 
197         | (n,_,_) when n = metano -> (metano,context',ty')
198         | _ as t -> t)
199       metasenv
200   in
201   (curi,metasenv',pbo,pty), [metano]
202   in
203     mk_tactic (change_tac ~pattern ~with_what)
204
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 *)
208   let reduced_term =
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)
213   in
214    apply_tactic
215     (change_tac ~pattern:(Some reduced_term,hyps_pat,concl_pat) term) status
216  in
217   mk_tactic (fold_tac ~reduction ~term ~pattern)
218