author  wenzelm 
Wed, 25 Mar 2015 00:22:10 +0100  
changeset 59805  a162f779925a 
parent 59801  ca948c89828e 
child 59825  9fb7661651ad 
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 

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

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

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

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

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

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

33 
int > tactic 

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

35 
int > tactic 

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

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

53708  40 
(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

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

42 

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

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

45 

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

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

47 

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

48 
local 
59755  49 

50 
fun error_var msg (xi, pos) = 

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

59754  52 

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

59755  56 
 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

57 

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

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

62 

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

66 
val T = Syntax.read_typ ctxt s; 

67 
in 

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

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

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

59796  75 
val (ts, ctxt') = fold_map Variable.fix_dummy_patterns (map2 parse Ts ss) ctxt; 
25329  76 
val ts' = 
39288  77 
map2 (Type.constraint o Type_Infer.paramify_vars) Ts ts 
59796  78 
> Syntax.check_terms ctxt' 
79 
> Variable.polymorphic ctxt'; 

25329  80 
val Ts' = map Term.fastype_of ts'; 
81 
val tyenv = fold Type.raw_match (Ts ~~ Ts') Vartab.empty; 

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

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

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

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

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

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

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

90 

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

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

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

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

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

95 
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

96 

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

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

98 

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

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

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

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

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

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

106 

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

107 
(*explicit type instantiations*) 
59774  108 
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

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

110 

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

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

112 
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

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

115 

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

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

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

121 
#> Envir.beta_norm; 

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

122 

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

123 
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

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

126 

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

127 
end; 
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 

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

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

132 

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

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

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

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

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

139 
in 
59769  140 
thm 
141 
> Drule.instantiate_normalize 

59796  142 
(map (apply2 (Thm.ctyp_of ctxt'') o apfst TVar) inst_tvars, 
143 
map (apply2 (Thm.cterm_of ctxt'') o apfst Var) inst_vars) 

144 
> 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

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

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

147 

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

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

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

150 
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

151 
 zip_vars (_ :: xs) (NONE :: rest) = zip_vars xs rest 
59755  152 
 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

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

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

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

156 
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

157 
in where_rule ctxt insts fixes thm end; 
27236  158 

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

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

160 
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

161 

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

162 

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

163 

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

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

165 

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

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

167 

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

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

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

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

175 

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

176 

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

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

178 

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

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

180 

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

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

183 

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

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

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

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

187 

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

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

189 

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

192 
(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

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

195 

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

196 
end; 
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 

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

199 

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

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

201 

59801  202 
val schematic = Attrib.setup_config_bool @{binding rule_insts_schematic} (K true); 
59800  203 

59754  204 
(* 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

205 

59780  206 
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

207 
let 
59775  208 
(* goal parameters *) 
59759  209 

59754  210 
val goal = Thm.term_of cgoal; 
211 
val params = 

212 
Logic.strip_params goal 

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

214 
> Term.rename_wrt_term goal 

215 
> rev; 

59775  216 
val (param_names, param_ctxt) = ctxt 
59754  217 
> Variable.declare_thm thm 
218 
> Thm.fold_terms Variable.declare_constraints st 

59790  219 
> Variable.improper_fixes 
59772  220 
> Proof_Context.add_fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params) 
59790  221 
> Variable.restore_proper_fixes ctxt 
59801  222 
> Config.get ctxt schematic ? Proof_Context.set_mode Proof_Context.mode_schematic; 
59775  223 
val paramTs = map #2 params; 
59754  224 

225 

59780  226 
(* local fixes *) 
227 

59805  228 
val fixes_ctxt = param_ctxt 
229 
> Proof_Context.read_vars fixes > Proof_Context.add_fixes > #2; 

59754  230 

59796  231 
val ((inst_tvars, inst_vars), inst_ctxt) = read_insts thm mixed_insts fixes_ctxt; 
59780  232 

59805  233 
fun add_fixed (Free (x, _)) = 
234 
if Variable.newly_fixed inst_ctxt param_ctxt x 

235 
then insert (op =) x else I 

236 
 add_fixed _ = I; 

237 
val fixed = fold (Term.fold_aterms add_fixed o #2) inst_vars []; 

238 

59780  239 

240 
(* lift and instantiate rule *) 

59754  241 

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

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

244 
Var ((a, j + inc), paramTs > Logic.incr_tvar inc T); 
59754  245 
fun lift_term t = 
59774  246 
fold_rev Term.absfree (param_names ~~ paramTs) 
59787  247 
(Logic.incr_indexes (fixed, paramTs, inc) t); 
59754  248 

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

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

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

253 

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

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

255 
Drule.instantiate_normalize (inst_tvars', inst_vars') 
59780  256 
(Thm.lift_rule cgoal thm) 
59796  257 
> singleton (Variable.export inst_ctxt param_ctxt); 
59754  258 
in 
59775  259 
compose_tac param_ctxt (bires_flag, thm', Thm.nprems_of thm) i 
59754  260 
end) i st; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

261 

27120  262 
val res_inst_tac = bires_inst_tac false; 
263 
val eres_inst_tac = bires_inst_tac true; 

264 

265 

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

266 
(* 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

267 

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

268 
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

269 
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

270 
val maxidx = Thm.maxidx_of rl; 
59623  271 
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

272 
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

273 
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

274 
(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

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

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

277 
(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

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

279 
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

280 
[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

281 
 _ => 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

282 
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

283 

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 
(*instantiate and cut  for atomic fact*) 
59780  285 
fun cut_inst_tac ctxt insts fixes rule = 
286 
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

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 
(*forward tactic applies a rule to an assumption without deleting it*) 
59780  289 
fun forw_inst_tac ctxt insts fixes rule = 
290 
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

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 
(*dresolve tactic applies a rule to replace an assumption*) 
59780  293 
fun dres_inst_tac ctxt insts fixes rule = 
294 
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

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 

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

297 
(* 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

298 

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

299 
(*deletion of an assumption*) 
59780  300 
fun thin_tac ctxt s fixes = 
301 
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

302 

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

303 
(*Introduce the given proposition as lemma and subgoal*) 
59780  304 
fun subgoal_tac ctxt A fixes = 
305 
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

306 

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

307 

53708  308 
(* 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

309 

30545  310 
fun method inst_tac tac = 
30515  311 
Args.goal_spec  
312 
Scan.optional (Scan.lift 

59780  313 
(Parse.and_list1 
314 
(Parse.position Args.var  (Args.$$$ "="  Parse.!!! Args.name_inner_syntax))  

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

30515  316 
Attrib.thms >> 
59780  317 
(fn ((quant, (insts, fixes)), thms) => fn ctxt => METHOD (fn facts => 
318 
if null insts andalso null fixes 

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

30515  320 
else 
321 
(case thms of 

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

324 

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

325 

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

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

327 

53708  328 
(*warning: rule_tac etc. refer to dynamic subgoal context!*) 
329 

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

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

333 
Method.setup @{binding erule_tac} (method eres_inst_tac eresolve_tac) 
30515  334 
"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

335 
Method.setup @{binding drule_tac} (method dres_inst_tac dresolve_tac) 
30515  336 
"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

337 
Method.setup @{binding frule_tac} (method forw_inst_tac forward_tac) 
30515  338 
"apply rule in forward manner (dynamic instantiation)" #> 
53708  339 
Method.setup @{binding cut_tac} (method cut_inst_tac (K cut_rules_tac)) 
340 
"cut rule (dynamic instantiation)" #> 

341 
Method.setup @{binding subgoal_tac} 

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

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

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

349 
"remove premise (dynamic instantiation)"); 

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

350 

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

351 
end; 