author  wenzelm 
Mon, 23 Mar 2015 13:30:59 +0100  
changeset 59780  23b67731f4f0 
parent 59775  cdd0f4d0835e 
child 59787  6e2a20486897 
permissions  rwrr 
53707  1 
(* Title: Pure/Tools/rule_insts.ML 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

2 
Author: Makarius 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

3 

53708  4 
Rule instantiations  operations within implicit rule / subgoal context. 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

5 
*) 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

6 

59763  7 
signature RULE_INSTS = 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

8 
sig 
59763  9 
val where_rule: Proof.context > 
10 
((indexname * Position.T) * string) list > 

11 
(binding * string option * mixfix) list > thm > thm 

12 
val of_rule: Proof.context > string option list * string option list > 

13 
(binding * string option * mixfix) list > thm > thm 

14 
val read_instantiate: Proof.context > 

15 
((indexname * Position.T) * string) list > string list > thm > thm 

59755  16 
val res_inst_tac: Proof.context > 
59780  17 
((indexname * Position.T) * string) list > (binding * string option * mixfix) list > thm > 
18 
int > tactic 

59755  19 
val eres_inst_tac: Proof.context > 
59780  20 
((indexname * Position.T) * string) list > (binding * string option * mixfix) list > thm > 
21 
int > tactic 

59755  22 
val cut_inst_tac: Proof.context > 
59780  23 
((indexname * Position.T) * string) list > (binding * string option * mixfix) list > thm > 
24 
int > tactic 

59755  25 
val forw_inst_tac: Proof.context > 
59780  26 
((indexname * Position.T) * string) list > (binding * string option * mixfix) list > thm > 
27 
int > tactic 

59755  28 
val dres_inst_tac: Proof.context > 
59780  29 
((indexname * Position.T) * string) list > (binding * string option * mixfix) list > thm > 
30 
int > tactic 

31 
val thin_tac: Proof.context > string > (binding * string option * mixfix) list > 

32 
int > tactic 

33 
val subgoal_tac: Proof.context > string > (binding * string option * mixfix) list > 

34 
int > tactic 

58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset

35 
val make_elim_preserve: Proof.context > thm > thm 
59755  36 
val method: 
59780  37 
(Proof.context > ((indexname * Position.T) * string) list > 
38 
(binding * string option * mixfix) list > thm > int > tactic) > 

53708  39 
(Proof.context > thm list > int > tactic) > (Proof.context > Proof.method) context_parser 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

40 
end; 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

41 

42806  42 
structure Rule_Insts: RULE_INSTS = 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

43 
struct 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

44 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

45 
(** read instantiations **) 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

46 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

47 
local 
59755  48 

49 
fun error_var msg (xi, pos) = 

50 
error (msg ^ quote (Term.string_of_vname xi) ^ Position.here pos); 

59754  51 

59755  52 
fun the_sort tvars (xi, pos) : sort = 
45611  53 
(case AList.lookup (op =) tvars xi of 
54 
SOME S => S 

59755  55 
 NONE => error_var "No such type variable in theorem: " (xi, pos)); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

56 

59755  57 
fun the_type vars (xi, pos) : typ = 
45611  58 
(case AList.lookup (op =) vars xi of 
59 
SOME T => T 

59755  60 
 NONE => error_var "No such variable in theorem: " (xi, pos)); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

61 

59774  62 
fun read_type ctxt tvars ((xi, pos), s) = 
59759  63 
let 
64 
val S = the_sort tvars (xi, pos); 

65 
val T = Syntax.read_typ ctxt s; 

66 
in 

67 
if Sign.of_sort (Proof_Context.theory_of ctxt) (T, S) then ((xi, S), T) 

59768  68 
else error_var "Bad sort for instantiation of type variable: " (xi, pos) 
59759  69 
end; 
70 

59774  71 
fun read_terms ctxt ss Ts = 
25329  72 
let 
73 
fun parse T = if T = propT then Syntax.parse_prop ctxt else Syntax.parse_term ctxt; 

74 
val ts = map2 parse Ts ss; 

75 
val ts' = 

39288  76 
map2 (Type.constraint o Type_Infer.paramify_vars) Ts ts 
55156  77 
> Syntax.check_terms ctxt 
25329  78 
> Variable.polymorphic ctxt; 
79 
val Ts' = map Term.fastype_of ts'; 

80 
val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty; 

59759  81 
val tyenv' = Vartab.fold (fn (xi, (S, T)) => cons ((xi, S), T)) tyenv []; 
82 
in (ts', tyenv') end; 

25329  83 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

84 
fun make_instT f v = 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

85 
let 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

86 
val T = TVar v; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

87 
val T' = f T; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

88 
in if T = T' then NONE else SOME (v, T') end; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

89 

