1 (* Copyright (C) 2004-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 module Ast = CicNotationPt
30 let visit_ast ?(special_k = fun _ -> assert false) k =
31 let rec aux = function
32 | Ast.Appl terms -> Ast.Appl (List.map k terms)
33 | Ast.Binder (kind, var, body) ->
34 Ast.Binder (kind, aux_capture_variable var, k body)
35 | Ast.Case (term, indtype, typ, patterns) ->
36 Ast.Case (k term, indtype, aux_opt typ, aux_patterns patterns)
37 | Ast.Cast (t1, t2) -> Ast.Cast (k t1, k t2)
38 | Ast.LetIn (var, t1, t3) ->
39 Ast.LetIn (aux_capture_variable var, k t1, k t3)
40 | Ast.LetRec (kind, definitions, term) ->
43 (fun (params, var, ty, decrno) ->
44 List.map aux_capture_variable params, aux_capture_variable var,
48 Ast.LetRec (kind, definitions, k term)
49 | Ast.Ident (name, Some substs) ->
50 Ast.Ident (name, Some (aux_substs substs))
51 | Ast.Uri (name, Some substs) -> Ast.Uri (name, Some (aux_substs substs))
52 | Ast.Meta (index, substs) -> Ast.Meta (index, List.map aux_opt substs)
53 | (Ast.AttributedTerm _
57 | Ast.Variable _) as t -> special_k t
64 | Ast.UserInput) as t -> t
65 and aux_opt = function
67 | Some term -> Some (k term)
68 and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
69 and aux_patterns patterns = List.map aux_pattern patterns
72 Ast.Pattern (head, hrefs, vars), term ->
73 Ast.Pattern (head, hrefs, List.map aux_capture_variable vars), k term
74 | Ast.Wildcard, term -> Ast.Wildcard, k term
75 and aux_subst (name, term) = (name, k term)
76 and aux_substs substs = List.map aux_subst substs
80 let visit_layout k = function
81 | Ast.Sub (t1, t2) -> Ast.Sub (k t1, k t2)
82 | Ast.Sup (t1, t2) -> Ast.Sup (k t1, k t2)
83 | Ast.Below (t1, t2) -> Ast.Below (k t1, k t2)
84 | Ast.Above (t1, t2) -> Ast.Above (k t1, k t2)
85 | Ast.Over (t1, t2) -> Ast.Over (k t1, k t2)
86 | Ast.Atop (t1, t2) -> Ast.Atop (k t1, k t2)
87 | Ast.Frac (t1, t2) -> Ast.Frac (k t1, k t2)
88 | Ast.InfRule (t1, t2, t3) -> Ast.InfRule (k t1, k t2, k t3)
89 | Ast.Sqrt t -> Ast.Sqrt (k t)
90 | Ast.Root (arg, index) -> Ast.Root (k arg, k index)
91 | Ast.Break -> Ast.Break
92 | Ast.Box (kind, terms) -> Ast.Box (kind, List.map k terms)
93 | Ast.Group terms -> Ast.Group (List.map k terms)
94 | Ast.Mstyle (l, term) -> Ast.Mstyle (l, List.map k term)
95 | Ast.Mpadded (l, term) -> Ast.Mpadded (l, List.map k term)
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
106 let visit_variable k = function
110 | Ast.FreshVar _ as t -> t
111 | Ast.Ascription (t, s) -> Ast.Ascription (k t, s)
113 let variables_of_term t =
114 let rec vars = ref [] in
116 if List.mem v !vars then ()
117 else vars := v :: !vars
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
129 | Ast.TermVar _) as t ->
132 | Ast.FreshVar _ as t -> t
133 | Ast.Ascription _ -> assert false
138 let names_of_term t =
142 | Ast.TermVar (s,_) -> s
145 List.map aux (variables_of_term t)
147 let keywords_of_term t =
148 let rec 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 ->
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
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
171 visit_ast ~special_k strip_attributes t
175 | Ast.AttributedTerm (`IdRef id, t) -> id :: get_idrefs t
176 | Ast.AttributedTerm (_, t) -> get_idrefs t
179 let meta_names_of_term term =
180 let rec names = ref [] in
182 if List.mem n !names then ()
183 else names := n :: !names
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) ->
192 List.iter aux_branch patterns
193 | Ast.LetIn (_, t1, t3) ->
196 | Ast.LetRec (_, definitions, body) ->
197 List.iter aux_definition definitions ;
199 | Ast.Uri (_, Some substs) -> aux_substs substs
200 | Ast.Ident (_, Some substs) -> aux_substs substs
201 | Ast.Meta (_, substs) -> aux_meta_substs substs
209 | Ast.UserInput -> ()
211 | Ast.Magic magic -> aux_magic magic
212 | Ast.Variable var -> aux_variable var
215 and aux_opt = function
216 | Some term -> aux term
218 and aux_capture_var (_, ty_opt) = aux_opt ty_opt
219 and aux_branch (pattern, term) =
220 aux_pattern pattern ;
224 Ast.Pattern (head, _, vars) -> List.iter aux_capture_var vars
226 and aux_definition (params, var, term, decrno) =
227 List.iter aux_capture_var params ;
228 aux_capture_var var ;
230 and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
231 and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
232 and aux_variable = function
233 | Ast.NumVar name -> add_name name
234 | Ast.IdentVar name -> add_name name
235 | Ast.TermVar (name,_) -> add_name name
236 | Ast.FreshVar _ -> ()
237 | Ast.Ascription _ -> assert false
238 and aux_magic = function
239 | Ast.Default (t1, t2)
240 | Ast.Fold (_, t1, _, t2) ->
243 | Ast.If (t1, t2, t3) ->
253 let rectangular matrix =
254 let columns = Array.length matrix.(0) in
256 Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
261 let matrix = Array.of_list (List.map Array.of_list ll) in
262 assert (rectangular matrix);
263 let rows = Array.length matrix in
264 let columns = Array.length matrix.(0) in
265 let lists = ref [] in
266 for j = 0 to columns - 1 do
268 for i = 0 to rows - 1 do
269 l := matrix.(i).(j) :: !l
271 lists := List.rev !l :: !lists
275 let string_of_literal = function
280 let boxify = function
282 | l -> Ast.Layout (Ast.Box ((Ast.H, false, false), l))
284 let unboxify = function
285 | Ast.Layout (Ast.Box ((Ast.H, false, false), [ a ])) -> a
290 | l -> Ast.Layout (Ast.Group l)
296 | Ast.Layout (Ast.Group terms) :: terms' -> aux acc (terms @ terms')
297 | term :: terms -> aux (term :: acc) terms
301 let dress ~sep:sauce =
306 | hd :: tl -> hd :: sauce :: aux tl
310 let dressn ~sep:sauces =
315 | hd :: tl -> hd :: sauces @ aux tl
319 let find_appl_pattern_uris ap =
322 | Ast.UriPattern uri -> uri :: acc
323 | Ast.ImplicitPattern
324 | Ast.VarPattern _ -> acc
325 | Ast.ApplPattern apl -> List.fold_left aux acc apl
327 let uris = aux [] ap in
328 HExtlib.list_uniq (List.fast_sort UriManager.compare uris)
330 let rec find_branch =
332 Ast.Magic (Ast.If (_, Ast.Magic Ast.Fail, t)) -> find_branch t
333 | Ast.Magic (Ast.If (_, t, _)) -> find_branch t
336 let cic_name_of_name = function
337 | Ast.Ident ("_", None) -> Cic.Anonymous
338 | Ast.Ident (name, None) -> Cic.Name name
341 let name_of_cic_name =
342 (* let add_dummy_xref t = Ast.AttributedTerm (`IdRef "", t) in *)
343 (* ZACK why we used to generate dummy xrefs? *)
344 let add_dummy_xref t = t in
346 | Cic.Name s -> add_dummy_xref (Ast.Ident (s, None))
347 | Cic.Anonymous -> add_dummy_xref (Ast.Ident ("_", None))
349 let fresh_index = ref ~-1
351 type notation_id = int
357 (* TODO ensure that names generated by fresh_var do not clash with user's *)
358 let fresh_name () = "fresh" ^ string_of_int (fresh_id ())
360 let rec freshen_term ?(index = ref 0) term =
361 let freshen_term = freshen_term ~index in
362 let fresh_instance () = incr index; !index in
363 let special_k = function
364 | Ast.AttributedTerm (attr, t) -> Ast.AttributedTerm (attr, freshen_term t)
365 | Ast.Layout l -> Ast.Layout (visit_layout freshen_term l)
366 | Ast.Magic m -> Ast.Magic (visit_magic freshen_term m)
367 | Ast.Variable v -> Ast.Variable (visit_variable freshen_term v)
368 | Ast.Literal _ as t -> t
372 | Ast.Symbol (s, instance) -> Ast.Symbol (s, fresh_instance ())
373 | Ast.Num (s, instance) -> Ast.Num (s, fresh_instance ())
374 | t -> visit_ast ~special_k freshen_term t
376 let freshen_obj obj =
378 let freshen_term = freshen_term ~index in
379 let freshen_name_ty = List.map (fun (n, t) -> (n, freshen_term t)) in
380 let freshen_name_ty_b = List.map (fun (n,t,b,i) -> (n,freshen_term t,b,i)) in
381 let freshen_capture_variables =
382 List.map (fun (n,t) -> (freshen_term n, HExtlib.map_option freshen_term t))
385 | CicNotationPt.Inductive (params, indtypes) ->
388 (fun (n, co, ty, ctors) -> (n, co, ty, freshen_name_ty ctors))
391 CicNotationPt.Inductive (freshen_capture_variables params, indtypes)
392 | CicNotationPt.Theorem (flav, n, t, ty_opt) ->
394 match ty_opt with None -> None | Some ty -> Some (freshen_term ty)
396 CicNotationPt.Theorem (flav, n, freshen_term t, ty_opt)
397 | CicNotationPt.Record (params, n, ty, fields) ->
398 CicNotationPt.Record (freshen_capture_variables params, n,
399 freshen_term ty, freshen_name_ty_b fields)
401 let freshen_term = freshen_term ?index:None