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