]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_tactics/continuationals.ml
b1087713009670ef9bcbedcfe8f1cea40942363c
[helm.git] / matita / components / ng_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 (* $Id$ *)
27
28 open Printf
29
30 let debug = false
31 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
32
33 exception Error of string lazy_t
34 let fail msg = raise (Error msg)
35
36 type goal = int
37
38 module Stack =
39 struct
40   type switch = Open of goal | Closed of goal
41   type locator = int * switch
42   type tag = [ `BranchTag | `FocusTag | `NoTag ]
43   type entry = locator list * locator list * locator list * tag
44   type t = entry list
45
46   let empty = [ [], [], [], `NoTag ]
47
48   let fold ~env ~cont ~todo init stack =
49     let rec aux acc depth =
50       function
51       | [] -> acc
52       | (locs, todos, conts, tag) :: tl ->
53           let acc = List.fold_left (fun acc -> env acc depth tag)  acc locs in
54           let acc = List.fold_left (fun acc -> cont acc depth tag) acc conts in
55           let acc = List.fold_left (fun acc -> todo acc depth tag) acc todos in
56           aux acc (depth + 1) tl
57     in
58     assert (stack <> []);
59     aux init 0 stack
60
61   let iter ~env ~cont ~todo =
62     fold ~env:(fun _ -> env) ~cont:(fun _ -> cont) ~todo:(fun _ -> todo) ()
63
64   let map ~env ~cont ~todo =
65     let depth = ref ~-1 in
66     List.map
67       (fun (s, t, c, tag) ->
68         incr depth;
69         let d = !depth in
70         env d tag s, todo d tag t, cont d tag c, tag)
71
72   let is_open = function _, Open _ -> true | _ -> false
73   let close = function n, Open g -> n, Closed g | l -> l
74   let filter_open = List.filter is_open
75   let is_fresh = 
76     function n, Open _ when n > 0 -> true | _,Closed _ -> true | _ -> false
77   let goal_of_loc = function _, Open g | _, Closed g -> g
78   let goal_of_switch = function Open g | Closed g -> g
79   let switch_of_loc = snd
80
81   let zero_pos = List.map (fun g -> 0, Open g)
82
83   let init_pos locs =
84     let pos = ref 0 in  (* positions are 1-based *)
85     List.map (function _, sw -> incr pos; !pos, sw) locs
86
87   let extract_pos i =
88     let rec aux acc =
89       function
90       | [] -> fail (lazy (sprintf "relative position %d not found" i))
91       | (i', _) as loc :: tl when i = i' -> loc, (List.rev acc) @ tl
92       | hd :: tl -> aux (hd :: acc) tl
93     in
94     aux []
95
96   let deep_close gs =
97     let close _ _ =
98       List.map (fun l -> if List.mem (goal_of_loc l) gs then close l else l)
99     in
100     let rm _ _ = List.filter (fun l -> not (List.mem (goal_of_loc l) gs)) in
101     map ~env:close ~cont:rm ~todo:rm
102
103   let rec find_goal =
104     function
105     | [] -> raise (Failure "Continuationals.find_goal")
106     | (l :: _,   _   ,   _   , _) :: _ -> goal_of_loc l
107     | (  _   ,   _   , l :: _, _) :: _ -> goal_of_loc l
108     | (  _   , l :: _,   _   , _) :: _ -> goal_of_loc l
109     | _ :: tl -> find_goal tl
110
111   let is_empty =
112     function
113     | [] -> assert false
114     | [ [], [], [], `NoTag ] -> true
115     | _ -> false
116
117   let of_nmetasenv metasenv =
118     let goals = List.map (fun (g, _) -> g) metasenv in
119     [ zero_pos goals, [], [], `NoTag ]
120
121   let head_switches =
122     function
123     | (locs, _, _, _) :: _ -> List.map switch_of_loc locs
124     | [] -> assert false
125
126   let head_goals =
127     function
128     | (locs, _, _, _) :: _ -> List.map goal_of_loc locs
129     | [] -> assert false
130
131   let head_tag =
132     function
133     | (_, _, _, tag) :: _ -> tag
134     | [] -> assert false
135
136   let shift_goals =
137     function
138     | _ :: (locs, _, _, _) :: _ -> List.map goal_of_loc locs
139     | [] -> assert false
140     | _ -> []
141
142   let open_goals stack =
143     let add_open acc _ _ l = if is_open l then goal_of_loc l :: acc else acc in
144     List.rev (fold ~env:add_open ~cont:add_open ~todo:add_open [] stack)
145
146   let (@+) = (@)  (* union *)
147
148   let (@-) s1 s2 =  (* difference *)
149     List.fold_right
150       (fun e acc -> if List.mem e s2 then acc else e :: acc)
151       s1 []
152
153   let (@~-) locs gs = (* remove some goals from a locators list *)
154     List.fold_right
155       (fun loc acc -> if List.mem (goal_of_loc loc) gs then acc else loc :: acc)
156       locs []
157
158   let pp stack =
159     let pp_goal = string_of_int in
160     let pp_switch =
161       function Open g -> "o" ^ pp_goal g | Closed g -> "c" ^ pp_goal g
162     in
163     let pp_loc (i, s) = string_of_int i ^ pp_switch s in
164     let pp_env env = sprintf "[%s]" (String.concat ";" (List.map pp_loc env)) in
165     let pp_tag = function `BranchTag -> "B" | `FocusTag -> "F" | `NoTag -> "N" in
166     let pp_stack_entry (env, todo, cont, tag) =
167       sprintf "(%s, %s, %s, %s)" (pp_env env) (pp_env todo) (pp_env cont)
168         (pp_tag tag)
169     in
170     String.concat " :: " (List.map pp_stack_entry stack)
171 end
172
173 module type Status =
174 sig
175   type input_status
176   type output_status
177
178   type tactic
179   val mk_tactic : (input_status -> output_status) -> tactic
180   val apply_tactic : tactic -> input_status -> output_status
181
182   val goals : output_status -> goal list * goal list (** opened, closed goals *)
183   val get_stack : input_status -> Stack.t
184   val set_stack : Stack.t -> output_status -> output_status
185
186   val inject : input_status -> output_status
187   val focus : goal -> output_status -> input_status
188 end
189
190 module type C =
191 sig
192   type input_status
193   type output_status
194   type tactic
195
196   type tactical =
197     | Tactic of tactic
198     | Skip
199
200   type t =
201     | Dot
202     | Semicolon
203
204     | Branch
205     | Shift
206     | Pos of int list
207     | Wildcard
208     | Merge
209
210     | Focus of goal list
211     | Unfocus
212
213     | Tactical of tactical
214
215   val eval: t -> input_status -> output_status
216 end
217
218 module Make (S: Status) =
219 struct
220   open Stack
221
222   type input_status = S.input_status
223   type output_status = S.output_status
224   type tactic = S.tactic
225
226   type tactical =
227     | Tactic of tactic
228     | Skip
229
230   type t =
231     | Dot
232     | Semicolon
233     | Branch
234     | Shift
235     | Pos of int list
236     | Wildcard
237     | Merge
238     | Focus of goal list
239     | Unfocus
240     | Tactical of tactical
241
242   let pp_t =
243     function
244     | Dot -> "Dot"
245     | Semicolon -> "Semicolon"
246     | Branch -> "Branch"
247     | Shift -> "Shift"
248     | Pos i -> "Pos " ^ (String.concat "," (List.map string_of_int i))
249     | Wildcard -> "Wildcard"
250     | Merge -> "Merge"
251     | Focus gs ->
252         sprintf "Focus [%s]" (String.concat "; " (List.map string_of_int gs))
253     | Unfocus -> "Unfocus"
254     | Tactical _ -> "Tactical <abs>"
255
256   let eval_tactical tactical ostatus switch =
257     match tactical, switch with
258     | Tactic tac, Open n ->
259         let ostatus = S.apply_tactic tac (S.focus n ostatus) in
260         let opened, closed = S.goals ostatus in
261         ostatus, opened, closed
262     | Skip, Closed n -> ostatus, [], [n]
263     | Tactic _, Closed _ -> fail (lazy "can't apply tactic to a closed goal")
264     | Skip, Open _ -> fail (lazy "can't skip an open goal")
265
266   let eval cmd istatus =
267     let stack = S.get_stack istatus in
268     debug_print (lazy (sprintf "EVAL CONT %s <- %s" (pp_t cmd) (pp stack)));
269     let new_stack stack = S.inject istatus, stack in
270     let ostatus, stack =
271       match cmd, stack with
272       | _, [] -> assert false
273       | Tactical tac, (g, t, k, tag) :: s ->
274 (* COMMENTED OUT TO ALLOW PARAMODULATION TO DO A 
275  *   auto paramodulation.try assumption.
276  * EVEN IF NO GOALS ARE LEFT OPEN BY AUTO.
277   
278   if g = [] then fail (lazy "can't apply a tactic to zero goals");
279   
280 *)
281           debug_print (lazy ("context length " ^string_of_int (List.length g)));
282           let rec aux s go gc =
283             function
284             | [] -> s, go, gc
285             | loc :: loc_tl ->
286                 debug_print (lazy "inner eval tactical");
287                 let s, go, gc =
288                   if List.exists ((=) (goal_of_loc loc)) gc then
289                     s, go, gc
290                   else
291                     let s, go', gc' = eval_tactical tac s (switch_of_loc loc) in
292                     s, (go @- gc') @+ go', gc @+ gc'
293                 in
294                 aux s go gc loc_tl
295           in
296           let s0, go0, gc0 = S.inject istatus, [], [] in
297           let sn, gon, gcn = aux s0 go0 gc0 g in
298           debug_print (lazy ("opened: "
299             ^ String.concat " " (List.map string_of_int gon)));
300           debug_print (lazy ("closed: "
301             ^ String.concat " " (List.map string_of_int gcn)));
302           let stack =
303             (zero_pos gon, t @~- gcn, k @~- gcn, tag) :: deep_close gcn s
304           in
305           sn, stack
306       | Dot, ([], _, [], _) :: _ ->
307           (* backward compatibility: do-nothing-dot *)
308           new_stack stack
309       | Dot, (g, t, k, tag) :: s ->
310           (match filter_open g, k with
311           | loc :: loc_tl, _ -> new_stack (([ loc ], t, loc_tl @+ k, tag) :: s)
312           | [], loc :: k ->
313               assert (is_open loc);
314               new_stack (([ loc ], t, k, tag) :: s)
315           | _ -> fail (lazy "can't use \".\" here"))
316       | Semicolon, _ -> new_stack stack
317       | Branch, (g, t, k, tag) :: s ->
318           (match init_pos g with
319           | [] | [ _ ] -> fail (lazy "too few goals to branch");
320           | loc :: loc_tl ->
321               new_stack
322                 (([ loc ], [], [], `BranchTag) :: (loc_tl, t, k, tag) :: s))
323       | Shift, (g, t, k, `BranchTag) :: (g', t', k', tag) :: s ->
324           (match g' with
325           | [] -> fail (lazy "no more goals to shift")
326           | loc :: loc_tl ->
327               new_stack
328                 (([ loc ], t @+ filter_open g @+ k, [],`BranchTag)
329                 :: (loc_tl, t', k', tag) :: s))
330       | Shift, _ -> fail (lazy "can't shift goals here")
331       | Pos i_s, ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
332         when is_fresh loc ->
333           let l_js = List.filter (fun (i, _) -> List.mem i i_s) ([loc] @+ g') in
334           new_stack
335             ((l_js, t , [],`BranchTag)
336              :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
337       | Pos _, _ -> fail (lazy "can't use relative positioning here")
338       | Wildcard, ([ loc ] , t, [], `BranchTag) :: (g', t', k', tag) :: s
339           when is_fresh loc ->
340             new_stack
341               (([loc] @+ g', t, [], `BranchTag)
342                 :: ([], t', k', tag) :: s)
343       | Wildcard, _ -> fail (lazy "can't use wildcard here")
344       | Merge, (g, t, k,`BranchTag) :: (g', t', k', tag) :: s ->
345           new_stack ((t @+ filter_open g @+ g' @+ k, t', k', tag) :: s)
346       | Merge, _ -> fail (lazy "can't merge goals here")
347       | Focus [], _ -> assert false
348       | Focus gs, s ->
349           let stack_locs =
350             let add_l acc _ _ l = if is_open l then l :: acc else acc in
351             Stack.fold ~env:add_l ~cont:add_l ~todo:add_l [] s
352           in
353           List.iter
354             (fun g ->
355               if not (List.exists (fun l -> goal_of_loc l = g) stack_locs) then
356                 fail (lazy (sprintf "goal %d not found (or closed)" g)))
357             gs;
358           new_stack ((zero_pos gs, [], [], `FocusTag) :: deep_close gs s)
359       | Unfocus, ([], [], [], `FocusTag) :: s -> new_stack s
360       | Unfocus, _ -> fail (lazy "can't unfocus, some goals are still open")
361     in
362     debug_print (lazy (sprintf "EVAL CONT %s -> %s" (pp_t cmd) (pp stack)));
363     S.set_stack stack ostatus
364 end
365