]> 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     | 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
156 let visit_magic k = function
157   | List0 (t, l) -> List0 (k t, l)
158   | List1 (t, l) -> List1 (k t, l)
159   | Opt t -> Opt (k t)
160   | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2)
161   | Default (t1, t2) -> Default (k t1, k t2)
162   | If (t1, t2, t3) -> If (k t1, k t2, k t3)
163   | Fail -> Fail
164
165 let variables_of_term t =
166   let rec vars = ref [] in
167   let add_variable v =
168     if List.mem v !vars then ()
169     else vars := v :: !vars
170   in
171   let rec aux = function
172     | Magic m -> Magic (visit_magic aux m)
173     | Layout l -> Layout (visit_layout aux l)
174     | Variable v -> Variable (aux_variable v)
175     | Literal _ as t -> t
176     | AttributedTerm (_, t) -> aux t
177     | t -> visit_ast aux t
178   and aux_variable = function
179     | (NumVar _
180       | IdentVar _
181       | TermVar _) as t ->
182         add_variable t ;
183         t
184     | FreshVar _ as t -> t
185     | Ascription _ -> assert false
186   in
187     ignore (aux t) ;
188     !vars
189
190 let names_of_term t =
191   let aux = function
192     | NumVar s
193     | IdentVar s
194     | TermVar s -> s
195     | _ -> assert false
196   in
197     List.map aux (variables_of_term t)
198
199 let keywords_of_term t =
200   let rec keywords = ref [] in
201   let add_keyword k = keywords := k :: !keywords in
202   let rec aux = function
203     | AttributedTerm (_, t) -> aux t
204     | Layout l -> Layout (visit_layout aux l)
205     | Literal (`Keyword k) as t ->
206         add_keyword k;
207         t
208     | Literal _ as t -> t
209     | Magic m -> Magic (visit_magic aux m)
210     | Variable _ as v -> v
211     | t -> visit_ast aux t
212   in
213     ignore (aux t) ;
214     !keywords
215
216 let rec strip_attributes t =
217   let special_k = function
218     | AttributedTerm (_, term) -> strip_attributes term
219     | Magic m -> Magic (visit_magic strip_attributes m)
220     | Variable _ as t -> t
221     | t -> assert false
222   in
223   visit_ast ~special_k strip_attributes t
224
225 let meta_names_of_term term =
226   let rec names = ref [] in
227   let add_name n =
228     if List.mem n !names then ()
229     else names := n :: !names
230   in
231   let rec aux = function
232     | AttributedTerm (_, term) -> aux term
233     | Appl terms -> List.iter aux terms
234     | Binder (_, _, body) -> aux body
235     | Case (term, indty, outty_opt, patterns) ->
236         aux term ;
237         aux_opt outty_opt ;
238         List.iter aux_branch patterns
239     | LetIn (_, t1, t2) ->
240         aux t1 ;
241         aux t2
242     | LetRec (_, definitions, body) ->
243         List.iter aux_definition definitions ;
244         aux body
245     | Uri (_, Some substs) -> aux_substs substs
246     | Ident (_, Some substs) -> aux_substs substs
247     | Meta (_, substs) -> aux_meta_substs substs
248
249     | Implicit
250     | Ident _
251     | Num _
252     | Sort _
253     | Symbol _
254     | Uri _
255     | UserInput -> ()
256
257     | Magic magic -> aux_magic magic
258     | Variable var -> aux_variable var
259
260     | _ -> assert false
261   and aux_opt = function
262     | Some term -> aux term
263     | None -> ()
264   and aux_capture_var (_, ty_opt) = aux_opt ty_opt
265   and aux_branch (pattern, term) =
266     aux_pattern pattern ;
267     aux term
268   and aux_pattern (head, vars) = 
269     List.iter aux_capture_var vars
270   and aux_definition (var, term, i) =
271     aux_capture_var var ;
272     aux term
273   and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
274   and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
275   and aux_variable = function
276     | NumVar name -> add_name name
277     | IdentVar name -> add_name name
278     | TermVar name -> add_name name
279     | FreshVar _ -> ()
280     | Ascription _ -> assert false
281   and aux_magic = function
282     | Default (t1, t2)
283     | Fold (_, t1, _, t2) ->
284         aux t1 ;
285         aux t2
286     | If (t1, t2, t3) ->
287         aux t1 ;
288         aux t2 ;
289         aux t3
290     | Fail -> ()
291     | _ -> assert false
292   in
293   aux term ;
294   !names
295
296 let rectangular matrix =
297   let columns = Array.length matrix.(0) in
298   try
299     Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
300     true
301   with Exit -> false
302
303 let ncombine ll =
304   let matrix = Array.of_list (List.map Array.of_list ll) in
305   assert (rectangular matrix);
306   let rows = Array.length matrix in
307   let columns = Array.length matrix.(0) in
308   let lists = ref [] in
309   for j = 0 to columns - 1 do
310     let l = ref [] in
311     for i = 0 to rows - 1 do
312       l := matrix.(i).(j) :: !l
313     done;
314     lists := List.rev !l :: !lists
315   done;
316   List.rev !lists
317
318 let string_of_literal = function
319   | `Symbol s
320   | `Keyword s
321   | `Number s -> s
322
323 let boxify = function
324   | [ a ] -> a
325   | l -> Layout (Box ((H, false, false), l))
326
327 let find_appl_pattern_uris ap =
328   let rec aux acc =
329     function
330     | UriPattern uri ->
331         (try
332           ignore (List.find (fun uri' -> UriManager.eq uri uri') acc);
333           acc
334         with Not_found -> uri :: acc)
335     | VarPattern _ -> acc
336     | ApplPattern apl -> List.fold_left aux acc apl
337   in
338   aux [] ap
339
340 let rec find_branch =
341   function
342       Magic (If (_, Magic Fail, t)) -> find_branch t
343     | Magic (If (_, t, _)) -> find_branch t
344     | t -> t