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