author  wenzelm 
Fri, 20 Mar 2015 14:48:04 +0100  
changeset 59763  56d2c357e6b5 
parent 59761  558acf0426f1 
child 59767  745f5e43cf92 
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 > 
17 
((indexname * Position.T) * string) list > thm > int > tactic 

18 
val eres_inst_tac: Proof.context > 

19 
((indexname * Position.T) * string) list > thm > int > tactic 

20 
val cut_inst_tac: Proof.context > 

21 
((indexname * Position.T) * string) list > thm > int > tactic 

22 
val forw_inst_tac: Proof.context > 

23 
((indexname * Position.T) * string) list > thm > int > tactic 

24 
val dres_inst_tac: Proof.context > 

25 
((indexname * Position.T) * string) list > thm > int > tactic 

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

26 
val thin_tac: Proof.context > string > int > tactic 
27219  27 
val subgoal_tac: Proof.context > string > int > tactic 
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset

28 
val make_elim_preserve: Proof.context > thm > thm 
59755  29 
val method: 
30 
(Proof.context > ((indexname * Position.T) * string) list > thm > int > tactic) > 

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

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

33 

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

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

36 

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

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

38 

59755  39 
val partition_insts = List.partition (fn (((x, _), _), _) => String.isPrefix "'" x); 
40 

41 
fun error_var msg (xi, pos) = 

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

59754  43 

59755  44 
fun the_sort tvars (xi, pos) : sort = 
45611  45 
(case AList.lookup (op =) tvars xi of 
46 
SOME S => S 

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

48 

59755  49 
fun the_type vars (xi, pos) : typ = 
45611  50 
(case AList.lookup (op =) vars xi of 
51 
SOME T => T 

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

53 

59759  54 
local 
55 

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

56 
fun instantiate inst = 
31977  57 
Term_Subst.instantiate ([], map (fn (xi, t) => ((xi, Term.fastype_of t), t)) inst) #> 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

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

59 

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

60 
fun make_instT f v = 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

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

62 
val T = TVar v; 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

63 
val T' = f T; 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

64 
in if T = T' then NONE else SOME (T, T') end; 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

65 

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

66 
fun make_inst f v = 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

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

68 
val t = Var v; 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

69 
val t' = f t; 
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

70 
in if t aconv t' then NONE else SOME (t, t') end; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

71 

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

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

73 

59759  74 
fun readT ctxt tvars ((xi, pos), s) = 
75 
let 

76 
val S = the_sort tvars (xi, pos); 

77 
val T = Syntax.read_typ ctxt s; 

78 
in 

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

80 
else error_var "Incompatible sort for typ instantiation of " (xi, pos) 

81 
end; 

82 

55156  83 
fun read_termTs ctxt ss Ts = 
25329  84 
let 
85 
fun parse T = if T = propT then Syntax.parse_prop ctxt else Syntax.parse_term ctxt; 

86 
val ts = map2 parse Ts ss; 

87 
val ts' = 

39288  88 
map2 (Type.constraint o Type_Infer.paramify_vars) Ts ts 
55156  89 
> Syntax.check_terms ctxt 
25329  90 
> Variable.polymorphic ctxt; 
91 
val Ts' = map Term.fastype_of ts'; 

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

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

25329  95 

59759  96 
fun read_insts ctxt (tvars, vars) mixed_insts = 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

97 
let 
59754  98 
val (type_insts, term_insts) = partition_insts mixed_insts; 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

99 

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

100 

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

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

102 

59759  103 
val instT1 = Term_Subst.instantiateT (map (readT ctxt tvars) type_insts); 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

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

105 

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

106 

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

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

108 

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

109 
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

110 
val Ts = map (the_type vars1) xs; 
55156  111 
val (ts, inferred) = read_termTs 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

112 

59759  113 
val instT2 = Term_Subst.instantiateT inferred; 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

