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 Pt = CicNotationPt
29 module Env = CicNotationEnv
30 module Util = CicNotationUtil
39 let compare (x1:t) (x2:t) = Pervasives.compare x2 x1 (* reverse order *)
42 module IntSet = Set.Make (OrderedInt)
44 let int_set_of_int_list l =
45 List.fold_left (fun acc i -> IntSet.add i acc) IntSet.empty l
47 type pattern_kind = Variable | Constructor
54 val classify : pattern_t -> pattern_kind
55 val tag_of_pattern : pattern_t -> tag_t * pattern_t list
56 val tag_of_term : term_t -> tag_t * term_t list
59 module Matcher (P: PATTERN) =
61 type row_t = P.pattern_t list * P.pattern_t list * pattern_id
64 let compatible p1 p2 = P.classify p1 = P.classify p2
66 let matched = List.map (fun (matched, _, pid) -> matched, pid)
68 let partition t pidl =
69 let partitions = Hashtbl.create 11 in
70 let add pid row = Hashtbl.add partitions pid row in
73 with Invalid_argument _ -> assert false);
74 let pidset = int_set_of_int_list pidl in
77 match Hashtbl.find_all partitions pid with
79 | patterns -> (pid, List.rev patterns) :: acc)
84 | (_, [], _) :: _ -> true
85 (* if first row has an empty list of patterns, then others have as well *)
88 (* return 2 lists of rows, first one containing homogeneous rows according
89 * to "compatible" below *)
90 let horizontal_split t =
91 let ap, first_row, t', first_row_class =
95 assert false (* are_empty should have been invoked in advance *)
96 | ((_, hd :: _ , _) as row) :: tl -> hd, row, tl, P.classify hd
98 let rec aux prev_t = function
99 | [] -> List.rev prev_t, []
100 | (_, [], _) :: _ -> assert false
101 | ((_, hd :: _, _) as row) :: tl when compatible ap hd ->
102 aux (row :: prev_t) tl
103 | t -> List.rev prev_t, t
105 let rows1, rows2 = aux [first_row] t' in
106 first_row_class, rows1, rows2
108 (* return 2 lists, first one representing first column, second one
109 * representing a new pattern matrix where matched patterns have been moved
111 let vertical_split t =
114 | decls, hd :: tl, pid -> hd :: decls, tl, pid
118 let variable_closure k =
119 (fun matched_terms terms ->
120 prerr_endline "variable_closure";
122 | hd :: tl -> k (hd :: matched_terms) tl
125 let constructor_closure ks k =
126 (fun matched_terms terms ->
127 prerr_endline "constructor_closure";
131 let tag, subterms = P.tag_of_term t in
132 let k' = List.assoc tag ks in
133 k' matched_terms (subterms @ tl)
134 with Not_found -> k matched_terms terms)
135 | [] -> assert false)
137 let compiler rows match_cb fail_k =
141 else if are_empty t then
142 let res = match_cb (matched t) in
143 (fun matched_terms _ -> res matched_terms)
145 match horizontal_split t with
146 | _, [], _ -> assert false
147 | Variable, t', [] -> variable_closure (aux (vertical_split t') k)
148 | Constructor, t', [] ->
152 | _, p :: _, _ -> fst (P.tag_of_pattern p)
156 let clusters = partition t' tagl in
159 (fun (tag, cluster) ->
161 List.map (* add args as patterns heads *)
163 | matched_p, p :: tl, pid ->
164 let _, subpatterns = P.tag_of_pattern p in
165 matched_p, subpatterns @ tl, pid
172 constructor_closure ks k
173 | _, t', t'' -> aux t' (aux t'' k)
175 let t = List.map (fun (p, pid) -> [], [p], pid) rows in
176 let matcher = aux t (fun _ _ -> fail_k ()) in
177 (fun term -> matcher [] [term])
184 type pattern_t = Pt.term
185 type term_t = Pt.term
186 let classify = function
187 | Pt.Variable _ -> Variable
190 | Pt.Literal _ as t ->
191 prerr_endline (CicNotationPp.pp_term t);
194 let tag_of_pattern = CicNotationTag.get_tag
195 let tag_of_term = CicNotationTag.get_tag
198 module M = Matcher (Pattern21)
200 let extract_magic term =
201 let magic_map = ref [] in
203 let name = Util.fresh_name () in
204 magic_map := (name, m) :: !magic_map;
205 Pt.Variable (Pt.TermVar name)
207 let rec aux = function
208 | Pt.AttributedTerm (_, t) -> aux t
210 | Pt.Layout _ -> assert false
211 | Pt.Variable v -> Pt.Variable v
212 | Pt.Magic m -> add_magic m
213 | t -> Util.visit_ast aux t
215 let term' = aux term in
218 let env_of_matched pl tl =
222 Pt.Variable (Pt.TermVar name), _ ->
223 name, (Env.TermType, Env.TermValue t)
224 | Pt.Variable (Pt.NumVar name), (Pt.Num (s, _)) ->
225 name, (Env.NumType, Env.NumValue s)
226 | Pt.Variable (Pt.IdentVar name), (Pt.Ident (s, None)) ->
227 name, (Env.StringType, Env.StringValue s)
231 let rec compiler rows =
232 let rows', magic_maps =
236 let p', map = extract_magic p in
237 (p', pid), (pid, map))
240 let magichecker map =
243 let m_checker = compile_magic m in
245 match m_checker (Env.lookup_term env name) env with
247 | Some env' -> f env'))
248 (fun env -> Some env)
251 let magichooser candidates =
253 (fun f (pid, pl, checker) ->
254 (fun matched_terms ->
255 let env = env_of_matched pl matched_terms in
256 match checker env with
257 | None -> f matched_terms
258 | Some env -> Some (env, pid)))
263 prerr_endline (sprintf "match_cb on %d row(s)" (List.length rows));
268 try List.assoc pid magic_maps with Not_found -> assert false
270 pid, pl, magichecker magic_map)
273 magichooser candidates
275 M.compiler rows' match_cb (fun _ -> None)
277 and compile_magic = function
278 | Pt.Fold (kind, p_base, names, p_rec) ->
279 let p_rec_decls = Env.declarations_of_term p_rec in
280 let acc_name = try List.hd names with Failure _ -> assert false in
281 let compiled = compiler [p_base, 0; p_rec, 1] in
284 match compiled term with
286 | Some (env', 0) -> Some (env', [])
289 let acc = Env.lookup_term env' acc_name in
290 let env'' = Env.remove env' acc_name in
293 | Some (base_env, rec_envl) ->
294 Some (base_env, env'' :: rec_envl )
300 | Some (base_env, rec_envl) ->
301 Some (base_env @ Env.coalesce_env p_rec_decls rec_envl))
302 | Pt.Default (p_some, p_none) -> (* p_none can't bound names *)
303 let p_some_decls = Env.declarations_of_term p_some in
304 let none_env = List.map Env.opt_binding_of_name p_some_decls in
305 let compiled = compiler [p_some, 0] in
307 match compiled term with
308 | None -> Some none_env
309 | Some (env', 0) -> Some (List.map Env.opt_binding_some env' @ env)
320 | Uri of UriManager.uri
321 | Appl of cic_mask_t list
323 let uri_of_term t = CicUtil.uri_of_term (Deannotate.deannotate_term t)
325 let mask_of_cic = function
326 | Cic.AAppl (_, tl) -> Appl (List.map (fun _ -> Blob) tl), tl
327 | Cic.AConst (_, _, [])
328 | Cic.AVar (_, _, [])
329 | Cic.AMutInd (_, _, _, [])
330 | Cic.AMutConstruct (_, _, _, _, []) as t -> Uri (uri_of_term t), []
334 let mask, tl = mask_of_cic t in
335 Hashtbl.hash mask, tl
337 let mask_of_appl_pattern = function
338 | Pt.UriPattern s -> Uri (UriManager.uri_of_string s), []
339 | Pt.VarPattern _ -> Blob, []
340 | Pt.ApplPattern pl -> Appl (List.map (fun _ -> Blob) pl), pl
342 let tag_of_pattern p =
343 let mask, pl = mask_of_appl_pattern p in
344 Hashtbl.hash mask, pl
346 type pattern_t = Pt.cic_appl_pattern
347 type term_t = Cic.annterm
349 let classify = function
350 | Pt.VarPattern _ -> Variable
354 module M = Matcher (Pattern32)
358 prerr_endline (sprintf "match_cb on %d row(s)" (List.length rows));
359 let pl, pid = try List.hd rows with Not_found -> assert false in
360 (fun matched_terms ->
365 | Pt.VarPattern name -> name, t
371 M.compiler rows match_cb (fun () -> None)