]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationUtil.ml
snapshot
[helm.git] / helm / ocaml / cic_notation / cicNotationUtil.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 CicNotationPt
27
28   (* TODO ensure that names generated by fresh_var do not clash with user's *)
29 let fresh_name =
30   let index = ref ~-1 in
31   fun () ->
32     incr index;
33     "fresh" ^ string_of_int !index
34
35 (* let meta_names_of term = *)
36 (*   let rec names = ref [] in *)
37 (*   let add_name n = *)
38 (*     if List.mem n !names then () *)
39 (*     else names := n :: !names *)
40 (*   in *)
41 (*   let rec aux = function *)
42 (*     | AttributedTerm (_, term) -> aux term *)
43 (*     | Appl terms -> List.iter aux terms *)
44 (*     | Binder (_, _, body) -> aux body *)
45 (*     | Case (term, indty, outty_opt, patterns) -> *)
46 (*         aux term ; *)
47 (*         aux_opt outty_opt ; *)
48 (*         List.iter aux_branch patterns *)
49 (*     | LetIn (_, t1, t2) -> *)
50 (*         aux t1 ; *)
51 (*         aux t2 *)
52 (*     | LetRec (_, definitions, body) -> *)
53 (*         List.iter aux_definition definitions ; *)
54 (*         aux body *)
55 (*     | Uri (_, Some substs) -> aux_substs substs *)
56 (*     | Ident (_, Some substs) -> aux_substs substs *)
57 (*     | Meta (_, substs) -> aux_meta_substs substs *)
58
59 (*     | Implicit *)
60 (*     | Ident _ *)
61 (*     | Num _ *)
62 (*     | Sort _ *)
63 (*     | Symbol _ *)
64 (*     | Uri _ *)
65 (*     | UserInput -> () *)
66
67 (*     | Magic magic -> aux_magic magic *)
68 (*     | Variable var -> aux_variable var *)
69
70 (*     | _ -> assert false *)
71 (*   and aux_opt = function *)
72 (*     | Some term -> aux term *)
73 (*     | None -> () *)
74 (*   and aux_capture_var (_, ty_opt) = aux_opt ty_opt *)
75 (*   and aux_branch (pattern, term) = *)
76 (*     aux_pattern pattern ; *)
77 (*     aux term *)
78 (*   and aux_pattern (head, vars) =  *)
79 (*     List.iter aux_capture_var vars *)
80 (*   and aux_definition (var, term, i) = *)
81 (*     aux_capture_var var ; *)
82 (*     aux term *)
83 (*   and aux_substs substs = List.iter (fun (_, term) -> aux term) substs *)
84 (*   and aux_meta_substs meta_substs = List.iter aux_opt meta_substs *)
85 (*   and aux_variable = function *)
86 (*     | NumVar name -> add_name name *)
87 (*     | IdentVar name -> add_name name *)
88 (*     | TermVar name -> add_name name *)
89 (*     | FreshVar _ -> () *)
90 (*     | Ascription _ -> assert false *)
91 (*   and aux_magic = function *)
92 (*     | Default (t1, t2) *)
93 (*     | Fold (_, t1, _, t2) -> *)
94 (*         aux t1 ; *)
95 (*         aux t2 *)
96 (*     | _ -> assert false *)
97 (*   in *)
98 (*   aux term ; *)
99 (*   !names *)
100
101 let visit_ast ?(special_k = fun _ -> assert false) k =
102   let rec aux = function
103
104     | Appl terms -> Appl (List.map k terms)
105     | Binder (kind, var, body) ->
106         Binder (kind, aux_capture_variable var, k body) 
107     | Case (term, indtype, typ, patterns) ->
108         Case (k term, indtype, aux_opt typ, aux_patterns patterns)
109     | LetIn (var, t1, t2) -> LetIn (aux_capture_variable var, k t1, k t2)
110     | LetRec (kind, definitions, term) ->
111         let definitions =
112           List.map
113             (fun (var, ty, n) -> aux_capture_variable var, k ty, n)
114             definitions
115         in
116         LetRec (kind, definitions, k term)
117     | Ident (name, Some substs) -> Ident (name, Some (aux_substs substs))
118     | Uri (name, Some substs) -> Uri (name, Some (aux_substs substs))
119     | Meta (index, substs) -> Meta (index, List.map aux_opt substs)
120
121     | (AttributedTerm _
122       | Layout _
123       | Literal _
124       | Magic _
125       | Variable _) as t -> special_k t
126
127     | (Ident _
128       | Implicit
129       | Num _
130       | Sort _
131       | Symbol _
132       | Uri _
133       | UserInput) as t -> t
134
135   and aux_opt = function
136     | None -> None
137     | Some term -> Some (k term)
138
139   and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
140
141   and aux_patterns patterns = List.map aux_pattern patterns
142
143   and aux_pattern ((head, vars), term) =
144     ((head, List.map aux_capture_variable vars), k term)
145
146   and aux_subst (name, term) = (name, k term)
147
148   and aux_substs substs = List.map aux_subst substs
149
150   in
151
152   aux
153
154 let visit_layout k = function
155   | Sub (t1, t2) -> Sub (k t1, k t2)
156   | Sup (t1, t2) -> Sup (k t1, k t2)
157   | Below (t1, t2) -> Below (k t1, k t2)
158   | Above (t1, t2) -> Above (k t1, k t2)
159   | Over (t1, t2) -> Over (k t1, k t2)
160   | Atop (t1, t2) -> Atop (k t1, k t2)
161   | Frac (t1, t2) -> Frac (k t1, k t2)
162   | Sqrt t -> Sqrt (k t)
163   | Root (arg, index) -> Root (k arg, k index)
164   | Break -> Break
165   | Box (kind, terms) -> Box (kind, List.map k terms)
166
167 let visit_magic k = function
168   | List0 (t, l) -> List0 (k t, l)
169   | List1 (t, l) -> List1 (k t, l)
170   | Opt t -> Opt (k t)
171   | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2)
172   | Default (t1, t2) -> Default (k t1, k t2)
173   | If (t1, t2, t3) -> If (k t1, k t2, k t3)
174   | Fail -> Fail
175
176 let variables_of_term t =
177   let rec vars = ref [] in
178   let add_variable v =
179     if List.mem v !vars then ()
180     else vars := v :: !vars
181   in
182   let rec aux = function
183     | Magic m -> Magic (visit_magic aux m)
184     | Layout l -> Layout (visit_layout aux l)
185     | Variable v -> Variable (aux_variable v)
186     | Literal _ as t -> t
187     | AttributedTerm (_, t) -> aux t
188     | t -> visit_ast aux t
189   and aux_variable = function
190     | (NumVar _
191       | IdentVar _
192       | TermVar _) as t ->
193         add_variable t ;
194         t
195     | FreshVar _ as t -> t
196     | Ascription _ -> assert false
197   in
198     ignore (aux t) ;
199     !vars
200
201 let names_of_term t =
202   let aux = function
203     | NumVar s
204     | IdentVar s
205     | TermVar s -> s
206     | _ -> assert false
207   in
208     List.map aux (variables_of_term t)
209
210 let rec strip_attributes t =
211   let special_k = function
212     | AttributedTerm (_, term) -> strip_attributes term
213     | Magic m -> Magic (visit_magic strip_attributes m)
214     | Variable _ as t -> t
215     | t -> assert false
216   in
217   visit_ast ~special_k strip_attributes t
218
219 let meta_names_of_term term =
220   let rec names = ref [] in
221   let add_name n =
222     if List.mem n !names then ()
223     else names := n :: !names
224   in
225   let rec aux = function
226     | AttributedTerm (_, term) -> aux term
227     | Appl terms -> List.iter aux terms
228     | Binder (_, _, body) -> aux body
229     | Case (term, indty, outty_opt, patterns) ->
230         aux term ;
231         aux_opt outty_opt ;
232         List.iter aux_branch patterns
233     | LetIn (_, t1, t2) ->
234         aux t1 ;
235         aux t2
236     | LetRec (_, definitions, body) ->
237         List.iter aux_definition definitions ;
238         aux body
239     | Uri (_, Some substs) -> aux_substs substs
240     | Ident (_, Some substs) -> aux_substs substs
241     | Meta (_, substs) -> aux_meta_substs substs
242
243     | Implicit
244     | Ident _
245     | Num _
246     | Sort _
247     | Symbol _
248     | Uri _
249     | UserInput -> ()
250
251     | Magic magic -> aux_magic magic
252     | Variable var -> aux_variable var
253
254     | _ -> assert false
255   and aux_opt = function
256     | Some term -> aux term
257     | None -> ()
258   and aux_capture_var (_, ty_opt) = aux_opt ty_opt
259   and aux_branch (pattern, term) =
260     aux_pattern pattern ;
261     aux term
262   and aux_pattern (head, vars) = 
263     List.iter aux_capture_var vars
264   and aux_definition (var, term, i) =
265     aux_capture_var var ;
266     aux term
267   and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
268   and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
269   and aux_variable = function
270     | NumVar name -> add_name name
271     | IdentVar name -> add_name name
272     | TermVar name -> add_name name
273     | FreshVar _ -> ()
274     | Ascription _ -> assert false
275   and aux_magic = function
276     | Default (t1, t2)
277     | Fold (_, t1, _, t2) ->
278         aux t1 ;
279         aux t2
280     | If (t1, t2, t3) ->
281         aux t1 ;
282         aux t2 ;
283         aux t3
284     | Fail -> ()
285     | _ -> assert false
286   in
287   aux term ;
288   !names
289
290 let rectangular matrix =
291   let columns = Array.length matrix.(0) in
292   try
293     Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
294     true
295   with Exit -> false
296
297 let ncombine ll =
298   let matrix = Array.of_list (List.map Array.of_list ll) in
299   assert (rectangular matrix);
300   let rows = Array.length matrix in
301   let columns = Array.length matrix.(0) in
302   let lists = ref [] in
303   for j = 0 to columns - 1 do
304     let l = ref [] in
305     for i = 0 to rows - 1 do
306       l := matrix.(i).(j) :: !l
307     done;
308     lists := List.rev !l :: !lists
309   done;
310   List.rev !lists
311
312 let string_of_literal = function
313   | `Symbol s
314   | `Keyword s
315   | `Number s -> s
316
317 let boxify = function
318   | [ a ] -> a
319   | l -> Layout (Box ((H, false, false), l))
320
321 let find_appl_pattern_uris ap =
322   let rec aux acc =
323     function
324     | UriPattern uri ->
325         (try
326           ignore (List.find (fun uri' -> UriManager.eq uri uri') acc);
327           acc
328         with Not_found -> uri :: acc)
329     | VarPattern _ -> acc
330     | ApplPattern apl -> List.fold_left aux acc apl
331   in
332   aux [] ap
333
334 let rec find_branch =
335   function
336       Magic (If (_, Magic Fail, t)) -> find_branch t
337     | Magic (If (_, t, _)) -> find_branch t
338     | t -> t