114 
val vars2 = map (apsnd instT2) vars1; 
59755  115 
val inst2 = instantiate (map #1 xs ~~ ts); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

116 

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

117 

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

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

119 

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

120 
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

121 
val inst_vars = map_filter (make_inst inst2) vars2; 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

122 
in 
59623  123 
(map (apply2 (Thm.ctyp_of ctxt)) inst_tvars, 
124 
map (apply2 (Thm.cterm_of ctxt)) inst_vars) 

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

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

126 

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

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

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

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

130 
> Proof_Context.read_vars fixes > Proof_Context.add_fixes > #2 
58027
dc58ab4d9f44
discontinued odd "temporary" workaround from 2006 (6ac7a4fc32a0), which has no measurable relevance;
wenzelm
parents:
55156
diff
changeset

131 
> Variable.declare_thm thm; 
22692  132 
val tvars = Thm.fold_terms Term.add_tvars thm []; 
133 
val vars = Thm.fold_terms Term.add_vars thm []; 

59759  134 
val insts = read_insts ctxt' (tvars, vars) mixed_insts; 
20343
e093a54bf25e
reworked read_instantiate  separate read_insts;
wenzelm
parents:
20336
diff
changeset

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

136 
Drule.instantiate_normalize insts thm 
55143
04448228381d
explicit eigencontext for attributes "where", "of", and corresponding read_instantiate, instantiate_tac;
wenzelm
parents:
55111
diff
changeset

137 
> singleton (Proof_Context.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

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

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

140 

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

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

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

143 
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

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

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

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

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

149 
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

150 
in where_rule ctxt insts fixes thm end; 
27236  151 

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

152 
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

153 

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

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

155 
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

156 

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

157 

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

158 

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

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

160 

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

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

162 

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

165 
(Scan.lift 
59755  166 
(Parse.and_list (Parse.position Args.var  (Args.$$$ "="  Args.name_inner_syntax)) 
167 
 Parse.for_fixes) >> (fn (insts, fixes) => 

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

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

170 

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

171 

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

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

173 

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

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

175 

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

177 
val concl = Args.$$$ "concl"  Args.colon; 
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 
val insts = 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

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

181 
Scan.optional (concl  Scan.repeat inst) []; 
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 
in 
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

184 

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

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

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

190 

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

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

192 

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

193 

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

194 

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

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

196 

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

198 

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

200 
let 
59759  201 
val (Tinsts, tinsts) = partition_insts mixed_insts; 
202 

59754  203 

59759  204 
(* goal context *) 
205 

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

208 
Logic.strip_params goal 

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

210 
> Term.rename_wrt_term goal 

211 
> rev; 

212 
val (param_names, ctxt') = ctxt 

213 
> Variable.declare_thm thm 

214 
> Thm.fold_terms Variable.declare_constraints st 

215 
> Proof_Context.add_fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) params); 

216 

217 

59759  218 
(* preprocess rule *) 
59754  219 

59759  220 
val tvars = Thm.fold_terms Term.add_tvars thm []; 
221 
val vars = Thm.fold_terms Term.add_vars thm []; 

25333  222 

59759  223 
val Tinsts_env = map (readT ctxt' tvars) Tinsts; 
59755  224 
val (xis, ss) = split_list tinsts; 
59759  225 
val Ts = map (Term_Subst.instantiateT Tinsts_env o the_type vars) xis; 
59754  226 

227 
val (ts, envT) = 

228 
read_termTs (Proof_Context.set_mode Proof_Context.mode_schematic ctxt') ss Ts; 

59759  229 
val envT' = map (fn (v, T) => (TVar v, T)) (envT @ Tinsts_env); 
59754  230 
val cenv = 
59755  231 
map (fn ((xi, _), t) => apply2 (Thm.cterm_of ctxt') (Var (xi, fastype_of t), t)) 
59754  232 
(distinct 
233 
(fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2) 

234 
(xis ~~ ts)); 

235 

25333  236 

59754  237 
(* lift and instantiate rule *) 
238 

239 
val maxidx = Thm.maxidx_of st; 

240 
val paramTs = map #2 params; 

241 
val inc = maxidx + 1; 

242 

243 
fun lift_var (Var ((a, j), T)) = Var ((a, j + inc), paramTs > Logic.incr_tvar inc T) 

244 
 lift_var t = raise TERM ("Variable expected", [t]); 

245 
fun lift_term t = 

246 
fold_rev absfree (param_names ~~ paramTs) (Logic.incr_indexes (paramTs, inc) t); 

247 
fun lift_inst (cv, ct) = (cterm_fun lift_var cv, cterm_fun lift_term ct); 

248 
val lift_tvar = apply2 (Thm.ctyp_of ctxt' o Logic.incr_tvar inc); 

249 

250 
val rule = 

251 
Drule.instantiate_normalize 

252 
(map lift_tvar envT', map lift_inst cenv) 

253 
(Thm.lift_rule cgoal thm); 

254 
in 

255 
compose_tac ctxt' (bires_flag, rule, Thm.nprems_of thm) i 

256 
end) i st; 

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

257 

27120  258 
val res_inst_tac = bires_inst_tac false; 
259 
val eres_inst_tac = bires_inst_tac true; 

260 

261 

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

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

263 

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

264 
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

265 
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

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

268 
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

269 
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

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

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

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

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

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

275 
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

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

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

278 
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

279 

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 
(*instantiate and cut  for atomic fact*) 
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset

281 
fun cut_inst_tac ctxt insts rule = res_inst_tac ctxt insts (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

282 

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 
(*forward tactic applies a rule to an assumption without deleting it*) 
58963
26bf09b95dda
proper context for assume_tac (atac remains as fallback without context);
wenzelm
parents:
58956
diff
changeset

284 
fun forw_inst_tac ctxt insts rule = cut_inst_tac ctxt insts 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

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 
(*dresolve tactic applies a rule to replace an assumption*) 
58950
d07464875dd4
optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents:
58027
diff
changeset

287 
fun dres_inst_tac ctxt insts rule = eres_inst_tac ctxt insts (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

288 

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

289 

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

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

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 
(*deletion of an assumption*) 
59755  293 
fun thin_tac ctxt s = 
294 
eres_inst_tac ctxt [((("V", 0), Position.none), s)] 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

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 
(*Introduce the given proposition as lemma and subgoal*) 
59755  297 
fun subgoal_tac ctxt A = 
298 
DETERM o res_inst_tac ctxt [((("psi", 0), Position.none), A)] 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

299 

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

300 

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

301 

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

303 

30545  304 
fun method inst_tac tac = 
30515  305 
Args.goal_spec  
306 
Scan.optional (Scan.lift 

59755  307 
(Parse.and_list1 (Parse.position Args.var  (Args.$$$ "="  Parse.!!! Args.name_inner_syntax)) 
308 
 Args.$$$ "in")) []  

30515  309 
Attrib.thms >> 
310 
(fn ((quant, insts), thms) => fn ctxt => METHOD (fn facts => 

30551  311 
if null insts then quant (Method.insert_tac facts THEN' tac ctxt thms) 
30515  312 
else 
313 
(case thms of 

314 
[thm] => quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm) 

315 
 _ => error "Cannot have instantiations with multiple rules"))); 

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

316 

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

317 

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

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

319 

53708  320 
(*warning: rule_tac etc. refer to dynamic subgoal context!*) 
321 

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

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

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

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

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

333 
Method.setup @{binding subgoal_tac} 

55111  334 
(Args.goal_spec  Scan.lift (Scan.repeat1 Args.name_inner_syntax) >> 
46461  335 
(fn (quant, props) => fn ctxt => 
336 
SIMPLE_METHOD'' quant (EVERY' (map (subgoal_tac ctxt) props)))) 

30515  337 
"insert subgoal (dynamic instantiation)" #> 
53708  338 
Method.setup @{binding thin_tac} 
55111  339 
(Args.goal_spec  Scan.lift Args.name_inner_syntax >> 
30515  340 
(fn (quant, prop) => fn ctxt => SIMPLE_METHOD'' quant (thin_tac ctxt prop))) 
53171  341 
"remove premise (dynamic instantiation)"); 
20336
aac494583949
Rule instantiations  operations within a rule/subgoal context.
wenzelm
parents:
diff
changeset

342 

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

343 
end; 