]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/proofEngineHelpers.ml
support for _ in binders, and a more coplex pattern that should help if
[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 (Cic.Anonymous, s1, t1), Cic.Prod (name, s2, t2)
174     | Cic.Lambda (Cic.Anonymous, s1, t1), Cic.Lambda (name, s2, t2) ->
175         aux i s1 s2 @ aux (add_ctx i name (Cic.Decl s2)) t1 t2
176     | Cic.Prod (Cic.Name n1, s1, t1), 
177       Cic.Prod ((Cic.Name n2) as name , s2, t2)
178     | Cic.Lambda (Cic.Name n1, s1, t1), 
179       Cic.Lambda ((Cic.Name n2) as name, s2, t2) when n1 = n2->
180         aux i s1 s2 @ aux (add_ctx i name (Cic.Decl s2)) t1 t2
181     | Cic.Prod (name1, s1, t1), Cic.Prod (name2, s2, t2)
182     | Cic.Lambda (name1, s1, t1), Cic.Lambda (name2, s2, t2) -> []
183     | Cic.LetIn (Cic.Anonymous, s1, t1), Cic.LetIn (name, s2, t2) -> 
184         aux i s1 s2 @ aux (add_ctx i name (Cic.Def (s2,None))) t1 t2
185     | Cic.LetIn (Cic.Name n1, s1, t1), 
186       Cic.LetIn ((Cic.Name n2) as name, s2, t2) when n1 = n2-> 
187         aux i s1 s2 @ aux (add_ctx i name (Cic.Def (s2,None))) t1 t2
188     | Cic.LetIn (name1, s1, t1), Cic.LetIn (name2, s2, t2) -> []
189     | Cic.Appl terms1, Cic.Appl terms2 -> auxs i terms1 terms2
190     | Cic.Var (_, subst1), Cic.Var (_, subst2)
191     | Cic.Const (_, subst1), Cic.Const (_, subst2)
192     | Cic.MutInd (_, _, subst1), Cic.MutInd (_, _, subst2)
193     | Cic.MutConstruct (_, _, _, subst1), Cic.MutConstruct (_, _, _, subst2) ->
194         auxs i (List.map snd subst1) (List.map snd subst2)
195     | Cic.MutCase (_, _, out1, t1, pat1), Cic.MutCase (_ , _, out2, t2, pat2) ->
196         aux i out1 out2 @ aux i t1 t2 @ auxs i pat1 pat2
197     | Cic.Fix (_, funs1), Cic.Fix (_, funs2) ->
198         List.concat
199           (List.map2
200             (fun (_, _, ty1, bo1) (_, _, ty2, bo2) -> 
201               aux i ty1 ty2 @ aux i bo1 bo2)
202             funs1 funs2)
203     | Cic.CoFix (_, funs1), Cic.CoFix (_, funs2) ->
204         List.concat
205           (List.map2
206             (fun (_, ty1, bo1) (_, ty2, bo2) -> aux i ty1 ty2 @ aux i bo1 bo2)
207             funs1 funs2)
208     | x,y -> 
209         raise (Bad_pattern 
210                 (Printf.sprintf "Pattern %s versus term %s" 
211                   (CicPp.ppterm x)
212                   (CicPp.ppterm y)))
213   and auxs i terms1 terms2 =  (* as aux for list of terms *)
214     List.concat (List.map2 (fun t1 t2 -> aux i t1 t2) terms1 terms2)
215   in
216   aux [] pattern term
217
218 let pattern_of ?(equality=(==)) ~term terms =
219   let (===) x y = equality x y in
220   let rec aux t =
221     match t with
222     | t when List.exists (fun t' -> t === t') terms -> Cic.Implicit (Some `Hole)
223     | Cic.Var (uri, subst) -> Cic.Var (uri, aux_subst subst)
224     | Cic.Meta (i, ctxt) ->
225         let ctxt =
226           List.map (function None -> None | Some t -> Some (aux t)) ctxt
227         in
228         Cic.Meta (i, ctxt)
229     | Cic.Cast (t, ty) -> Cic.Cast (aux t, aux ty)
230     | Cic.Prod (name, s, t) -> Cic.Prod (name, aux s, aux t)
231     | Cic.Lambda (name, s, t) -> Cic.Lambda (name, aux s, aux t)
232     | Cic.LetIn (name, s, t) -> Cic.LetIn (name, aux s, aux t)
233     | Cic.Appl terms -> Cic.Appl (List.map aux terms)
234     | Cic.Const (uri, subst) -> Cic.Const (uri, aux_subst subst)
235     | Cic.MutInd (uri, tyno, subst) -> Cic.MutInd (uri, tyno, aux_subst subst)
236     | Cic.MutConstruct (uri, tyno, consno, subst) ->
237         Cic.MutConstruct (uri, tyno, consno, aux_subst subst)
238     | Cic.MutCase (uri, tyno, outty, t, pat) ->
239         Cic.MutCase (uri, tyno, aux outty, aux t, List.map aux pat)
240     | Cic.Fix (funno, funs) ->
241         let funs =
242           List.map (fun (name, i, ty, bo) -> (name, i, aux ty, aux bo)) funs
243         in
244         Cic.Fix (funno, funs)
245     | Cic.CoFix (funno, funs) ->
246         let funs =
247           List.map (fun (name, ty, bo) -> (name, aux ty, aux bo)) funs
248         in
249         Cic.CoFix (funno, funs)
250     | Cic.Rel _
251     | Cic.Sort _
252     | Cic.Implicit _ -> t
253   and aux_subst subst =
254     List.map (fun (uri, t) -> (uri, aux t)) subst
255   in
256   aux term
257