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)
31 ?(map_xref_option= fun x -> x) ?(map_case_indty= fun x -> x)
33 fun k x -> match x with None -> None | Some x -> Some (k x))
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) ->
49 (fun (params, var, ty, decrno) ->
50 List.map aux_capture_variable params, aux_capture_variable var,
54 Ast.LetRec (kind, definitions, k term)
55 | Ast.Ident (name, Some substs) ->
56 Ast.Ident (name, Some (aux_substs substs))
57 | Ast.Uri (name, Some substs) -> Ast.Uri (name, Some (aux_substs substs))
58 | Ast.Meta (index, substs) -> Ast.Meta (index, List.map aux_opt substs)
59 | (Ast.AttributedTerm _
63 | Ast.Variable _) as t -> special_k t
70 | Ast.UserInput) as t -> t
71 and aux_opt = function
73 | Some term -> Some (k term)
74 and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
75 and aux_patterns k_xref patterns = List.map (aux_pattern k_xref) patterns
76 and aux_pattern k_xref =
78 Ast.Pattern (head, hrefs, vars), term ->
79 Ast.Pattern (head, k_xref hrefs, List.map aux_capture_variable vars), k term
80 | Ast.Wildcard, term -> Ast.Wildcard, k term
81 and aux_subst (name, term) = (name, k term)
82 and aux_substs substs = List.map aux_subst substs
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)
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
113 let visit_variable k = function
117 | Ast.FreshVar _ as t -> t
118 | Ast.Ascription (t, s) -> Ast.Ascription (k t, s)
120 let variables_of_term t =
121 let rec vars = ref [] in
123 if List.mem v !vars then ()
124 else vars := v :: !vars
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
136 | Ast.TermVar _) as t ->
139 | Ast.FreshVar _ as t -> t
140 | Ast.Ascription _ -> assert false
145 let names_of_term t =
149 | Ast.TermVar (s,_) -> s
152 List.map aux (variables_of_term t)
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 ->
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
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
178 visit_ast ~special_k strip_attributes t
182 | Ast.AttributedTerm (`IdRef id, t) -> id :: get_idrefs t
183 | Ast.AttributedTerm (_, t) -> get_idrefs t
186 let meta_names_of_term term =
187 let rec names = ref [] in
189 if List.mem n !names then ()
190 else names := n :: !names
192 let rec aux = function
193 | Ast.AttributedTerm (_, term) -> aux term
194 | Ast.Appl terms -> List.iter aux terms
195 | Ast.Binder (_, _, body) -> aux body
196 | Ast.Case (term, indty, outty_opt, patterns) ->
199 List.iter aux_branch patterns
200 | Ast.LetIn (_, t1, t3) ->
203 | Ast.LetRec (_, definitions, body) ->
204 List.iter aux_definition definitions ;
206 | Ast.Uri (_, Some substs) -> aux_substs substs
207 | Ast.Ident (_, Some substs) -> aux_substs substs
208 | Ast.Meta (_, substs) -> aux_meta_substs substs
216 | Ast.UserInput -> ()
218 | Ast.Magic magic -> aux_magic magic
219 | Ast.Variable var -> aux_variable var
222 and aux_opt = function
223 | Some term -> aux term
225 and aux_capture_var (_, ty_opt) = aux_opt ty_opt
226 and aux_branch (pattern, term) =
227 aux_pattern pattern ;
231 Ast.Pattern (head, _, vars) -> List.iter aux_capture_var vars
233 and aux_definition (params, var, term, decrno) =
234 List.iter aux_capture_var params ;
235 aux_capture_var var ;
237 and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
238 and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
239 and aux_variable = function
240 | Ast.NumVar name -> add_name name
241 | Ast.IdentVar name -> add_name name
242 | Ast.TermVar (name,_) -> add_name name
243 | Ast.FreshVar _ -> ()
244 | Ast.Ascription _ -> assert false
245 and aux_magic = function
246 | Ast.Default (t1, t2)
247 | Ast.Fold (_, t1, _, t2) ->
250 | Ast.If (t1, t2, t3) ->
260 let rectangular matrix =
261 let columns = Array.length matrix.(0) in
263 Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
268 let matrix = Array.of_list (List.map Array.of_list ll) in
269 assert (rectangular matrix);
270 let rows = Array.length matrix in
271 let columns = Array.length matrix.(0) in
272 let lists = ref [] in
273 for j = 0 to columns - 1 do
275 for i = 0 to rows - 1 do
276 l := matrix.(i).(j) :: !l
278 lists := List.rev !l :: !lists
282 let string_of_literal = function
287 let boxify = function
289 | l -> Ast.Layout (Ast.Box ((Ast.H, false, false), l))
291 let unboxify = function
292 | Ast.Layout (Ast.Box ((Ast.H, false, false), [ a ])) -> a
297 | l -> Ast.Layout (Ast.Group l)
303 | Ast.Layout (Ast.Group terms) :: terms' -> aux acc (terms @ terms')
304 | term :: terms -> aux (term :: acc) terms
308 let dress ~sep:sauce =
313 | hd :: tl -> hd :: sauce :: aux tl
317 let dressn ~sep:sauces =
322 | hd :: tl -> hd :: sauces @ aux tl
326 let find_appl_pattern_uris ap =
329 | Ast.UriPattern uri -> uri :: acc
330 | Ast.ImplicitPattern
331 | Ast.VarPattern _ -> acc
332 | Ast.ApplPattern apl -> List.fold_left aux acc apl
334 let uris = aux [] ap in
335 HExtlib.list_uniq (List.fast_sort UriManager.compare uris)
337 let rec find_branch =
339 Ast.Magic (Ast.If (_, Ast.Magic Ast.Fail, t)) -> find_branch t
340 | Ast.Magic (Ast.If (_, t, _)) -> find_branch t
343 let cic_name_of_name = function
344 | Ast.Ident ("_", None) -> Cic.Anonymous
345 | Ast.Ident (name, None) -> Cic.Name name
348 let name_of_cic_name =
349 (* let add_dummy_xref t = Ast.AttributedTerm (`IdRef "", t) in *)
350 (* ZACK why we used to generate dummy xrefs? *)
351 let add_dummy_xref t = t in
353 | Cic.Name s -> add_dummy_xref (Ast.Ident (s, None))
354 | Cic.Anonymous -> add_dummy_xref (Ast.Ident ("_", None))
356 let fresh_index = ref ~-1
358 type notation_id = int
364 (* TODO ensure that names generated by fresh_var do not clash with user's *)
365 let fresh_name () = "η" ^ string_of_int (fresh_id ())
367 let rec freshen_term ?(index = ref 0) term =
368 let freshen_term = freshen_term ~index in
369 let fresh_instance () = incr index; !index in
370 let special_k = function
371 | Ast.AttributedTerm (attr, t) -> Ast.AttributedTerm (attr, freshen_term t)
372 | Ast.Layout l -> Ast.Layout (visit_layout freshen_term l)
373 | Ast.Magic m -> Ast.Magic (visit_magic freshen_term m)
374 | Ast.Variable v -> Ast.Variable (visit_variable freshen_term v)
375 | Ast.Literal _ as t -> t
379 | Ast.Symbol (s, instance) -> Ast.Symbol (s, fresh_instance ())
380 | Ast.Num (s, instance) -> Ast.Num (s, fresh_instance ())
381 | t -> visit_ast ~special_k freshen_term t
383 let freshen_obj obj =
385 let freshen_term = freshen_term ~index in
386 let freshen_name_ty = List.map (fun (n, t) -> (n, freshen_term t)) in
387 let freshen_name_ty_b = List.map (fun (n,t,b,i) -> (n,freshen_term t,b,i)) in
388 let freshen_capture_variables =
389 List.map (fun (n,t) -> (freshen_term n, HExtlib.map_option freshen_term t))
392 | CicNotationPt.Inductive (params, indtypes) ->
395 (fun (n, co, ty, ctors) -> (n, co, ty, freshen_name_ty ctors))
398 CicNotationPt.Inductive (freshen_capture_variables params, indtypes)
399 | CicNotationPt.Theorem (flav, n, t, ty_opt) ->
401 match ty_opt with None -> None | Some ty -> Some (freshen_term ty)
403 CicNotationPt.Theorem (flav, n, freshen_term t, ty_opt)
404 | CicNotationPt.Record (params, n, ty, fields) ->
405 CicNotationPt.Record (freshen_capture_variables params, n,
406 freshen_term ty, freshen_name_ty_b fields)
408 let freshen_term = freshen_term ?index:None