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
21 module Ast = CicNotationPt
23 let id_tac status = status ;;
24 let print_tac print_status message status =
25 if print_status then pp_status status;
26 prerr_endline message;
32 match status#stack with
34 | ([], _, [], _) :: _ as stack ->
35 (* backward compatibility: do-nothing-dot *)
37 | (g, t, k, tag) :: s ->
38 match filter_open g, k with
40 (([ loc ], t, loc_tl @+ k, tag) :: s)
43 (([ loc ], t, k, tag) :: s)
44 | _ -> fail (lazy "can't use \".\" here")
46 status#set_stack gstatus
49 let branch_tac status =
51 match status#stack with
53 | (g, t, k, tag) :: s ->
54 match init_pos g with (* TODO *)
55 | [] | [ _ ] -> fail (lazy "too few goals to branch");
57 ([ loc ], [], [], `BranchTag) :: (loc_tl, t, k, tag) :: s
59 status#set_stack gstatus
62 let shift_tac status =
64 match status#stack with
65 | (g, t, k, `BranchTag) :: (g', t', k', tag) :: s ->
67 | [] -> fail (lazy "no more goals to shift")
69 (([ loc ], t @+ filter_open g @+ k, [],`BranchTag)
70 :: (loc_tl, t', k', tag) :: s))
71 | _ -> fail (lazy "can't shift goals here")
73 status#set_stack gstatus
76 let pos_tac i_s status =
78 match status#stack with
80 | ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
82 let l_js = List.filter (fun (i, _) -> List.mem i i_s) ([loc] @+ g') in
83 ((l_js, t , [],`BranchTag)
84 :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
85 | _ -> fail (lazy "can't use relative positioning here")
87 status#set_stack gstatus
90 let case_tac lab status =
92 match status#stack with
94 | ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
99 let _,_,metasenv,_,_ = status#obj in
100 match NCicUtils.lookup_meta (goal_of_loc curloc) metasenv with
101 attrs,_,_ when List.mem (`Name lab) attrs -> true
102 | _ -> false) ([loc] @+ g') in
103 ((l_js, t , [],`BranchTag)
104 :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
105 | _ -> fail (lazy "can't use relative positioning here")
107 status#set_stack gstatus
110 let wildcard_tac status =
112 match status#stack with
114 | ([ loc ] , t, [], `BranchTag) :: (g', t', k', tag) :: s
116 (([loc] @+ g', t, [], `BranchTag) :: ([], t', k', tag) :: s)
117 | _ -> fail (lazy "can't use wildcard here")
119 status#set_stack gstatus
122 let merge_tac status =
124 match status#stack with
126 | (g, t, k,`BranchTag) :: (g', t', k', tag) :: s ->
127 ((t @+ filter_open g @+ g' @+ k, t', k', tag) :: s)
128 | _ -> fail (lazy "can't merge goals here")
130 status#set_stack gstatus
133 let focus_tac gs status =
135 match status#stack with
137 | s -> assert(gs <> []);
139 let add_l acc _ _ l = if is_open l then l :: acc else acc in
140 fold ~env:add_l ~cont:add_l ~todo:add_l [] s
144 if not (List.exists (fun l -> goal_of_loc l = g) stack_locs) then
145 fail (lazy (sprintf "goal %d not found (or closed)" g)))
147 (zero_pos gs, [], [], `FocusTag) :: deep_close gs s
149 status#set_stack gstatus
152 let unfocus_tac status =
154 match status#stack with
156 | ([], [], [], `FocusTag) :: s -> s
157 | _ -> fail (lazy "can't unfocus, some goals are still open")
159 status#set_stack gstatus
162 let skip_tac status =
164 match status#stack with
166 | (gl, t, k, tag) :: s ->
167 let gl = List.map switch_of_loc gl in
168 if List.exists (function Open _ -> true | Closed _ -> false) gl then
169 fail (lazy "cannot skip an open goal")
173 status#set_stack gstatus
176 let block_tac l status =
177 List.fold_left (fun status tac -> tac status) status l
181 let compare_statuses ~past ~present =
182 let _,_,past,_,_ = past#obj in
183 let _,_,present,_,_ = present#obj in
184 List.map fst (List.filter (fun (i,_) -> not(List.mem_assoc i past)) present),
185 List.map fst (List.filter (fun (i,_) -> not (List.mem_assoc i present)) past)
190 (* Exec and distribute_tac form a retraction pair:
191 1) exec (distribute_tac low_tac) (s,i) = low_tac (s,i)
192 2) tac [s]::G = G1::...::Gn::G' && G' is G with some goals closed =>
193 distribute_tac (exec tac) [s]::G = (G1@...Gn)::G'
194 3) tac G = distribute_tac (exec tac) G if
195 tac = distribute_tac lowtac
196 4) atomic_tac t === distribute_tac (exec t)
198 Note that executing an high tactic on a set of goals may be stronger
199 than executing the same tactic on those goals, but once at a time
200 (e.g. the tactic could perform a global analysis of the set of goals)
203 let exec tac (low_status : #lowtac_status) g =
204 let stack = [ [0,Open g], [], [], `NoTag ] in
206 (new NTacStatus.status low_status#obj stack)#set_estatus low_status
208 let status = tac status in
209 (low_status#set_estatus status)#set_obj status#obj
212 let distribute_tac tac (status : #tac_status) =
213 match status#stack with
215 | (g, t, k, tag) :: s ->
216 debug_print (lazy ("context length " ^string_of_int (List.length g)));
217 let rec aux s go gc =
220 debug_print (lazy "no selected goals");
223 debug_print (lazy "inner eval tactical");
225 if List.exists ((=) (goal_of_loc loc)) gc then
228 match switch_of_loc loc with
229 | Closed _ -> fail (lazy "cannot apply to a Closed goal")
232 let go', gc' = compare_statuses ~past:s ~present:sn in
233 sn, ((go @+ [n]) @- gc') @+ go', gc @+ gc'
238 (new NTacStatus.status status#obj ())#set_estatus
239 (status :> NEstatus.status) in
240 let s0, go0, gc0 = s0, [], [] in
241 let sn, gon, gcn = aux s0 go0 gc0 g in
242 debug_print (lazy ("opened: "
243 ^ String.concat " " (List.map string_of_int gon)));
244 debug_print (lazy ("closed: "
245 ^ String.concat " " (List.map string_of_int gcn)));
247 (zero_pos gon, t @~- gcn, k @~- gcn, tag) :: deep_close gcn s
249 ((status#set_stack stack)#set_obj(sn:>lowtac_status)#obj)#set_estatus sn
252 let atomic_tac htac : #tac_status as 'a -> 'a = distribute_tac (exec htac) ;;
255 let rec repeat t (status : #tac_status as 'a) : 'a =
256 try repeat t (t status)
257 with NTacStatus.Error _ -> status
259 atomic_tac (repeat t) s
263 let try_tac tac status =
266 with NTacStatus.Error _ ->
270 let first_tac tacl status =
273 (fun tac _ -> try Some (tac status) with NTacStatus.Error _ -> None) tacl
276 | None -> fail (lazy "No tactics left")
280 let exact_tac t : 's tactic = distribute_tac (fun status goal ->
281 let goalty = get_goalty status goal in
282 let status, t = disambiguate status t (Some goalty) (ctx_of goalty) in
283 instantiate status goal t)
286 let assumption_tac status = distribute_tac (fun status goal ->
287 let gty = get_goalty status goal in
288 let context = ctx_of gty in
291 (List.map (fun (name,_) -> exact_tac ("",0,(Ast.Ident (name,None))))
294 exec htac status goal) status
297 let find_in_context name context =
298 let rec aux acc = function
299 | [] -> raise Not_found
300 | (hd,_) :: tl when hd = name -> acc
301 | _ :: tl -> aux (acc + 1) tl
306 let clear_tac names =
307 if names = [] then id_tac
309 distribute_tac (fun status goal ->
310 let goalty = get_goalty status goal in
314 try find_in_context name (ctx_of goalty)
316 fail (lazy ("hypothesis '" ^ name ^ "' not found")))
319 let n,h,metasenv,subst,o = status#obj in
320 let metasenv,subst,_ = NCicMetaSubst.restrict metasenv subst goal js in
321 status#set_obj (n,h,metasenv,subst,o))
324 let generalize0_tac args =
325 if args = [] then id_tac
326 else exact_tac ("",0,Ast.Appl (Ast.Implicit `JustOne :: args))
329 let select0_tac ~where:(wanted,hyps,where) ~job =
330 let found, postprocess =
332 | `Substexpand argsno -> mk_in_scope, mk_out_scope argsno
333 | `Collect l -> (fun s t -> l := t::!l; mk_in_scope s t), mk_out_scope 1
334 | `ChangeWith f -> f,(fun s t -> s, t)
336 distribute_tac (fun status goal ->
337 let goalty = get_goalty status goal in
339 match where with None -> NCic.Implicit `Term | Some where -> where
341 let status, newgoalctx =
343 (fun (name,d as entry) (status,ctx) ->
345 let path = List.assoc name hyps in
349 select_term status ~found ~postprocess (mk_cic_term ctx ty)
351 let status,ty = term_of_cic_term status ty ctx in
352 status,(name,NCic.Decl ty)::ctx
353 | NCic.Def (bo,ty) ->
355 select_term status ~found ~postprocess (mk_cic_term ctx bo)
357 let status,bo = term_of_cic_term status bo ctx in
358 status,(name,NCic.Def (bo,ty))::ctx
360 Not_found -> status, entry::ctx
361 ) (ctx_of goalty) (status,[])
363 let status, newgoalty =
364 select_term status ~found ~postprocess goalty (wanted,path) in
365 (* WARNING: the next two lines simply change the context of newgoalty
366 from the old to the new one. Otherwise mk_meta will do that herself,
367 calling relocate that calls delift. However, newgoalty is now
368 ?[out_scope] and thus the delift would trigger the special unification
369 case, which is wrong now :-( *)
370 let status,newgoalty = term_of_cic_term status newgoalty (ctx_of goalty) in
371 let newgoalty = mk_cic_term newgoalctx newgoalty in
373 let status, instance =
374 mk_meta status newgoalctx (`Decl newgoalty)
376 instantiate status goal instance)
379 let select_tac ~where ~job move_down_hyps =
380 let (wanted,hyps,where) = GrafiteDisambiguate.disambiguate_npattern where in
382 match where with None -> NCic.Implicit `Term | Some where -> where in
383 if not move_down_hyps then
384 select0_tac ~where:(wanted,hyps,Some path) ~job
388 (fun path (name,path_name) -> NCic.Prod ("_",path_name,path))
392 generalize0_tac (List.map (fun (name,_) -> Ast.Ident (name,None)) hyps);
393 select0_tac ~where:(wanted,[],Some path) ~job;
394 clear_tac (List.map fst hyps) ]
397 let generalize_tac ~where =
400 select_tac ~where ~job:(`Collect l) true;
401 print_tac true "ha selezionato?";
402 (fun s -> distribute_tac (fun status goal ->
403 let goalty = get_goalty status goal in
404 let status,canon,rest =
408 _,_,(None,_,_) -> fail (lazy "No term to generalize")
409 | txt,txtlen,(Some what,_,_) ->
411 disambiguate status (txt,txtlen,what) None (ctx_of goalty)
415 | he::tl -> status,he,tl in
418 (fun s t -> unify s (ctx_of goalty) canon t) status rest in
419 let status, canon = term_of_cic_term status canon (ctx_of goalty) in
420 instantiate status goal
421 (mk_cic_term (ctx_of goalty) (NCic.Appl [NCic.Implicit `Term ; canon ]))
427 exact_tac ("",0, Ast.Appl [Ast.Implicit `JustOne; Ast.Implicit `JustOne]);
429 pos_tac [2]; exact_tac t;
430 shift_tac; pos_tac [1]; skip_tac;
434 let lapply_tac (s,n,t) =
435 exact_tac (s,n, Ast.Appl [Ast.Implicit `JustOne; t])
438 let reduce_tac ~reduction ~where =
439 let change status t =
441 | `Normalize perform_delta ->
443 ?delta:(if perform_delta then None else Some max_int) (ctx_of t) t
444 | `Whd perform_delta ->
446 ?delta:(if perform_delta then None else Some max_int) (ctx_of t) t
448 let where = GrafiteDisambiguate.disambiguate_npattern where in
449 select0_tac ~where ~job:(`ChangeWith change)
452 let change_tac ~where ~with_what =
453 let change status t =
454 let status, ww = disambiguate status with_what None (ctx_of t) in
455 let status = unify status (ctx_of t) t ww in
458 let where = GrafiteDisambiguate.disambiguate_npattern where in
459 select0_tac ~where ~job:(`ChangeWith change)
462 let letin_tac ~where ~what:(_,_,w) name =
464 select_tac ~where ~job:(`Substexpand 1) true;
466 ("",0,Ast.LetIn((Ast.Ident (name,None),None),w,Ast.Implicit `JustOne));
470 let apply_tac (s,n,t) =
471 let t = Ast.Appl [t; Ast.Implicit `Vector] in
479 lefts: NCic.term list;
480 rights: NCic.term list;
481 reference: NReference.reference;
485 let analyze_indty_tac ~what indtyref =
486 distribute_tac (fun status goal ->
487 let goalty = get_goalty status goal in
488 let status, what = disambiguate status what None (ctx_of goalty) in
489 let status, ty_what = typeof status (ctx_of what) what in
490 let status, (r,consno,lefts,rights) = analyse_indty status ty_what in
491 let leftno = List.length lefts in
492 let rightno = List.length rights in
494 rightno = rightno; leftno = leftno; consno = consno;
495 lefts = lefts; rights = rights; reference = r;
497 exec id_tac status goal)
500 let sort_of_goal_tac sortref = distribute_tac (fun status goal ->
501 let goalty = get_goalty status goal in
502 let status,sort = typeof status (ctx_of goalty) goalty in
503 let sort = fix_sorts sort in
504 let status, sort = term_of_cic_term status sort (ctx_of goalty) in
509 let elim_tac ~what:(txt,len,what) ~where =
510 let what = txt, len, Ast.Appl [what; Ast.Implicit `Vector] in
511 let indtyinfo = ref None in
512 let sort = ref (NCic.Rel 1) in
513 atomic_tac (block_tac [
514 analyze_indty_tac ~what indtyinfo;
516 ~where ~job:(`Substexpand ((HExtlib.unopt !indtyinfo).rightno+1)) true s);
517 sort_of_goal_tac sort;
519 let ity = HExtlib.unopt !indtyinfo in
520 let NReference.Ref (uri, _) = ity.reference in
522 NUri.name_of_uri uri ^ "_" ^
523 snd (NCicElim.ast_of_sort
524 (match !sort with NCic.Sort x -> x | _ -> assert false))
528 Ast.Appl [ Ast.Ident (name,None) ; Ast.Implicit `Vector ; w ]
530 exact_tac ("",0,eliminator) status) ])
533 let rewrite_tac ~dir ~what:(_,_,what) ~where status =
534 let sortref = ref (NCic.Rel 1) in
535 let status = sort_of_goal_tac sortref status in
536 let suffix = "_" ^ snd (NCicElim.ast_of_sort
537 (match !sortref with NCic.Sort x -> x | _ -> assert false))
541 `LeftToRight -> "eq" ^ suffix ^ "_r"
542 | `RightToLeft -> "eq" ^ suffix
545 [ select_tac ~where ~job:(`Substexpand 1) true;
548 Ast.Appl(Ast.Ident(name,None)::HExtlib.mk_list (Ast.Implicit `JustOne) 5@
555 ("",0,(Ast.Binder (`Lambda,
556 (Ast.Ident (name,None),None),Ast.Implicit `JustOne)));
557 if name = "_" then clear_tac [name] else id_tac ]
560 let cases ~what status goal =
561 let gty = get_goalty status goal in
562 let status, what = disambiguate status what None (ctx_of gty) in
563 let status, ty = typeof status (ctx_of what) what in
564 let status, (ref, consno, _, _) = analyse_indty status ty in
565 let status, what = term_of_cic_term status what (ctx_of gty) in
567 NCic.Match (ref,NCic.Implicit `Term, what,
568 HExtlib.mk_list (NCic.Implicit `Term) consno)
570 let ctx = ctx_of gty in
571 let status,t,ty = refine status ctx (mk_cic_term ctx t) (Some gty) in
572 instantiate status goal t
575 let cases_tac ~what:(txt,len,what) ~where =
576 let what = txt, len, Ast.Appl [what; Ast.Implicit `Vector] in
577 let indtyinfo = ref None in
580 analyze_indty_tac ~what indtyinfo;
582 ~where ~job:(`Substexpand ((HExtlib.unopt !indtyinfo).rightno+1))true s);
583 distribute_tac (cases ~what) ])
587 let name = if name = "_" then "_clearme" else name in
588 block_tac [ intro_tac name;
590 ~where:("",0,(None,[],None))
591 ~what:("",0,Ast.Ident (name,None));
592 if name = "_clearme" then clear_tac ["_clearme"] else id_tac ]
595 let constructor ?(num=1) ~args status goal =
596 if num < 1 then fail (lazy "constructor numbers begin with 1");
597 let gty = get_goalty status goal in
598 let status, (r,_,_,_) = analyse_indty status gty in
599 let ref = NReference.mk_constructor num r in
601 if args = [] then Ast.NRef ref else
602 Ast.Appl (HExtlib.list_concat ~sep:[Ast.Implicit `Vector]
603 ([Ast.NRef ref] :: List.map (fun _,_,x -> [x]) args))
605 exec (apply_tac ("",0,t)) status goal
608 let constructor_tac ?num ~args = distribute_tac (constructor ?num ~args);;
610 let assert0_tac (hyps,concl) = distribute_tac (fun status goal ->
611 let gty = get_goalty status goal in
612 let eq status ctx t1 t2 =
613 let status,t1 = disambiguate status t1 None ctx in
614 let status,t1 = apply_subst status ctx t1 in
615 let status,t1 = term_of_cic_term status t1 ctx in
616 let t2 = mk_cic_term ctx t2 in
617 let status,t2 = apply_subst status ctx t2 in
618 let status,t2 = term_of_cic_term status t2 ctx in
619 prerr_endline ("COMPARING: " ^ NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:ctx t1 ^ " vs " ^ NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:ctx t2);
623 let status,gty' = term_of_cic_term status gty (ctx_of gty) in
624 let status = eq status (ctx_of gty) concl gty' in
627 (fun (id1,e1) ((id2,e2) as item) (status,ctx) ->
628 assert (id1=id2 || (prerr_endline (id1 ^ " vs " ^ id2); false));
630 `Decl t1, NCic.Decl t2 ->
631 let status = eq status ctx t1 t2 in
633 | `Def (b1,t1), NCic.Def (b2,t2) ->
634 let status = eq status ctx t1 t2 in
635 let status = eq status ctx b1 b2 in
638 ) hyps (ctx_of gty) (status,[])
640 exec id_tac status goal)
643 let assert_tac seqs status =
644 match status#stack with
647 assert (List.length g = List.length seqs);
650 | [seq] -> assert0_tac seq
654 HExtlib.list_concat ~sep:[shift_tac]
655 (List.map (fun seq -> [assert0_tac seq]) seqs)@
660 let auto ~params:(l,_) status goal =
661 let gty = get_goalty status goal in
662 let n,h,metasenv,subst,o = status#obj in
663 let status,t = term_of_cic_term status gty (ctx_of gty) in
666 (fun (status, l) t ->
667 let status, t = disambiguate status t None (ctx_of gty) in
668 let status, ty = typeof status (ctx_of t) t in
669 let status, t = term_of_cic_term status t (ctx_of gty) in
670 let status, ty = term_of_cic_term status ty (ctx_of ty) in
671 (status, (t,ty) :: l))
675 NCicParamod.nparamod status metasenv subst (ctx_of gty) (NCic.Rel ~-1,t) l
677 | [] -> raise (NTacStatus.Error (lazy "no proof found",None))
678 | (pt, metasenv, subst)::_ ->
679 let status = status#set_obj (n,h,metasenv,subst,o) in
680 instantiate status goal (NTacStatus.mk_cic_term (ctx_of gty) pt)
683 let auto_tac ~params status =
684 distribute_tac (auto ~params) status