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