]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/content/notationUtil.ml
First commit with new (incomplete) disambiguation engine.
[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   ?(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.Meta (index, substs) -> Ast.Meta (index, List.map aux_opt substs)
56     | (Ast.AttributedTerm _
57       | Ast.Layout _
58       | Ast.Literal _
59       | Ast.Magic _
60       | Ast.Variable _) as t -> special_k t
61     | (Ast.Ident _
62       | Ast.NRef _
63       | Ast.NCic _
64       | Ast.Implicit _
65       | Ast.Num _
66       | Ast.Sort _
67       | Ast.Symbol _
68       | Ast.UserInput) as t -> t
69   and aux_opt = function
70     | None -> None
71     | Some term -> Some (k term)
72   and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
73   and aux_patterns k_xref patterns = List.map (aux_pattern k_xref) patterns
74   and aux_pattern k_xref =
75    function
76       Ast.Pattern (head, hrefs, vars), term ->
77         Ast.Pattern (head, k_xref hrefs, List.map aux_capture_variable vars), k term
78     | Ast.Wildcard, term -> Ast.Wildcard, k term
79   in
80   aux
81
82 let visit_layout k = function
83   | Ast.Sub (t1, t2) -> Ast.Sub (k t1, k t2)
84   | Ast.Sup (t1, t2) -> Ast.Sup (k t1, k t2)
85   | Ast.Below (t1, t2) -> Ast.Below (k t1, k t2)
86   | Ast.Above (t1, t2) -> Ast.Above (k t1, k t2)
87   | Ast.Over (t1, t2) -> Ast.Over (k t1, k t2)
88   | Ast.Atop (t1, t2) -> Ast.Atop (k t1, k t2)
89   | Ast.Frac (t1, t2) -> Ast.Frac (k t1, k t2)
90   | Ast.InfRule (t1, t2, t3) -> Ast.InfRule (k t1, k t2, k t3)
91   | Ast.Sqrt t -> Ast.Sqrt (k t)
92   | Ast.Root (arg, index) -> Ast.Root (k arg, k index)
93   | Ast.Break -> Ast.Break
94   | Ast.Box (kind, terms) -> Ast.Box (kind, List.map k terms)
95   | Ast.Group terms -> Ast.Group (List.map k terms)
96   | Ast.Mstyle (l, term) -> Ast.Mstyle (l, List.map k term)
97   | Ast.Mpadded (l, term) -> Ast.Mpadded (l, List.map k term)
98   | Ast.Maction terms -> Ast.Maction (List.map k terms)
99
100 let visit_magic k = function
101   | Ast.List0 (t, l) -> Ast.List0 (k t, l)
102   | Ast.List1 (t, l) -> Ast.List1 (k t, l)
103   | Ast.Opt t -> Ast.Opt (k t)
104   | Ast.Fold (kind, t1, names, t2) -> Ast.Fold (kind, k t1, names, k t2)
105   | Ast.Default (t1, t2) -> Ast.Default (k t1, k t2)
106   | Ast.If (t1, t2, t3) -> Ast.If (k t1, k t2, k t3)
107   | Ast.Fail -> Ast.Fail
108
109 let visit_variable k = function
110   | Ast.NumVar _
111   | Ast.IdentVar _
112   | Ast.TermVar _
113   | Ast.FreshVar _ as t -> t
114   | Ast.Ascription (t, s) -> Ast.Ascription (k t, s)
115
116 let variables_of_term t =
117   let rec vars = ref [] in
118   let add_variable v =
119     if List.mem v !vars then ()
120     else vars := v :: !vars
121   in
122   let rec aux = function
123     | Ast.Magic m -> Ast.Magic (visit_magic aux m)
124     | Ast.Layout l -> Ast.Layout (visit_layout aux l)
125     | Ast.Variable v -> Ast.Variable (aux_variable v)
126     | Ast.Literal _ as t -> t
127     | Ast.AttributedTerm (_, t) -> aux t
128     | t -> visit_ast aux t
129   and aux_variable = function
130     | (Ast.NumVar _
131       | Ast.IdentVar _
132       | Ast.TermVar _) as t ->
133         add_variable t ;
134         t
135     | Ast.FreshVar _ as t -> t
136     | Ast.Ascription _ -> assert false
137   in
138     ignore (aux t) ;
139     !vars
140
141 let names_of_term t =
142   let aux = function
143     | Ast.NumVar s
144     | Ast.IdentVar s
145     | Ast.TermVar (s,_) -> s
146     | _ -> assert false
147   in
148     List.map aux (variables_of_term t)
149
150 let keywords_of_term t =
151   let rec keywords = ref [] in
152   let add_keyword k = keywords := k :: !keywords in
153   let rec aux = function
154     | Ast.AttributedTerm (_, t) -> aux t
155     | Ast.Layout l -> Ast.Layout (visit_layout aux l)
156     | Ast.Literal (`Keyword k) as t ->
157         add_keyword k;
158         t
159     | Ast.Literal _ as t -> t
160     | Ast.Magic m -> Ast.Magic (visit_magic aux m)
161     | Ast.Variable _ as v -> v
162     | t -> visit_ast aux t
163   in
164     ignore (aux t) ;
165     !keywords
166
167 let rec strip_attributes t =
168   let special_k = function
169     | Ast.AttributedTerm (_, term) -> strip_attributes term
170     | Ast.Magic m -> Ast.Magic (visit_magic strip_attributes m)
171     | Ast.Variable _ as t -> t
172     | t -> assert false
173   in
174   visit_ast ~special_k strip_attributes t
175
176 let rec get_idrefs =
177   function
178   | Ast.AttributedTerm (`IdRef id, t) -> id :: get_idrefs t
179   | Ast.AttributedTerm (_, t) -> get_idrefs t
180   | _ -> []
181
182 let meta_names_of_term term =
183   let rec names = ref [] in
184   let add_name n =
185     if List.mem n !names then ()
186     else names := n :: !names
187   in
188   let rec aux = function
189     | Ast.AttributedTerm (_, term) -> aux term
190     | Ast.Appl terms -> List.iter aux terms
191     | Ast.Binder (_, _, body) -> aux body
192     | Ast.Case (term, indty, outty_opt, patterns) ->
193         aux term ;
194         aux_opt outty_opt ;
195         List.iter aux_branch patterns
196     | Ast.LetIn (_, t1, t3) ->
197         aux t1 ;
198         aux t3
199     | Ast.LetRec (_, definitions, body) ->
200         List.iter aux_definition definitions ;
201         aux body
202     | Ast.Meta (_, substs) -> aux_meta_substs substs
203
204     | Ast.Implicit _
205     | Ast.Ident _
206     | Ast.Num _
207     | Ast.Sort _
208     | Ast.Symbol _
209     | Ast.UserInput -> ()
210
211     | Ast.Magic magic -> aux_magic magic
212     | Ast.Variable var -> aux_variable var
213
214     | _ -> assert false
215   and aux_opt = function
216     | Some term -> aux term
217     | None -> ()
218   and aux_capture_var (_, ty_opt) = aux_opt ty_opt
219   and aux_branch (pattern, term) =
220     aux_pattern pattern ;
221     aux term
222   and aux_pattern =
223    function
224       Ast.Pattern (head, _, vars) -> List.iter aux_capture_var vars
225     | Ast.Wildcard -> ()
226   and aux_definition (params, var, term, decrno) =
227     List.iter aux_capture_var params ;
228     aux_capture_var var ;
229     aux term
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.NRefPattern nref -> nref :: 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 NReference.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 fresh_index = ref ~-1
336
337 type notation_id = int
338
339 let fresh_id () =
340   incr fresh_index;
341   !fresh_index
342
343   (* TODO ensure that names generated by fresh_var do not clash with user's *)
344   (* FG: "η" is not an identifier (it is rendered, but not be parsed) *)
345 let fresh_name () = "eta" ^ string_of_int (fresh_id ())
346
347 (* XXX FIXME: was used to add instance indices to symbols/numbers
348  * do we even need a similar operation? 
349  * currently an identity function! *)
350 let rec freshen_term ?(index = ref 0) term =
351   let freshen_term = freshen_term ~index in
352   let fresh_instance () = incr index; !index in
353   let special_k = function
354     | Ast.AttributedTerm (attr, t) -> Ast.AttributedTerm (attr, freshen_term t)
355     | Ast.Layout l -> Ast.Layout (visit_layout freshen_term l)
356     | Ast.Magic m -> Ast.Magic (visit_magic freshen_term m)
357     | Ast.Variable v -> Ast.Variable (visit_variable freshen_term v)
358     | Ast.Literal _ as t -> t
359     | _ -> assert false
360   in
361   match term with
362   | Ast.Symbol (s, x) -> Ast.Symbol (s, x (* fresh_instance () *))
363   | Ast.Num (s, x) -> Ast.Num (s, x (* fresh_instance () *))
364   | t -> visit_ast ~special_k freshen_term t
365
366 let freshen_obj obj =
367   let index = ref 0 in
368   let freshen_term = freshen_term ~index in
369   let freshen_name_ty = List.map (fun (n, t) -> (n, freshen_term t)) in
370   let freshen_name_ty_b = List.map (fun (n,t,b,i) -> (n,freshen_term t,b,i)) in
371   let freshen_capture_variables =
372    List.map (fun (n,t) -> (freshen_term n, HExtlib.map_option freshen_term t))
373   in
374   match obj with
375   | NotationPt.Inductive (params, indtypes) ->
376       let indtypes =
377         List.map
378           (fun (n, co, ty, ctors) -> (n, co, ty, freshen_name_ty ctors))
379           indtypes
380       in
381       NotationPt.Inductive (freshen_capture_variables params, indtypes)
382   | NotationPt.Theorem (flav, n, t, ty_opt,p) ->
383       let ty_opt =
384         match ty_opt with None -> None | Some ty -> Some (freshen_term ty)
385       in
386       NotationPt.Theorem (flav, n, freshen_term t, ty_opt,p)
387   | NotationPt.Record (params, n, ty, fields) ->
388       NotationPt.Record (freshen_capture_variables params, n,
389         freshen_term ty, freshen_name_ty_b fields)
390
391 let freshen_term = freshen_term ?index:None
392
393 let rec refresh_uri_in_term ~refresh_uri_in_term:refresh_in_cic
394  ~refresh_uri_in_reference
395 =
396  function
397     NotationPt.NRef ref -> NotationPt.NRef (refresh_uri_in_reference ref)
398   | NotationPt.NCic t -> NotationPt.NCic (refresh_in_cic t)
399   | t ->
400      visit_ast
401      (refresh_uri_in_term ~refresh_uri_in_term:refresh_in_cic
402        ~refresh_uri_in_reference) t
403       ~special_k:(fun x -> x)
404       ~map_xref_option:(function Some ref -> Some (refresh_uri_in_reference ref)
405                          | x -> x)
406       ~map_case_indty:(function (Some (s,Some ref)) -> Some (s, Some
407                         (refresh_uri_in_reference ref)) | x -> x)
408 ;;