c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

90 
fun make_inst f v = 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

91 
let 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

92 
val t = Var v; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

93 
val t' = f t; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

94 
in if t aconv t' then NONE else SOME (v, t') end; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

95 

c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

96 
in 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

97 

59769  98 
fun read_insts ctxt mixed_insts thm = 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

99 
let 
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

100 
val (type_insts, term_insts) = 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

101 
List.partition (fn (((x, _), _), _) => String.isPrefix "'" x) mixed_insts; 
59768  102 

59769  103 
val tvars = Thm.fold_terms Term.add_tvars thm []; 
104 
val vars = Thm.fold_terms Term.add_vars thm []; 

20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

105 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

106 
(*explicit type instantiations*) 
59774  107 
val instT1 = Term_Subst.instantiateT (map (read_type ctxt tvars) type_insts); 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

108 
val vars1 = map (apsnd instT1) vars; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

109 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

110 
(*term instantiations*) 
45613
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

111 
val (xs, ss) = split_list term_insts; 
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

112 
val Ts = map (the_type vars1) xs; 
59774  113 
val (ts, inferred) = read_terms ctxt ss Ts; 
45613
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

114 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

115 
(*implicit type instantiations*) 
59759  116 
val instT2 = Term_Subst.instantiateT inferred; 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

117 
val vars2 = map (apsnd instT2) vars1; 
59774  118 
val inst2 = 
119 
Term_Subst.instantiate ([], map2 (fn (xi, _) => fn t => ((xi, Term.fastype_of t), t)) xs ts) 

120 
#> Envir.beta_norm; 

20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

121 

45613
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

122 
val inst_tvars = map_filter (make_instT (instT2 o instT1)) tvars; 
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

123 
val inst_vars = map_filter (make_inst inst2) vars2; 
59769  124 
in (inst_tvars, inst_vars) end; 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

125 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

126 
end; 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

127 

c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

128 

c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

129 

c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

130 
(** forward rules **) 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

131 

55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

132 
fun where_rule ctxt mixed_insts fixes thm = 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

133 
let 
45613
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

134 
val ctxt' = ctxt 
59780  135 
> Variable.declare_thm thm 
136 
> Proof_Context.read_vars fixes > Proof_Context.add_fixes > #2; 

59769  137 
val (inst_tvars, inst_vars) = read_insts ctxt' mixed_insts thm; 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

138 
in 
59769  139 
thm 
140 
> Drule.instantiate_normalize 

141 
(map (apply2 (Thm.ctyp_of ctxt) o apfst TVar) inst_tvars, 

142 
map (apply2 (Thm.cterm_of ctxt) o apfst Var) inst_vars) 

