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 (* TODO ensure that names generated by fresh_var do not clash with user's *)
30 let index = ref ~-1 in
33 "fresh" ^ string_of_int !index
35 (* let meta_names_of term = *)
36 (* let rec names = ref [] in *)
37 (* let add_name n = *)
38 (* if List.mem n !names then () *)
39 (* else names := n :: !names *)
41 (* let rec aux = function *)
42 (* | AttributedTerm (_, term) -> aux term *)
43 (* | Appl terms -> List.iter aux terms *)
44 (* | Binder (_, _, body) -> aux body *)
45 (* | Case (term, indty, outty_opt, patterns) -> *)
47 (* aux_opt outty_opt ; *)
48 (* List.iter aux_branch patterns *)
49 (* | LetIn (_, t1, t2) -> *)
52 (* | LetRec (_, definitions, body) -> *)
53 (* List.iter aux_definition definitions ; *)
55 (* | Uri (_, Some substs) -> aux_substs substs *)
56 (* | Ident (_, Some substs) -> aux_substs substs *)
57 (* | Meta (_, substs) -> aux_meta_substs substs *)
65 (* | UserInput -> () *)
67 (* | Magic magic -> aux_magic magic *)
68 (* | Variable var -> aux_variable var *)
70 (* | _ -> assert false *)
71 (* and aux_opt = function *)
72 (* | Some term -> aux term *)
74 (* and aux_capture_var (_, ty_opt) = aux_opt ty_opt *)
75 (* and aux_branch (pattern, term) = *)
76 (* aux_pattern pattern ; *)
78 (* and aux_pattern (head, vars) = *)
79 (* List.iter aux_capture_var vars *)
80 (* and aux_definition (var, term, i) = *)
81 (* aux_capture_var var ; *)
83 (* and aux_substs substs = List.iter (fun (_, term) -> aux term) substs *)
84 (* and aux_meta_substs meta_substs = List.iter aux_opt meta_substs *)
85 (* and aux_variable = function *)
86 (* | NumVar name -> add_name name *)
87 (* | IdentVar name -> add_name name *)
88 (* | TermVar name -> add_name name *)
89 (* | FreshVar _ -> () *)
90 (* | Ascription _ -> assert false *)
91 (* and aux_magic = function *)
92 (* | Default (t1, t2) *)
93 (* | Fold (_, t1, _, t2) -> *)
96 (* | _ -> assert false *)
101 let visit_ast ?(special_k = fun _ -> assert false) k =
102 let rec aux = function
104 | Appl terms -> Appl (List.map k terms)
105 | Binder (kind, var, body) ->
106 Binder (kind, aux_capture_variable var, k body)
107 | Case (term, indtype, typ, patterns) ->
108 Case (k term, indtype, aux_opt typ, aux_patterns patterns)
109 | LetIn (var, t1, t2) -> LetIn (aux_capture_variable var, k t1, k t2)
110 | LetRec (kind, definitions, term) ->
113 (fun (var, ty, n) -> aux_capture_variable var, k ty, n)
116 LetRec (kind, definitions, k term)
117 | Ident (name, Some substs) -> Ident (name, Some (aux_substs substs))
118 | Uri (name, Some substs) -> Uri (name, Some (aux_substs substs))
119 | Meta (index, substs) -> Meta (index, List.map aux_opt substs)
125 | Variable _) as t -> special_k t
133 | UserInput) as t -> t
135 and aux_opt = function
137 | Some term -> Some (k term)
139 and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
141 and aux_patterns patterns = List.map aux_pattern patterns
143 and aux_pattern ((head, vars), term) =
144 ((head, List.map aux_capture_variable vars), k term)
146 and aux_subst (name, term) = (name, k term)
148 and aux_substs substs = List.map aux_subst substs
154 let visit_layout k = function
155 | Sub (t1, t2) -> Sub (k t1, k t2)
156 | Sup (t1, t2) -> Sup (k t1, k t2)
157 | Below (t1, t2) -> Below (k t1, k t2)
158 | Above (t1, t2) -> Above (k t1, k t2)
159 | Over (t1, t2) -> Over (k t1, k t2)
160 | Atop (t1, t2) -> Atop (k t1, k t2)
161 | Frac (t1, t2) -> Frac (k t1, k t2)
162 | Sqrt t -> Sqrt (k t)
163 | Root (arg, index) -> Root (k arg, k index)
165 | Box (kind, terms) -> Box (kind, List.map k terms)
167 let visit_magic k = function
168 | List0 (t, l) -> List0 (k t, l)
169 | List1 (t, l) -> List1 (k t, l )
171 | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2)
172 | Default (t1, t2) -> Default (k t1, k t2)
174 let variables_of_term t =
175 let rec vars = ref [] in
177 if List.mem v !vars then ()
178 else vars := v :: !vars
180 let rec aux = function
181 | Magic m -> Magic (visit_magic aux m)
182 | Layout l -> Layout (visit_layout aux l)
183 | Variable v -> Variable (aux_variable v)
184 | Literal _ as t -> t
185 | AttributedTerm (_, t) -> aux t
186 | t -> visit_ast aux t
187 and aux_variable = function
193 | FreshVar _ as t -> t
194 | Ascription _ -> assert false
199 let names_of_term t =
206 List.map aux (variables_of_term t)
208 let rec strip_attributes t =
209 let special_k = function
210 | AttributedTerm (_, term) -> strip_attributes term
211 | Magic m -> Magic (visit_magic strip_attributes m)
212 | Variable _ as t -> t
215 visit_ast ~special_k strip_attributes t
217 let meta_names_of_term term =
218 let rec names = ref [] in
220 if List.mem n !names then ()
221 else names := n :: !names
223 let rec aux = function
224 | AttributedTerm (_, term) -> aux term
225 | Appl terms -> List.iter aux terms
226 | Binder (_, _, body) -> aux body
227 | Case (term, indty, outty_opt, patterns) ->
230 List.iter aux_branch patterns
231 | LetIn (_, t1, t2) ->
234 | LetRec (_, definitions, body) ->
235 List.iter aux_definition definitions ;
237 | Uri (_, Some substs) -> aux_substs substs
238 | Ident (_, Some substs) -> aux_substs substs
239 | Meta (_, substs) -> aux_meta_substs substs
249 | Magic magic -> aux_magic magic
250 | Variable var -> aux_variable var
253 and aux_opt = function
254 | Some term -> aux term
256 and aux_capture_var (_, ty_opt) = aux_opt ty_opt
257 and aux_branch (pattern, term) =
258 aux_pattern pattern ;
260 and aux_pattern (head, vars) =
261 List.iter aux_capture_var vars
262 and aux_definition (var, term, i) =
263 aux_capture_var var ;
265 and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
266 and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
267 and aux_variable = function
268 | NumVar name -> add_name name
269 | IdentVar name -> add_name name
270 | TermVar name -> add_name name
272 | Ascription _ -> assert false
273 and aux_magic = function
275 | Fold (_, t1, _, t2) ->
283 let rectangular matrix =
284 let columns = Array.length matrix.(0) in
286 Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
291 let matrix = Array.of_list (List.map Array.of_list ll) in
292 assert (rectangular matrix);
293 let rows = Array.length matrix in
294 let columns = Array.length matrix.(0) in
295 let lists = ref [] in
296 for j = 0 to columns - 1 do
298 for i = 0 to rows - 1 do
299 l := matrix.(i).(j) :: !l
301 lists := List.rev !l :: !lists
305 let string_of_literal = function
310 let boxify = function
312 | l -> Layout (Box (H, l))