]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationUtil.ml
snapshot, notably:
[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     | Appl terms -> Appl (List.map k terms)
104     | Binder (kind, var, body) ->
105         Binder (kind, aux_capture_variable var, k body) 
106     | Case (term, indtype, typ, patterns) ->
107         Case (k term, indtype, aux_opt typ, aux_patterns patterns)
108     | LetIn (var, t1, t2) -> LetIn (aux_capture_variable var, k t1, k t2)
109     | LetRec (kind, definitions, term) ->
110         let definitions =
111           List.map
112             (fun (var, ty, n) -> aux_capture_variable var, k ty, n)
113             definitions
114         in
115         LetRec (kind, definitions, k term)
116     | Ident (name, Some substs) -> Ident (name, Some (aux_substs substs))
117     | Uri (name, Some substs) -> Uri (name, Some (aux_substs substs))
118     | Meta (index, substs) -> Meta (index, List.map aux_opt substs)
119     | (AttributedTerm _
120       | Layout _
121       | Literal _
122       | Magic _
123       | Variable _) as t -> special_k t
124     | (Ident _
125       | Implicit
126       | Num _
127       | Sort _
128       | Symbol _
129       | Uri _
130       | UserInput) as t -> t
131   and aux_opt = function
132     | None -> None
133     | Some term -> Some (k term)
134   and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
135   and aux_patterns patterns = List.map aux_pattern patterns
136   and aux_pattern ((head, vars), term) =
137     ((head, List.map aux_capture_variable vars), k term)
138   and aux_subst (name, term) = (name, k term)
139   and aux_substs substs = List.map aux_subst substs
140   in
141   aux
142
143 let visit_layout k = function
144   | Sub (t1, t2) -> Sub (k t1, k t2)
145   | Sup (t1, t2) -> Sup (k t1, k t2)
146   | Below (t1, t2) -> Below (k t1, k t2)
147   | Above (t1, t2) -> Above (k t1, k t2)
148   | Over (t1, t2) -> Over (k t1, k t2)
149   | Atop (t1, t2) -> Atop (k t1, k t2)
150   | Frac (t1, t2) -> Frac (k t1, k t2)
151   | Sqrt t -> Sqrt (k t)
152   | Root (arg, index) -> Root (k arg, k index)
153   | Break -> Break
154   | Box (kind, terms) -> Box (kind, List.map k terms)
155   | Group terms -> Group (List.map k terms)
156
157 let visit_magic k = function
158   | List0 (t, l) -> List0 (k t, l)
159   | List1 (t, l) -> List1 (k t, l)
160   | Opt t -> Opt (k t)
161   | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2)
162   | Default (t1, t2) -> Default (k t1, k t2)
163   | If (t1, t2, t3) -> If (k t1, k t2, k t3)
164   | Fail -> Fail
165
166 let variables_of_term t =
167   let rec vars = ref [] in
168   let add_variable v =
169     if List.mem v !vars then ()
170     else vars := v :: !vars
171   in
172   let rec aux = function
173     | Magic m -> Magic (visit_magic aux m)
174     | Layout l -> Layout (visit_layout aux l)
175     | Variable v -> Variable (aux_variable v)
176     | Literal _ as t -> t
177     | AttributedTerm (_, t) -> aux t
178     | t -> visit_ast aux t
179   and aux_variable = function
180     | (NumVar _
181       | IdentVar _
182       | TermVar _) as t ->
183         add_variable t ;
184         t
185     | FreshVar _ as t -> t
186     | Ascription _ -> assert false
187   in
188     ignore (aux t) ;
189     !vars
190
191 let names_of_term t =
192   let aux = function
193     | NumVar s
194     | IdentVar s
195     | TermVar s -> s
196     | _ -> assert false
197   in
198     List.map aux (variables_of_term t)
199
200 let keywords_of_term t =
201   let rec keywords = ref [] in
202   let add_keyword k = keywords := k :: !keywords in
203   let rec aux = function
204     | AttributedTerm (_, t) -> aux t
205     | Layout l -> Layout (visit_layout aux l)
206     | Literal (`Keyword k) as t ->
207         add_keyword k;
208         t
209     | Literal _ as t -> t
210     | Magic m -> Magic (visit_magic aux m)
211     | Variable _ as v -> v
212     | t -> visit_ast aux t
213   in
214     ignore (aux t) ;
215     !keywords
216
217 let rec strip_attributes t =
218   let special_k = function
219     | AttributedTerm (_, term) -> strip_attributes term
220     | Magic m -> Magic (visit_magic strip_attributes m)
221     | Variable _ as t -> t
222     | t -> assert false
223   in
224   visit_ast ~special_k strip_attributes t
225
226 let meta_names_of_term term =
227   let rec names = ref [] in
228   let add_name n =
229     if List.mem n !names then ()
230     else names := n :: !names
231   in
232   let rec aux = function
233     | AttributedTerm (_, term) -> aux term
234     | Appl terms -> List.iter aux terms
235     | Binder (_, _, body) -> aux body
236     | Case (term, indty, outty_opt, patterns) ->
237         aux term ;
238         aux_opt outty_opt ;
239         List.iter aux_branch patterns
240     | LetIn (_, t1, t2) ->
241         aux t1 ;
242         aux t2
243     | LetRec (_, definitions, body) ->
244         List.iter aux_definition definitions ;
245         aux body
246     | Uri (_, Some substs) -> aux_substs substs
247     | Ident (_, Some substs) -> aux_substs substs
248     | Meta (_, substs) -> aux_meta_substs substs
249
250     | Implicit
251     | Ident _
252     | Num _
253     | Sort _
254     | Symbol _
255     | Uri _
256     | UserInput -> ()
257
258     | Magic magic -> aux_magic magic
259     | Variable var -> aux_variable var
260
261     | _ -> assert false
262   and aux_opt = function
263     | Some term -> aux term
264     | None -> ()
265   and aux_capture_var (_, ty_opt) = aux_opt ty_opt
266   and aux_branch (pattern, term) =
267     aux_pattern pattern ;
268     aux term
269   and aux_pattern (head, vars) = 
270     List.iter aux_capture_var vars
271   and aux_definition (var, term, i) =
272     aux_capture_var var ;
273     aux term
274   and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
275   and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
276   and aux_variable = function
277     | NumVar name -> add_name name
278     | IdentVar name -> add_name name
279     | TermVar name -> add_name name
280     | FreshVar _ -> ()
281     | Ascription _ -> assert false
282   and aux_magic = function
283     | Default (t1, t2)
284     | Fold (_, t1, _, t2) ->
285         aux t1 ;
286         aux t2
287     | If (t1, t2, t3) ->
288         aux t1 ;
289         aux t2 ;
290         aux t3
291     | Fail -> ()
292     | _ -> assert false
293   in
294   aux term ;
295   !names
296
297 let rectangular matrix =
298   let columns = Array.length matrix.(0) in
299   try
300     Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
301     true
302   with Exit -> false
303
304 let ncombine ll =
305   let matrix = Array.of_list (List.map Array.of_list ll) in
306   assert (rectangular matrix);
307   let rows = Array.length matrix in
308   let columns = Array.length matrix.(0) in
309   let lists = ref [] in
310   for j = 0 to columns - 1 do
311     let l = ref [] in
312     for i = 0 to rows - 1 do
313       l := matrix.(i).(j) :: !l
314     done;
315     lists := List.rev !l :: !lists
316   done;
317   List.rev !lists
318
319 let string_of_literal = function
320   | `Symbol s
321   | `Keyword s
322   | `Number s -> s
323
324 let boxify = function
325   | [ a ] -> a
326   | l -> Layout (Box ((H, false, false), l))
327
328 let group = function
329   | [ a ] -> a
330   | l -> Layout (Group l)
331
332 let ungroup =
333   let rec aux acc =
334     function
335         [] -> List.rev acc
336       | Layout (Group terms) :: terms' -> aux acc (terms @ terms')
337       | term :: terms -> aux (term :: acc) terms
338   in
339     aux []
340
341 let dress sauce =
342   let rec aux =
343     function
344       | [] -> []
345       | [hd] -> [hd]
346       | hd :: tl -> hd :: sauce :: aux tl
347   in
348     aux
349
350 let find_appl_pattern_uris ap =
351   let rec aux acc =
352     function
353     | GrafiteAst.UriPattern uri ->
354         (try
355           ignore (List.find (fun uri' -> UriManager.eq uri uri') acc);
356           acc
357         with Not_found -> uri :: acc)
358     | GrafiteAst.VarPattern _ -> acc
359     | GrafiteAst.ApplPattern apl -> List.fold_left aux acc apl
360   in
361   aux [] ap
362
363 let rec find_branch =
364   function
365       Magic (If (_, Magic Fail, t)) -> find_branch t
366     | Magic (If (_, t, _)) -> find_branch t
367     | t -> t