author  wenzelm 
Fri, 04 Aug 2000 23:01:39 +0200  
changeset 9539  7ff8f3516d54 
parent 9485  e56577a63005 
child 9565  3eb2ea15cc69 
permissions  rwrr 
5824  1 
(* Title: Pure/Isar/method.ML 
2 
ID: $Id$ 

3 
Author: Markus Wenzel, TU Muenchen 

8807  4 
License: GPL (GNU GENERAL PUBLIC LICENSE) 
5824  5 

6 
Proof methods. 

7 
*) 

8 

9 
signature BASIC_METHOD = 

10 
sig 

11 
val print_methods: theory > unit 

12 
val Method: bstring > (Args.src > Proof.context > Proof.method) > string > unit 

13 
end; 

14 

15 
signature METHOD = 

16 
sig 

17 
include BASIC_METHOD 

8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

18 
val print_global_rules: theory > unit 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

19 
val print_local_rules: Proof.context > unit 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

20 
val dest_global: theory attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

21 
val elim_global: theory attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

22 
val intro_global: theory attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

23 
val delrule_global: theory attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

24 
val dest_local: Proof.context attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

25 
val elim_local: Proof.context attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

26 
val intro_local: Proof.context attribute 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

27 
val delrule_local: Proof.context attribute 
6091  28 
val METHOD: (thm list > tactic) > Proof.method 
8372  29 
val METHOD_CASES: 
30 
(thm list > thm > (thm * (string * RuleCases.T) list) Seq.seq) > Proof.method 

5824  31 
val METHOD0: tactic > Proof.method 
32 
val fail: Proof.method 

33 
val succeed: Proof.method 

8167  34 
val defer: int option > Proof.method 
35 
val prefer: int > Proof.method 

7419  36 
val insert_tac: thm list > int > tactic 
7574  37 
val insert: thm list > Proof.method 
7555  38 
val insert_facts: Proof.method 
7601  39 
val unfold: thm list > Proof.method 
7419  40 
val fold: thm list > Proof.method 
9484  41 
val atomize_tac: thm list > int > tactic 
9485  42 
val atomize_goal: thm list > int > thm > thm 
7419  43 
val multi_resolve: thm list > thm > thm Seq.seq 
44 
val multi_resolves: thm list > thm list > thm Seq.seq 

8335  45 
val resolveq_tac: thm Seq.seq > int > tactic 
9294  46 
val resolveq_cases_tac: bool > (thm * string list) Seq.seq 
8372  47 
> int > thm > (thm * (string * RuleCases.T) list) Seq.seq 
6091  48 
val rule_tac: thm list > thm list > int > tactic 
49 
val rule: thm list > Proof.method 

7130  50 
val erule: thm list > Proof.method 
8220  51 
val drule: thm list > Proof.method 
52 
val frule: thm list > Proof.method 

8195  53 
val this: Proof.method 
7555  54 
val assumption: Proof.context > Proof.method 
8351  55 
val set_tactic: (Proof.context > thm list > tactic) > unit 
56 
val tactic: string > Proof.context > Proof.method 

5916  57 
exception METHOD_FAIL of (string * Position.T) * exn 
5824  58 
val method: theory > Args.src > Proof.context > Proof.method 
9539  59 
val add_method: bstring * (Args.src > Proof.context > Proof.method) * string 
60 
> theory > theory 

5824  61 
val add_methods: (bstring * (Args.src > Proof.context > Proof.method) * string) list 
62 
> theory > theory 

