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/
26 exception Error of string
34 type metasenv = conjecture list
37 val goal_of_conjecture: conjecture -> goal
38 val is_goal_in_metasenv: metasenv -> goal -> bool
41 tactic -> metasenv -> goal ->
42 metasenv * goal list * goal list
45 (** like List.assoc returning a pair: binding matching the given key,
46 * associative list without the returned binding
48 let list_assoc_extract key =
51 | [] -> raise Not_found
52 | (key', _) as hd :: tl when key = key' -> hd, (List.rev acc @ tl)
53 | hd :: tl -> aux (hd :: acc) tl
57 module Make (E:Engine) =
59 type goal_switch = Open of E.goal | Closed of E.goal
60 type 'a stack = 'a list
62 E.metasenv * ((int * goal_switch) list * E.goal list * E.goal list) stack
74 | Select of E.goal list
76 | Tactical of tactical
78 let goal_of = function _, Open g -> g | _, Closed g -> g
79 let is_open = function _, Open _ -> true | _, Closed _ -> false
80 let open_all = L.map (fun p, g -> p, Open g)
84 let complementary part full = (* not really efficient *)
85 L.fold_left (fun acc g -> if L.mem g part then acc else g::acc) full []
87 let close to_close l =
88 L.map (function p, Open g when L.mem g to_close -> p, Closed g | g -> g) l
90 let stack_map f g h stack =
91 L.map (fun (a,b,c) -> f a, g b, h c) stack
94 let add_dummy_pos g = dummy_pos, g
95 let map_dummy_pos = List.map add_dummy_pos
97 let eval_tactical tactical metasenv switch =
98 match tactical, switch with
99 | Tactic tac, Open n -> E.apply_tactic tac metasenv n
100 | Skip, Closed n -> metasenv, [], [n]
101 | Tactic _, Closed _ -> raise (Error "can't apply tactic to a closed goal")
102 | Skip, Open _ -> raise (Error "can't skip an open goal")
104 let eval continuational (status: status) =
105 match continuational, status with
106 | Tactical t, (metasenv, (env, todo, left)::tail) ->
107 assert (L.length env > 1);
108 let metasenv, opened, closed =
110 (fun (metasenv, opened, closed) cur_goal ->
111 if L.exists ((=) (goal_of cur_goal)) closed then
112 metasenv, opened, closed
114 let metasenv, newopened, newclosed =
115 eval_tactical t metasenv (snd cur_goal)
118 union (complementary newclosed opened) newopened,
119 union closed newclosed
120 ) (metasenv, [],[]) env
123 let stack_opened = List.map (fun g -> incr pos; !pos, g) opened in
125 (open_all stack_opened,
126 complementary closed todo,
127 complementary opened left)
129 (close closed) (complementary closed) (complementary opened) tail
130 | Tactical _, (_, []) -> assert false
131 | Semicolon, (metasenv, stack) -> metasenv, stack
132 | Dot, (metasenv, (env, todo, left)::tail) ->
134 match L.filter is_open env, left with
135 | [], [] -> raise (Error "There is nothig to do for '.'")
136 | g::env,left -> [g], union (L.map goal_of env) left
137 | [], g::left -> [dummy_pos, Open g], left
139 metasenv, (env, todo, left)::tail
140 | Dot, (_, []) -> assert false
141 | Branch, (metasenv, (g1::tl, todo, left)::tail) ->
142 assert (L.length tl >= 1);
143 metasenv, ([g1], [], [])::(tl, todo, left)::tail
144 | Branch, (_, _) -> raise (Error "can't branch on a singleton context")
145 | Shift opt_pos, (metasenv, (leftopen, t, l)::(goals, todo, left)::tail) ->
146 let next_goal, rem_goals =
147 match opt_pos, goals with
148 | None, g1 :: tl -> g1, tl
151 list_assoc_extract pos goals
153 raise (Error (Printf.sprintf "position %d not found" pos)))
154 | None, [] -> raise (Error "no more goals to shift")
156 let t = union t (union (L.map goal_of (L.filter is_open leftopen)) l) in
157 metasenv, ([next_goal], t, [])::(rem_goals, todo,left)::tail
158 | Shift _, (_, _) -> raise (Error "no more goals to shift")
159 | Merge, (metasenv, (leftopen,t,l)::(not_processed,todo,left)::tail) ->
161 (union (open_all (map_dummy_pos t))
162 (union (open_all (map_dummy_pos l))
163 (union not_processed (L.filter is_open leftopen ))))
165 metasenv, (env,todo,left)::tail
167 | Merge, (_, [_]) -> raise (Error "can't merge here")
168 | Select gl, (metasenv, stack) ->
170 (fun g -> if not (E.is_goal_in_metasenv metasenv g) then
171 raise (Error "you can't select a closed goal")) gl;
172 (metasenv, (open_all (map_dummy_pos gl),[],[])::stack)
173 | End_select, (metasenv, stack) ->
175 | ([], [], [])::tail -> metasenv, tail
176 | _ -> raise (Error "select left some goals open"))