]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationFwd.ml
snapshot (the thing on the doorstep)
[helm.git] / helm / ocaml / cic_notation / cicNotationFwd.ml
1 (* Copyright (C) 2004-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://helm.cs.unibo.it/
24  *)
25
26 open CicNotationEnv
27 open CicNotationPt
28
29 let meta_names_of term =
30   let rec names = ref [] in
31   let add_name n =
32     if List.mem n !names then ()
33     else names := n :: !names
34   in
35   let rec aux = function
36     | AttributedTerm (_, term) -> aux term
37     | Appl terms -> List.iter aux terms
38     | Binder (_, _, body) -> aux body
39     | Case (term, indty, outty_opt, patterns) ->
40         aux term ;
41         aux_opt outty_opt ;
42         List.iter aux_branch patterns
43     | LetIn (_, t1, t2) ->
44         aux t1 ;
45         aux t2
46     | LetRec (_, definitions, body) ->
47         List.iter aux_definition definitions ;
48         aux body
49     | Uri (_, Some substs) -> aux_substs substs
50     | Ident (_, Some substs) -> aux_substs substs
51     | Meta (_, substs) -> aux_meta_substs substs
52
53     | Implicit
54     | Ident _
55     | Num _
56     | Sort _
57     | Symbol _
58     | Uri _
59     | UserInput -> ()
60
61     | Magic magic -> aux_magic magic
62     | Variable var -> aux_variable var
63
64     | _ -> assert false
65   and aux_opt = function
66     | Some term -> aux term
67     | None -> ()
68   and aux_capture_var (_, ty_opt) = aux_opt ty_opt
69   and aux_branch (pattern, term) =
70     aux_pattern pattern ;
71     aux term
72   and aux_pattern (head, vars) = 
73     List.iter aux_capture_var vars
74   and aux_definition (var, term, i) =
75     aux_capture_var var ;
76     aux term
77   and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
78   and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
79   and aux_variable = function
80     | NumVar name -> add_name name
81     | IdentVar name -> add_name name
82     | TermVar name -> add_name name
83     | FreshVar _ -> ()
84     | Ascription _ -> assert false
85   and aux_magic = function
86     | Default (t1, t2)
87     | Fold (_, t1, _, t2) ->
88         aux t1 ;
89         aux t2
90     | _ -> assert false
91   in
92   aux term ;
93   !names
94
95 let unopt_names names env =
96   let rec aux acc = function
97     | (name, (ty, v)) :: tl when List.mem name names ->
98         (match ty, v with
99         | OptType ty, OptValue (Some v) -> aux ((name, (ty, v)) :: acc) tl
100         | _ -> assert false)
101     | _ :: tl -> aux acc tl
102       (* some pattern may contain only meta names, thus we trash all others *)
103     | [] -> acc
104   in
105   aux [] env
106
107 let head_names names env =
108   let rec aux acc = function
109     | (name, (ty, v)) :: tl when List.mem name names ->
110         (match ty, v with
111         | ListType ty, ListValue (v :: _) -> aux ((name, (ty, v)) :: acc) tl
112         | _ -> assert false)
113     | _ :: tl -> aux acc tl
114       (* base pattern may contain only meta names, thus we trash all others *)
115     | [] -> acc
116   in
117   aux [] env
118
119 let tail_names names env =
120   let rec aux acc = function
121     | (name, (ty, v)) :: tl when List.mem name names ->
122         (match ty, v with
123         | ListType ty, ListValue (_ :: vtl) ->
124             aux ((name, (ListType ty, ListValue vtl)) :: acc) tl
125         | _ -> assert false)
126     | binding :: tl -> aux (binding :: acc) tl
127     | [] -> acc
128   in
129   aux [] env
130
131 let instantiate_level2 env term =
132   let fresh_env = ref [] in
133   let lookup_fresh_name n =
134     try
135       List.assoc n !fresh_env
136     with Not_found ->
137       let new_name = CicNotationUtil.fresh_name () in
138       fresh_env := (n, new_name) :: !fresh_env;
139       new_name
140   in
141   let rec aux env term =
142     match term with
143     | AttributedTerm (_, term) -> aux env term
144     | Appl terms -> Appl (List.map (aux env) terms)
145     | Binder (binder, var, body) ->
146         Binder (binder, aux_capture_var env var, aux env body)
147     | Case (term, indty, outty_opt, patterns) ->
148         Case (aux env term, indty, aux_opt env outty_opt,
149           List.map (aux_branch env) patterns)
150     | LetIn (var, t1, t2) ->
151         LetIn (aux_capture_var env var, aux env t1, aux env t2)
152     | LetRec (kind, definitions, body) ->
153         LetRec (kind, List.map (aux_definition env) definitions, aux env body)
154     | Uri (name, None) -> Uri (name, None)
155     | Uri (name, Some substs) -> Uri (name, Some (aux_substs env substs))
156     | Ident (name, Some substs) -> Ident (name, Some (aux_substs env substs))
157     | Meta (index, substs) -> Meta (index, aux_meta_substs env substs)
158
159     | Implicit
160     | Ident _
161     | Num _
162     | Sort _
163     | Symbol _
164     | UserInput -> term
165
166     | Magic magic -> aux_magic env magic
167     | Variable var -> aux_variable env var
168
169     | _ -> assert false
170   and aux_opt env = function
171     | Some term -> Some (aux env term)
172     | None -> None
173   and aux_capture_var env (name, ty_opt) = (aux env name, aux_opt env ty_opt)
174   and aux_branch env (pattern, term) =
175     (aux_pattern env pattern, aux env term)
176   and aux_pattern env (head, vars) =
177     (head, List.map (aux_capture_var env) vars)
178   and aux_definition env (var, term, i) =
179     (aux_capture_var env var, aux env term, i)
180   and aux_substs env substs =
181     List.map (fun (name, term) -> (name, aux env term)) substs
182   and aux_meta_substs env meta_substs = List.map (aux_opt env) meta_substs
183   and aux_variable env = function
184     | NumVar name -> Num (lookup_num env name, 0)
185     | IdentVar name -> Ident (lookup_string env name, None)
186     | TermVar name -> lookup_term env name
187     | FreshVar name -> Ident (lookup_fresh_name name, None)
188     | Ascription (term, name) -> assert false
189   and aux_magic env = function
190     | Default (some_pattern, none_pattern) ->
191         (match meta_names_of some_pattern with
192         | [] -> assert false (* some pattern must contain at least 1 name *)
193         | (name :: _) as names ->
194             (match lookup_value env name with
195             | OptValue (Some _) ->
196                 (* assumption: if "name" above is bound to Some _, then all
197                  * names returned by "meta_names_of" are bound to Some _ as well
198                  *)
199                 aux (unopt_names names env) some_pattern
200             | OptValue None -> aux env none_pattern
201             | _ -> assert false))
202     | Fold (`Left, base_pattern, names, rec_pattern) ->
203         let acc_name = List.hd names in (* names can't be empty, cfr. parser *)
204         let meta_names =
205           List.filter ((<>) acc_name) (meta_names_of rec_pattern)
206         in
207         (match meta_names with
208         | [] -> assert false (* as above *)
209         | (name :: _) as names ->
210             let rec instantiate_fold_left acc env' =
211               prerr_endline "instantiate_fold_left";
212               match lookup_value env' name with
213               | ListValue (_ :: _) ->
214                   instantiate_fold_left 
215                     (let acc_binding = acc_name, (TermType, TermValue acc) in
216                      aux (acc_binding :: head_names names env') rec_pattern)
217                     (tail_names names env')
218               | ListValue [] -> acc
219               | _ -> assert false
220             in
221             instantiate_fold_left (aux env base_pattern) env)
222     | Fold (`Right, base_pattern, names, rec_pattern) ->
223         let acc_name = List.hd names in (* names can't be empty, cfr. parser *)
224         let meta_names =
225           List.filter ((<>) acc_name) (meta_names_of rec_pattern)
226         in
227         (match meta_names with
228         | [] -> assert false (* as above *)
229         | (name :: _) as names ->
230             let rec instantiate_fold_right env' =
231               prerr_endline "instantiate_fold_right";
232               match lookup_value env' name with
233               | ListValue (_ :: _) ->
234                   let acc = instantiate_fold_right (tail_names names env') in
235                   let acc_binding = acc_name, (TermType, TermValue acc) in
236                   aux (acc_binding :: head_names names env') rec_pattern
237               | ListValue [] -> aux env base_pattern
238               | _ -> assert false
239             in
240             instantiate_fold_right env)
241     | _ -> assert false
242   in
243   aux env term
244