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