59780  143 
> singleton (Variable.export ctxt' ctxt) 
45613
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

144 
> Rule_Cases.save thm 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

145 
end; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

146 

55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

147 
fun of_rule ctxt (args, concl_args) fixes thm = 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

148 
let 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

149 
fun zip_vars _ [] = [] 
45613
70e5b43535cd
simplified read_instantiate  no longer need to assign values, since rule attributes are now static;
wenzelm
parents:
45611
diff
changeset

150 
 zip_vars (_ :: xs) (NONE :: rest) = zip_vars xs rest 
59755  151 
 zip_vars ((x, _) :: xs) (SOME t :: rest) = ((x, Position.none), t) :: zip_vars xs rest 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

152 
 zip_vars [] _ = error "More instantiations than variables in theorem"; 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

153 
val insts = 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

154 
zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @ 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

155 
zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args; 
55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

156 
in where_rule ctxt insts fixes thm end; 
27236  157 

55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

158 
fun read_instantiate ctxt insts xs = 
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

159 
where_rule ctxt insts (map (fn x => (Binding.name x, NONE, NoSyn)) xs); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

160 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

161 

20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

162 

e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

163 
(** attributes **) 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

164 

20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

165 
(* where: named instantiation *) 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

166 

53171  167 
val _ = Theory.setup 
53708  168 
(Attrib.setup @{binding "where"} 
55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

169 
(Scan.lift 
59755  170 
(Parse.and_list (Parse.position Args.var  (Args.$$$ "="  Args.name_inner_syntax)) 
171 
 Parse.for_fixes) >> (fn (insts, fixes) => 

172 
Thm.rule_attribute (fn context => where_rule (Context.proof_of context) insts fixes))) 

53171  173 
"named instantiation of theorem"); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

174 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

175 

20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

176 
(* of: positional instantiation (terms only) *) 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

177 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

178 
local 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

179 

55111  180 
val inst = Args.maybe Args.name_inner_syntax; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

181 
val concl = Args.$$$ "concl"  Args.colon; 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

182 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

183 
val insts = 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

184 
Scan.repeat (Scan.unless concl inst)  
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

185 
Scan.optional (concl  Scan.repeat inst) []; 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

186 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

187 
in 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

188 

53171  189 
val _ = Theory.setup 
53708  190 
(Attrib.setup @{binding "of"} 
55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

191 
(Scan.lift (insts  Parse.for_fixes) >> (fn (args, fixes) => 
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

192 
Thm.rule_attribute (fn context => of_rule (Context.proof_of context) args fixes))) 
53171  193 
"positional instantiation of theorem"); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

194 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

195 
end; 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

196 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

197 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

198 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

199 
(** tactics **) 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

200 

59754  201 
(* resolution after lifting and instantiation; may refer to parameters of the subgoal *) 
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

202 

59780  203 
fun bires_inst_tac bires_flag ctxt mixed_insts fixes thm i st = CSUBGOAL (fn (cgoal, _) => 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

204 
let 
59775  205 
(* goal parameters *) 
59759  206 

59754  207 
val goal = Thm.term_of cgoal; 
208 
val params = 

209 
Logic.strip_params goal 

210 
(*as they are printed: bound variables with the same name are renamed*) 

211 
> Term.rename_wrt_term goal 

212 
> rev; 

59775  213 
val (param_names, param_ctxt) = ctxt 
59754  214 
> Variable.declare_thm thm 
215 
> Thm.fold_terms Variable.declare_constraints st 

59772  216 
> Proof_Context.add_fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params) 
217 
> Proof_Context.set_mode Proof_Context.mode_schematic; 

59775  218 
val paramTs = map #2 params; 
59754  219 

220 

59780  221 
(* local fixes *) 
222 

223 
val fixes_ctxt = param_ctxt 

224 
> Proof_Context.read_vars fixes > Proof_Context.add_fixes > #2; 

59754  225 

59780  226 
val (inst_tvars, inst_vars) = read_insts fixes_ctxt mixed_insts thm; 
227 

228 

229 
(* lift and instantiate rule *) 

59754  230 

59774  231 
val inc = Thm.maxidx_of st + 1; 
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

232 
fun lift_var ((a, j), T) = 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

233 
Var ((a, j + inc), paramTs > Logic.incr_tvar inc T); 
59754  234 
fun lift_term t = 
59774  235 
fold_rev Term.absfree (param_names ~~ paramTs) 
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

236 
(Logic.incr_indexes (paramTs, inc) t); 
59754  237 

59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

238 
val inst_tvars' = inst_tvars 
59780  239 
> map (apply2 (Thm.ctyp_of fixes_ctxt o Logic.incr_tvar inc) o apfst TVar); 
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

240 
val inst_vars' = inst_vars 
59780  241 
> map (fn (v, t) => apply2 (Thm.cterm_of fixes_ctxt) (lift_var v, lift_term t)); 
59771
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

242 

c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

243 
val thm' = 
c6e60787ffe2
read instantiations uniformly for rules and tactics;
wenzelm
parents:
59770
diff
changeset

244 
Drule.instantiate_normalize (inst_tvars', inst_vars') 
59780  245 
(Thm.lift_rule cgoal thm) 
246 
> singleton (Variable.export fixes_ctxt param_ctxt); 

59754  247 
in 
59775  248 
compose_tac param_ctxt (bires_flag, thm', Thm.nprems_of thm) i 
59754  249 
end) i st; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

250 

27120  251 
val res_inst_tac = bires_inst_tac false; 
252 
val eres_inst_tac = bires_inst_tac true; 

253 

254 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

255 
(* forward resolution *) 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

256 

58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset

257 
fun make_elim_preserve ctxt rl = 
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

258 
let 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

259 
val maxidx = Thm.maxidx_of rl; 
59623  260 
fun cvar xi = Thm.cterm_of ctxt (Var (xi, propT)); 
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

261 
val revcut_rl' = 
43333
2bdec7f430d3
renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents:
42806
diff
changeset

262 
Drule.instantiate_normalize ([], [(cvar ("V", 0), cvar ("V", maxidx + 1)), 
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

263 
(cvar ("W", 0), cvar ("W", maxidx + 1))]) Drule.revcut_rl; 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

264 
in 
52223
5bb6ae8acb87
tuned signature  more explicit flags for lowlevel Thm.bicompose;
wenzelm
parents:
46476
diff
changeset

265 
(case Seq.list_of 
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset

266 
(Thm.bicompose (SOME ctxt) {flatten = true, match = false, incremented = false} 
52223
5bb6ae8acb87
tuned signature  more explicit flags for lowlevel Thm.bicompose;
wenzelm
parents:
46476
diff
changeset

267 
(false, rl, Thm.nprems_of rl) 1 revcut_rl') 
5bb6ae8acb87
tuned signature  more explicit flags for lowlevel Thm.bicompose;
wenzelm
parents:
46476
diff
changeset

268 
of 
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

269 
[th] => th 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

270 
 _ => raise THM ("make_elim_preserve", 1, [rl])) 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

271 
end; 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

272 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

273 
(*instantiate and cut  for atomic fact*) 
59780  274 
fun cut_inst_tac ctxt insts fixes rule = 
275 
res_inst_tac ctxt insts fixes (make_elim_preserve ctxt rule); 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

276 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

277 
(*forward tactic applies a rule to an assumption without deleting it*) 
59780  278 
fun forw_inst_tac ctxt insts fixes rule = 
279 
cut_inst_tac ctxt insts fixes rule THEN' assume_tac ctxt; 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

280 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

281 
(*dresolve tactic applies a rule to replace an assumption*) 
59780  282 
fun dres_inst_tac ctxt insts fixes rule = 
283 
eres_inst_tac ctxt insts fixes (make_elim_preserve ctxt rule); 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

284 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

285 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

286 
(* derived tactics *) 
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

287 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

288 
(*deletion of an assumption*) 
59780  289 
fun thin_tac ctxt s fixes = 
290 
eres_inst_tac ctxt [((("V", 0), Position.none), s)] fixes Drule.thin_rl; 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

291 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

292 
(*Introduce the given proposition as lemma and subgoal*) 
59780  293 
fun subgoal_tac ctxt A fixes = 
294 
DETERM o res_inst_tac ctxt [((("psi", 0), Position.none), A)] fixes cut_rl; 

27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

295 

817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

296 

53708  297 
(* method wrapper *) 
27245
817d34377170
added instantiate_tac, cut_inst_tac, forw_inst_tac, dres_inst_tac, make_elim_preserve (from tactic.ML);
wenzelm
parents:
27236
diff
changeset

298 

30545  299 
fun method inst_tac tac = 
30515  300 
Args.goal_spec  
301 
Scan.optional (Scan.lift 

59780  302 
(Parse.and_list1 
303 
(Parse.position Args.var  (Args.$$$ "="  Parse.!!! Args.name_inner_syntax))  

304 
Parse.for_fixes  Args.$$$ "in")) ([], [])  

30515  305 
Attrib.thms >> 
59780  306 
(fn ((quant, (insts, fixes)), thms) => fn ctxt => METHOD (fn facts => 
307 
if null insts andalso null fixes 

308 
then quant (Method.insert_tac facts THEN' tac ctxt thms) 

30515  309 
else 
310 
(case thms of 

59780  311 
[thm] => quant (Method.insert_tac facts THEN' inst_tac ctxt insts fixes thm) 
30515  312 
 _ => error "Cannot have instantiations with multiple rules"))); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

313 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

314 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

315 
(* setup *) 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

316 

53708  317 
(*warning: rule_tac etc. refer to dynamic subgoal context!*) 
318 

53171  319 
val _ = Theory.setup 
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset

320 
(Method.setup @{binding rule_tac} (method res_inst_tac resolve_tac) 
53708  321 
"apply rule (dynamic instantiation)" #> 
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset

322 
Method.setup @{binding erule_tac} (method eres_inst_tac eresolve_tac) 
30515  323 
"apply rule in elimination manner (dynamic instantiation)" #> 
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset

324 
Method.setup @{binding drule_tac} (method dres_inst_tac dresolve_tac) 
30515  325 
"apply rule in destruct manner (dynamic instantiation)" #> 
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
59058
diff
changeset

326 
Method.setup @{binding frule_tac} (method forw_inst_tac forward_tac) 
30515  327 
"apply rule in forward manner (dynamic instantiation)" #> 
53708  328 
Method.setup @{binding cut_tac} (method cut_inst_tac (K cut_rules_tac)) 
329 
"cut rule (dynamic instantiation)" #> 

330 
Method.setup @{binding subgoal_tac} 

59780  331 
(Args.goal_spec  Scan.lift (Scan.repeat1 Args.name_inner_syntax  Parse.for_fixes) >> 
332 
(fn (quant, (props, fixes)) => fn ctxt => 

333 
SIMPLE_METHOD'' quant (EVERY' (map (fn prop => subgoal_tac ctxt prop fixes) props)))) 

30515  334 
"insert subgoal (dynamic instantiation)" #> 
53708  335 
Method.setup @{binding thin_tac} 
59780  336 
(Args.goal_spec  Scan.lift (Args.name_inner_syntax  Parse.for_fixes) >> 
337 
(fn (quant, (prop, fixes)) => fn ctxt => SIMPLE_METHOD'' quant (thin_tac ctxt prop fixes))) 

338 
"remove premise (dynamic instantiation)"); 

20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

339 

aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

340 
end; 