5884  63 
val syntax: (Proof.context * Args.T list > 'a * (Proof.context * Args.T list)) > 
8282  64 
Args.src > Proof.context > Proof.context * 'a 
8351  65 
val simple_args: (Args.T list > 'a * Args.T list) 
66 
> ('a > Proof.context > Proof.method) > Args.src > Proof.context > Proof.method 

7555  67 
val ctxt_args: (Proof.context > Proof.method) > Args.src > Proof.context > Proof.method 
5884  68 
val no_args: Proof.method > Args.src > Proof.context > Proof.method 
7268  69 
type modifier 
7601  70 
val sectioned_args: (Proof.context * Args.T list > 'a * (Proof.context * Args.T list)) > 
7268  71 
(Args.T list > modifier * Args.T list) list > 
5884  72 
('a > Proof.context > Proof.method) > Args.src > Proof.context > Proof.method 
7601  73 
val bang_sectioned_args: 
74 
(Args.T list > modifier * Args.T list) list > 

7555  75 
(thm list > Proof.context > Proof.method) > Args.src > Proof.context > Proof.method 
7601  76 
val only_sectioned_args: 
77 
(Args.T list > modifier * Args.T list) list > 

5884  78 
(Proof.context > Proof.method) > Args.src > Proof.context > Proof.method 
8093  79 
val thms_ctxt_args: (thm list > Proof.context > Proof.method) 
80 
> Args.src > Proof.context > Proof.method 

6091  81 
val thms_args: (thm list > Proof.method) > Args.src > Proof.context > Proof.method 
5824  82 
datatype text = 
83 
Basic of (Proof.context > Proof.method)  

84 
Source of Args.src  

85 
Then of text list  

86 
Orelse of text list  

87 
Try of text  

88 
Repeat1 of text 

89 
val refine: text > Proof.state > Proof.state Seq.seq 

8238  90 
val refine_end: text > Proof.state > Proof.state Seq.seq 
5824  91 
val proof: text option > Proof.state > Proof.state Seq.seq 
8966  92 
val local_qed: bool > text option 
6981  93 
> ({kind: string, name: string, thm: thm} > unit) * (thm > unit) 
6736  94 
> Proof.state > Proof.state Seq.seq 
6981  95 
val local_terminal_proof: text * text option 
96 
> ({kind: string, name: string, thm: thm} > unit) * (thm > unit) 

6736  97 
> Proof.state > Proof.state Seq.seq 
6981  98 
val local_default_proof: ({kind: string, name: string, thm: thm} > unit) * (thm > unit) 
6736  99 
> Proof.state > Proof.state Seq.seq 
8966  100 
val local_immediate_proof: ({kind: string, name: string, thm: thm} > unit) * (thm > unit) 
101 
> Proof.state > Proof.state Seq.seq 

102 
val local_done_proof: ({kind: string, name: string, thm: thm} > unit) * (thm > unit) 

103 
> Proof.state > Proof.state Seq.seq 

104 
val global_qed: bool > text option 

105 
> Proof.state > theory * {kind: string, name: string, thm: thm} 

6934  106 
val global_terminal_proof: text * text option 
107 
> Proof.state > theory * {kind: string, name: string, thm: thm} 

8966  108 
val global_default_proof: Proof.state > theory * {kind: string, name: string, thm: thm} 
6532  109 
val global_immediate_proof: Proof.state > theory * {kind: string, name: string, thm: thm} 
8966  110 
val global_done_proof: Proof.state > theory * {kind: string, name: string, thm: thm} 
9539  111 
val goal_args: (Args.T list > 'a * Args.T list) > ('a > int > tactic) 
112 
> Args.src > Proof.context > Proof.method 

113 
val goal_args': (Proof.context * Args.T list > 'a * (Proof.context * Args.T list)) 

114 
> ('a > int > tactic) > Args.src > Proof.context > Proof.method 

5824  115 
val setup: (theory > theory) list 
116 
end; 

117 

118 
structure Method: METHOD = 

119 
struct 

120 

121 

8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

122 
(** global and local rule data **) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

123 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

124 
fun prt_rules kind ths = 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

125 
Pretty.writeln (Pretty.big_list ("standard " ^ kind ^ " rules:") (map Display.pretty_thm ths)); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

126 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

127 
fun print_rules (intro, elim) = 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

128 
(prt_rules "introduction" intro; prt_rules "elimination" elim); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

129 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

130 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

131 
(* theory data kind 'Isar/rules' *) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

132 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

133 
structure GlobalRulesArgs = 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

134 
struct 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

135 
val name = "Isar/rules"; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

136 
type T = thm list * thm list; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

137 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

138 
val empty = ([], []); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

139 
val copy = I; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

140 
val prep_ext = I; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

141 
fun merge ((intro1, elim1), (intro2, elim2)) = 
9418  142 
(Drule.merge_rules (intro1, intro2), Drule.merge_rules (elim1, elim2)); 
8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

143 
fun print _ = print_rules; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

144 
end; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

145 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

146 
structure GlobalRules = TheoryDataFun(GlobalRulesArgs); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

147 
val print_global_rules = GlobalRules.print; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

148 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

149 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

150 
(* proof data kind 'Isar/rules' *) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

151 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

152 
structure LocalRulesArgs = 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

153 
struct 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

154 
val name = "Isar/rules"; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

155 
type T = thm list * thm list; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

156 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

157 
val init = GlobalRules.get; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

158 
fun print _ = print_rules; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

159 
end; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

160 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

161 
structure LocalRules = ProofDataFun(LocalRulesArgs); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

162 
val print_local_rules = LocalRules.print; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

163 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

164 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

165 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

166 
(** attributes **) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

167 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

168 
(* add rules *) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

169 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

170 
local 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

171 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

172 
fun add_rule thm rules = Library.gen_ins Thm.eq_thm (thm, rules); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

173 
fun del_rule thm rules = Library.gen_rem Thm.eq_thm (rules, thm); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

174 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

175 
fun add_dest thm (intro, elim) = (intro, add_rule (Tactic.make_elim thm) elim); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

176 
fun add_elim thm (intro, elim) = (intro, add_rule thm elim); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

177 
fun add_intro thm (intro, elim) = (add_rule thm intro, elim); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

178 
fun delrule thm (intro, elim) = (del_rule thm intro, del_rule thm elim); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

179 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

180 
fun mk_att f g (x, thm) = (f (g thm) x, thm); 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

181 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

182 
in 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

183 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

184 
val dest_global = mk_att GlobalRules.map add_dest; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

185 
val elim_global = mk_att GlobalRules.map add_elim; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

186 
val intro_global = mk_att GlobalRules.map add_intro; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

187 
val delrule_global = mk_att GlobalRules.map delrule; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

188 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

189 
val dest_local = mk_att LocalRules.map add_dest; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

190 
val elim_local = mk_att LocalRules.map add_elim; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

191 
val intro_local = mk_att LocalRules.map add_intro; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

192 
val delrule_local = mk_att LocalRules.map delrule; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

193 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

194 
end; 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

195 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

196 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

197 
(* concrete syntax *) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

198 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

199 
val rule_atts = 
8519  200 
[("dest", (Attrib.no_args dest_global, Attrib.no_args dest_local), "declare destruction rule"), 
201 
("elim", (Attrib.no_args elim_global, Attrib.no_args elim_local), "declare elimination rule"), 

202 
("intro", (Attrib.no_args intro_global, Attrib.no_args intro_local), "declare introduction rule"), 

203 
("delrule", (Attrib.no_args delrule_global, Attrib.no_args delrule_local), "undeclare rule")]; 

8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

204 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

205 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

206 

5824  207 
(** proof methods **) 
208 

8372  209 
(* make methods *) 
5824  210 

6849  211 
val METHOD = Proof.method; 
8372  212 
val METHOD_CASES = Proof.method_cases; 
213 

8966  214 
fun METHOD0 tac = METHOD (fn [] => tac  _ => error "Cannot handle current facts"); 
5824  215 

216 

217 
(* primitive *) 

218 

219 
val fail = METHOD (K no_tac); 

220 
val succeed = METHOD (K all_tac); 

221 

222 

8167  223 
(* shuffle *) 
224 

8240  225 
fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1))); 
8167  226 
fun defer opt_i = METHOD (K (Tactic.defer_tac (if_none opt_i 1))); 
227 

228 

7419  229 
(* insert *) 
230 

231 
local 

5824  232 

6981  233 
fun cut_rule_tac raw_rule = 
234 
let 

235 
val rule = Drule.forall_intr_vars raw_rule; 

236 
val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl; 

7555  237 
in Tactic.rtac (rule COMP revcut_rl) end; 
6981  238 

7419  239 
in 
5824  240 

7419  241 
fun insert_tac [] i = all_tac 
242 
 insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts); 

6981  243 

7555  244 
val insert_facts = METHOD (ALLGOALS o insert_tac); 
7664  245 
fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms)); 
7419  246 

