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