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