247 
end; 

5824  248 

249 

7601  250 
(* unfold / fold definitions *) 
6532  251 

9484  252 
fun unfold thms = METHOD (fn facts => 
253 
ALLGOALS (insert_tac facts) THEN CHANGED (rewrite_goals_tac thms)); 

254 

255 
fun fold thms = METHOD (fn facts => 

256 
ALLGOALS (insert_tac facts) THEN CHANGED (fold_goals_tac thms)); 

257 

258 

259 
(* atomize metaconnectives *) 

260 

9485  261 
fun atomize_tac rews i thm = 
262 
if Logic.has_meta_prems (#prop (Thm.rep_thm thm)) i then 

263 
Tactic.asm_rewrite_goal_tac (true, false, false) (K no_tac) (Thm.mss_of rews) i thm 

264 
else all_tac thm; 

265 

266 
fun atomize_goal rews i thm = 

267 
(case Seq.pull (atomize_tac rews i thm) of 

268 
None => thm 

269 
 Some (thm', _) => thm'); 

6532  270 

271 

7419  272 
(* multi_resolve *) 
273 

274 
local 

275 

276 
fun res th i rule = 

277 
Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty; 

278 

279 
fun multi_res _ [] rule = Seq.single rule 

280 
 multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule)); 

281 

