author  schirmer 
Tue, 20 Jan 2004 13:56:27 +0100  
changeset 14359  3d9948163018 
parent 14337  e13731554e50 
child 14565  c6dc17aab88a 
permissions  rwrr 
10213  1 
(* Title: HOL/Product_Type.thy 
2 
ID: $Id$ 

3 
Author: Lawrence C Paulson, Cambridge University Computer Laboratory 

4 
Copyright 1992 University of Cambridge 

11777  5 
*) 
10213  6 

11838  7 
header {* Cartesian products *} 
10213  8 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

9 
theory Product_Type = Fun 
11838  10 
files ("Tools/split_rule.ML"): 
11 

12 
subsection {* Unit *} 

13 

14 
typedef unit = "{True}" 

15 
proof 

16 
show "True : ?unit" by blast 

17 
qed 

18 

19 
constdefs 

20 
Unity :: unit ("'(')") 

21 
"() == Abs_unit True" 

22 

23 
lemma unit_eq: "u = ()" 

24 
by (induct u) (simp add: unit_def Unity_def) 

25 

26 
text {* 

27 
Simplification procedure for @{thm [source] unit_eq}. Cannot use 

28 
this rule directly  it loops! 

29 
*} 

30 

31 
ML_setup {* 

13462  32 
val unit_eq_proc = 
33 
let val unit_meta_eq = mk_meta_eq (thm "unit_eq") in 

34 
Simplifier.simproc (Theory.sign_of (the_context ())) "unit_eq" ["x::unit"] 

35 
(fn _ => fn _ => fn t => if HOLogic.is_unit t then None else Some unit_meta_eq) 

36 
end; 

11838  37 

38 
Addsimprocs [unit_eq_proc]; 

39 
*} 

40 

41 
lemma unit_all_eq1: "(!!x::unit. PROP P x) == PROP P ()" 

42 
by simp 

43 

44 
lemma unit_all_eq2: "(!!x::unit. PROP P) == PROP P" 

45 
by (rule triv_forall_equality) 

46 

47 
lemma unit_induct [induct type: unit]: "P () ==> P x" 

48 
by simp 

49 

50 
text {* 

51 
This rewrite counters the effect of @{text unit_eq_proc} on @{term 

52 
[source] "%u::unit. f u"}, replacing it by @{term [source] 

53 
f} rather than by @{term [source] "%u. f ()"}. 

54 
*} 

55 

56 
lemma unit_abs_eta_conv [simp]: "(%u::unit. f ()) = f" 

57 
by (rule ext) simp 

10213  58 

59 

11838  60 
subsection {* Pairs *} 
10213  61 

11777  62 
subsubsection {* Type definition *} 
10213  63 

64 
constdefs 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

65 
Pair_Rep :: "['a, 'b] => ['a, 'b] => bool" 
11032  66 
"Pair_Rep == (%a b. %x y. x=a & y=b)" 
10213  67 

68 
global 

69 

70 
typedef (Prod) 

11838  71 
('a, 'b) "*" (infixr 20) 
11032  72 
= "{f. EX a b. f = Pair_Rep (a::'a) (b::'b)}" 
11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

73 
proof 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

74 
fix a b show "Pair_Rep a b : ?Prod" 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

75 
by blast 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

76 
qed 
10213  77 

12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
11966
diff
changeset

78 
syntax (xsymbols) 
11493  79 
"*" :: "[type, type] => type" ("(_ \<times>/ _)" [21, 20] 20) 
10213  80 
syntax (HTML output) 
11493  81 
"*" :: "[type, type] => type" ("(_ \<times>/ _)" [21, 20] 20) 
10213  82 

11777  83 
local 
10213  84 

11777  85 

86 
subsubsection {* Abstract constants and syntax *} 

87 

88 
global 

10213  89 

90 
consts 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

91 
fst :: "'a * 'b => 'a" 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

92 
snd :: "'a * 'b => 'b" 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

93 
split :: "[['a, 'b] => 'c, 'a * 'b] => 'c" 
14189  94 
curry :: "['a * 'b => 'c, 'a, 'b] => 'c" 
11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

95 
prod_fun :: "['a => 'b, 'c => 'd, 'a * 'c] => 'b * 'd" 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

96 
Pair :: "['a, 'b] => 'a * 'b" 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

97 
Sigma :: "['a set, 'a => 'b set] => ('a * 'b) set" 
10213  98 

11777  99 
local 
10213  100 

11777  101 
text {* 
102 
Patterns  extends predefined type @{typ pttrn} used in 

103 
abstractions. 

104 
*} 

10213  105 

106 
nonterminals 

107 
tuple_args patterns 

108 

109 
syntax 

110 
"_tuple" :: "'a => tuple_args => 'a * 'b" ("(1'(_,/ _'))") 

111 
"_tuple_arg" :: "'a => tuple_args" ("_") 

