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