282 
in 

283 

284 
val multi_resolve = multi_res 1; 

8372  285 
fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules)); 
7419  286 

287 
end; 

288 

289 

8372  290 
(* general rule *) 
5824  291 

8335  292 
fun gen_resolveq_tac tac rules i st = 
8372  293 
Seq.flat (Seq.map (fn rule => tac rule i st) rules); 
294 

295 
val resolveq_tac = gen_resolveq_tac Tactic.rtac; 

8335  296 

9294  297 
fun resolveq_cases_tac opaq = gen_resolveq_tac (fn (rule, cases) => fn i => fn st => 
298 
Seq.map (rpair (RuleCases.make opaq rule cases)) (Tactic.rtac rule i st)); 

8335  299 

300 

8372  301 
(* simple rule *) 
302 

7419  303 
local 
5824  304 

7130  305 
fun gen_rule_tac tac rules [] = tac rules 
8372  306 
 gen_rule_tac tac erules facts = gen_resolveq_tac (tac o single) (multi_resolves facts erules); 
7130  307 

8671  308 
fun gen_rule tac rules = METHOD (HEADGOAL o tac rules); 
8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

309 

9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

310 
fun gen_rule' tac arg_rules ctxt = METHOD (fn facts => 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

311 
let val rules = 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

312 
if not (null arg_rules) then arg_rules 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

313 
else if null facts then #1 (LocalRules.get ctxt) 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

314 
else op @ (LocalRules.get ctxt); 
8671  315 
in HEADGOAL (tac rules facts) end); 
8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

316 

8220  317 
fun setup raw_tac = 
318 
let val tac = gen_rule_tac raw_tac 