112 
"_tuple_args" :: "'a => tuple_args => tuple_args" ("_,/ _") 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

113 
"_pattern" :: "[pttrn, patterns] => pttrn" ("'(_,/ _')") 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

114 
"" :: "pttrn => patterns" ("_") 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

115 
"_patterns" :: "[pttrn, patterns] => patterns" ("_,/ _") 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

116 
"@Sigma" ::"[pttrn, 'a set, 'b set] => ('a * 'b) set" ("(3SIGMA _:_./ _)" 10) 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

117 
"@Times" ::"['a set, 'a => 'b set] => ('a * 'b) set" (infixr "<*>" 80) 
10213  118 

119 
translations 

120 
"(x, y)" == "Pair x y" 

121 
"_tuple x (_tuple_args y z)" == "_tuple x (_tuple_arg (_tuple y z))" 

122 
"%(x,y,zs).b" == "split(%x (y,zs).b)" 

123 
"%(x,y).b" == "split(%x y. b)" 

124 
"_abs (Pair x y) t" => "%(x,y).t" 

125 
(* The last rule accommodates tuples in `case C ... (x,y) ... => ...' 

126 
The (x,y) is parsed as `Pair x y' because it is logic, not pttrn *) 

127 

128 
"SIGMA x:A. B" => "Sigma A (%x. B)" 

129 
"A <*> B" => "Sigma A (_K B)" 

130 

14359  131 
(* reconstructs pattern from (nested) splits, avoiding etacontraction of body*) 
132 
(* works best with enclosing "let", if "let" does not avoid etacontraction *) 

133 
print_translation {* 

134 
let fun split_tr' [Abs (x,T,t as (Abs abs))] = 

135 
(* split (%x y. t) => %(x,y) t *) 

136 
let val (y,t') = atomic_abs_tr' abs; 

137 
val (x',t'') = atomic_abs_tr' (x,T,t'); 

138 

139 
in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end 

140 
 split_tr' [Abs (x,T,(s as Const ("split",_)$t))] = 

141 
(* split (%x. (split (%y z. t))) => %(x,y,z). t *) 

142 
let val (Const ("_abs",_)$(Const ("_pattern",_)$y$z)$t') = split_tr' [t]; 

143 
val (x',t'') = atomic_abs_tr' (x,T,t'); 

144 
in Syntax.const "_abs"$ 

145 
(Syntax.const "_pattern"$x'$(Syntax.const "_patterns"$y$z))$t'' end 

146 
 split_tr' [Const ("split",_)$t] = 

147 
(* split (split (%x y z. t)) => %((x,y),z). t *) 

148 
split_tr' [(split_tr' [t])] (* inner split_tr' creates next pattern *) 

149 
 split_tr' [Const ("_abs",_)$x_y$(Abs abs)] = 

150 
(* split (%pttrn z. t) => %(pttrn,z). t *) 

151 
let val (z,t) = atomic_abs_tr' abs; 

152 
in Syntax.const "_abs" $ (Syntax.const "_pattern" $x_y$z) $ t end 

153 
 split_tr' _ = raise Match; 

154 
in [("split", split_tr')] 

155 
end 

156 
*} 

157 

12114
a8e860c86252
eliminated old "symbols" syntax, use "xsymbols" instead;
wenzelm
parents:
11966
diff
changeset

158 
syntax (xsymbols) 
11493  159 
"@Sigma" :: "[pttrn, 'a set, 'b set] => ('a * 'b) set" ("(3\<Sigma> _\<in>_./ _)" 10) 
160 
"@Times" :: "['a set, 'a => 'b set] => ('a * 'b) set" ("_ \<times> _" [81, 80] 80) 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

161 

11032  162 
print_translation {* [("Sigma", dependent_tr' ("@Sigma", "@Times"))] *} 
10213  163 

164 

11777  165 
subsubsection {* Definitions *} 
10213  166 

167 
defs 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

168 
Pair_def: "Pair a b == Abs_Prod(Pair_Rep a b)" 
11451
8abfb4f7bd02
partial restructuring to reduce dependence on Axiom of Choice
paulson
parents:
11425
diff
changeset

169 
fst_def: "fst p == THE a. EX b. p = (a, b)" 
8abfb4f7bd02
partial restructuring to reduce dependence on Axiom of Choice
paulson
parents:
11425
diff
changeset

170 
snd_def: "snd p == THE b. EX a. p = (a, b)" 
11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

171 
split_def: "split == (%c p. c (fst p) (snd p))" 
14189  172 
curry_def: "curry == (%c x y. c (x,y))" 
11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

173 
prod_fun_def: "prod_fun f g == split(%x y.(f(x), g(y)))" 
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

174 
Sigma_def: "Sigma A B == UN x:A. UN y:B(x). {(x, y)}" 
10213  175 

176 

11966  177 
subsubsection {* Lemmas and proof tool setup *} 
11838  178 

179 
lemma ProdI: "Pair_Rep a b : Prod" 

180 
by (unfold Prod_def) blast 

181 

182 
lemma Pair_Rep_inject: "Pair_Rep a b = Pair_Rep a' b' ==> a = a' & b = b'" 

183 
apply (unfold Pair_Rep_def) 

14208  184 
apply (drule fun_cong [THEN fun_cong], blast) 
11838  185 
done 
10213  186 

11838  187 
lemma inj_on_Abs_Prod: "inj_on Abs_Prod Prod" 
188 
apply (rule inj_on_inverseI) 

189 
apply (erule Abs_Prod_inverse) 

190 
done 

191 

192 
lemma Pair_inject: 

193 
"(a, b) = (a', b') ==> (a = a' ==> b = b' ==> R) ==> R" 

194 
proof  

195 
case rule_context [unfolded Pair_def] 

196 
show ?thesis 

197 
apply (rule inj_on_Abs_Prod [THEN inj_onD, THEN Pair_Rep_inject, THEN conjE]) 

198 
apply (rule rule_context ProdI)+ 

199 
. 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

200 
qed 
10213  201 

11838  202 
lemma Pair_eq [iff]: "((a, b) = (a', b')) = (a = a' & b = b')" 
203 
by (blast elim!: Pair_inject) 

204 

205 
lemma fst_conv [simp]: "fst (a, b) = a" 

206 
by (unfold fst_def) blast 

207 

208 
lemma snd_conv [simp]: "snd (a, b) = b" 

209 
by (unfold snd_def) blast 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

210 

11838  211 
lemma fst_eqD: "fst (x, y) = a ==> x = a" 
212 
by simp 

213 

214 
lemma snd_eqD: "snd (x, y) = a ==> y = a" 

215 
by simp 

216 

217 
lemma PairE_lemma: "EX x y. p = (x, y)" 

218 
apply (unfold Pair_def) 

219 
apply (rule Rep_Prod [unfolded Prod_def, THEN CollectE]) 

220 
apply (erule exE, erule exE, rule exI, rule exI) 

221 
apply (rule Rep_Prod_inverse [symmetric, THEN trans]) 

222 
apply (erule arg_cong) 

223 
done 

11032  224 

11838  225 
lemma PairE [cases type: *]: "(!!x y. p = (x, y) ==> Q) ==> Q" 
226 
by (insert PairE_lemma [of p]) blast 

227 

228 
ML_setup {* 

229 
local val PairE = thm "PairE" in 

230 
fun pair_tac s = 

231 
EVERY' [res_inst_tac [("p", s)] PairE, hyp_subst_tac, K prune_params_tac]; 

232 
end; 

233 
*} 

11032  234 

11838  235 
lemma surjective_pairing: "p = (fst p, snd p)" 
236 
 {* Do not add as rewrite rule: invalidates some proofs in IMP *} 

237 
by (cases p) simp 

238 

239 
declare surjective_pairing [symmetric, simp] 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

240 

11838  241 
lemma surj_pair [simp]: "EX x y. z = (x, y)" 
242 
apply (rule exI) 

243 
apply (rule exI) 

244 
apply (rule surjective_pairing) 

245 
done 

246 

247 
lemma split_paired_all: "(!!x. PROP P x) == (!!a b. PROP P (a, b))" 

11820
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

248 
proof 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

249 
fix a b 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

250 
assume "!!x. PROP P x" 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

251 
thus "PROP P (a, b)" . 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

252 
next 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

253 
fix x 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

254 
assume "!!a b. PROP P (a, b)" 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

255 
hence "PROP P (fst x, snd x)" . 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

256 
thus "PROP P x" by simp 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

257 
qed 
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

258 

11838  259 
lemmas split_tupled_all = split_paired_all unit_all_eq2 
260 

261 
text {* 

262 
The rule @{thm [source] split_paired_all} does not work with the 

263 
Simplifier because it also affects premises in congrence rules, 

264 
where this can lead to premises of the form @{text "!!a b. ... = 

265 
?P(a, b)"} which cannot be solved by reflexivity. 

266 
*} 

267 

268 
ML_setup " 

269 
(* replace parameters of product type by individual component parameters *) 

270 
val safe_full_simp_tac = generic_simp_tac true (true, false, false); 

271 
local (* filtering with exists_paired_all is an essential optimization *) 

272 
fun exists_paired_all (Const (\"all\", _) $ Abs (_, T, t)) = 

273 
can HOLogic.dest_prodT T orelse exists_paired_all t 

274 
 exists_paired_all (t $ u) = exists_paired_all t orelse exists_paired_all u 

275 
 exists_paired_all (Abs (_, _, t)) = exists_paired_all t 

276 
 exists_paired_all _ = false; 

277 
val ss = HOL_basic_ss 

278 
addsimps [thm \"split_paired_all\", thm \"unit_all_eq2\", thm \"unit_abs_eta_conv\"] 

279 
addsimprocs [unit_eq_proc]; 

280 
in 

281 
val split_all_tac = SUBGOAL (fn (t, i) => 

282 
if exists_paired_all t then safe_full_simp_tac ss i else no_tac); 

283 
val unsafe_split_all_tac = SUBGOAL (fn (t, i) => 

284 
if exists_paired_all t then full_simp_tac ss i else no_tac); 

285 
fun split_all th = 

286 
if exists_paired_all (#prop (Thm.rep_thm th)) then full_simplify ss th else th; 

287 
end; 

288 

289 
claset_ref() := claset() addSbefore (\"split_all_tac\", split_all_tac); 

290 
" 

291 

292 
lemma split_paired_All [simp]: "(ALL x. P x) = (ALL a b. P (a, b))" 

293 
 {* @{text "[iff]"} is not a good idea because it makes @{text blast} loop *} 

294 
by fast 

295 

14189  296 
lemma curry_split [simp]: "curry (split f) = f" 
297 
by (simp add: curry_def split_def) 

298 

299 
lemma split_curry [simp]: "split (curry f) = f" 

300 
by (simp add: curry_def split_def) 

301 

302 
lemma curryI [intro!]: "f (a,b) ==> curry f a b" 

303 
by (simp add: curry_def) 

304 

14190
609c072edf90
Fixed blunder in the setup of the classical reasoner wrt. the constant
skalberg
parents:
14189
diff
changeset

305 
lemma curryD [dest!]: "curry f a b ==> f (a,b)" 
14189  306 
by (simp add: curry_def) 
307 

14190
609c072edf90
Fixed blunder in the setup of the classical reasoner wrt. the constant
skalberg
parents:
14189
diff
changeset

308 
lemma curryE: "[ curry f a b ; f (a,b) ==> Q ] ==> Q" 
14189  309 
by (simp add: curry_def) 
310 

311 
lemma curry_conv [simp]: "curry f a b = f (a,b)" 

312 
by (simp add: curry_def) 

313 

11838  314 
lemma prod_induct [induct type: *]: "!!x. (!!a b. P (a, b)) ==> P x" 
315 
by fast 

316 

317 
lemma split_paired_Ex [simp]: "(EX x. P x) = (EX a b. P (a, b))" 

318 
by fast 

319 

320 
lemma split_conv [simp]: "split c (a, b) = c a b" 

321 
by (simp add: split_def) 

322 

323 
lemmas split = split_conv  {* for backwards compatibility *} 

324 

325 
lemmas splitI = split_conv [THEN iffD2, standard] 

326 
lemmas splitD = split_conv [THEN iffD1, standard] 

327 

328 
lemma split_Pair_apply: "split (%x y. f (x, y)) = f" 

329 
 {* Subsumes the old @{text split_Pair} when @{term f} is the identity function. *} 

330 
apply (rule ext) 

14208  331 
apply (tactic {* pair_tac "x" 1 *}, simp) 
11838  332 
done 
333 

334 
lemma split_paired_The: "(THE x. P x) = (THE (a, b). P (a, b))" 

335 
 {* Can't be added to simpset: loops! *} 

336 
by (simp add: split_Pair_apply) 

337 

338 
lemma The_split: "The (split P) = (THE xy. P (fst xy) (snd xy))" 

339 
by (simp add: split_def) 

340 

341 
lemma Pair_fst_snd_eq: "!!s t. (s = t) = (fst s = fst t & snd s = snd t)" 

14208  342 
by (simp only: split_tupled_all, simp) 
11838  343 

344 
lemma prod_eqI [intro?]: "fst p = fst q ==> snd p = snd q ==> p = q" 

345 
by (simp add: Pair_fst_snd_eq) 

346 

347 
lemma split_weak_cong: "p = q ==> split c p = split c q" 

348 
 {* Prevents simplification of @{term c}: much faster *} 

349 
by (erule arg_cong) 

350 

351 
lemma split_eta: "(%(x, y). f (x, y)) = f" 

352 
apply (rule ext) 

353 
apply (simp only: split_tupled_all) 

354 
apply (rule split_conv) 

355 
done 

356 

357 
lemma cond_split_eta: "(!!x y. f x y = g (x, y)) ==> (%(x, y). f x y) = g" 

358 
by (simp add: split_eta) 

359 

360 
text {* 

361 
Simplification procedure for @{thm [source] cond_split_eta}. Using 

362 
@{thm [source] split_eta} as a rewrite rule is not general enough, 

363 
and using @{thm [source] cond_split_eta} directly would render some 

364 
existing proofs very inefficient; similarly for @{text 

365 
split_beta}. *} 

366 

367 
ML_setup {* 

368 

369 
local 

370 
val cond_split_eta = thm "cond_split_eta"; 

371 
fun Pair_pat k 0 (Bound m) = (m = k) 

372 
 Pair_pat k i (Const ("Pair", _) $ Bound m $ t) = i > 0 andalso 

373 
m = k+i andalso Pair_pat k (i1) t 

374 
 Pair_pat _ _ _ = false; 

375 
fun no_args k i (Abs (_, _, t)) = no_args (k+1) i t 

376 
 no_args k i (t $ u) = no_args k i t andalso no_args k i u 

377 
 no_args k i (Bound m) = m < k orelse m > k+i 

378 
 no_args _ _ _ = true; 

379 
fun split_pat tp i (Abs (_,_,t)) = if tp 0 i t then Some (i,t) else None 

380 
 split_pat tp i (Const ("split", _) $ Abs (_, _, t)) = split_pat tp (i+1) t 

381 
 split_pat tp i _ = None; 

13480
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset

382 
fun metaeq sg lhs rhs = mk_meta_eq (Tactic.prove sg [] [] 
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset

383 
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs,rhs))) 
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset

384 
(K (simp_tac (HOL_basic_ss addsimps [cond_split_eta]) 1))); 
11838  385 

386 
fun beta_term_pat k i (Abs (_, _, t)) = beta_term_pat (k+1) i t 

387 
 beta_term_pat k i (t $ u) = Pair_pat k i (t $ u) orelse 

388 
(beta_term_pat k i t andalso beta_term_pat k i u) 

389 
 beta_term_pat k i t = no_args k i t; 

390 
fun eta_term_pat k i (f $ arg) = no_args k i f andalso Pair_pat k i arg 

391 
 eta_term_pat _ _ _ = false; 

392 
fun subst arg k i (Abs (x, T, t)) = Abs (x, T, subst arg (k+1) i t) 

393 
 subst arg k i (t $ u) = if Pair_pat k i (t $ u) then incr_boundvars k arg 

394 
else (subst arg k i t $ subst arg k i u) 

395 
 subst arg k i t = t; 

396 
fun beta_proc sg _ (s as Const ("split", _) $ Abs (_, _, t) $ arg) = 

397 
(case split_pat beta_term_pat 1 t of 

398 
Some (i,f) => Some (metaeq sg s (subst arg 0 i f)) 

399 
 None => None) 

400 
 beta_proc _ _ _ = None; 

401 
fun eta_proc sg _ (s as Const ("split", _) $ Abs (_, _, t)) = 

402 
(case split_pat eta_term_pat 1 t of 

403 
Some (_,ft) => Some (metaeq sg s (let val (f $ arg) = ft in f end)) 

404 
 None => None) 

405 
 eta_proc _ _ _ = None; 

406 
in 

13462  407 
val split_beta_proc = Simplifier.simproc (Theory.sign_of (the_context ())) 
408 
"split_beta" ["split f z"] beta_proc; 

409 
val split_eta_proc = Simplifier.simproc (Theory.sign_of (the_context ())) 

410 
"split_eta" ["split f"] eta_proc; 

11838  411 
end; 
412 

413 
Addsimprocs [split_beta_proc, split_eta_proc]; 

414 
*} 

415 

416 
lemma split_beta: "(%(x, y). P x y) z = P (fst z) (snd z)" 

417 
by (subst surjective_pairing, rule split_conv) 

418 

419 
lemma split_split: "R (split c p) = (ALL x y. p = (x, y) > R (c x y))" 

420 
 {* For use with @{text split} and the Simplifier. *} 

421 
apply (subst surjective_pairing) 

14208  422 
apply (subst split_conv, blast) 
11838  423 
done 
424 

425 
text {* 

426 
@{thm [source] split_split} could be declared as @{text "[split]"} 

427 
done after the Splitter has been speeded up significantly; 

428 
precompute the constants involved and don't do anything unless the 

429 
current goal contains one of those constants. 

430 
*} 

431 

432 
lemma split_split_asm: "R (split c p) = (~(EX x y. p = (x, y) & (~R (c x y))))" 

14208  433 
by (subst split_split, simp) 
11838  434 

435 

436 
text {* 

437 
\medskip @{term split} used as a logical connective or set former. 

438 

439 
\medskip These rules are for use with @{text blast}; could instead 

440 
call @{text simp} using @{thm [source] split} as rewrite. *} 

441 

442 
lemma splitI2: "!!p. [ !!a b. p = (a, b) ==> c a b ] ==> split c p" 

443 
apply (simp only: split_tupled_all) 

444 
apply (simp (no_asm_simp)) 

445 
done 

446 

447 
lemma splitI2': "!!p. [ !!a b. (a, b) = p ==> c a b x ] ==> split c p x" 

448 
apply (simp only: split_tupled_all) 

449 
apply (simp (no_asm_simp)) 

450 
done 

451 

452 
lemma splitE: "split c p ==> (!!x y. p = (x, y) ==> c x y ==> Q) ==> Q" 

453 
by (induct p) (auto simp add: split_def) 

454 

455 
lemma splitE': "split c p z ==> (!!x y. p = (x, y) ==> c x y z ==> Q) ==> Q" 

456 
by (induct p) (auto simp add: split_def) 

457 

458 
lemma splitE2: 

459 
"[ Q (split P z); !!x y. [z = (x, y); Q (P x y)] ==> R ] ==> R" 

460 
proof  

461 
assume q: "Q (split P z)" 

462 
assume r: "!!x y. [z = (x, y); Q (P x y)] ==> R" 

463 
show R 

464 
apply (rule r surjective_pairing)+ 

465 
apply (rule split_beta [THEN subst], rule q) 

466 
done 

467 
qed 

468 

469 
lemma splitD': "split R (a,b) c ==> R a b c" 

470 
by simp 

471 

472 
lemma mem_splitI: "z: c a b ==> z: split c (a, b)" 

473 
by simp 

474 

475 
lemma mem_splitI2: "!!p. [ !!a b. p = (a, b) ==> z: c a b ] ==> z: split c p" 

14208  476 
by (simp only: split_tupled_all, simp) 
11838  477 

478 
lemma mem_splitE: "[ z: split c p; !!x y. [ p = (x,y); z: c x y ] ==> Q ] ==> Q" 

479 
proof  

480 
case rule_context [unfolded split_def] 

481 
show ?thesis by (rule rule_context surjective_pairing)+ 

482 
qed 

483 

484 
declare mem_splitI2 [intro!] mem_splitI [intro!] splitI2' [intro!] splitI2 [intro!] splitI [intro!] 

485 
declare mem_splitE [elim!] splitE' [elim!] splitE [elim!] 

486 

487 
ML_setup " 

488 
local (* filtering with exists_p_split is an essential optimization *) 

489 
fun exists_p_split (Const (\"split\",_) $ _ $ (Const (\"Pair\",_)$_$_)) = true 

490 
 exists_p_split (t $ u) = exists_p_split t orelse exists_p_split u 

491 
 exists_p_split (Abs (_, _, t)) = exists_p_split t 

492 
 exists_p_split _ = false; 

493 
val ss = HOL_basic_ss addsimps [thm \"split_conv\"]; 

494 
in 

495 
val split_conv_tac = SUBGOAL (fn (t, i) => 

496 
if exists_p_split t then safe_full_simp_tac ss i else no_tac); 

497 
end; 

498 
(* This prevents applications of splitE for already splitted arguments leading 

499 
to quite timeconsuming computations (in particular for nested tuples) *) 

500 
claset_ref() := claset() addSbefore (\"split_conv_tac\", split_conv_tac); 

501 
" 

502 

503 
lemma split_eta_SetCompr [simp]: "(%u. EX x y. u = (x, y) & P (x, y)) = P" 

14208  504 
by (rule ext, fast) 
11838  505 

506 
lemma split_eta_SetCompr2 [simp]: "(%u. EX x y. u = (x, y) & P x y) = split P" 

14208  507 
by (rule ext, fast) 
11838  508 

509 
lemma split_part [simp]: "(%(a,b). P & Q a b) = (%ab. P & split Q ab)" 

510 
 {* Allows simplifications of nested splits in case of independent predicates. *} 

14208  511 
apply (rule ext, blast) 
11838  512 
done 
513 

14337
e13731554e50
undid split_comp_eq[simp] because it leads to nontermination together with split_def!
nipkow
parents:
14208
diff
changeset

514 
(* Do NOT make this a simp rule as it 
e13731554e50
undid split_comp_eq[simp] because it leads to nontermination together with split_def!
nipkow
parents:
14208
diff
changeset

515 
a) only helps in special situations 
e13731554e50
undid split_comp_eq[simp] because it leads to nontermination together with split_def!
nipkow
parents:
14208
diff
changeset

516 
b) can lead to nontermination in the presence of split_def 
e13731554e50
undid split_comp_eq[simp] because it leads to nontermination together with split_def!
nipkow
parents:
14208
diff
changeset

517 
*) 
e13731554e50
undid split_comp_eq[simp] because it leads to nontermination together with split_def!
nipkow
parents:
14208
diff
changeset

518 
lemma split_comp_eq: 
14101  519 
"(%u. f (g (fst u)) (snd u)) = (split (%x. f (g x)))" 
520 
by (rule ext, auto) 

521 

11838  522 
lemma The_split_eq [simp]: "(THE (x',y'). x = x' & y = y') = (x, y)" 
523 
by blast 

524 

525 
(* 

526 
the following would be slightly more general, 

527 
but cannot be used as rewrite rule: 

528 
### Cannot add premise as rewrite rule because it contains (type) unknowns: 

529 
### ?y = .x 

530 
Goal "[ P y; !!x. P x ==> x = y ] ==> (@(x',y). x = x' & P y) = (x,y)" 

14208  531 
by (rtac some_equality 1) 
532 
by ( Simp_tac 1) 

533 
by (split_all_tac 1) 

534 
by (Asm_full_simp_tac 1) 

11838  535 
qed "The_split_eq"; 
536 
*) 

537 

538 
lemma injective_fst_snd: "!!x y. [fst x = fst y; snd x = snd y] ==> x = y" 

539 
by auto 

540 

541 

542 
text {* 

543 
\bigskip @{term prod_fun}  action of the product functor upon 

544 
functions. 

545 
*} 

546 

547 
lemma prod_fun [simp]: "prod_fun f g (a, b) = (f a, g b)" 

548 
by (simp add: prod_fun_def) 

549 

550 
lemma prod_fun_compose: "prod_fun (f1 o f2) (g1 o g2) = (prod_fun f1 g1 o prod_fun f2 g2)" 

551 
apply (rule ext) 

14208  552 
apply (tactic {* pair_tac "x" 1 *}, simp) 
11838  553 
done 
554 

555 
lemma prod_fun_ident [simp]: "prod_fun (%x. x) (%y. y) = (%z. z)" 

556 
apply (rule ext) 

14208  557 
apply (tactic {* pair_tac "z" 1 *}, simp) 
11838  558 
done 
559 

560 
lemma prod_fun_imageI [intro]: "(a, b) : r ==> (f a, g b) : prod_fun f g ` r" 

561 
apply (rule image_eqI) 

14208  562 
apply (rule prod_fun [symmetric], assumption) 
11838  563 
done 
564 

565 
lemma prod_fun_imageE [elim!]: 

566 
"[ c: (prod_fun f g)`r; !!x y. [ c=(f(x),g(y)); (x,y):r ] ==> P 

567 
] ==> P" 

568 
proof  

569 
case rule_context 

570 
assume major: "c: (prod_fun f g)`r" 

571 
show ?thesis 

572 
apply (rule major [THEN imageE]) 

573 
apply (rule_tac p = x in PairE) 

574 
apply (rule rule_context) 

575 
prefer 2 

576 
apply blast 

577 
apply (blast intro: prod_fun) 

578 
done 

579 
qed 

580 

581 

14101  582 
constdefs 
583 
upd_fst :: "('a => 'c) => 'a * 'b => 'c * 'b" 

584 
"upd_fst f == prod_fun f id" 

585 

586 
upd_snd :: "('b => 'c) => 'a * 'b => 'a * 'c" 

587 
"upd_snd f == prod_fun id f" 

588 

589 
lemma upd_fst_conv [simp]: "upd_fst f (x,y) = (f x,y)" 

590 
by (simp add: upd_fst_def) 

591 

592 
lemma upd_snd_conv [simp]: "upd_snd f (x,y) = (x,f y)" 

593 
by (simp add: upd_snd_def) 

594 

11838  595 
text {* 
596 
\bigskip Disjoint union of a family of sets  Sigma. 

597 
*} 

598 

599 
lemma SigmaI [intro!]: "[ a:A; b:B(a) ] ==> (a,b) : Sigma A B" 

600 
by (unfold Sigma_def) blast 

601 

602 

603 
lemma SigmaE: 

604 
"[ c: Sigma A B; 

605 
!!x y.[ x:A; y:B(x); c=(x,y) ] ==> P 

606 
] ==> P" 

607 
 {* The general elimination rule. *} 

608 
by (unfold Sigma_def) blast 

609 

610 
text {* 

611 
Elimination of @{term "(a, b) : A \<times> B"}  introduces no 

612 
eigenvariables. 

613 
*} 

614 

615 
lemma SigmaD1: "(a, b) : Sigma A B ==> a : A" 

14208  616 
by (erule SigmaE, blast) 
11838  617 

618 
lemma SigmaD2: "(a, b) : Sigma A B ==> b : B a" 

14208  619 
by (erule SigmaE, blast) 
11838  620 

621 
lemma SigmaE2: 

622 
"[ (a, b) : Sigma A B; 

623 
[ a:A; b:B(a) ] ==> P 

624 
] ==> P" 

625 
by (blast dest: SigmaD1 SigmaD2) 

626 

627 
declare SigmaE [elim!] SigmaE2 [elim!] 

628 

629 
lemma Sigma_mono: "[ A <= C; !!x. x:A ==> B x <= D x ] ==> Sigma A B <= Sigma C D" 

630 
by blast 

631 

632 
lemma Sigma_empty1 [simp]: "Sigma {} B = {}" 

633 
by blast 

634 

635 
lemma Sigma_empty2 [simp]: "A <*> {} = {}" 

636 
by blast 

637 

638 
lemma UNIV_Times_UNIV [simp]: "UNIV <*> UNIV = UNIV" 

639 
by auto 

640 

641 
lemma Compl_Times_UNIV1 [simp]: " (UNIV <*> A) = UNIV <*> (A)" 

642 
by auto 

643 

644 
lemma Compl_Times_UNIV2 [simp]: " (A <*> UNIV) = (A) <*> UNIV" 

645 
by auto 

646 

647 
lemma mem_Sigma_iff [iff]: "((a,b): Sigma A B) = (a:A & b:B(a))" 

648 
by blast 

649 

650 
lemma Times_subset_cancel2: "x:C ==> (A <*> C <= B <*> C) = (A <= B)" 

651 
by blast 

652 

653 
lemma Times_eq_cancel2: "x:C ==> (A <*> C = B <*> C) = (A = B)" 

654 
by (blast elim: equalityE) 

655 

656 
lemma SetCompr_Sigma_eq: 

657 
"Collect (split (%x y. P x & Q x y)) = (SIGMA x:Collect P. Collect (Q x))" 

658 
by blast 

659 

660 
text {* 

661 
\bigskip Complex rules for Sigma. 

662 
*} 

663 

664 
lemma Collect_split [simp]: "{(a,b). P a & Q b} = Collect P <*> Collect Q" 

665 
by blast 

666 

667 
lemma UN_Times_distrib: 

668 
"(UN (a,b):(A <*> B). E a <*> F b) = (UNION A E) <*> (UNION B F)" 

669 
 {* Suggested by Pierre Chartier *} 

670 
by blast 

671 

672 
lemma split_paired_Ball_Sigma [simp]: 

673 
"(ALL z: Sigma A B. P z) = (ALL x:A. ALL y: B x. P(x,y))" 

674 
by blast 

675 

676 
lemma split_paired_Bex_Sigma [simp]: 

677 
"(EX z: Sigma A B. P z) = (EX x:A. EX y: B x. P(x,y))" 

678 
by blast 

679 

680 
lemma Sigma_Un_distrib1: "(SIGMA i:I Un J. C(i)) = (SIGMA i:I. C(i)) Un (SIGMA j:J. C(j))" 

681 
by blast 

682 

683 
lemma Sigma_Un_distrib2: "(SIGMA i:I. A(i) Un B(i)) = (SIGMA i:I. A(i)) Un (SIGMA i:I. B(i))" 

684 
by blast 

685 

686 
lemma Sigma_Int_distrib1: "(SIGMA i:I Int J. C(i)) = (SIGMA i:I. C(i)) Int (SIGMA j:J. C(j))" 

687 
by blast 

688 

689 
lemma Sigma_Int_distrib2: "(SIGMA i:I. A(i) Int B(i)) = (SIGMA i:I. A(i)) Int (SIGMA i:I. B(i))" 

690 
by blast 

691 

692 
lemma Sigma_Diff_distrib1: "(SIGMA i:I  J. C(i)) = (SIGMA i:I. C(i))  (SIGMA j:J. C(j))" 

693 
by blast 

694 

695 
lemma Sigma_Diff_distrib2: "(SIGMA i:I. A(i)  B(i)) = (SIGMA i:I. A(i))  (SIGMA i:I. B(i))" 

696 
by blast 

697 

698 
lemma Sigma_Union: "Sigma (Union X) B = (UN A:X. Sigma A B)" 

699 
by blast 

700 

701 
text {* 

702 
Nondependent versions are needed to avoid the need for higherorder 

703 
matching, especially when the rules are reoriented. 

704 
*} 

705 

706 
lemma Times_Un_distrib1: "(A Un B) <*> C = (A <*> C) Un (B <*> C)" 

707 
by blast 

708 

709 
lemma Times_Int_distrib1: "(A Int B) <*> C = (A <*> C) Int (B <*> C)" 

710 
by blast 

711 

712 
lemma Times_Diff_distrib1: "(A  B) <*> C = (A <*> C)  (B <*> C)" 

713 
by blast 

714 

715 

11493  716 
lemma pair_imageI [intro]: "(a, b) : A ==> f a b : (%(a, b). f a b) ` A" 
11777  717 
apply (rule_tac x = "(a, b)" in image_eqI) 
718 
apply auto 

719 
done 

720 

11493  721 

11838  722 
text {* 
723 
Setup of internal @{text split_rule}. 

724 
*} 

725 

11032  726 
constdefs 
11425  727 
internal_split :: "('a => 'b => 'c) => 'a * 'b => 'c" 
11032  728 
"internal_split == split" 
729 

730 
lemma internal_split_conv: "internal_split c (a, b) = c a b" 

731 
by (simp only: internal_split_def split_conv) 

732 

733 
hide const internal_split 

734 

11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

735 
use "Tools/split_rule.ML" 
11032  736 
setup SplitRule.setup 
10213  737 

738 
end 