1 (* Copyright (C) 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/
30 type pattern_kind = Variable | Constructor
38 let compare (x1:t) (x2:t) = Pervasives.compare x2 x1 (* reverse order *)
41 module IntSet = Set.Make (OrderedInt)
43 let int_set_of_int_list l =
44 List.fold_left (fun acc i -> IntSet.add i acc) IntSet.empty l
50 val classify : pattern_t -> pattern_kind
51 val tag_of_pattern : pattern_t -> tag_t * pattern_t list
52 val tag_of_term : term_t -> tag_t * term_t list
53 val string_of_term: term_t -> string
54 val string_of_pattern: pattern_t -> string
57 module Matcher (P: PATTERN) =
59 type row_t = P.pattern_t list * P.pattern_t list * pattern_id
62 let compatible p1 p2 = P.classify p1 = P.classify p2
64 let matched = List.map (fun (matched, _, pid) -> matched, pid)
66 let partition t pidl =
67 let partitions = Hashtbl.create 11 in
68 let add pid row = Hashtbl.add partitions pid row in
71 with Invalid_argument _ -> assert false);
72 let pidset = int_set_of_int_list pidl in
75 match Hashtbl.find_all partitions pid with
77 | patterns -> (pid, List.rev patterns) :: acc)
82 | (_, [], _) :: _ -> true
83 (* if first row has an empty list of patterns, then others have as well *)
86 (* return 2 lists of rows, first one containing homogeneous rows according
87 * to "compatible" below *)
88 let horizontal_split t =
89 let ap, first_row, t', first_row_class =
93 assert false (* are_empty should have been invoked in advance *)
94 | ((_, hd :: _ , _) as row) :: tl -> hd, row, tl, P.classify hd
96 let rec aux prev_t = function
97 | [] -> List.rev prev_t, []
98 | (_, [], _) :: _ -> assert false
99 | ((_, hd :: _, _) as row) :: tl when compatible ap hd ->
100 aux (row :: prev_t) tl
101 | t -> List.rev prev_t, t
103 let rows1, rows2 = aux [first_row] t' in
104 first_row_class, rows1, rows2
106 (* return 2 lists, first one representing first column, second one
107 * representing a new pattern matrix where matched patterns have been moved
109 let vertical_split t =
112 | decls, hd :: tl, pid -> hd :: decls, tl, pid
116 let variable_closure ksucc =
117 (fun matched_terms constructors terms ->
118 (* prerr_endline "variable_closure"; *)
120 | hd :: tl -> ksucc (hd :: matched_terms) constructors tl
123 let success_closure ksucc =
124 (fun matched_terms constructors terms ->
125 (* prerr_endline "success_closure"; *)
126 ksucc matched_terms constructors)
128 let constructor_closure ksuccs =
129 (fun matched_terms constructors terms ->
130 (* prerr_endline "constructor_closure"; *)
134 let tag, subterms = P.tag_of_term t in
136 if subterms = [] then t :: constructors else constructors
138 let k' = List.assoc tag ksuccs in
139 k' matched_terms constructors' (subterms @ tl)
140 with Not_found -> None)
141 | [] -> assert false)
143 let backtrack_closure ksucc kfail =
144 (fun matched_terms constructors terms ->
145 (* prerr_endline "backtrack_closure"; *)
146 match ksucc matched_terms constructors terms with
148 | None -> kfail matched_terms constructors terms)
150 let compiler rows match_cb fail_k =
153 (fun _ _ _ -> fail_k ())
154 else if are_empty t then
155 success_closure (match_cb (matched t))
157 match horizontal_split t with
158 | _, [], _ -> assert false
159 | Variable, t', [] -> variable_closure (aux (vertical_split t'))
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 ksuccs
185 | _, t', t'' -> backtrack_closure (aux t') (aux t'')
187 let t = List.map (fun (p, pid) -> [], [p], pid) rows in
188 let matcher = aux t in
189 (fun term -> matcher [] [] [term])