319 
in (tac, gen_rule tac, gen_rule' tac) end; 

320 

7419  321 
in 
322 

8220  323 
val (rule_tac, rule, some_rule) = setup Tactic.resolve_tac; 
324 
val (erule_tac, erule, some_erule) = setup Tactic.eresolve_tac; 

325 
val (drule_tac, drule, some_drule) = setup Tactic.dresolve_tac; 

326 
val (frule_tac, frule, some_frule) = setup Tactic.forward_tac; 

5824  327 

7419  328 
end; 
329 

330 

8195  331 
(* this *) 
332 

8671  333 
val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac)); 
8195  334 

335 

336 
(* assumption *) 

7555  337 

338 
fun assm_tac ctxt = 

339 
assume_tac APPEND' resolve_tac (filter Thm.no_prems (ProofContext.prems_of ctxt)); 

7419  340 

7555  341 
fun assumption_tac ctxt [] = assm_tac ctxt 
342 
 assumption_tac _ [fact] = resolve_tac [fact] 

343 
 assumption_tac _ _ = K no_tac; 

7419  344 

8671  345 
fun assumption ctxt = METHOD (HEADGOAL o assumption_tac ctxt); 
7419  346 

347 

9539  348 
(* res_inst_tac etc. *) 
8238  349 

9539  350 
(*Note: insts refer to the implicit (!!) goal context; use at your own risk*) 
8537  351 
fun gen_res_inst tac (quant, (insts, thm)) = 
352 
METHOD (fn facts => (quant (insert_tac facts THEN' tac insts thm))); 

8238  353 

354 
val res_inst = gen_res_inst Tactic.res_inst_tac; 

355 
val eres_inst = gen_res_inst Tactic.eres_inst_tac; 

356 
val dres_inst = gen_res_inst Tactic.dres_inst_tac; 

357 
val forw_inst = gen_res_inst Tactic.forw_inst_tac; 

9539  358 
val cut_inst = gen_res_inst Tactic.cut_inst_tac; 
8238  359 

360 

8329  361 
(* simple Prolog interpreter *) 
362 

363 
fun prolog_tac rules facts = 

364 
DEPTH_SOLVE_1 (HEADGOAL (Tactic.assume_tac APPEND' Tactic.resolve_tac (facts @ rules))); 

365 

366 
val prolog = METHOD o prolog_tac; 

367 

368 

8351  369 
(* ML tactics *) 
370 

371 
val tactic_ref = ref ((fn _ => raise Match): Proof.context > thm list > tactic); 

372 
fun set_tactic f = tactic_ref := f; 

373 

374 
fun tactic txt ctxt = METHOD (fn facts => 

8372  375 
(Context.use_mltext 
8671  376 
("let fun tactic (ctxt: PureIsar.Proof.context) (facts: thm list) : tactic = \ 
8613  377 
\let val thm = PureIsar.ProofContext.get_thm ctxt and thms = PureIsar.ProofContext.get_thms ctxt in\n" 
8372  378 
^ txt ^ 
8613  379 
"\nend in PureIsar.Method.set_tactic tactic end") 
8372  380 
false (Some (ProofContext.theory_of ctxt)); ! tactic_ref ctxt facts)); 
8351  381 

382 

5824  383 

384 
(** methods theory data **) 

385 

386 
(* data kind 'Isar/methods' *) 

387 

388 
structure MethodsDataArgs = 

389 
struct 

390 
val name = "Isar/methods"; 

391 
type T = 

392 
{space: NameSpace.T, 

393 
meths: (((Args.src > Proof.context > Proof.method) * string) * stamp) Symtab.table}; 

394 

395 
val empty = {space = NameSpace.empty, meths = Symtab.empty}; 

6546  396 
val copy = I; 
5824  397 
val prep_ext = I; 
398 
fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) = 

399 
{space = NameSpace.merge (space1, space2), 

400 
meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups => 

401 
error ("Attempt to merge different versions of methods " ^ commas_quote dups)}; 

402 

9222  403 
fun print _ {space, meths} = 
5824  404 
let 
405 
fun prt_meth (name, ((_, comment), _)) = Pretty.block 

6849  406 
[Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment]; 
5824  407 
in 
8720  408 
[Pretty.big_list "methods:" (map prt_meth (NameSpace.cond_extern_table space meths))] 
9222  409 
> Pretty.chunks > Pretty.writeln 
5824  410 
end; 
411 
end; 

412 

413 
structure MethodsData = TheoryDataFun(MethodsDataArgs); 

414 
val print_methods = MethodsData.print; 

7611  415 

5824  416 

417 
(* get methods *) 

418 

5916  419 
exception METHOD_FAIL of (string * Position.T) * exn; 
420 

5824  421 
fun method thy = 
422 
let 

423 
val {space, meths} = MethodsData.get thy; 

424 

5884  425 
fun meth src = 
426 
let 

427 
val ((raw_name, _), pos) = Args.dest_src src; 

428 
val name = NameSpace.intern space raw_name; 

429 
in 

5824  430 
(case Symtab.lookup (meths, name) of 
431 
None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos) 

5916  432 
 Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src)) 
5824  433 
end; 
434 
in meth end; 

435 

436 

9194  437 
(* add_method(s) *) 
5824  438 

439 
fun add_methods raw_meths thy = 

440 
let 

441 
val full = Sign.full_name (Theory.sign_of thy); 

442 
val new_meths = 

443 
map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths; 

444 

445 
val {space, meths} = MethodsData.get thy; 

446 
val space' = NameSpace.extend (space, map fst new_meths); 

447 
val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups => 

448 
error ("Duplicate declaration of method(s) " ^ commas_quote dups); 

449 
in 

450 
thy > MethodsData.put {space = space', meths = meths'} 

451 
end; 

452 

9194  453 
val add_method = add_methods o Library.single; 
454 

5824  455 
(*implicit version*) 
456 
fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]); 

