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