]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/proofEngineHelpers.ml
1) moved select and pattern_of from cicUtil to proofEngineHelpers
[helm.git] / helm / ocaml / tactics / proofEngineHelpers.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 exception Bad_pattern of string
27
28 let new_meta_of_proof ~proof:(_, metasenv, _, _) =
29   CicMkImplicit.new_meta metasenv []
30
31 let subst_meta_in_proof proof meta term newmetasenv =
32  let uri,metasenv,bo,ty = proof in
33    (* empty context is ok for term since it wont be used by apply_subst *)
34    (* hack: since we do not know the context and the type of term, we
35       create a substitution with cc =[] and type = Implicit; they will be
36       in  any case dropped by apply_subst, but it would be better to rewrite
37       the code. Cannot we just use apply_subst_metasenv, etc. ?? *)
38   let subst_in = CicMetaSubst.apply_subst [meta,([], term,Cic.Implicit None)] in
39    let metasenv' =
40     newmetasenv @ (List.filter (function (m,_,_) -> m <> meta) metasenv)
41    in
42     let metasenv'' =
43      List.map
44       (function i,canonical_context,ty ->
45         let canonical_context' =
46          List.map
47           (function
48               Some (n,Cic.Decl s) -> Some (n,Cic.Decl (subst_in s))
49             | Some (n,Cic.Def (s,None)) -> Some (n,Cic.Def ((subst_in s),None))
50             | None -> None
51             | Some (_,Cic.Def (_,Some _)) -> assert false
52           ) canonical_context
53         in
54          i,canonical_context',(subst_in ty)
55       ) metasenv'
56     in
57      let bo' = subst_in bo in
58      (* Metavariables can appear also in the *statement* of the theorem
59       * since the parser does not reject as statements terms with
60       * metavariable therein *)
61      let ty' = subst_in ty in
62       let newproof = uri,metasenv'',bo',ty' in
63        (newproof, metasenv'')
64
65 (*CSC: commento vecchio *)
66 (* refine_meta_with_brand_new_metasenv meta term subst_in newmetasenv     *)
67 (* This (heavy) function must be called when a tactic can instantiate old *)
68 (* metavariables (i.e. existential variables). It substitues the metasenv *)
69 (* of the proof with the result of removing [meta] from the domain of     *)
70 (* [newmetasenv]. Then it replaces Cic.Meta [meta] with [term] everywhere *)
71 (* in the current proof. Finally it applies [apply_subst_replacing] to    *)
72 (*  current proof.                                                        *)
73 (*CSC: A questo punto perche' passare un bo' gia' istantiato, se tanto poi *)
74 (*CSC: ci ripasso sopra apply_subst!!!                                     *)
75 (*CSC: Attenzione! Ora questa funzione applica anche [subst_in] a *)
76 (*CSC: [newmetasenv].                                             *)
77 let subst_meta_and_metasenv_in_proof proof meta subst_in newmetasenv =
78  let (uri,_,bo,ty) = proof in
79   let bo' = subst_in bo in
80   (* Metavariables can appear also in the *statement* of the theorem
81    * since the parser does not reject as statements terms with
82    * metavariable therein *)
83   let ty' = subst_in ty in
84   let metasenv' =
85    List.fold_right
86     (fun metasenv_entry i ->
87       match metasenv_entry with
88          (m,canonical_context,ty) when m <> meta ->
89            let canonical_context' =
90             List.map
91              (function
92                  None -> None
93                | Some (i,Cic.Decl t) -> Some (i,Cic.Decl (subst_in t))
94                | Some (i,Cic.Def (t,None))  ->
95                   Some (i,Cic.Def ((subst_in t),None))
96                | Some (_,Cic.Def (_,Some _))  -> assert false
97              ) canonical_context
98            in
99             (m,canonical_context',subst_in ty)::i
100        | _ -> i
101     ) newmetasenv []
102   in
103    let newproof = uri,metasenv',bo',ty' in
104     (newproof, metasenv')
105
106 let compare_metasenvs ~oldmetasenv ~newmetasenv =
107  List.map (function (i,_,_) -> i)
108   (List.filter
109    (function (i,_,_) ->
110      not (List.exists (fun (j,_,_) -> i=j) oldmetasenv)) newmetasenv)
111 ;;
112
113 (** finds the _pointers_ to subterms that are alpha-equivalent to wanted in t *)
114 let find_subterms ~eq ~wanted t =
115   let rec find w t =
116     if eq w t then 
117       [t]
118     else
119       match t with
120       | Cic.Sort _ 
121       | Cic.Rel _ -> []
122       | Cic.Meta (_, ctx) -> 
123           List.fold_left (
124             fun acc e -> 
125               match e with 
126               | None -> acc 
127               | Some t -> find w t @ acc
128           ) [] ctx
129       | Cic.Lambda (_, t1, t2) 
130       | Cic.Prod (_, t1, t2) 
131       | Cic.LetIn (_, t1, t2) -> 
132           find w t1 @ find (CicSubstitution.lift 1 w) t2
133       | Cic.Appl l -> 
134           List.fold_left (fun acc t -> find w t @ acc) [] l
135       | Cic.Cast (t, ty) -> find w t @ find w ty
136       | Cic.Implicit _ -> assert false
137       | Cic.Const (_, esubst)
138       | Cic.Var (_, esubst) 
139       | Cic.MutInd (_, _, esubst) 
140       | Cic.MutConstruct (_, _, _, esubst) -> 
141           List.fold_left (fun acc (_, t) -> find w t @ acc) [] esubst
142       | Cic.MutCase (_, _, outty, indterm, patterns) -> 
143           find w outty @ find w indterm @ 
144             List.fold_left (fun acc p -> find w p @ acc) [] patterns
145       | Cic.Fix (_, funl) -> 
146           List.fold_left (
147             fun acc (_, _, ty, bo) -> find w ty @ find w bo @ acc
148           ) [] funl
149       | Cic.CoFix (_, funl) ->
150           List.fold_left (
151             fun acc (_, ty, bo) -> find w ty @ find w bo @ acc
152           ) [] funl
153   in
154   find wanted t
155   
156 let select ~term ~pattern =
157   let add_ctx i name entry =
158       (Some (name, entry)) :: i
159   in
160   (* i is the number of binder traversed *) 
161   let rec aux i pattern term =
162     match (pattern, term) with
163     | Cic.Implicit (Some `Hole), t -> [i,t]
164     | Cic.Implicit (Some `Type), t -> []
165     | Cic.Implicit None,_ -> []
166     | Cic.Meta (_, ctxt1), Cic.Meta (_, ctxt2) ->
167         List.concat
168           (List.map2
169             (fun t1 t2 ->
170               (match (t1, t2) with Some t1, Some t2 -> aux i t1 t2 | _ -> []))
171             ctxt1 ctxt2)
172     | Cic.Cast (te1, ty1), Cic.Cast (te2, ty2) -> aux i te1 te2 @ aux i ty1 ty2
173     | Cic.Prod (_, s1, t1), Cic.Prod (name, s2, t2)
174     | Cic.Lambda (_, s1, t1), Cic.Lambda (name, s2, t2) ->
175         aux i s1 s2 @ aux (add_ctx i name (Cic.Decl s2)) t1 t2
176     | Cic.LetIn (_, s1, t1), Cic.LetIn (name, s2, t2) -> 
177         aux i s1 s2 @ aux (add_ctx i name (Cic.Def (s2,None))) t1 t2
178     | Cic.Appl terms1, Cic.Appl terms2 -> auxs i terms1 terms2
179     | Cic.Var (_, subst1), Cic.Var (_, subst2)
180     | Cic.Const (_, subst1), Cic.Const (_, subst2)
181     | Cic.MutInd (_, _, subst1), Cic.MutInd (_, _, subst2)
182     | Cic.MutConstruct (_, _, _, subst1), Cic.MutConstruct (_, _, _, subst2) ->
183         auxs i (List.map snd subst1) (List.map snd subst2)
184     | Cic.MutCase (_, _, out1, t1, pat1), Cic.MutCase (_ , _, out2, t2, pat2) ->
185         aux i out1 out2 @ aux i t1 t2 @ auxs i pat1 pat2
186     | Cic.Fix (_, funs1), Cic.Fix (_, funs2) ->
187         List.concat
188           (List.map2
189             (fun (_, _, ty1, bo1) (_, _, ty2, bo2) -> 
190               aux i ty1 ty2 @ aux i bo1 bo2)
191             funs1 funs2)
192     | Cic.CoFix (_, funs1), Cic.CoFix (_, funs2) ->
193         List.concat
194           (List.map2
195             (fun (_, ty1, bo1) (_, ty2, bo2) -> aux i ty1 ty2 @ aux i bo1 bo2)
196             funs1 funs2)
197     | x,y -> 
198         raise (Bad_pattern 
199                 (Printf.sprintf "Pattern %s versus term %s" 
200                   (CicPp.ppterm x)
201                   (CicPp.ppterm y)))
202   and auxs i terms1 terms2 =  (* as aux for list of terms *)
203     List.concat (List.map2 (fun t1 t2 -> aux i t1 t2) terms1 terms2)
204   in
205   aux [] pattern term
206
207 let pattern_of ?(equality=(==)) ~term terms =
208   let (===) x y = equality x y in
209   let rec aux t =
210     match t with
211     | t when List.exists (fun t' -> t === t') terms -> Cic.Implicit (Some `Hole)
212     | Cic.Var (uri, subst) -> Cic.Var (uri, aux_subst subst)
213     | Cic.Meta (i, ctxt) ->
214         let ctxt =
215           List.map (function None -> None | Some t -> Some (aux t)) ctxt
216         in
217         Cic.Meta (i, ctxt)
218     | Cic.Cast (t, ty) -> Cic.Cast (aux t, aux ty)
219     | Cic.Prod (name, s, t) -> Cic.Prod (name, aux s, aux t)
220     | Cic.Lambda (name, s, t) -> Cic.Lambda (name, aux s, aux t)
221     | Cic.LetIn (name, s, t) -> Cic.LetIn (name, aux s, aux t)
222     | Cic.Appl terms -> Cic.Appl (List.map aux terms)
223     | Cic.Const (uri, subst) -> Cic.Const (uri, aux_subst subst)
224     | Cic.MutInd (uri, tyno, subst) -> Cic.MutInd (uri, tyno, aux_subst subst)
225     | Cic.MutConstruct (uri, tyno, consno, subst) ->
226         Cic.MutConstruct (uri, tyno, consno, aux_subst subst)
227     | Cic.MutCase (uri, tyno, outty, t, pat) ->
228         Cic.MutCase (uri, tyno, aux outty, aux t, List.map aux pat)
229     | Cic.Fix (funno, funs) ->
230         let funs =
231           List.map (fun (name, i, ty, bo) -> (name, i, aux ty, aux bo)) funs
232         in
233         Cic.Fix (funno, funs)
234     | Cic.CoFix (funno, funs) ->
235         let funs =
236           List.map (fun (name, ty, bo) -> (name, aux ty, aux bo)) funs
237         in
238         Cic.CoFix (funno, funs)
239     | Cic.Rel _
240     | Cic.Sort _
241     | Cic.Implicit _ -> t
242   and aux_subst subst =
243     List.map (fun (uri, t) -> (uri, aux t)) subst
244   in
245   aux term
246