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