+let change_tac ~where ~with_what =
+ let change status t =
+ let status, ww = disambiguate status (ctx_of t) with_what None in
+ let status = unify status (ctx_of t) t ww in
+ status, ww
+ in
+ let where = GrafiteDisambiguate.disambiguate_npattern where in
+ select0_tac ~where ~job:(`ChangeWith change)
+;;
+
+let letin_tac ~where ~what:(_,_,w) name =
+ block_tac [
+ select_tac ~where ~job:(`Substexpand 1) true;
+ exact_tac
+ ("",0,Ast.LetIn((Ast.Ident (name,None),None),w,Ast.Implicit `JustOne));
+ ]
+;;
+
+let apply_tac (s,n,t) =
+ let t = Ast.Appl [t; Ast.Implicit `Vector] in
+ exact_tac (s,n,t)
+;;
+
+type indtyinfo = {
+ rightno: int;
+ leftno: int;
+ consno: int;
+ reference: NReference.reference;
+ }
+;;
+
+let ref_of_indtyinfo iti = iti.reference;;
+
+let analyze_indty_tac ~what indtyref =
+ distribute_tac (fun (status as orig_status) goal ->
+ let goalty = get_goalty status goal in
+ let status, what = disambiguate status (ctx_of goalty) what None in
+ let status, ty_what = typeof status (ctx_of what) what in
+ let status, (r,consno,lefts,rights) = analyse_indty status ty_what in
+ let leftno = List.length lefts in
+ let rightno = List.length rights in
+ indtyref := Some {
+ rightno = rightno; leftno = leftno; consno = consno; reference = r;
+ };
+ exec id_tac orig_status goal)
+;;
+
+let sort_of_goal_tac sortref = distribute_tac (fun status goal ->
+ let goalty = get_goalty status goal in
+ let status,sort = typeof status (ctx_of goalty) goalty in
+ let status, sort = fix_sorts status sort in
+ let status, sort = term_of_cic_term status sort (ctx_of goalty) in
+ sortref := sort;
+ status)
+;;
+
+let elim_tac ~what:(txt,len,what) ~where =
+ let what = txt, len, Ast.Appl [what; Ast.Implicit `Vector] in
+ let indtyinfo = ref None in
+ let sort = ref (NCic.Rel 1) in
+ atomic_tac (block_tac [
+ analyze_indty_tac ~what indtyinfo;
+ (fun s -> select_tac
+ ~where ~job:(`Substexpand ((HExtlib.unopt !indtyinfo).rightno+1)) true s);
+ sort_of_goal_tac sort;
+ (fun status ->
+ let ity = HExtlib.unopt !indtyinfo in
+ let NReference.Ref (uri, _) = ity.reference in
+ let name =
+ NUri.name_of_uri uri ^ "_" ^
+ snd (NCicElim.ast_of_sort
+ (match !sort with NCic.Sort x -> x | _ -> assert false))
+ in
+ let eliminator =
+ let _,_,w = what in
+ Ast.Appl [ Ast.Ident (name,None) ; Ast.Implicit `Vector ; w ]
+ in
+ exact_tac ("",0,eliminator) status) ])
+;;
+
+let rewrite_tac ~dir ~what:(_,_,what) ~where status =
+ let sortref = ref (NCic.Rel 1) in
+ let status = sort_of_goal_tac sortref status in
+ let suffix = "_" ^ snd (NCicElim.ast_of_sort
+ (match !sortref with NCic.Sort x -> x | _ -> assert false))
+ in
+ let name =
+ match dir with
+ `LeftToRight -> "eq" ^ suffix ^ "_r"
+ | `RightToLeft -> "eq" ^ suffix
+ in
+ let what = Ast.Appl [what; Ast.Implicit `Vector] in
+ block_tac
+ [ select_tac ~where ~job:(`Substexpand 2) true;
+ exact_tac
+ ("",0,
+ Ast.Appl(Ast.Ident(name,None)::HExtlib.mk_list (Ast.Implicit `JustOne) 5@
+ [what]))] status
+;;
+
+let intro_tac name =
+ block_tac
+ [ exact_tac
+ ("",0,(Ast.Binder (`Lambda,
+ (Ast.Ident (name,None),None),Ast.Implicit `JustOne)));
+ if name = "_" then clear_tac [name] else id_tac ]
+;;
+
+let name_counter = ref 0;;
+let intros_tac ?names_ref names s =
+ let names_ref, prefix =
+ match names_ref with | None -> ref [], "__" | Some r -> r, "H"
+ in
+ if names = [] then
+ repeat_tac
+ (fun s ->
+ incr name_counter;
+ (* TODO: generate better names *)
+ let name = prefix ^ string_of_int !name_counter in
+ let s = intro_tac name s in
+ names_ref := !names_ref @ [name];
+ s)
+ s
+ else
+ block_tac (List.map intro_tac names) s
+;;
+
+let cases ~what status goal =
+ let gty = get_goalty status goal in
+ let status, what = disambiguate status (ctx_of gty) what None in
+ let status, ty = typeof status (ctx_of what) what in
+ let status, (ref, consno, _, _) = analyse_indty status ty in
+ let status, what = term_of_cic_term status what (ctx_of gty) in
+ let t =
+ NCic.Match (ref,NCic.Implicit `Term, what,
+ HExtlib.mk_list (NCic.Implicit `Term) consno)
+ in
+ instantiate status goal (mk_cic_term (ctx_of gty) t)
+;;
+
+let cases_tac ~what:(txt,len,what) ~where =
+ let what = txt, len, Ast.Appl [what; Ast.Implicit `Vector] in
+ let indtyinfo = ref None in
+ atomic_tac
+ (block_tac [
+ analyze_indty_tac ~what indtyinfo;
+ (fun s -> select_tac
+ ~where ~job:(`Substexpand ((HExtlib.unopt !indtyinfo).rightno+1))true s);
+ distribute_tac (cases ~what) ])
+;;
+
+let case1_tac name =
+ let name = if name = "_" then "_clearme" else name in
+ block_tac [ intro_tac name;
+ cases_tac
+ ~where:("",0,(None,[],None))
+ ~what:("",0,Ast.Ident (name,None));
+ if name = "_clearme" then clear_tac ["_clearme"] else id_tac ]
+;;
+
+let constructor_tac ?(num=1) ~args = distribute_tac (fun status goal ->
+ if num < 1 then fail (lazy "constructor numbers begin with 1");
+ let gty = get_goalty status goal in
+ let status, (r,_,_,_) = analyse_indty status gty in
+ let ref = NReference.mk_constructor num r in
+ let t =
+ if args = [] then Ast.NRef ref else
+ Ast.Appl (HExtlib.list_concat ~sep:[Ast.Implicit `Vector]
+ ([Ast.NRef ref] :: List.map (fun _,_,x -> [x]) args))
+ in
+ exec (apply_tac ("",0,t)) status goal)
+;;
+
+let assert0_tac (hyps,concl) = distribute_tac (fun status goal ->
+ let gty = get_goalty status goal in
+ let eq status ctx t1 t2 =
+ let status,t1 = disambiguate status ctx t1 None in
+ let status,t1 = apply_subst status ctx t1 in
+ let status,t1 = term_of_cic_term status t1 ctx in
+ let t2 = mk_cic_term ctx t2 in
+ let status,t2 = apply_subst status ctx t2 in
+ let status,t2 = term_of_cic_term status t2 ctx in
+ prerr_endline ("COMPARING: " ^ NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:ctx t1 ^ " vs " ^ NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:ctx t2);
+ assert (t1=t2);
+ status
+ in
+ let status,gty' = term_of_cic_term status gty (ctx_of gty) in
+ let status = eq status (ctx_of gty) concl gty' in
+ let status,_ =
+ List.fold_right2
+ (fun (id1,e1) ((id2,e2) as item) (status,ctx) ->
+ assert (id1=id2 || (prerr_endline (id1 ^ " vs " ^ id2); false));
+ match e1,e2 with
+ `Decl t1, NCic.Decl t2 ->
+ let status = eq status ctx t1 t2 in
+ status,item::ctx
+ | `Def (b1,t1), NCic.Def (b2,t2) ->
+ let status = eq status ctx t1 t2 in
+ let status = eq status ctx b1 b2 in
+ status,item::ctx
+ | _ -> assert false
+ ) hyps (ctx_of gty) (status,[])