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