2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
17 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
19 exception Error of string lazy_t
20 let fail msg = raise (Error msg)
22 type lowtac_status = {
24 lstatus : LexiconEngine.status
27 type lowtactic = lowtac_status * int -> lowtac_status * int list * int list
30 gstatus : Continuationals.Stack.t;
31 istatus : lowtac_status;
34 type tactic = tac_status -> tac_status
36 type tactic_term = CicNotationPt.term Disambiguate.disambiguator_input
38 let pp_tac_status status =
39 prerr_endline (NCicPp.ppobj status.istatus.pstatus)
42 open Continuationals.Stack
46 match status.gstatus with
48 | ([], _, [], _) :: _ as stack ->
49 (* backward compatibility: do-nothing-dot *)
51 | (g, t, k, tag) :: s ->
52 match filter_open g, k with
54 (([ loc ], t, loc_tl @+ k, tag) :: s)
57 (([ loc ], t, k, tag) :: s)
58 | _ -> fail (lazy "can't use \".\" here")
60 { status with gstatus = new_gstatus }
63 let branch_tac status =
65 match status.gstatus with
67 | (g, t, k, tag) :: s ->
68 match init_pos g with (* TODO *)
69 | [] | [ _ ] -> fail (lazy "too few goals to branch");
71 ([ loc ], [], [], `BranchTag) :: (loc_tl, t, k, tag) :: s
73 { status with gstatus = new_gstatus }
76 let shift_tac status =
78 match status.gstatus with
79 | (g, t, k, `BranchTag) :: (g', t', k', tag) :: s ->
81 | [] -> fail (lazy "no more goals to shift")
83 (([ loc ], t @+ filter_open g @+ k, [],`BranchTag)
84 :: (loc_tl, t', k', tag) :: s))
85 | _ -> fail (lazy "can't shift goals here")
87 { status with gstatus = new_gstatus }
90 let pos_tac i_s status =
92 match status.gstatus with
94 | ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
96 let l_js = List.filter (fun (i, _) -> List.mem i i_s) ([loc] @+ g') in
97 ((l_js, t , [],`BranchTag)
98 :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
99 | _ -> fail (lazy "can't use relative positioning here")
101 { status with gstatus = new_gstatus }
104 let wildcard_tac status =
106 match status.gstatus with
108 | ([ loc ] , t, [], `BranchTag) :: (g', t', k', tag) :: s
110 (([loc] @+ g', t, [], `BranchTag) :: ([], t', k', tag) :: s)
111 | _ -> fail (lazy "can't use wildcard here")
113 { status with gstatus = new_gstatus }
116 let merge_tac status =
118 match status.gstatus with
120 | (g, t, k,`BranchTag) :: (g', t', k', tag) :: s ->
121 ((t @+ filter_open g @+ g' @+ k, t', k', tag) :: s)
122 | _ -> fail (lazy "can't merge goals here")
124 { status with gstatus = new_gstatus }
127 let focus_tac gs status =
129 match status.gstatus with
131 | s -> assert(gs <> []);
133 let add_l acc _ _ l = if is_open l then l :: acc else acc in
134 fold ~env:add_l ~cont:add_l ~todo:add_l [] s
138 if not (List.exists (fun l -> goal_of_loc l = g) stack_locs) then
139 fail (lazy (sprintf "goal %d not found (or closed)" g)))
141 (zero_pos gs, [], [], `FocusTag) :: deep_close gs s
143 { status with gstatus = new_gstatus }
146 let unfocus_tac status =
148 match status.gstatus with
150 | ([], [], [], `FocusTag) :: s -> s
151 | _ -> fail (lazy "can't unfocus, some goals are still open")
153 { status with gstatus = new_gstatus }
156 let skip_tac status =
158 match status.gstatus with
160 | (gl, t, k, tag) :: s ->
161 let gl = List.map switch_of_loc gl in
162 if List.exists (function Open _ -> true | Closed _ -> false) gl then
163 fail (lazy "cannot skip an open goal")
167 { status with gstatus = new_gstatus }
170 let fold_tactic tac status =
171 match status.gstatus with
173 | (g, t, k, tag) :: s ->
174 debug_print (lazy ("context length " ^string_of_int (List.length g)));
175 let rec aux s go gc =
179 debug_print (lazy "inner eval tactical");
181 if List.exists ((=) (goal_of_loc loc)) gc then
184 match switch_of_loc loc with
185 | Closed _ -> fail (lazy "cannot apply to a Closed goal")
187 let s, go', gc' = tac (s,n) in
188 s, (go @- gc') @+ go', gc @+ gc'
192 let s0, go0, gc0 = status.istatus, [], [] in
193 let sn, gon, gcn = aux s0 go0 gc0 g in
194 debug_print (lazy ("opened: "
195 ^ String.concat " " (List.map string_of_int gon)));
196 debug_print (lazy ("closed: "
197 ^ String.concat " " (List.map string_of_int gcn)));
199 (zero_pos gon, t @~- gcn, k @~- gcn, tag) :: deep_close gcn s
201 { gstatus = stack; istatus = sn }
204 let compare_menv ~past ~present =
205 List.map fst (List.filter (fun (i,_) -> not (List.mem_assoc i past)) present),
206 List.map fst (List.filter (fun (i,_) -> not (List.mem_assoc i present)) past)
209 let apply t (status,goal) =
210 let uri,height,(metasenv as old_metasenv), subst,obj = status.pstatus in
211 let name,context,gty = List.assoc goal metasenv in
212 let metasenv, subst, lexicon_status, t =
213 GrafiteDisambiguate.disambiguate_nterm (Some gty) status.lstatus context metasenv subst t
215 let subst, metasenv =
216 (goal, (name, context, t, gty)):: subst,
217 List.filter(fun (x,_) -> x <> goal) metasenv
219 let open_goals, closed_goals = compare_menv ~past:old_metasenv ~present:metasenv in
220 let new_pstatus = uri,height,metasenv,subst,obj in
222 prerr_endline ("termine disambiguato: " ^ NCicPp.ppterm ~context ~metasenv ~subst t);
223 prerr_endline ("menv:" ^ NCicPp.ppmetasenv ~subst metasenv);
224 prerr_endline ("subst:" ^ NCicPp.ppsubst subst ~metasenv);
225 prerr_endline "fine napply";
227 { lstatus = lexicon_status; pstatus = new_pstatus }, open_goals, closed_goals
230 let apply_tac t = fold_tactic (apply t) ;;