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