457 

458 

5884  459 

460 
(** method syntax **) 

5824  461 

5884  462 
(* basic *) 
463 

464 
fun syntax (scan: (Proof.context * Args.T list > 'a * (Proof.context * Args.T list))) = 

465 
Args.syntax "method" scan; 

5824  466 

8351  467 
fun simple_args scan f src ctxt : Proof.method = 
468 
#2 (syntax (Scan.lift (scan >> (fn x => f x ctxt))) src ctxt); 

469 

7555  470 
fun ctxt_args (f: Proof.context > Proof.method) src ctxt = 
8282  471 
#2 (syntax (Scan.succeed (f ctxt)) src ctxt); 
7555  472 

473 
fun no_args m = ctxt_args (K m); 

5884  474 

475 

8351  476 

5884  477 
(* sections *) 
5824  478 

7268  479 
type modifier = (Proof.context > Proof.context) * Proof.context attribute; 
480 

481 
local 

482 

8381  483 
fun sect ss = Scan.first (map Scan.lift ss); 
5884  484 
fun thms ss = Scan.unless (sect ss) Attrib.local_thms; 
485 
fun thmss ss = Scan.repeat (thms ss) >> flat; 

486 

7268  487 
fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]); 
5824  488 

7268  489 
fun section ss = (sect ss  thmss ss) : (fn (m, ths) => Scan.depend (fn ctxt => 
490 
Scan.succeed (apply m (ctxt, ths)))) >> #2; 

5884  491 

7601  492 
fun sectioned args ss = args  Scan.repeat (section ss); 
5884  493 

7268  494 
in 
5824  495 

5884  496 
fun sectioned_args args ss f src ctxt = 
8282  497 
let val (ctxt', (x, _)) = syntax (sectioned args ss) src ctxt 
5921  498 
in f x ctxt' end; 
5884  499 

7601  500 
fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f; 
501 
fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f); 

7268  502 

8093  503 
fun thms_ctxt_args f = sectioned_args (thmss []) [] f; 
504 
fun thms_args f = thms_ctxt_args (K o f); 

