src/HOL/Typedef.thy
author haftmann
Thu Aug 12 17:56:41 2010 +0200 (2010-08-12)
changeset 38393 7c045c03598f
parent 37863 7f113caabcf4
child 38536 7e57a0dcbd4f
permissions -rw-r--r--
group record-related ML files
wenzelm@11608
     1
(*  Title:      HOL/Typedef.thy
wenzelm@11608
     2
    Author:     Markus Wenzel, TU Munich
wenzelm@11743
     3
*)
wenzelm@11608
     4
wenzelm@11979
     5
header {* HOL type definitions *}
wenzelm@11608
     6
nipkow@15131
     7
theory Typedef
nipkow@15140
     8
imports Set
haftmann@20426
     9
uses
haftmann@31723
    10
  ("Tools/typedef.ML")
haftmann@20426
    11
  ("Tools/typedef_codegen.ML")
nipkow@15131
    12
begin
wenzelm@11608
    13
haftmann@23247
    14
ML {*
wenzelm@37863
    15
structure HOL = struct val thy = @{theory HOL} end;
haftmann@23247
    16
*}  -- "belongs to theory HOL"
haftmann@23247
    17
wenzelm@13412
    18
locale type_definition =
wenzelm@13412
    19
  fixes Rep and Abs and A
wenzelm@13412
    20
  assumes Rep: "Rep x \<in> A"
wenzelm@13412
    21
    and Rep_inverse: "Abs (Rep x) = x"
wenzelm@13412
    22
    and Abs_inverse: "y \<in> A ==> Rep (Abs y) = y"
wenzelm@13412
    23
  -- {* This will be axiomatized for each typedef! *}
haftmann@23247
    24
begin
wenzelm@11608
    25
haftmann@23247
    26
lemma Rep_inject:
wenzelm@13412
    27
  "(Rep x = Rep y) = (x = y)"
wenzelm@13412
    28
proof
wenzelm@13412
    29
  assume "Rep x = Rep y"
haftmann@23710
    30
  then have "Abs (Rep x) = Abs (Rep y)" by (simp only:)
haftmann@23710
    31
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
haftmann@23710
    32
  moreover have "Abs (Rep y) = y" by (rule Rep_inverse)
haftmann@23710
    33
  ultimately show "x = y" by simp
wenzelm@13412
    34
next
wenzelm@13412
    35
  assume "x = y"
wenzelm@13412
    36
  thus "Rep x = Rep y" by (simp only:)
wenzelm@13412
    37
qed
wenzelm@11608
    38
haftmann@23247
    39
lemma Abs_inject:
wenzelm@13412
    40
  assumes x: "x \<in> A" and y: "y \<in> A"
wenzelm@13412
    41
  shows "(Abs x = Abs y) = (x = y)"
wenzelm@13412
    42
proof
wenzelm@13412
    43
  assume "Abs x = Abs y"
haftmann@23710
    44
  then have "Rep (Abs x) = Rep (Abs y)" by (simp only:)
haftmann@23710
    45
  moreover from x have "Rep (Abs x) = x" by (rule Abs_inverse)
haftmann@23710
    46
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
haftmann@23710
    47
  ultimately show "x = y" by simp
wenzelm@13412
    48
next
wenzelm@13412
    49
  assume "x = y"
wenzelm@13412
    50
  thus "Abs x = Abs y" by (simp only:)
wenzelm@11608
    51
qed
wenzelm@11608
    52
haftmann@23247
    53
lemma Rep_cases [cases set]:
wenzelm@13412
    54
  assumes y: "y \<in> A"
wenzelm@13412
    55
    and hyp: "!!x. y = Rep x ==> P"
wenzelm@13412
    56
  shows P
wenzelm@13412
    57
proof (rule hyp)
wenzelm@13412
    58
  from y have "Rep (Abs y) = y" by (rule Abs_inverse)
wenzelm@13412
    59
  thus "y = Rep (Abs y)" ..
wenzelm@11608
    60
qed
wenzelm@11608
    61
haftmann@23247
    62
lemma Abs_cases [cases type]:
wenzelm@13412
    63
  assumes r: "!!y. x = Abs y ==> y \<in> A ==> P"
wenzelm@13412
    64
  shows P
wenzelm@13412
    65
proof (rule r)
wenzelm@13412
    66
  have "Abs (Rep x) = x" by (rule Rep_inverse)
wenzelm@13412
    67
  thus "x = Abs (Rep x)" ..
wenzelm@13412
    68
  show "Rep x \<in> A" by (rule Rep)
wenzelm@11608
    69
qed
wenzelm@11608
    70
haftmann@23247
    71
lemma Rep_induct [induct set]:
wenzelm@13412
    72
  assumes y: "y \<in> A"
wenzelm@13412
    73
    and hyp: "!!x. P (Rep x)"
wenzelm@13412
    74
  shows "P y"
wenzelm@11608
    75
proof -
wenzelm@13412
    76
  have "P (Rep (Abs y))" by (rule hyp)
haftmann@23710
    77
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
haftmann@23710
    78
  ultimately show "P y" by simp
wenzelm@11608
    79
qed
wenzelm@11608
    80
haftmann@23247
    81
lemma Abs_induct [induct type]:
wenzelm@13412
    82
  assumes r: "!!y. y \<in> A ==> P (Abs y)"
wenzelm@13412
    83
  shows "P x"
wenzelm@11608
    84
proof -
wenzelm@13412
    85
  have "Rep x \<in> A" by (rule Rep)
haftmann@23710
    86
  then have "P (Abs (Rep x))" by (rule r)
haftmann@23710
    87
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
haftmann@23710
    88
  ultimately show "P x" by simp
wenzelm@11608
    89
qed
wenzelm@11608
    90
huffman@27295
    91
lemma Rep_range: "range Rep = A"
huffman@24269
    92
proof
huffman@24269
    93
  show "range Rep <= A" using Rep by (auto simp add: image_def)
huffman@24269
    94
  show "A <= range Rep"
nipkow@23433
    95
  proof
nipkow@23433
    96
    fix x assume "x : A"
huffman@24269
    97
    hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
huffman@24269
    98
    thus "x : range Rep" by (rule range_eqI)
nipkow@23433
    99
  qed
nipkow@23433
   100
qed
nipkow@23433
   101
huffman@27295
   102
lemma Abs_image: "Abs ` A = UNIV"
huffman@27295
   103
proof
huffman@27295
   104
  show "Abs ` A <= UNIV" by (rule subset_UNIV)
huffman@27295
   105
next
huffman@27295
   106
  show "UNIV <= Abs ` A"
huffman@27295
   107
  proof
huffman@27295
   108
    fix x
huffman@27295
   109
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
huffman@27295
   110
    moreover have "Rep x : A" by (rule Rep)
huffman@27295
   111
    ultimately show "x : Abs ` A" by (rule image_eqI)
huffman@27295
   112
  qed
huffman@27295
   113
qed
huffman@27295
   114
haftmann@23247
   115
end
haftmann@23247
   116
haftmann@31723
   117
use "Tools/typedef.ML" setup Typedef.setup
wenzelm@29056
   118
use "Tools/typedef_codegen.ML" setup TypedefCodegen.setup
wenzelm@11608
   119
wenzelm@11608
   120
end