]> matita.cs.unibo.it Git - helm.git/blob - components/tactics/paramodulation/subst.ml
tagged 0.5.0-rc1
[helm.git] / components / tactics / paramodulation / subst.ml
1 (* cOpyright (C) 2005, 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: inference.ml 6245 2006-04-05 12:07:51Z tassi $ *)
27
28
29 (******* CIC substitution ***************************************************)
30
31 type cic_substitution = Cic.substitution
32 let cic_apply_subst = CicMetaSubst.apply_subst
33 let cic_apply_subst_metasenv = CicMetaSubst.apply_subst_metasenv
34 let cic_ppsubst = CicMetaSubst.ppsubst
35 let cic_buildsubst n context t ty tail = (n,(context,t,ty)) :: tail
36 let cic_flatten_subst subst =
37     List.map
38       (fun (i, (context, term, ty)) ->
39          let context = (* cic_apply_subst_context subst*) context in
40          let term = cic_apply_subst subst term in
41          let ty = cic_apply_subst subst ty in  
42          (i, (context, term, ty))) subst
43 let rec cic_lookup_subst meta subst =
44   match meta with
45   | Cic.Meta (i, _) -> (
46       try let _, (_, t, _) = List.find (fun (m, _) -> m = i) subst 
47       in cic_lookup_subst t subst 
48       with Not_found -> meta
49     )
50   | _ -> meta
51 ;;
52
53 let cic_merge_subst_if_possible s1 s2 =
54   let already_in = Hashtbl.create 13 in
55   let rec aux acc = function
56     | ((i,_,x) as s)::tl ->
57         (try 
58           let x' = Hashtbl.find already_in i in
59           if x = x' then aux acc tl else None
60         with
61         | Not_found -> 
62             Hashtbl.add already_in i x;
63             aux (s::acc) tl)
64     | [] -> Some acc 
65   in  
66     aux [] (s1@s2)
67 ;;
68
69 (******** NAIF substitution **************************************************)
70 (* 
71  * naif version of apply subst; the local context of metas is ignored;
72  * we assume the substituted term must be lifted according to the nesting
73  * depth of the meta. 
74  * Alternatively, we could used implicit instead of metas 
75  *)
76
77 type naif_substitution = (int * Cic.term) list 
78
79 let naif_apply_subst lift subst term =
80  let rec aux k t =
81    match t with
82        Cic.Rel _ -> t
83      | Cic.Var (uri,exp_named_subst) -> 
84          let exp_named_subst' =
85            List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
86          in
87            Cic.Var (uri, exp_named_subst')
88     | Cic.Meta (i, l) -> 
89         (try
90           aux k (CicSubstitution.lift (k+lift) (List.assoc i subst)) 
91          with Not_found -> t)
92     | Cic.Sort _
93     | Cic.Implicit _ -> t
94     | Cic.Cast (te,ty) -> Cic.Cast (aux k te, aux k ty)
95     | Cic.Prod (n,s,t) -> Cic.Prod (n, aux k s, aux (k+1) t)
96     | Cic.Lambda (n,s,t) -> Cic.Lambda (n, aux k s, aux (k+1) t)
97     | Cic.LetIn (n,s,ty,t) -> Cic.LetIn (n, aux k s, aux k ty, aux (k+1) t)
98     | Cic.Appl [] -> assert false
99     | Cic.Appl l -> Cic.Appl (List.map (aux k) l)
100     | Cic.Const (uri,exp_named_subst) ->
101         let exp_named_subst' =
102           List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
103         in
104           if exp_named_subst' != exp_named_subst then
105             Cic.Const (uri, exp_named_subst')
106           else
107             t (* TODO: provare a mantenere il piu' possibile sharing *)
108     | Cic.MutInd (uri,typeno,exp_named_subst) ->
109         let exp_named_subst' =
110           List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
111         in
112           Cic.MutInd (uri,typeno,exp_named_subst')
113     | Cic.MutConstruct (uri,typeno,consno,exp_named_subst) ->
114         let exp_named_subst' =
115           List.map (fun (uri, t) -> (uri, aux k t)) exp_named_subst
116         in
117           Cic.MutConstruct (uri,typeno,consno,exp_named_subst')
118     | Cic.MutCase (sp,i,outty,t,pl) ->
119         let pl' = List.map (aux k) pl in
120           Cic.MutCase (sp, i, aux k outty, aux k t, pl')
121     | Cic.Fix (i, fl) ->
122         let len = List.length fl in
123         let fl' =
124          List.map 
125            (fun (name, i, ty, bo) -> (name, i, aux k ty, aux (k+len) bo)) fl
126         in
127           Cic.Fix (i, fl')
128     | Cic.CoFix (i, fl) ->
129         let len = List.length fl in
130         let fl' =
131           List.map (fun (name, ty, bo) -> (name, aux k ty, aux (k+len) bo)) fl
132         in
133           Cic.CoFix (i, fl')
134 in
135   aux 0 term
136 ;;
137
138 (* naif version of apply_subst_metasenv: we do not apply the 
139 substitution to the context *)
140
141 let naif_apply_subst_metasenv subst metasenv =
142   List.map
143     (fun (n, context, ty) ->
144       (n, context, naif_apply_subst 0 subst ty))
145     (List.filter
146       (fun (i, _, _) -> not (List.mem_assoc i subst))
147       metasenv)
148
149 let naif_ppsubst names subst =
150   "{" ^ String.concat "; "
151     (List.map
152       (fun (idx, t) ->
153          Printf.sprintf "%d:= %s" idx (CicPp.pp t names))
154     subst) ^ "}"
155 ;;
156
157 let naif_buildsubst n context t ty tail = (n,t) :: tail ;;
158
159 let naif_flatten_subst subst = 
160   List.map (fun (i,t) -> i, naif_apply_subst 0 subst t ) subst
161 ;;
162
163 let rec naif_lookup_subst meta subst =
164   match meta with
165     | Cic.Meta (i, _) ->
166         (try
167           naif_lookup_subst (List.assoc i subst) subst
168         with
169             Not_found -> meta)
170     | _ -> meta
171 ;;
172
173 let naif_merge_subst_if_possible s1 s2 =
174   let already_in = Hashtbl.create 13 in
175   let rec aux acc = function
176     | ((i,x) as s)::tl ->
177         (try 
178           let x' = Hashtbl.find already_in i in
179           if x = x' then aux acc tl else None
180         with
181         | Not_found -> 
182             Hashtbl.add already_in i x;
183             aux (s::acc) tl)
184     | [] -> Some acc 
185   in  
186     aux [] (s1@s2)
187 ;;
188
189 (********** ACTUAL SUBSTITUTION IMPLEMENTATION *******************************)
190
191 type substitution = naif_substitution
192 let apply_subst = naif_apply_subst 0
193 let apply_subst_lift = naif_apply_subst
194 let apply_subst_metasenv = naif_apply_subst_metasenv
195 let ppsubst ?(names=[]) l = naif_ppsubst names l
196 let buildsubst = naif_buildsubst
197 let flatten_subst = naif_flatten_subst
198 let lookup_subst = naif_lookup_subst
199
200 (* filter out from metasenv the variables in substs *)
201 let filter subst metasenv =
202   List.filter
203     (fun (m, _, _) ->
204          try let _ = List.find (fun (i, _) -> m = i) subst in false
205          with Not_found -> true)
206     metasenv
207 ;;
208
209 let is_in_subst i subst = List.mem_assoc i subst;;
210   
211 let merge_subst_if_possible = naif_merge_subst_if_possible;;
212
213 let empty_subst = [];;
214
215 let concat x y = x @ y;;
216
217