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
103 | Appl terms -> Appl (List.map k terms)
104 | Binder (kind, var, body) ->
105 Binder (kind, aux_capture_variable var, k body)
106 | Case (term, indtype, typ, patterns) ->
107 Case (k term, indtype, aux_opt typ, aux_patterns patterns)
108 | LetIn (var, t1, t2) -> LetIn (aux_capture_variable var, k t1, k t2)
109 | LetRec (kind, definitions, term) ->
112 (fun (var, ty, n) -> aux_capture_variable var, k ty, n)
115 LetRec (kind, definitions, k term)
116 | Ident (name, Some substs) -> Ident (name, Some (aux_substs substs))
117 | Uri (name, Some substs) -> Uri (name, Some (aux_substs substs))
118 | Meta (index, substs) -> Meta (index, List.map aux_opt substs)
123 | Variable _) as t -> special_k t
130 | UserInput) as t -> t
131 and aux_opt = function
133 | Some term -> Some (k term)
134 and aux_capture_variable (term, typ_opt) = k term, aux_opt typ_opt
135 and aux_patterns patterns = List.map aux_pattern patterns
136 and aux_pattern ((head, vars), term) =
137 ((head, List.map aux_capture_variable vars), k term)
138 and aux_subst (name, term) = (name, k term)
139 and aux_substs substs = List.map aux_subst substs
143 let visit_layout k = function
144 | Sub (t1, t2) -> Sub (k t1, k t2)
145 | Sup (t1, t2) -> Sup (k t1, k t2)
146 | Below (t1, t2) -> Below (k t1, k t2)
147 | Above (t1, t2) -> Above (k t1, k t2)
148 | Over (t1, t2) -> Over (k t1, k t2)
149 | Atop (t1, t2) -> Atop (k t1, k t2)
150 | Frac (t1, t2) -> Frac (k t1, k t2)
151 | Sqrt t -> Sqrt (k t)
152 | Root (arg, index) -> Root (k arg, k index)
154 | Box (kind, terms) -> Box (kind, List.map k terms)
155 | Group terms -> Group (List.map k terms)
157 let visit_magic k = function
158 | List0 (t, l) -> List0 (k t, l)
159 | List1 (t, l) -> List1 (k t, l)
161 | Fold (kind, t1, names, t2) -> Fold (kind, k t1, names, k t2)
162 | Default (t1, t2) -> Default (k t1, k t2)
163 | If (t1, t2, t3) -> If (k t1, k t2, k t3)
166 let variables_of_term t =
167 let rec vars = ref [] in
169 if List.mem v !vars then ()
170 else vars := v :: !vars
172 let rec aux = function
173 | Magic m -> Magic (visit_magic aux m)
174 | Layout l -> Layout (visit_layout aux l)
175 | Variable v -> Variable (aux_variable v)
176 | Literal _ as t -> t
177 | AttributedTerm (_, t) -> aux t
178 | t -> visit_ast aux t
179 and aux_variable = function
185 | FreshVar _ as t -> t
186 | Ascription _ -> assert false
191 let names_of_term t =
198 List.map aux (variables_of_term t)
200 let keywords_of_term t =
201 let rec keywords = ref [] in
202 let add_keyword k = keywords := k :: !keywords in
203 let rec aux = function
204 | AttributedTerm (_, t) -> aux t
205 | Layout l -> Layout (visit_layout aux l)
206 | Literal (`Keyword k) as t ->
209 | Literal _ as t -> t
210 | Magic m -> Magic (visit_magic aux m)
211 | Variable _ as v -> v
212 | t -> visit_ast aux t
217 let rec strip_attributes t =
218 let special_k = function
219 | AttributedTerm (_, term) -> strip_attributes term
220 | Magic m -> Magic (visit_magic strip_attributes m)
221 | Variable _ as t -> t
224 visit_ast ~special_k strip_attributes t
226 let meta_names_of_term term =
227 let rec names = ref [] in
229 if List.mem n !names then ()
230 else names := n :: !names
232 let rec aux = function
233 | AttributedTerm (_, term) -> aux term
234 | Appl terms -> List.iter aux terms
235 | Binder (_, _, body) -> aux body
236 | Case (term, indty, outty_opt, patterns) ->
239 List.iter aux_branch patterns
240 | LetIn (_, t1, t2) ->
243 | LetRec (_, definitions, body) ->
244 List.iter aux_definition definitions ;
246 | Uri (_, Some substs) -> aux_substs substs
247 | Ident (_, Some substs) -> aux_substs substs
248 | Meta (_, substs) -> aux_meta_substs substs
258 | Magic magic -> aux_magic magic
259 | Variable var -> aux_variable var
262 and aux_opt = function
263 | Some term -> aux term
265 and aux_capture_var (_, ty_opt) = aux_opt ty_opt
266 and aux_branch (pattern, term) =
267 aux_pattern pattern ;
269 and aux_pattern (head, vars) =
270 List.iter aux_capture_var vars
271 and aux_definition (var, term, i) =
272 aux_capture_var var ;
274 and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
275 and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
276 and aux_variable = function
277 | NumVar name -> add_name name
278 | IdentVar name -> add_name name
279 | TermVar name -> add_name name
281 | Ascription _ -> assert false
282 and aux_magic = function
284 | Fold (_, t1, _, t2) ->
297 let rectangular matrix =
298 let columns = Array.length matrix.(0) in
300 Array.iter (fun a -> if Array.length a <> columns then raise Exit) matrix;
305 let matrix = Array.of_list (List.map Array.of_list ll) in
306 assert (rectangular matrix);
307 let rows = Array.length matrix in
308 let columns = Array.length matrix.(0) in
309 let lists = ref [] in
310 for j = 0 to columns - 1 do
312 for i = 0 to rows - 1 do
313 l := matrix.(i).(j) :: !l
315 lists := List.rev !l :: !lists
319 let string_of_literal = function
324 let boxify = function
326 | l -> Layout (Box ((H, false, false), l))
330 | l -> Layout (Group l)
336 | Layout (Group terms) :: terms' -> aux acc (terms @ terms')
337 | term :: terms -> aux (term :: acc) terms
346 | hd :: tl -> hd :: sauce :: aux tl
350 let find_appl_pattern_uris ap =
355 ignore (List.find (fun uri' -> UriManager.eq uri uri') acc);
357 with Not_found -> uri :: acc)
358 | VarPattern _ -> acc
359 | ApplPattern apl -> List.fold_left aux acc apl
363 let rec find_branch =
365 Magic (If (_, Magic Fail, t)) -> find_branch t
366 | Magic (If (_, t, _)) -> find_branch t