author  paulson 
Fri, 25 Aug 2006 18:45:57 +0200  
changeset 20415  e3d2d7b01279 
parent 20380  14f9f2a1caa6 
child 20588  c847c56edf0c 
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 

15131  9 
theory Product_Type 
15140  10 
imports Fun 
16417  11 
uses ("Tools/split_rule.ML") 
15131  12 
begin 
11838  13 

14 
subsection {* Unit *} 

15 

16 
typedef unit = "{True}" 

17 
proof 

18 
show "True : ?unit" by blast 

19 
qed 

20 

21 
constdefs 

22 
Unity :: unit ("'(')") 

23 
"() == Abs_unit True" 

24 

25 
lemma unit_eq: "u = ()" 

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

27 

28 
text {* 

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

30 
this rule directly  it loops! 

31 
*} 

32 

33 
ML_setup {* 

13462  34 
val unit_eq_proc = 
35 
let val unit_meta_eq = mk_meta_eq (thm "unit_eq") in 

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

15531  37 
(fn _ => fn _ => fn t => if HOLogic.is_unit t then NONE else SOME unit_meta_eq) 
13462  38 
end; 
11838  39 

40 
Addsimprocs [unit_eq_proc]; 

41 
*} 

42 

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

44 
by simp 

45 

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

47 
by (rule triv_forall_equality) 

48 

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

50 
by simp 

51 

52 
text {* 

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

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

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

56 
*} 

57 

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

59 
by (rule ext) simp 

10213  60 

61 

11838  62 
subsection {* Pairs *} 
10213  63 

11777  64 
subsubsection {* Type definition *} 
10213  65 

66 
constdefs 

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

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

70 
global 

71 

72 
typedef (Prod) 

11838  73 
('a, 'b) "*" (infixr 20) 
11032  74 
= "{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

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

76 
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

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

78 
qed 
10213  79 

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

80 
syntax (xsymbols) 
15422
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

81 
"*" :: "[type, type] => type" ("(_ \<times>/ _)" [21, 20] 20) 
10213  82 
syntax (HTML output) 
15422
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

83 
"*" :: "[type, type] => type" ("(_ \<times>/ _)" [21, 20] 20) 
10213  84 

11777  85 
local 
10213  86 

11777  87 

19535  88 
subsubsection {* Definitions *} 
11777  89 

90 
global 

10213  91 

92 
consts 

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

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

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

95 
split :: "[['a, 'b] => 'c, 'a * 'b] => 'c" 
14189  96 
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

97 
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

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

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

11777  101 
local 
10213  102 

19535  103 
defs 
104 
Pair_def: "Pair a b == Abs_Prod (Pair_Rep a b)" 

105 
fst_def: "fst p == THE a. EX b. p = Pair a b" 

106 
snd_def: "snd p == THE b. EX a. p = Pair a b" 

107 
split_def: "split == (%c p. c (fst p) (snd p))" 

108 
curry_def: "curry == (%c x y. c (Pair x y))" 

109 
prod_fun_def: "prod_fun f g == split (%x y. Pair (f x) (g y))" 

110 
Sigma_def: "Sigma A B == UN x:A. UN y:B x. {Pair x y}" 

111 

112 
abbreviation 

113 
Times :: "['a set, 'b set] => ('a * 'b) set" (infixr "<*>" 80) 

114 
"A <*> B == Sigma A (%_. B)" 

115 

19656
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19535
diff
changeset

116 
const_syntax (xsymbols) 
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19535
diff
changeset

117 
Times (infixr "\<times>" 80) 
19535  118 

19656
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19535
diff
changeset

119 
const_syntax (HTML output) 
09be06943252
tuned concrete syntax  abbreviation/const_syntax;
wenzelm
parents:
19535
diff
changeset

120 
Times (infixr "\<times>" 80) 
19535  121 

122 

123 
subsubsection {* Concrete syntax *} 

124 

11777  125 
text {* 
126 
Patterns  extends predefined type @{typ pttrn} used in 

127 
abstractions. 

128 
*} 

10213  129 

130 
nonterminals 

131 
tuple_args patterns 

132 

133 
syntax 

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

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

136 
"_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

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

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

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

140 
"@Sigma" ::"[pttrn, 'a set, 'b set] => ('a * 'b) set" ("(3SIGMA _:_./ _)" 10) 
10213  141 

