]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/continuationals.ml
first check in of continuationals implementation
[helm.git] / helm / ocaml / tactics / continuationals.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 exception Error of string 
27
28 module L = List
29
30 module type Engine =
31 sig
32   type goal
33   type conjecture
34   type metasenv = conjecture list
35   type tactic
36
37   val goal_of_conjecture: conjecture -> goal
38   val is_goal_in_metasenv: metasenv -> goal -> bool
39
40   val apply_tactic:
41     tactic -> metasenv -> goal ->
42       metasenv * goal list * goal list
43 end
44
45 (** like List.assoc returning a pair: binding matching the given key,
46  * associative list without the returned binding
47  * @raise Not_found *)
48 let list_assoc_extract key =
49   let rec aux acc =
50     function
51     | [] -> raise Not_found
52     | (key', _) as hd :: tl when key = key' -> hd, (List.rev acc @ tl)
53     | hd :: tl -> aux (hd :: acc) tl
54   in
55   aux []
56
57 module Make (E:Engine) =
58 struct
59   type goal_switch = Open of E.goal | Closed of E.goal
60   type 'a stack = 'a list
61   type status =
62     E.metasenv * ((int * goal_switch) list * E.goal list * E.goal list) stack
63
64   type tactical =
65     | Tactic of E.tactic
66     | Skip
67
68   type t =
69     | Dot
70     | Semicolon
71     | Branch
72     | Shift of int option
73     | Merge
74     | Select of E.goal list
75     | End_select
76     | Tactical of tactical
77
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)
81     
82   let union a b = a @ b
83   
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 []
86
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
89
90   let stack_map f g h stack =
91     L.map (fun (a,b,c) -> f a, g b, h c) stack
92
93   let dummy_pos = ~-1
94   let add_dummy_pos g = dummy_pos, g
95   let map_dummy_pos = List.map add_dummy_pos
96
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")
103
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 =
109           L.fold_left 
110             (fun (metasenv, opened, closed) cur_goal ->
111               if L.exists ((=) (goal_of cur_goal)) closed then
112                 metasenv, opened, closed 
113               else
114                 let metasenv, newopened, newclosed = 
115                   eval_tactical t metasenv (snd cur_goal)
116                 in
117                 metasenv, 
118                 union (complementary newclosed opened) newopened,
119                 union closed newclosed
120             ) (metasenv, [],[]) env
121         in
122         let pos = ref 0 in
123         let stack_opened = List.map (fun g -> incr pos; !pos, g) opened in
124         metasenv, 
125         (open_all stack_opened,
126          complementary closed todo,
127          complementary opened left)
128         :: stack_map
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) ->
133         let env, left = 
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
138         in
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
149           | Some pos, _ ->
150               (try
151                 list_assoc_extract pos goals
152               with Not_found ->
153                 raise (Error (Printf.sprintf "position %d not found" pos)))
154           | None, [] -> raise (Error "no more goals to shift")
155         in
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) -> 
160         let env = 
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 ))))
164         in
165         metasenv, (env,todo,left)::tail
166     | Merge, (_, [])
167     | Merge, (_, [_]) -> raise (Error "can't merge here")
168     | Select gl, (metasenv, stack) ->
169         List.iter
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) ->
174         (match stack with 
175         | ([], [], [])::tail -> metasenv, tail
176         | _ -> raise (Error "select left some goals open"))
177 end
178