5824  505 

7268  506 
end; 
507 

5824  508 

9539  509 
(* tactic syntax *) 
8238  510 

511 
val insts = 

9539  512 
Scan.optional 
513 
(Args.enum1 "and" (Scan.lift (Args.name  Args.!!! (Args.$$$ "="  Args.name)))  

514 
Scan.lift (Args.$$$ "in")) []  Attrib.local_thm; 

8238  515 

8537  516 
fun inst_args f = f oo (#2 oo syntax (Args.goal_spec HEADGOAL  insts)); 
517 

518 

9539  519 
fun goal_args' args tac = #2 oo syntax (Args.goal_spec HEADGOAL  args >> 
520 
(fn (quant, s) => METHOD (fn facts => quant (insert_tac facts THEN' tac s)))); 

8537  521 

9539  522 
fun goal_args args tac = goal_args' (Scan.lift args) tac; 
8238  523 

524 

5824  525 

526 
(** method text **) 

527 

528 
(* datatype text *) 

529 

530 
datatype text = 

531 
Basic of (Proof.context > Proof.method)  

532 
Source of Args.src  

533 
Then of text list  

534 
Orelse of text list  

535 
Try of text  

536 
Repeat1 of text; 

537 

538 

539 
(* refine *) 

540 

8238  541 
fun gen_refine f text state = 
5824  542 
let 
543 
val thy = Proof.theory_of state; 

544 

8238  545 
fun eval (Basic mth) = f mth 
546 
 eval (Source src) = f (method thy src) 

5824  547 
 eval (Then txts) = Seq.EVERY (map eval txts) 
548 
 eval (Orelse txts) = Seq.FIRST (map eval txts) 

549 
 eval (Try txt) = Seq.TRY (eval txt) 

550 
 eval (Repeat1 txt) = Seq.REPEAT1 (eval txt); 

551 
in eval text state end; 

552 

8238  553 
val refine = gen_refine Proof.refine; 
554 
val refine_end = gen_refine Proof.refine_end; 

6404  555 

5824  556 

6404  557 
(* structured proof steps *) 
5824  558 

7506  559 
val default_text = Source (Args.src (("default", []), Position.none)); 
8195  560 
val this_text = Basic (K this); 
8966  561 
val done_text = Basic (K (METHOD0 all_tac)); 
7555  562 

8966  563 
fun close_text asm = Basic (fn ctxt => METHOD (K 
564 
(FILTER Thm.no_prems ((if asm then ALLGOALS (assm_tac ctxt) else all_tac) THEN flexflex_tac)))); 

565 

566 
fun finish_text asm None = close_text asm 

567 
 finish_text asm (Some txt) = Then [txt, close_text asm]; 

6872  568 

5824  569 
fun proof opt_text state = 
570 
state 

571 
> Proof.assert_backward 

6404  572 
> refine (if_none opt_text default_text) 
8242  573 
> Seq.map (Proof.goal_facts (K [])) 
5824  574 
> Seq.map Proof.enter_forward; 
575 

8966  576 
fun local_qed asm opt_text = Proof.local_qed (refine (finish_text asm opt_text)); 
577 
fun local_terminal_proof (text, opt_text) pr = 

578 
Seq.THEN (proof (Some text), local_qed true opt_text pr); 

579 
val local_default_proof = local_terminal_proof (default_text, None); 

8195  580 
val local_immediate_proof = local_terminal_proof (this_text, None); 
8966  581 
fun local_done_proof pr = Seq.THEN (proof (Some done_text), local_qed false None pr); 
5824  582 

6872  583 

8966  584 
fun global_qeds asm opt_text = Proof.global_qed (refine (finish_text asm opt_text)); 
5824  585 

8966  586 
fun global_qed asm opt_text state = 
6872  587 
state 
8966  588 
> global_qeds asm opt_text 
6872  589 
> Proof.check_result "Failed to finish proof" state 
590 
> Seq.hd; 

591 

8966  592 
fun global_term_proof asm (text, opt_text) state = 
6872  593 
state 
594 
> proof (Some text) 

595 
> Proof.check_result "Terminal proof method failed" state 

8966  596 
> (Seq.flat o Seq.map (global_qeds asm opt_text)) 
6872  597 
> Proof.check_result "Failed to finish proof (after successful terminal method)" state 
598 
> Seq.hd; 

599 

8966  600 
val global_terminal_proof = global_term_proof true; 
6934  601 
val global_default_proof = global_terminal_proof (default_text, None); 
8966  602 
val global_immediate_proof = global_terminal_proof (this_text, None); 
603 
val global_done_proof = global_term_proof false (done_text, None); 

5824  604 

605 

606 
(** theory setup **) 

607 

9539  608 
(* misc tactic emulations *) 
609 

610 
val subgoal_meth = goal_args (Scan.repeat1 Args.name) Tactic.subgoals_tac; 

611 
val thin_meth = goal_args Args.name Tactic.thin_tac; 

612 
val rename_meth = goal_args (Scan.repeat1 Args.name) Tactic.rename_params_tac; 

613 

614 

5824  615 
(* pure_methods *) 
616 

617 
val pure_methods = 

618 
[("fail", no_args fail, "force failure"), 

619 
("succeed", no_args succeed, "succeed"), 

7574  620 
("", no_args insert_facts, "do nothing, inserting current facts only"), 
9539  621 
("insert", thms_args insert, "insert theorems, ignoring facts (improper)"), 
7601  622 
("unfold", thms_args unfold, "unfold definitions"), 
623 
("fold", thms_args fold, "fold definitions"), 

8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

624 
("default", thms_ctxt_args some_rule, "apply some rule"), 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

625 
("rule", thms_ctxt_args some_rule, "apply some rule"), 
9539  626 
("erule", thms_ctxt_args some_erule, "apply some rule in elimination manner (improper)"), 
627 
("drule", thms_ctxt_args some_drule, "apply some rule in destruct manner (improper)"), 

628 
("frule", thms_ctxt_args some_frule, "apply some rule in forward manner (improper)"), 

8195  629 
("this", no_args this, "apply current facts as rules"), 
8238  630 
("assumption", ctxt_args assumption, "proof by assumption, preferring facts"), 
9539  631 
("rule_tac", inst_args res_inst, "apply rule (dynamic instantiation!)"), 
632 
("erule_tac", inst_args eres_inst, "apply rule in elimination manner (dynamic instantiation!)"), 

633 
("drule_tac", inst_args dres_inst, "apply rule in destruct manner (dynamic instantiation!)"), 

634 
("frule_tac", inst_args forw_inst, "apply rule in forward manner (dynamic instantiation!)"), 

635 
("cut_tac", inst_args cut_inst, "cut rule (dynamic instantiation!)"), 

8537  636 
("subgoal_tac", subgoal_meth, "subgoal_tac emulation (dynamic instantiation!)"), 
9539  637 
("thin_tac", thin_meth, "thin_tac emulation (dynamic instantiation!)"), 
638 
("rename_tac", rename_meth, "rename_tac emulation (dynamic instantiation!)"), 

8351  639 
("prolog", thms_args prolog, "simple prolog interpreter"), 
640 
("tactic", simple_args Args.name tactic, "ML tactic as proof method")]; 

5824  641 

642 

643 
(* setup *) 

644 

8153
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

645 
val setup = 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

646 
[GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts, 
9bdbcb71dc56
maintain standard rules (beware: classical provers provides another version!);
wenzelm
parents:
8093
diff
changeset

647 
MethodsData.init, add_methods pure_methods]; 
5824  648 

649 

650 
end; 

651 

652 

653 
structure BasicMethod: BASIC_METHOD = Method; 

654 
open BasicMethod; 