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