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 Pp = CicNotationPp
29 module Pt = CicNotationPt
30 module Env = CicNotationEnv
31 module Util = CicNotationUtil
40 let compare (x1:t) (x2:t) = Pervasives.compare x2 x1 (* reverse order *)
43 module IntSet = Set.Make (OrderedInt)
45 let int_set_of_int_list l =
46 List.fold_left (fun acc i -> IntSet.add i acc) IntSet.empty l
48 type pattern_kind = Variable | Constructor
55 val classify : pattern_t -> pattern_kind
56 val tag_of_pattern : pattern_t -> tag_t * pattern_t list
57 val tag_of_term : term_t -> tag_t * term_t list
60 module Matcher (P: PATTERN) =
62 type row_t = P.pattern_t list * P.pattern_t list * pattern_id
65 let compatible p1 p2 = P.classify p1 = P.classify p2
67 let matched = List.map (fun (matched, _, pid) -> matched, pid)
69 let partition t pidl =
70 let partitions = Hashtbl.create 11 in
71 let add pid row = Hashtbl.add partitions pid row in
74 with Invalid_argument _ -> assert false);
75 let pidset = int_set_of_int_list pidl in
78 match Hashtbl.find_all partitions pid with
80 | patterns -> (pid, List.rev patterns) :: acc)
85 | (_, [], _) :: _ -> true
86 (* if first row has an empty list of patterns, then others have as well *)
89 (* return 2 lists of rows, first one containing homogeneous rows according
90 * to "compatible" below *)
91 let horizontal_split t =
92 let ap, first_row, t', first_row_class =
96 assert false (* are_empty should have been invoked in advance *)
97 | ((_, hd :: _ , _) as row) :: tl -> hd, row, tl, P.classify hd
99 let rec aux prev_t = function
100 | [] -> List.rev prev_t, []
101 | (_, [], _) :: _ -> assert false
102 | ((_, hd :: _, _) as row) :: tl when compatible ap hd ->
103 aux (row :: prev_t) tl
104 | t -> List.rev prev_t, t
106 let rows1, rows2 = aux [first_row] t' in
107 first_row_class, rows1, rows2
109 (* return 2 lists, first one representing first column, second one
110 * representing a new pattern matrix where matched patterns have been moved
112 let vertical_split t =
115 | decls, hd :: tl, pid -> hd :: decls, tl, pid
119 let variable_closure k =
120 (fun matched_terms terms ->
122 | hd :: tl -> k (hd :: matched_terms) tl
125 let success_closure ks k =
126 (fun matched_terms terms ->
127 match ks matched_terms with
130 (* the match has failed, we rollback the last matched term
131 * into the unmatched ones and call the failure continuation
133 match matched_terms with
134 hd :: tl -> k tl (hd :: terms)
139 let constructor_closure ks k =
140 (fun matched_terms terms ->
144 let tag, subterms = P.tag_of_term t in
145 let k' = List.assoc tag ks in
146 k' matched_terms (subterms @ tl)
147 with Not_found -> k matched_terms terms)
148 | [] -> assert false)
150 let compiler rows match_cb fail_k =
154 else if are_empty t then
155 success_closure (match_cb (matched t)) k
157 match horizontal_split t with
158 | _, [], _ -> assert false
159 | Variable, t', [] -> variable_closure (aux (vertical_split t') k)
160 | Constructor, t', [] ->
164 | _, p :: _, _ -> fst (P.tag_of_pattern p)
168 let clusters = partition t' tagl in
171 (fun (tag, cluster) ->
173 List.map (* add args as patterns heads *)
175 | matched_p, p :: tl, pid ->
176 let _, subpatterns = P.tag_of_pattern p in
177 matched_p, subpatterns @ tl, pid
184 constructor_closure ks k
185 | _, t', t'' -> aux t' (aux t'' k)
187 let t = List.map (fun (p, pid) -> [], [p], pid) rows in
188 let matcher = aux t (fun _ _ -> fail_k ()) in
189 (fun term -> matcher [] [term])
196 type pattern_t = Pt.term
197 type term_t = Pt.term
198 let rec classify = function
199 | Pt.AttributedTerm (_, t) -> classify t
200 | Pt.Variable _ -> Variable
203 | Pt.Literal _ as t -> assert false
205 let tag_of_pattern = CicNotationTag.get_tag
206 let tag_of_term = CicNotationTag.get_tag
209 module M = Matcher (Pattern21)
211 let extract_magic term =
212 let magic_map = ref [] in
214 let name = Util.fresh_name () in
215 magic_map := (name, m) :: !magic_map;
216 Pt.Variable (Pt.TermVar name)
218 let rec aux = function
219 | Pt.AttributedTerm (_, t) -> assert false
221 | Pt.Layout _ -> assert false
222 | Pt.Variable v -> Pt.Variable v
223 | Pt.Magic m -> add_magic m
224 | t -> Util.visit_ast aux t
226 let term' = aux term in
229 let env_of_matched pl tl =
233 Pt.Variable (Pt.TermVar name), _ ->
234 name, (Env.TermType, Env.TermValue t)
235 | Pt.Variable (Pt.NumVar name), (Pt.Num (s, _)) ->
236 name, (Env.NumType, Env.NumValue s)
237 | Pt.Variable (Pt.IdentVar name), (Pt.Ident (s, None)) ->
238 name, (Env.StringType, Env.StringValue s)
242 let rec compiler rows =
243 let rows', magic_maps =
247 let p', map = extract_magic p in
248 (p', pid), (pid, map))
251 let magichecker map =
254 let m_checker = compile_magic m in
256 match m_checker (Env.lookup_term env name) env with
260 (fun env -> Some env)
263 let magichooser candidates =
265 (fun f (pid, pl, checker) ->
266 (fun matched_terms ->
267 let env = env_of_matched pl matched_terms in
268 match checker env with
269 | None -> f matched_terms
272 try List.assoc pid magic_maps with Not_found -> assert false
274 let env' = Env.remove_names env (List.map fst magic_map) in
277 (List.rev candidates)
284 try List.assoc pid magic_maps with Not_found -> assert false
286 pid, pl, magichecker magic_map)
289 magichooser candidates
291 M.compiler rows' match_cb (fun _ -> None)
293 and compile_magic = function
294 | Pt.Fold (kind, p_base, names, p_rec) ->
295 let p_rec_decls = Env.declarations_of_term p_rec in
296 (* LUCA: p_rec_decls should not contain "names" *)
297 let acc_name = try List.hd names with Failure _ -> assert false in
298 let compiled_base = compiler [p_base, 0]
299 and compiled_rec = compiler [p_rec, 0] in
302 match compiled_base term with
304 | Some (env', _) -> Some (env', [])
307 match compiled_rec term with
308 | None -> aux_base term
311 let acc = Env.lookup_term env' acc_name in
312 let env'' = Env.remove_name env' acc_name in
314 | None -> aux_base term
315 | Some (base_env, rec_envl) ->
316 Some (base_env, env'' :: rec_envl)
321 | Some (base_env, rec_envl) ->
322 Some (base_env @ Env.coalesce_env p_rec_decls rec_envl @ env)) (* @ env LUCA!!! *)
324 | Pt.Default (p_some, p_none) -> (* p_none can't bound names *)
325 let p_some_decls = Env.declarations_of_term p_some in
326 let none_env = List.map Env.opt_binding_of_name p_some_decls in
327 let compiled = compiler [p_some, 0] in
329 match compiled term with
330 | None -> Some none_env (* LUCA: @ env ??? *)
331 | Some (env', 0) -> Some (List.map Env.opt_binding_some env' @ env)
334 | Pt.If (p_test, p_true, p_false) ->
335 let compiled_test = compiler [p_test, 0]
336 and compiled_true = compiler [p_true, 0]
337 and compiled_false = compiler [p_false, 0] in
340 match compiled_test term with
341 | None -> compiled_false
342 | Some _ -> compiled_true
344 match branch term with
346 | Some (env', _) -> Some (env' @ env))
348 | Pt.Fail -> (fun _ _ -> None)
359 | Uri of UriManager.uri
360 | Appl of cic_mask_t list
362 let uri_of_term t = CicUtil.uri_of_term (Deannotate.deannotate_term t)
364 let mask_of_cic = function
365 | Cic.AAppl (_, tl) -> Appl (List.map (fun _ -> Blob) tl), tl
366 | Cic.AConst (_, _, [])
367 | Cic.AVar (_, _, [])
368 | Cic.AMutInd (_, _, _, [])
369 | Cic.AMutConstruct (_, _, _, _, []) as t -> Uri (uri_of_term t), []
373 let mask, tl = mask_of_cic t in
374 Hashtbl.hash mask, tl
376 let mask_of_appl_pattern = function
377 | Pt.UriPattern uri -> Uri uri, []
379 | Pt.VarPattern _ -> Blob, []
380 | Pt.ApplPattern pl -> Appl (List.map (fun _ -> Blob) pl), pl
382 let tag_of_pattern p =
383 let mask, pl = mask_of_appl_pattern p in
384 Hashtbl.hash mask, pl
386 type pattern_t = Pt.cic_appl_pattern
387 type term_t = Cic.annterm
389 let classify = function
391 | Pt.VarPattern _ -> Variable
393 | Pt.ApplPattern _ -> Constructor
396 module M = Matcher (Pattern32)
400 let pl, pid = try List.hd rows with Not_found -> assert false in
401 (fun matched_terms ->
406 | Pt.ImplicitPattern -> Util.fresh_name (), t
407 | Pt.VarPattern name -> name, t
413 M.compiler rows match_cb (fun () -> None)