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 open Continuationals.Stack
24 match status.gstatus with
26 | ([], _, [], _) :: _ as stack ->
27 (* backward compatibility: do-nothing-dot *)
29 | (g, t, k, tag) :: s ->
30 match filter_open g, k with
32 (([ loc ], t, loc_tl @+ k, tag) :: s)
35 (([ loc ], t, k, tag) :: s)
36 | _ -> fail (lazy "can't use \".\" here")
38 { status with gstatus = new_gstatus }
41 let branch_tac status =
43 match status.gstatus with
45 | (g, t, k, tag) :: s ->
46 match init_pos g with (* TODO *)
47 | [] | [ _ ] -> fail (lazy "too few goals to branch");
49 ([ loc ], [], [], `BranchTag) :: (loc_tl, t, k, tag) :: s
51 { status with gstatus = new_gstatus }
54 let shift_tac status =
56 match status.gstatus with
57 | (g, t, k, `BranchTag) :: (g', t', k', tag) :: s ->
59 | [] -> fail (lazy "no more goals to shift")
61 (([ loc ], t @+ filter_open g @+ k, [],`BranchTag)
62 :: (loc_tl, t', k', tag) :: s))
63 | _ -> fail (lazy "can't shift goals here")
65 { status with gstatus = new_gstatus }
68 let pos_tac i_s status =
70 match status.gstatus with
72 | ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
74 let l_js = List.filter (fun (i, _) -> List.mem i i_s) ([loc] @+ g') in
75 ((l_js, t , [],`BranchTag)
76 :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
77 | _ -> fail (lazy "can't use relative positioning here")
79 { status with gstatus = new_gstatus }
82 let wildcard_tac status =
84 match status.gstatus with
86 | ([ loc ] , t, [], `BranchTag) :: (g', t', k', tag) :: s
88 (([loc] @+ g', t, [], `BranchTag) :: ([], t', k', tag) :: s)
89 | _ -> fail (lazy "can't use wildcard here")
91 { status with gstatus = new_gstatus }
94 let merge_tac status =
96 match status.gstatus with
98 | (g, t, k,`BranchTag) :: (g', t', k', tag) :: s ->
99 ((t @+ filter_open g @+ g' @+ k, t', k', tag) :: s)
100 | _ -> fail (lazy "can't merge goals here")
102 { status with gstatus = new_gstatus }
105 let focus_tac gs status =
107 match status.gstatus with
109 | s -> assert(gs <> []);
111 let add_l acc _ _ l = if is_open l then l :: acc else acc in
112 fold ~env:add_l ~cont:add_l ~todo:add_l [] s
116 if not (List.exists (fun l -> goal_of_loc l = g) stack_locs) then
117 fail (lazy (sprintf "goal %d not found (or closed)" g)))
119 (zero_pos gs, [], [], `FocusTag) :: deep_close gs s
121 { status with gstatus = new_gstatus }
124 let unfocus_tac status =
126 match status.gstatus with
128 | ([], [], [], `FocusTag) :: s -> s
129 | _ -> fail (lazy "can't unfocus, some goals are still open")
131 { status with gstatus = new_gstatus }
134 let skip_tac status =
136 match status.gstatus with
138 | (gl, t, k, tag) :: s ->
139 let gl = List.map switch_of_loc gl in
140 if List.exists (function Open _ -> true | Closed _ -> false) gl then
141 fail (lazy "cannot skip an open goal")
145 { status with gstatus = new_gstatus }
148 let block_tac l status =
149 List.fold_left (fun status tac -> tac status) status l
152 let compare_statuses ~past ~present =
153 let _,_,past,_,_ = past.pstatus in
154 let _,_,present,_,_ = present.pstatus in
155 List.map fst (List.filter (fun (i,_) -> not (List.mem_assoc i past)) present),
156 List.map fst (List.filter (fun (i,_) -> not (List.mem_assoc i present)) past)
161 (* Exec and distribute_tac form a retraction pair:
162 1) exec (distribute_tac low_tac) (s,i) = low_tac (s,i)
163 2) tac [s]::G = G1::...::Gn::G' && G' is G with some goals closed =>
164 distribute_tac (exec tac) [s]::G = (G1@...Gn)::G'
165 3) tac G = distribute_tac (exec tac) G if
166 tac = distribute_tac lowtac
168 Note that executing an high tactic on a set of goals may be stronger
169 than executing the same tactic on those goals, but once at a time
170 (e.g. the tactic could perform a global analysis of the set of goals)
173 let exec tac low_status g =
174 let stack = [ [0,Open g], [], [], `NoTag ] in
175 let status = tac { gstatus = stack ; istatus = low_status } in
179 let distribute_tac tac status =
180 match status.gstatus with
182 | (g, t, k, tag) :: s ->
183 debug_print (lazy ("context length " ^string_of_int (List.length g)));
184 let rec aux s go gc =
188 debug_print (lazy "inner eval tactical");
190 if List.exists ((=) (goal_of_loc loc)) gc then
193 match switch_of_loc loc with
194 | Closed _ -> fail (lazy "cannot apply to a Closed goal")
197 let go', gc' = compare_statuses ~past:s ~present:sn in
198 sn, (go @- gc') @+ go', gc @+ gc'
202 let s0, go0, gc0 = status.istatus, [], [] in
203 let sn, gon, gcn = aux s0 go0 gc0 g in
204 debug_print (lazy ("opened: "
205 ^ String.concat " " (List.map string_of_int gon)));
206 debug_print (lazy ("closed: "
207 ^ String.concat " " (List.map string_of_int gcn)));
209 (zero_pos gon, t @~- gcn, k @~- gcn, tag) :: deep_close gcn s
211 { gstatus = stack; istatus = sn }
215 let select ~where status goal =
216 let goalty = get_goalty status goal in
217 let (wanted,_,where) = GrafiteDisambiguate.disambiguate_npattern where in
219 match where with None -> NCic.Implicit `Term | Some where -> where
221 let status, newgoalty = select_term status goalty (wanted,path) in
222 let status, instance =
223 mk_meta status (ctx_of newgoalty) (`Decl newgoalty)
225 instantiate status goal instance
228 let select_tac ~where = distribute_tac (select ~where) ;;
230 let exact t status goal =
231 let goalty = get_goalty status goal in
232 let status, t = disambiguate status t (Some goalty) (ctx_of goalty) in
233 instantiate status goal t
236 let exact_tac t = distribute_tac (exact t) ;;
239 let n,h,metasenv,subst,o = status.pstatus in
242 (function (_,(Some tag,_,_,_)) ->
243 tag <> in_scope_tag && tag <> out_scope_tag
249 (function (_,(Some tag,_,_,_)) -> tag = in_scope_tag | _ -> assert false)
252 let metasenv = List.map (fun (i,(_,c,_,t)) -> i,(None,c,t)) in_m @ metasenv in
253 let in_m = List.map fst in_m in
254 let out_m = match out_m with [i] -> i | _ -> assert false in
255 { status with pstatus = n,h,metasenv,subst,o }, in_m, out_m
258 let change ~where ~with_what status goal =
259 let goalty = get_goalty status goal in
260 let (wanted,_,where) = GrafiteDisambiguate.disambiguate_npattern where in
262 match where with None -> NCic.Implicit `Term | Some where -> where
264 let status, newgoalty = select_term status goalty (wanted,path) in
265 let status, in_scope, out_scope = reopen status in
266 let status = List.fold_left (exact with_what) status in_scope in
268 let j,(n,cctx,bo,_) = out_scope in
269 let _ = typeof status (ctx_of goalty) (Obj.magic (n,cctx,bo)) in
271 let n,h,metasenv,subst,o = status.pstatus in
272 let subst = out_scope :: subst in
273 let status = { status with pstatus = n,h,metasenv,subst,o } in
275 let status, instance =
276 mk_meta status (ctx_of newgoalty) (`Decl newgoalty)
278 instantiate status goal instance
281 let apply t status goal = exact t status goal;;
283 let apply_tac t = distribute_tac (apply t) ;;
284 let change_tac ~where ~with_what = distribute_tac (change ~where ~with_what) ;;
286 let elim_tac ~what ~where status =
289 distribute_tac (fun status goal ->
290 let goalty = get_goalty status goal in
292 disambiguate status what None (ctx_of goalty) in
293 let _ty_what = typeof status (ctx_of what) what in
294 (* check inductive... find eliminator *)
295 let w = (*astify what *) CicNotationPt.Ident ("m",None) in
297 CicNotationPt.Implicit;CicNotationPt.Implicit;CicNotationPt.Implicit]
300 CicNotationPt.Appl(CicNotationPt.Ident("nat_ind",None)::holes @ [ w ])
302 exec (apply_tac ("",0,eliminator)) status goal) ]
308 ("",0,(CicNotationPt.Binder (`Lambda,
309 (CicNotationPt.Ident (name,None),None),CicNotationPt.Implicit)))
312 let case ~what status goal =
313 let gty = get_goalty status goal in
314 let status, what = disambiguate status what None (ctx_of gty) in
315 let ty = typeof status (ctx_of what) what in
316 let ref, consno, left, right = analyse_indty status ty in
318 NCic.Match (ref,NCic.Implicit `Term,NCic.Rel 1,
319 HExtlib.mk_list (NCic.Implicit `Term) consno)
321 let ctx = ctx_of gty in
322 let status,t,ty = refine status ctx (mk_cic_term ctx t) None in
323 instantiate status goal t
326 let case_tac ~what = distribute_tac (case ~what);;
329 block_tac [ intro_tac name;
330 case_tac ~what:("",0,CicNotationPt.Ident (name,None)) ]