142 
translations 

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

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

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

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

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

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

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

19535  150 
"SIGMA x:A. B" == "Sigma A (%x. B)" 
10213  151 

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

154 
print_translation {* 

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

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

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

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

159 

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

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

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

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

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

165 
in Syntax.const "_abs"$ 

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

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

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

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

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

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

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

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

174 
 split_tr' _ = raise Match; 

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

176 
end 

177 
*} 

178 

15422
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

179 
(* print "split f" as "\<lambda>(x,y). f x y" and "split (\<lambda>x. f x)" as "\<lambda>(x,y). f x y" *) 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

180 
typed_print_translation {* 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

181 
let 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

182 
fun split_guess_names_tr' _ T [Abs (x,_,Abs _)] = raise Match 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

183 
 split_guess_names_tr' _ T [Abs (x,xT,t)] = 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

184 
(case (head_of t) of 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

185 
Const ("split",_) => raise Match 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

186 
 _ => let 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

187 
val (_::yT::_) = binder_types (domain_type T) handle Bind => raise Match; 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

188 
val (y,t') = atomic_abs_tr' ("y",yT,(incr_boundvars 1 t)$Bound 0); 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

189 
val (x',t'') = atomic_abs_tr' (x,xT,t'); 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

190 
in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end) 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

191 
 split_guess_names_tr' _ T [t] = 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

192 
(case (head_of t) of 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

193 
Const ("split",_) => raise Match 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

194 
 _ => let 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

195 
val (xT::yT::_) = binder_types (domain_type T) handle Bind => raise Match; 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

196 
val (y,t') = 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

197 
atomic_abs_tr' ("y",yT,(incr_boundvars 2 t)$Bound 1$Bound 0); 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

198 
val (x',t'') = atomic_abs_tr' ("x",xT,t'); 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

199 
in Syntax.const "_abs" $ (Syntax.const "_pattern" $x'$y) $ t'' end) 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

200 
 split_guess_names_tr' _ _ _ = raise Match; 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

201 
in [("split", split_guess_names_tr')] 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

202 
end 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

203 
*} 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

204 

10213  205 

11966  206 
subsubsection {* Lemmas and proof tool setup *} 
11838  207 

208 
lemma ProdI: "Pair_Rep a b : Prod" 

19535  209 
unfolding Prod_def by blast 
11838  210 

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

212 
apply (unfold Pair_Rep_def) 

14208  213 
apply (drule fun_cong [THEN fun_cong], blast) 
11838  214 
done 
10213  215 

11838  216 
lemma inj_on_Abs_Prod: "inj_on Abs_Prod Prod" 
217 
apply (rule inj_on_inverseI) 

218 
apply (erule Abs_Prod_inverse) 

219 
done 

220 

221 
lemma Pair_inject: 

18372  222 
assumes "(a, b) = (a', b')" 
223 
and "a = a' ==> b = b' ==> R" 

224 
shows R 

225 
apply (insert prems [unfolded Pair_def]) 

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

227 
apply (assumption  rule ProdI)+ 

228 
done 

10213  229 

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

232 

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

19535  234 
unfolding fst_def by blast 
11838  235 

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

19535  237 
unfolding snd_def by blast 
11025
a70b796d9af8
converted to Isar therory, adding attributes complete_split and split_format
oheimb
parents:
10289
diff
changeset

238 

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

241 

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

243 
by simp 

244 

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

246 
apply (unfold Pair_def) 

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

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

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

250 
apply (erule arg_cong) 

251 
done 

11032  252 

11838  253 
lemma PairE [cases type: *]: "(!!x y. p = (x, y) ==> Q) ==> Q" 
19535  254 
using PairE_lemma [of p] by blast 
11838  255 

16121  256 
ML {* 
11838  257 
local val PairE = thm "PairE" in 
258 
fun pair_tac s = 

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

260 
end; 

261 
*} 

11032  262 

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

265 
by (cases p) simp 

266 

17085  267 
lemmas pair_collapse = surjective_pairing [symmetric] 
268 
declare pair_collapse [simp] 

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

269 

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

272 
apply (rule exI) 

273 
apply (rule surjective_pairing) 

274 
done 

275 

276 
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

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

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

279 
assume "!!x. PROP P x" 
19535  280 
then show "PROP P (a, b)" . 
11820
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

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

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

283 
assume "!!a b. PROP P (a, b)" 
19535  284 
from `PROP P (fst x, snd x)` show "PROP P x" by simp 
11820
015a82d4ee96
proper proof of split_paired_all (presently unused);
wenzelm
parents:
11777
diff
changeset

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

286 

11838  287 
lemmas split_tupled_all = split_paired_all unit_all_eq2 
288 

289 
text {* 

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

291 
Simplifier because it also affects premises in congrence rules, 

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

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

294 
*} 

295 

16121  296 
ML_setup {* 
11838  297 
(* replace parameters of product type by individual component parameters *) 
298 
val safe_full_simp_tac = generic_simp_tac true (true, false, false); 

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

16121  300 
fun exists_paired_all (Const ("all", _) $ Abs (_, T, t)) = 
11838  301 
can HOLogic.dest_prodT T orelse exists_paired_all t 
302 
 exists_paired_all (t $ u) = exists_paired_all t orelse exists_paired_all u 

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

304 
 exists_paired_all _ = false; 

305 
val ss = HOL_basic_ss 

16121  306 
addsimps [thm "split_paired_all", thm "unit_all_eq2", thm "unit_abs_eta_conv"] 
11838  307 
addsimprocs [unit_eq_proc]; 
308 
in 

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

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

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

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

313 
fun split_all th = 

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

315 
end; 

316 

17875  317 
change_claset (fn cs => cs addSbefore ("split_all_tac", split_all_tac)); 
16121  318 
*} 
11838  319 

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

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

322 
by fast 

323 

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

326 

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

328 
by (simp add: curry_def split_def) 

329 

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

331 
by (simp add: curry_def) 

332 

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

333 
lemma curryD [dest!]: "curry f a b ==> f (a,b)" 
14189  334 
by (simp add: curry_def) 
335 

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

336 
lemma curryE: "[ curry f a b ; f (a,b) ==> Q ] ==> Q" 
14189  337 
by (simp add: curry_def) 
338 

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

340 
by (simp add: curry_def) 

341 

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

344 

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

346 
by fast 

347 

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

349 
by (simp add: split_def) 

350 

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

352 

353 
lemmas splitI = split_conv [THEN iffD2, standard] 

354 
lemmas splitD = split_conv [THEN iffD1, standard] 

355 

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

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

358 
apply (rule ext) 

14208  359 
apply (tactic {* pair_tac "x" 1 *}, simp) 
11838  360 
done 
361 

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

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

364 
by (simp add: split_Pair_apply) 

365 

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

367 
by (simp add: split_def) 

368 

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

14208  370 
by (simp only: split_tupled_all, simp) 
11838  371 

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

373 
by (simp add: Pair_fst_snd_eq) 

374 

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

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

377 
by (erule arg_cong) 

378 

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

380 
apply (rule ext) 

381 
apply (simp only: split_tupled_all) 

382 
apply (rule split_conv) 

383 
done 

384 

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

386 
by (simp add: split_eta) 

387 

388 
text {* 

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

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

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

392 
existing proofs very inefficient; similarly for @{text 

393 
split_beta}. *} 

394 

395 
ML_setup {* 

396 

397 
local 

18328  398 
val cond_split_eta_ss = HOL_basic_ss addsimps [thm "cond_split_eta"] 
11838  399 
fun Pair_pat k 0 (Bound m) = (m = k) 
400 
 Pair_pat k i (Const ("Pair", _) $ Bound m $ t) = i > 0 andalso 

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

402 
 Pair_pat _ _ _ = false; 

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

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

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

406 
 no_args _ _ _ = true; 

15531  407 
fun split_pat tp i (Abs (_,_,t)) = if tp 0 i t then SOME (i,t) else NONE 
11838  408 
 split_pat tp i (Const ("split", _) $ Abs (_, _, t)) = split_pat tp (i+1) t 
15531  409 
 split_pat tp i _ = NONE; 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

410 
fun metaeq ss lhs rhs = mk_meta_eq (Goal.prove (Simplifier.the_context ss) [] [] 
13480
bb72bd43c6c3
use Tactic.prove instead of prove_goalw_cterm in internal proofs!
wenzelm
parents:
13462
diff
changeset

411 
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs,rhs))) 
18328  412 
(K (simp_tac (Simplifier.inherit_context ss cond_split_eta_ss) 1))); 
11838  413 

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

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

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

417 
 beta_term_pat k i t = no_args k i t; 

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

419 
 eta_term_pat _ _ _ = false; 

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

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

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

423 
 subst arg k i t = t; 

20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

424 
fun beta_proc ss (s as Const ("split", _) $ Abs (_, _, t) $ arg) = 
11838  425 
(case split_pat beta_term_pat 1 t of 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

426 
SOME (i,f) => SOME (metaeq ss s (subst arg 0 i f)) 
15531  427 
 NONE => NONE) 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

428 
 beta_proc _ _ = NONE; 
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

429 
fun eta_proc ss (s as Const ("split", _) $ Abs (_, _, t)) = 
11838  430 
(case split_pat eta_term_pat 1 t of 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

431 
SOME (_,ft) => SOME (metaeq ss s (let val (f $ arg) = ft in f end)) 
15531  432 
 NONE => NONE) 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

433 
 eta_proc _ _ = NONE; 
11838  434 
in 
13462  435 
val split_beta_proc = Simplifier.simproc (Theory.sign_of (the_context ())) 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

436 
"split_beta" ["split f z"] (K beta_proc); 
13462  437 
val split_eta_proc = Simplifier.simproc (Theory.sign_of (the_context ())) 
20044
92cc2f4c7335
simprocs: no theory argument  use simpset context instead;
wenzelm
parents:
19656
diff
changeset

438 
"split_eta" ["split f"] (K eta_proc); 
11838  439 
end; 
440 

441 
Addsimprocs [split_beta_proc, split_eta_proc]; 

442 
*} 

443 

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

445 
by (subst surjective_pairing, rule split_conv) 

446 

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

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

15481  449 
by (insert surj_pair [of p], clarify, simp) 
11838  450 

451 
text {* 

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

453 
done after the Splitter has been speeded up significantly; 

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

455 
current goal contains one of those constants. 

456 
*} 

457 

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

14208  459 
by (subst split_split, simp) 
11838  460 

461 

462 
text {* 

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

464 

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

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

467 

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

469 
apply (simp only: split_tupled_all) 

470 
apply (simp (no_asm_simp)) 

471 
done 

472 

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

474 
apply (simp only: split_tupled_all) 

475 
apply (simp (no_asm_simp)) 

476 
done 

477 

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

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

480 

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

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

483 

484 
lemma splitE2: 

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

486 
proof  

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

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

489 
show R 

490 
apply (rule r surjective_pairing)+ 

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

492 
done 

493 
qed 

494 

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

496 
by simp 

497 

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

499 
by simp 

500 

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

14208  502 
by (simp only: split_tupled_all, simp) 
11838  503 

18372  504 
lemma mem_splitE: 
505 
assumes major: "z: split c p" 

506 
and cases: "!!x y. [ p = (x,y); z: c x y ] ==> Q" 

507 
shows Q 

508 
by (rule major [unfolded split_def] cases surjective_pairing)+ 

11838  509 

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

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

512 

16121  513 
ML_setup {* 
11838  514 
local (* filtering with exists_p_split is an essential optimization *) 
16121  515 
fun exists_p_split (Const ("split",_) $ _ $ (Const ("Pair",_)$_$_)) = true 
11838  516 
 exists_p_split (t $ u) = exists_p_split t orelse exists_p_split u 
517 
 exists_p_split (Abs (_, _, t)) = exists_p_split t 

518 
 exists_p_split _ = false; 

16121  519 
val ss = HOL_basic_ss addsimps [thm "split_conv"]; 
11838  520 
in 
521 
val split_conv_tac = SUBGOAL (fn (t, i) => 

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

523 
end; 

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

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

17875  526 
change_claset (fn cs => cs addSbefore ("split_conv_tac", split_conv_tac)); 
16121  527 
*} 
11838  528 

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

18372  530 
by (rule ext) fast 
11838  531 

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

18372  533 
by (rule ext) fast 
11838  534 

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

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

18372  537 
by (rule ext) blast 
11838  538 

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

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

540 
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

541 
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

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

543 
lemma split_comp_eq: 
20415  544 
fixes f :: "'a => 'b => 'c" and g :: "'d => 'a" 
545 
shows "(%u. f (g (fst u)) (snd u)) = (split (%x. f (g x)))" 

18372  546 
by (rule ext) auto 
14101  547 

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

550 

551 
(* 

552 
the following would be slightly more general, 

553 
but cannot be used as rewrite rule: 

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

555 
### ?y = .x 

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

14208  557 
by (rtac some_equality 1) 
558 
by ( Simp_tac 1) 

559 
by (split_all_tac 1) 

560 
by (Asm_full_simp_tac 1) 

11838  561 
qed "The_split_eq"; 
562 
*) 

563 

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

565 
by auto 

566 

567 

568 
text {* 

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

570 
functions. 

571 
*} 

572 

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

574 
by (simp add: prod_fun_def) 

575 

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

577 
apply (rule ext) 

14208  578 
apply (tactic {* pair_tac "x" 1 *}, simp) 
11838  579 
done 
580 

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

582 
apply (rule ext) 

14208  583 
apply (tactic {* pair_tac "z" 1 *}, simp) 
11838  584 
done 
585 

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

587 
apply (rule image_eqI) 

14208  588 
apply (rule prod_fun [symmetric], assumption) 
11838  589 
done 
590 

591 
lemma prod_fun_imageE [elim!]: 

18372  592 
assumes major: "c: (prod_fun f g)`r" 
593 
and cases: "!!x y. [ c=(f(x),g(y)); (x,y):r ] ==> P" 

594 
shows P 

595 
apply (rule major [THEN imageE]) 

596 
apply (rule_tac p = x in PairE) 

597 
apply (rule cases) 

598 
apply (blast intro: prod_fun) 

599 
apply blast 

600 
done 

11838  601 

602 

14101  603 
constdefs 
604 
upd_fst :: "('a => 'c) => 'a * 'b => 'c * 'b" 

605 
"upd_fst f == prod_fun f id" 

606 

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

608 
"upd_snd f == prod_fun id f" 

609 

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

18372  611 
by (simp add: upd_fst_def) 
14101  612 

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

18372  614 
by (simp add: upd_snd_def) 
14101  615 

11838  616 
text {* 
617 
\bigskip Disjoint union of a family of sets  Sigma. 

618 
*} 

619 

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

621 
by (unfold Sigma_def) blast 

622 

14952
47455995693d
removal of xsymbol syntax <Sigma> for dependent products
paulson
parents:
14565
diff
changeset

623 
lemma SigmaE [elim!]: 
11838  624 
"[ c: Sigma A B; 
625 
!!x y.[ x:A; y:B(x); c=(x,y) ] ==> P 

626 
] ==> P" 

627 
 {* The general elimination rule. *} 

628 
by (unfold Sigma_def) blast 

629 

630 
text {* 

15422
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

631 
Elimination of @{term "(a, b) : A \<times> B"}  introduces no 
11838  632 
eigenvariables. 
633 
*} 

634 

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

18372  636 
by blast 
11838  637 

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

18372  639 
by blast 
11838  640 

641 
lemma SigmaE2: 

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

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

644 
] ==> P" 

14952
47455995693d
removal of xsymbol syntax <Sigma> for dependent products
paulson
parents:
14565
diff
changeset

645 
by blast 
11838  646 

14952
47455995693d
removal of xsymbol syntax <Sigma> for dependent products
paulson
parents:
14565
diff
changeset

647 
lemma Sigma_cong: 
15422
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

648 
"\<lbrakk>A = B; !!x. x \<in> B \<Longrightarrow> C x = D x\<rbrakk> 
cbdddc0efadf
added print translation for split: split f > %(x,y). f x y
schirmer
parents:
15404
diff
changeset

649 
\<Longrightarrow> (SIGMA x: A. C x) = (SIGMA x: B. D x)" 
18372  650 
by auto 
11838  651 

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

653 
by blast 

654 

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

656 
by blast 

657 

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

659 
by blast 

660 

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

662 
by auto 

663 

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

665 
by auto 

666 

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

668 
by auto 

669 

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

671 
by blast 

672 

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

674 
by blast 

675 

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

677 
by (blast elim: equalityE) 

678 

679 
lemma SetCompr_Sigma_eq: 

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

681 
by blast 

682 

683 
text {* 

684 
\bigskip Complex rules for Sigma. 

685 
*} 

686 

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

688 
by blast 

689 

690 
lemma UN_Times_distrib: 

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

692 
 {* Suggested by Pierre Chartier *} 

693 
by blast 

694 

695 
lemma split_paired_Ball_Sigma [simp]: 

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

697 
by blast 

698 

699 
lemma split_paired_Bex_Sigma [simp]: 

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

701 
by blast 

702 

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

704 
by blast 

705 

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

707 
by blast 

708 

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

710 
by blast 

711 

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

713 
by blast 

714 

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

716 
by blast 

717 

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

719 
by blast 

720 

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

722 
by blast 

723 

724 
text {* 

725 
Nondependent versions are needed to avoid the need for higherorder 

726 
matching, especially when the rules are reoriented. 

727 
*} 

728 

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

730 
by blast 

731 

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

733 
by blast 

734 

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

736 
by blast 

737 

738 

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

742 
done 

743 

11493  744 

11838  745 
text {* 
746 
Setup of internal @{text split_rule}. 

747 
*} 

748 

11032  749 
constdefs 
11425  750 
internal_split :: "('a => 'b => 'c) => 'a * 'b => 'c" 
11032  751 
"internal_split == split" 
752 

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

754 
by (simp only: internal_split_def split_conv) 

755 

756 
hide const internal_split 

757 

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

758 
use "Tools/split_rule.ML" 
11032  759 
setup SplitRule.setup 
10213  760 

15394  761 

762 
subsection {* Code generator setup *} 

763 

764 
types_code 

765 
"*" ("(_ */ _)") 

16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset

766 
attach (term_of) {* 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset

767 
fun term_of_id_42 f T g U (x, y) = HOLogic.pair_const T U $ f x $ g y; 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset

768 
*} 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset

769 
attach (test) {* 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset

770 
fun gen_id_42 aG bG i = (aG i, bG i); 
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16634
diff
changeset

771 
*} 
15394  772 

18706
1e7562c7afe6
Reinserted consts_code declaration accidentally deleted
berghofe
parents:
18702
diff
changeset

773 
consts_code 
1e7562c7afe6
Reinserted consts_code declaration accidentally deleted
berghofe
parents:
18702
diff
changeset

774 
"Pair" ("(_,/ _)") 
1e7562c7afe6
Reinserted consts_code declaration accidentally deleted
berghofe
parents:
18702
diff
changeset

775 

15394  776 
ML {* 
18013  777 

19039  778 
fun strip_abs_split 0 t = ([], t) 
779 
 strip_abs_split i (Abs (s, T, t)) = 

18013  780 
let 
781 
val s' = Codegen.new_name t s; 

782 
val v = Free (s', T) 

19039  783 
in apfst (cons v) (strip_abs_split (i1) (subst_bound (v, t))) end 
784 
 strip_abs_split i (u as Const ("split", _) $ t) = (case strip_abs_split (i+1) t of 

15394  785 
(v :: v' :: vs, u) => (HOLogic.mk_prod (v, v') :: vs, u) 
786 
 _ => ([], u)) 

19039  787 
 strip_abs_split i t = ([], t); 
18013  788 

16634  789 
fun let_codegen thy defs gr dep thyname brack t = (case strip_comb t of 
790 
(t1 as Const ("Let", _), t2 :: t3 :: ts) => 

15394  791 
let 
792 
fun dest_let (l as Const ("Let", _) $ t $ u) = 

19039  793 
(case strip_abs_split 1 u of 
15394  794 
([p], u') => apfst (cons (p, t)) (dest_let u') 
795 
 _ => ([], l)) 

796 
 dest_let t = ([], t); 

797 
fun mk_code (gr, (l, r)) = 

798 
let 

16634  799 
val (gr1, pl) = Codegen.invoke_codegen thy defs dep thyname false (gr, l); 
800 
val (gr2, pr) = Codegen.invoke_codegen thy defs dep thyname false (gr1, r); 

15394  801 
in (gr2, (pl, pr)) end 
16634  802 
in case dest_let (t1 $ t2 $ t3) of 
15531  803 
([], _) => NONE 
15394  804 
 (ps, u) => 
805 
let 

806 
val (gr1, qs) = foldl_map mk_code (gr, ps); 

16634  807 
val (gr2, pu) = Codegen.invoke_codegen thy defs dep thyname false (gr1, u); 
808 
val (gr3, pargs) = foldl_map 

17021
1c361a3de73d
Fixed bug in code generator for let and split leading to illformed code.
berghofe
parents:
17002
diff
changeset

809 
(Codegen.invoke_codegen thy defs dep thyname true) (gr2, ts) 
15394  810 
in 
16634  811 
SOME (gr3, Codegen.mk_app brack 
812 
(Pretty.blk (0, [Pretty.str "let ", Pretty.blk (0, List.concat 

813 
(separate [Pretty.str ";", Pretty.brk 1] (map (fn (pl, pr) => 

814 
[Pretty.block [Pretty.str "val ", pl, Pretty.str " =", 

815 
Pretty.brk 1, pr]]) qs))), 

816 
Pretty.brk 1, Pretty.str "in ", pu, 

817 
Pretty.brk 1, Pretty.str "end"])) pargs) 

15394  818 
end 
819 
end 

16634  820 
 _ => NONE); 
15394  821 

16634  822 
fun split_codegen thy defs gr dep thyname brack t = (case strip_comb t of 
823 
(t1 as Const ("split", _), t2 :: ts) => 

19039  824 
(case strip_abs_split 1 (t1 $ t2) of 
16634  825 
([p], u) => 
826 
let 

827 
val (gr1, q) = Codegen.invoke_codegen thy defs dep thyname false (gr, p); 

828 
val (gr2, pu) = Codegen.invoke_codegen thy defs dep thyname false (gr1, u); 

829 
val (gr3, pargs) = foldl_map 

17021
1c361a3de73d
Fixed bug in code generator for let and split leading to illformed code.
berghofe
parents:
17002
diff
changeset

830 
(Codegen.invoke_codegen thy defs dep thyname true) (gr2, ts) 
16634  831 
in 
832 
SOME (gr2, Codegen.mk_app brack 

833 
(Pretty.block [Pretty.str "(fn ", q, Pretty.str " =>", 

834 
Pretty.brk 1, pu, Pretty.str ")"]) pargs) 

835 
end 

836 
 _ => NONE) 

837 
 _ => NONE); 

15394  838 

18708  839 
val prod_codegen_setup = 
20105  840 
Codegen.add_codegen "let_codegen" let_codegen 
841 
#> Codegen.add_codegen "split_codegen" split_codegen 

842 
#> CodegenPackage.add_appconst 

843 
("Let", CodegenPackage.appgen_let) 

15394  844 

845 
*} 

846 

847 
setup prod_codegen_setup 

848 

15404  849 
ML 
850 
{* 

851 
val Collect_split = thm "Collect_split"; 

852 
val Compl_Times_UNIV1 = thm "Compl_Times_UNIV1"; 

853 
val Compl_Times_UNIV2 = thm "Compl_Times_UNIV2"; 

854 
val PairE = thm "PairE"; 

855 
val PairE_lemma = thm "PairE_lemma"; 

856 
val Pair_Rep_inject = thm "Pair_Rep_inject"; 

857 
val Pair_def = thm "Pair_def"; 

858 
val Pair_eq = thm "Pair_eq"; 

859 
val Pair_fst_snd_eq = thm "Pair_fst_snd_eq"; 

860 
val Pair_inject = thm "Pair_inject"; 

861 
val ProdI = thm "ProdI"; 

862 
val SetCompr_Sigma_eq = thm "SetCompr_Sigma_eq"; 

863 
val SigmaD1 = thm "SigmaD1"; 

864 
val SigmaD2 = thm "SigmaD2"; 

865 
val SigmaE = thm "SigmaE"; 

866 
val SigmaE2 = thm "SigmaE2"; 

867 
val SigmaI = thm "SigmaI"; 

868 
val Sigma_Diff_distrib1 = thm "Sigma_Diff_distrib1"; 

869 
val Sigma_Diff_distrib2 = thm "Sigma_Diff_distrib2"; 

870 
val Sigma_Int_distrib1 = thm "Sigma_Int_distrib1"; 

871 
val Sigma_Int_distrib2 = thm "Sigma_Int_distrib2"; 

872 
val Sigma_Un_distrib1 = thm "Sigma_Un_distrib1"; 

873 
val Sigma_Un_distrib2 = thm "Sigma_Un_distrib2"; 

874 
val Sigma_Union = thm "Sigma_Union"; 

875 
val Sigma_def = thm "Sigma_def"; 

876 
val Sigma_empty1 = thm "Sigma_empty1"; 

877 
val Sigma_empty2 = thm "Sigma_empty2"; 

878 
val Sigma_mono = thm "Sigma_mono"; 

879 
val The_split = thm "The_split"; 

880 
val The_split_eq = thm "The_split_eq"; 

881 
val The_split_eq = thm "The_split_eq"; 

882 
val Times_Diff_distrib1 = thm "Times_Diff_distrib1"; 

883 
val Times_Int_distrib1 = thm "Times_Int_distrib1"; 

884 
val Times_Un_distrib1 = thm "Times_Un_distrib1"; 

885 
val Times_eq_cancel2 = thm "Times_eq_cancel2"; 

886 
val Times_subset_cancel2 = thm "Times_subset_cancel2"; 

887 
val UNIV_Times_UNIV = thm "UNIV_Times_UNIV"; 

888 
val UN_Times_distrib = thm "UN_Times_distrib"; 

889 
val Unity_def = thm "Unity_def"; 

890 
val cond_split_eta = thm "cond_split_eta"; 

891 
val fst_conv = thm "fst_conv"; 

892 
val fst_def = thm "fst_def"; 

893 
val fst_eqD = thm "fst_eqD"; 

894 
val inj_on_Abs_Prod = thm "inj_on_Abs_Prod"; 

895 
val injective_fst_snd = thm "injective_fst_snd"; 

896 
val mem_Sigma_iff = thm "mem_Sigma_iff"; 

897 
val mem_splitE = thm "mem_splitE"; 

898 
val mem_splitI = thm "mem_splitI"; 

899 
val mem_splitI2 = thm "mem_splitI2"; 

900 
val prod_eqI = thm "prod_eqI"; 

901 
val prod_fun = thm "prod_fun"; 

902 
val prod_fun_compose = thm "prod_fun_compose"; 

903 
val prod_fun_def = thm "prod_fun_def"; 

904 
val prod_fun_ident = thm "prod_fun_ident"; 

905 
val prod_fun_imageE = thm "prod_fun_imageE"; 

906 
val prod_fun_imageI = thm "prod_fun_imageI"; 

907 
val prod_induct = thm "prod_induct"; 

908 
val snd_conv = thm "snd_conv"; 

909 
val snd_def = thm "snd_def"; 

910 
val snd_eqD = thm "snd_eqD"; 

911 
val split = thm "split"; 

912 
val splitD = thm "splitD"; 

913 
val splitD' = thm "splitD'"; 

914 
val splitE = thm "splitE"; 

915 
val splitE' = thm "splitE'"; 

916 
val splitE2 = thm "splitE2"; 

917 
val splitI = thm "splitI"; 

918 
val splitI2 = thm "splitI2"; 

919 
val splitI2' = thm "splitI2'"; 

920 
val split_Pair_apply = thm "split_Pair_apply"; 

921 
val split_beta = thm "split_beta"; 

922 
val split_conv = thm "split_conv"; 

923 
val split_def = thm "split_def"; 

924 
val split_eta = thm "split_eta"; 

925 
val split_eta_SetCompr = thm "split_eta_SetCompr"; 

926 
val split_eta_SetCompr2 = thm "split_eta_SetCompr2"; 

927 
val split_paired_All = thm "split_paired_All"; 

928 
val split_paired_Ball_Sigma = thm "split_paired_Ball_Sigma"; 

929 
val split_paired_Bex_Sigma = thm "split_paired_Bex_Sigma"; 

930 
val split_paired_Ex = thm "split_paired_Ex"; 

931 
val split_paired_The = thm "split_paired_The"; 

932 
val split_paired_all = thm "split_paired_all"; 

933 
val split_part = thm "split_part"; 

934 
val split_split = thm "split_split"; 

935 
val split_split_asm = thm "split_split_asm"; 

936 
val split_tupled_all = thms "split_tupled_all"; 

937 
val split_weak_cong = thm "split_weak_cong"; 

938 
val surj_pair = thm "surj_pair"; 

939 
val surjective_pairing = thm "surjective_pairing"; 

940 
val unit_abs_eta_conv = thm "unit_abs_eta_conv"; 

941 
val unit_all_eq1 = thm "unit_all_eq1"; 

942 
val unit_all_eq2 = thm "unit_all_eq2"; 

943 
val unit_eq = thm "unit_eq"; 

944 
val unit_induct = thm "unit_induct"; 

945 
*} 

946 

10213  947 
end 