]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/content/notationUtil.ml
d047eb58e17f66a649ca89276c0ebacf6cc8c715
[helm.git] / matitaB / components / content / notationUtil.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 = NotationPt
29
30 let visit_ast ?(special_k = fun _ -> assert false)
31   ?(clear_interpretation= false)
32   ?(map_xref_option= fun x -> x) ?(map_case_indty= fun x -> x) 
33   ?(map_case_outtype=
34       fun k x -> match x with None -> None | Some x -> Some (k x)) 
35   k 
36 =
37   let rec aux = function
38     | Ast.Appl terms -> Ast.Appl (List.map k terms)
39     | Ast.Binder (kind, var, body) ->
40         Ast.Binder (kind, aux_capture_variable var, k body) 
41     | Ast.Case (term, indtype, typ, patterns) ->
42         Ast.Case (k term, map_case_indty indtype, map_case_outtype k typ,
43           aux_patterns map_xref_option patterns)
44     | Ast.Cast (t1, t2) -> Ast.Cast (k t1, k t2)
45     | Ast.LetIn (var, t1, t3) ->
46         Ast.LetIn (aux_capture_variable var, k t1, k t3)
47     | Ast.LetRec (kind, definitions, term) ->
48         let definitions =
49           List.map
50             (fun (params, var, ty, decrno) ->
51               List.map aux_capture_variable params, aux_capture_variable var,
52               k ty, decrno)
53             definitions
54         in
55         Ast.LetRec (kind, definitions, k term)
56     | Ast.Meta (index, substs) -> Ast.Meta (index, List.map aux_opt substs)
57     | (Ast.AttributedTerm _
58       | Ast.Layout _
59       | Ast.Literal _
60       | Ast.Magic _
61       | Ast.Variable _) as t -> special_k t
62     | Ast.Ident (id,_) when clear_interpretation -> Ast.Ident (id,`Ambiguous)
63     | Ast.Symbol (s,_) when clear_interpretation -> Ast.Symbol (s,None)
64     | Ast.Num (n,_) when clear_interpretation -> Ast.Num (n,None)
65     | ( Ast.Ident _ 
66       | Ast.Symbol _
67       | Ast.Num _
68       | Ast.NRef _
69       | Ast.NCic _
70       | Ast.Implicit _
71       | Ast.Sort _
72       | Ast.UserInput) as t -> t
73   and aux_opt = function
74     | None -> None
75     | Some term -> Some (k term)
76   and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
77   and aux_patterns k_xref patterns = List.map (aux_pattern k_xref) patterns
78   and aux_pattern k_xref =
79    function
80       Ast.Pattern (head, hrefs, vars), term ->
81         Ast.Pattern (head, k_xref hrefs, List.map aux_capture_variable vars), k term
82     | Ast.Wildcard, term -> Ast.Wildcard, k term
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   | Ast.Maction terms -> Ast.Maction (List.map k terms)
103
104 let visit_magic k = function
105   | Ast.List0 (t, l) -> Ast.List0 (k t, l)
106   | Ast.List1 (t, l) -> Ast.List1 (k t, l)
107   | Ast.Opt t -> Ast.Opt (k t)
108   | Ast.Fold (kind, t1, names, t2) -> Ast.Fold (kind, k t1, names, k t2)
109   | Ast.Default (t1, t2) -> Ast.Default (k t1, k t2)
110   | Ast.If (t1, t2, t3) -> Ast.If (k t1, k t2, k t3)
111   | Ast.Fail -> Ast.Fail
112
113 let visit_variable k = function
114   | Ast.NumVar _
115   | Ast.IdentVar _
116   | Ast.TermVar _
117   | Ast.FreshVar _ as t -> t
118   | Ast.Ascription (t, s) -> Ast.Ascription (k t, s)
119
120 let variables_of_term t =
121   let rec vars = ref [] in
122   let add_variable v =
123     if List.mem v !vars then ()
124     else vars := v :: !vars
125   in
126   let rec aux = function
127     | Ast.Magic m -> Ast.Magic (visit_magic aux m)
128     | Ast.Layout l -> Ast.Layout (visit_layout aux l)
129     | Ast.Variable v -> Ast.Variable (aux_variable v)
130     | Ast.Literal _ as t -> t
131     | Ast.AttributedTerm (_, t) -> aux t
132     | t -> visit_ast aux t
133   and aux_variable = function
134     | (Ast.NumVar _
135       | Ast.IdentVar _
136       | Ast.TermVar _) as t ->
137         add_variable t ;
138         t
139     | Ast.FreshVar _ as t -> t
140     | Ast.Ascription _ -> assert false
141   in
142     ignore (aux t) ;
143     !vars
144
145 let names_of_term t =
146   let aux = function
147     | Ast.NumVar s
148     | Ast.IdentVar s
149     | Ast.TermVar (s,_) -> s
150     | _ -> assert false
151   in
152     List.map aux (variables_of_term t)
153
154 let keywords_of_term t =
155   let rec keywords = ref [] in
156   let add_keyword k = keywords := k :: !keywords in
157   let rec aux = function
158     | Ast.AttributedTerm (_, t) -> aux t
159     | Ast.Layout l -> Ast.Layout (visit_layout aux l)
160     | Ast.Literal (`Keyword (k,_)) as t ->
161         add_keyword k;
162         t
163     | Ast.Literal _ as t -> t
164     | Ast.Magic m -> Ast.Magic (visit_magic aux m)
165     | Ast.Variable _ as v -> v
166     | t -> visit_ast aux t
167   in
168     ignore (aux t) ;
169     !keywords
170
171 let rec strip_attributes t =
172   let special_k = function
173     | Ast.AttributedTerm (_, term) -> strip_attributes term
174     | Ast.Magic m -> Ast.Magic (visit_magic strip_attributes m)
175     | Ast.Variable _ as t -> t
176     | t -> assert false
177   in
178   visit_ast ~special_k strip_attributes t
179
180 let rec get_idrefs =
181   function
182   | Ast.Symbol (_,Some (_,desc)) -> [desc]
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.Meta (_, substs) -> aux_meta_substs substs
206
207     | Ast.Implicit _
208     | Ast.Ident _
209     | Ast.Num _
210     | Ast.Sort _
211     | Ast.Symbol _
212     | Ast.UserInput -> ()
213
214     | Ast.Magic magic -> aux_magic magic
215     | Ast.Variable var -> aux_variable var
216
217     | _ -> assert false
218   and aux_opt = function
219     | Some term -> aux term
220     | None -> ()
221   and aux_capture_var (_, ty_opt) = aux_opt ty_opt
222   and aux_branch (pattern, term) =
223     aux_pattern pattern ;
224     aux term
225   and aux_pattern =
226    function
227       Ast.Pattern (head, _, vars) -> List.iter aux_capture_var vars
228     | Ast.Wildcard -> ()
229   and aux_definition (params, var, term, decrno) =
230     List.iter aux_capture_var params ;
231     aux_capture_var var ;
232     aux term
233   and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
234   and aux_variable = function
235     | Ast.NumVar name -> add_name name
236     | Ast.IdentVar name -> add_name name
237     | Ast.TermVar (name,_) -> add_name name
238     | Ast.FreshVar _ -> ()
239     | Ast.Ascription _ -> assert false
240   and aux_magic = function
241     | Ast.Default (t1, t2)
242     | Ast.Fold (_, t1, _, t2) ->
243         aux t1 ;
244         aux t2
245     | Ast.If (t1, t2, t3) ->
246         aux t1 ;
247         aux t2 ;
248         aux t3
249     | Ast.Fail -> ()
250     | _ -> assert false
251   in
252   aux term ;
253   !names
254
255 let rectangular matrix =
256   let columns = Array.length matrix.(0) in
257   try
258     Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
259     true
260   with Exit -> false
261
262 let ncombine ll =
263   let matrix = Array.of_list (List.map Array.of_list ll) in
264   assert (rectangular matrix);
265   let rows = Array.length matrix in
266   let columns = Array.length matrix.(0) in
267   let lists = ref [] in
268   for j = 0 to columns - 1 do
269     let l = ref [] in
270     for i = 0 to rows - 1 do
271       l := matrix.(i).(j) :: !l
272     done;
273     lists := List.rev !l :: !lists
274   done;
275   List.rev !lists
276
277 let href s = function
278  | None, None -> s
279  | Some u, None -> "<A href=\"" ^ u ^ "\">" ^ s ^ "</A>"
280  | None, Some desc -> 
281      "<A href=\"cic:/fakeuri.con\" title=\"" ^ desc ^ "\">" ^ s ^ "</A>"
282  | Some u, Some desc -> 
283      "<A href=\"" ^ u ^ "\" title=\"" ^ desc ^ "\">" ^ s ^ "</A>"
284
285 let string_of_literal = function
286   | `Symbol (s,x)
287   | `Keyword (s,x)
288   | `Number (s,x) -> href s x
289
290 let html_of_literal = function
291   | `Symbol (s,x)
292   | `Keyword (s,x)
293   | `Number (s,x) -> 
294       href (Netencoding.Html.encode ~in_enc:`Enc_utf8 () 
295         (Utf8Macro.unicode_of_tex s)) x
296
297 let boxify = function
298   | [ a ] -> a
299   | l -> Ast.Layout (Ast.Box ((Ast.H, false, false), l))
300
301 let unboxify = function
302   | Ast.Layout (Ast.Box ((Ast.H, false, false), [ a ])) -> a
303   | l -> l
304
305 let group = function
306   | [ a ] -> a
307   | l -> Ast.Layout (Ast.Group l)
308
309 let ungroup =
310   let rec aux acc =
311     function
312         [] -> List.rev acc
313       | Ast.Layout (Ast.Group terms) :: terms' -> aux acc (terms @ terms')
314       | term :: terms -> aux (term :: acc) terms
315   in
316     aux []
317
318 let dress ~sep:sauce =
319   let rec aux =
320     function
321       | [] -> []
322       | [hd] -> [hd]
323       | hd :: tl -> hd :: sauce :: aux tl
324   in
325     aux
326
327 let dressn ~sep:sauces =
328   let rec aux =
329     function
330       | [] -> []
331       | [hd] -> [hd]
332       | hd :: tl -> hd :: sauces @ aux tl
333   in
334     aux
335
336 let find_appl_pattern_uris ap =
337   let rec aux acc =
338     function
339     | Ast.NRefPattern nref -> nref :: acc
340     | Ast.ImplicitPattern
341     | Ast.VarPattern _ -> acc
342     | Ast.ApplPattern apl -> List.fold_left aux acc apl
343   in
344   let uris = aux [] ap in
345   HExtlib.list_uniq (List.fast_sort NReference.compare uris)
346
347 let rec find_branch =
348   function
349       Ast.Magic (Ast.If (_, Ast.Magic Ast.Fail, t)) -> find_branch t
350     | Ast.Magic (Ast.If (_, t, _)) -> find_branch t
351     | t -> t
352
353 let fresh_index = ref ~-1
354
355 type notation_id = int
356
357 let fresh_id () =
358   incr fresh_index;
359   !fresh_index
360
361   (* TODO ensure that names generated by fresh_var do not clash with user's *)
362   (* FG: "η" is not an identifier (it is rendered, but not be parsed) *)
363 let fresh_name () = "eta" ^ string_of_int (fresh_id ())
364
365 (* XXX FIXME: was used to add instance indices to symbols/numbers
366  * do we even need a similar operation? 
367  * currently an identity function! *)
368 let rec freshen_term ?(index = ref 0) term =
369   let freshen_term = freshen_term ~index in
370   let fresh_instance () = incr index; !index in
371   let special_k = function
372     | Ast.AttributedTerm (attr, t) -> Ast.AttributedTerm (attr, freshen_term t)
373     | Ast.Layout l -> Ast.Layout (visit_layout freshen_term l)
374     | Ast.Magic m -> Ast.Magic (visit_magic freshen_term m)
375     | Ast.Variable v -> Ast.Variable (visit_variable freshen_term v)
376     | Ast.Literal _ as t -> t
377     | _ -> assert false
378   in
379   match term with
380   | Ast.Symbol (s, x) -> Ast.Symbol (s, x (* fresh_instance () *))
381   | Ast.Num (s, x) -> Ast.Num (s, x (* fresh_instance () *))
382   | t -> visit_ast ~special_k freshen_term t
383
384 let freshen_obj obj =
385   let index = ref 0 in
386   let freshen_term = freshen_term ~index in
387   let freshen_name_ty = List.map (fun (n, t) -> (n, freshen_term t)) in
388   let freshen_name_ty_b = List.map (fun (n,t,b,i) -> (n,freshen_term t,b,i)) in
389   let freshen_capture_variables =
390    List.map (fun (n,t) -> (freshen_term n, HExtlib.map_option freshen_term t))
391   in
392   match obj with
393   | NotationPt.Inductive (params, indtypes) ->
394       let indtypes =
395         List.map
396           (fun (n, co, ty, ctors) -> (n, co, ty, freshen_name_ty ctors))
397           indtypes
398       in
399       NotationPt.Inductive (freshen_capture_variables params, indtypes)
400   | NotationPt.Theorem (flav, n, t, ty_opt,p) ->
401       let ty_opt =
402         match ty_opt with None -> None | Some ty -> Some (freshen_term ty)
403       in
404       NotationPt.Theorem (flav, n, freshen_term t, ty_opt,p)
405   | NotationPt.Record (params, n, ty, fields) ->
406       NotationPt.Record (freshen_capture_variables params, n,
407         freshen_term ty, freshen_name_ty_b fields)
408
409 let freshen_term = freshen_term ?index:None
410
411 let rec refresh_uri_in_term ~refresh_uri_in_term:refresh_in_cic
412  ~refresh_uri_in_reference
413 =
414  function
415     NotationPt.NRef ref -> NotationPt.NRef (refresh_uri_in_reference ref)
416   | NotationPt.NCic t -> NotationPt.NCic (refresh_in_cic t)
417   | t ->
418      visit_ast
419      (refresh_uri_in_term ~refresh_uri_in_term:refresh_in_cic
420        ~refresh_uri_in_reference) t
421       ~special_k:(fun x -> x)
422       ~map_xref_option:(function Some ref -> Some (refresh_uri_in_reference ref)
423                          | x -> x)
424       ~map_case_indty:(function (Some (s,Some ref)) -> Some (s, Some
425                         (refresh_uri_in_reference ref)) | x -> x)
426 ;;