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