src/HOL/Typedef.thy
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (23 months ago)
changeset 66831 29ea2b900a05
parent 63434 c956d995bec6
child 69605 a96320074298
permissions -rw-r--r--
tuned: each session has at most one defining entry;
wenzelm@11608
     1
(*  Title:      HOL/Typedef.thy
wenzelm@11608
     2
    Author:     Markus Wenzel, TU Munich
wenzelm@11743
     3
*)
wenzelm@11608
     4
wenzelm@60758
     5
section \<open>HOL type definitions\<close>
wenzelm@11608
     6
nipkow@15131
     7
theory Typedef
nipkow@15140
     8
imports Set
wenzelm@63434
     9
keywords
wenzelm@63434
    10
  "typedef" :: thy_goal and
wenzelm@63434
    11
  "morphisms" :: quasi_command
nipkow@15131
    12
begin
wenzelm@11608
    13
wenzelm@13412
    14
locale type_definition =
wenzelm@13412
    15
  fixes Rep and Abs and A
wenzelm@13412
    16
  assumes Rep: "Rep x \<in> A"
wenzelm@13412
    17
    and Rep_inverse: "Abs (Rep x) = x"
wenzelm@61102
    18
    and Abs_inverse: "y \<in> A \<Longrightarrow> Rep (Abs y) = y"
wenzelm@61799
    19
  \<comment> \<open>This will be axiomatized for each typedef!\<close>
haftmann@23247
    20
begin
wenzelm@11608
    21
wenzelm@61102
    22
lemma Rep_inject: "Rep x = Rep y \<longleftrightarrow> x = y"
wenzelm@13412
    23
proof
wenzelm@13412
    24
  assume "Rep x = Rep y"
haftmann@23710
    25
  then have "Abs (Rep x) = Abs (Rep y)" by (simp only:)
haftmann@23710
    26
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
haftmann@23710
    27
  moreover have "Abs (Rep y) = y" by (rule Rep_inverse)
haftmann@23710
    28
  ultimately show "x = y" by simp
wenzelm@13412
    29
next
wenzelm@13412
    30
  assume "x = y"
wenzelm@61102
    31
  then show "Rep x = Rep y" by (simp only:)
wenzelm@13412
    32
qed
wenzelm@11608
    33
haftmann@23247
    34
lemma Abs_inject:
wenzelm@61102
    35
  assumes "x \<in> A" and "y \<in> A"
wenzelm@61102
    36
  shows "Abs x = Abs y \<longleftrightarrow> x = y"
wenzelm@13412
    37
proof
wenzelm@13412
    38
  assume "Abs x = Abs y"
haftmann@23710
    39
  then have "Rep (Abs x) = Rep (Abs y)" by (simp only:)
wenzelm@61102
    40
  moreover from \<open>x \<in> A\<close> have "Rep (Abs x) = x" by (rule Abs_inverse)
wenzelm@61102
    41
  moreover from \<open>y \<in> A\<close> have "Rep (Abs y) = y" by (rule Abs_inverse)
haftmann@23710
    42
  ultimately show "x = y" by simp
wenzelm@13412
    43
next
wenzelm@13412
    44
  assume "x = y"
wenzelm@61102
    45
  then show "Abs x = Abs y" by (simp only:)
wenzelm@11608
    46
qed
wenzelm@11608
    47
haftmann@23247
    48
lemma Rep_cases [cases set]:
wenzelm@61102
    49
  assumes "y \<in> A"
wenzelm@61102
    50
    and hyp: "\<And>x. y = Rep x \<Longrightarrow> P"
wenzelm@13412
    51
  shows P
wenzelm@13412
    52
proof (rule hyp)
wenzelm@61102
    53
  from \<open>y \<in> A\<close> have "Rep (Abs y) = y" by (rule Abs_inverse)
wenzelm@61102
    54
  then show "y = Rep (Abs y)" ..
wenzelm@11608
    55
qed
wenzelm@11608
    56
haftmann@23247
    57
lemma Abs_cases [cases type]:
wenzelm@61102
    58
  assumes r: "\<And>y. x = Abs y \<Longrightarrow> y \<in> A \<Longrightarrow> P"
wenzelm@13412
    59
  shows P
wenzelm@13412
    60
proof (rule r)
wenzelm@13412
    61
  have "Abs (Rep x) = x" by (rule Rep_inverse)
wenzelm@61102
    62
  then show "x = Abs (Rep x)" ..
wenzelm@13412
    63
  show "Rep x \<in> A" by (rule Rep)
wenzelm@11608
    64
qed
wenzelm@11608
    65
haftmann@23247
    66
lemma Rep_induct [induct set]:
wenzelm@13412
    67
  assumes y: "y \<in> A"
wenzelm@61102
    68
    and hyp: "\<And>x. P (Rep x)"
wenzelm@13412
    69
  shows "P y"
wenzelm@11608
    70
proof -
wenzelm@13412
    71
  have "P (Rep (Abs y))" by (rule hyp)
haftmann@23710
    72
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
haftmann@23710
    73
  ultimately show "P y" by simp
wenzelm@11608
    74
qed
wenzelm@11608
    75
haftmann@23247
    76
lemma Abs_induct [induct type]:
wenzelm@61102
    77
  assumes r: "\<And>y. y \<in> A \<Longrightarrow> P (Abs y)"
wenzelm@13412
    78
  shows "P x"
wenzelm@11608
    79
proof -
wenzelm@13412
    80
  have "Rep x \<in> A" by (rule Rep)
haftmann@23710
    81
  then have "P (Abs (Rep x))" by (rule r)
haftmann@23710
    82
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
haftmann@23710
    83
  ultimately show "P x" by simp
wenzelm@11608
    84
qed
wenzelm@11608
    85
huffman@27295
    86
lemma Rep_range: "range Rep = A"
huffman@24269
    87
proof
wenzelm@61102
    88
  show "range Rep \<subseteq> A" using Rep by (auto simp add: image_def)
wenzelm@61102
    89
  show "A \<subseteq> range Rep"
nipkow@23433
    90
  proof
wenzelm@61102
    91
    fix x assume "x \<in> A"
wenzelm@61102
    92
    then have "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
wenzelm@61102
    93
    then show "x \<in> range Rep" by (rule range_eqI)
nipkow@23433
    94
  qed
nipkow@23433
    95
qed
nipkow@23433
    96
huffman@27295
    97
lemma Abs_image: "Abs ` A = UNIV"
huffman@27295
    98
proof
wenzelm@61102
    99
  show "Abs ` A \<subseteq> UNIV" by (rule subset_UNIV)
wenzelm@61102
   100
  show "UNIV \<subseteq> Abs ` A"
huffman@27295
   101
  proof
huffman@27295
   102
    fix x
huffman@27295
   103
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
wenzelm@61102
   104
    moreover have "Rep x \<in> A" by (rule Rep)
wenzelm@61102
   105
    ultimately show "x \<in> Abs ` A" by (rule image_eqI)
huffman@27295
   106
  qed
huffman@27295
   107
qed
huffman@27295
   108
haftmann@23247
   109
end
haftmann@23247
   110
blanchet@58239
   111
ML_file "Tools/typedef.ML"
wenzelm@11608
   112
wenzelm@11608
   113
end