src/HOL/Typedef.thy
author nipkow
Wed Jun 20 14:38:24 2007 +0200 (2007-06-20)
changeset 23433 c2c10abd2a1e
parent 23247 b99dce43d252
child 23710 a8ac2305eaf2
permissions -rw-r--r--
added lemmas
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"
wenzelm@13412
    32
  hence "Abs (Rep x) = Abs (Rep y)" by (simp only:)
wenzelm@13412
    33
  also have "Abs (Rep x) = x" by (rule Rep_inverse)
wenzelm@13412
    34
  also have "Abs (Rep y) = y" by (rule Rep_inverse)
wenzelm@13412
    35
  finally show "x = y" .
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"
wenzelm@13412
    46
  hence "Rep (Abs x) = Rep (Abs y)" by (simp only:)
wenzelm@13412
    47
  also from x have "Rep (Abs x) = x" by (rule Abs_inverse)
wenzelm@13412
    48
  also from y have "Rep (Abs y) = y" by (rule Abs_inverse)
wenzelm@13412
    49
  finally show "x = y" .
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)
wenzelm@13412
    79
  also from y have "Rep (Abs y) = y" by (rule Abs_inverse)
wenzelm@13412
    80
  finally show "P y" .
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)
wenzelm@11608
    88
  hence "P (Abs (Rep x))" by (rule r)
wenzelm@13412
    89
  also have "Abs (Rep x) = x" by (rule Rep_inverse)
wenzelm@13412
    90
  finally show "P x" .
wenzelm@11608
    91
qed
wenzelm@11608
    92
nipkow@23433
    93
lemma Rep_range:
nipkow@23433
    94
assumes "type_definition Rep Abs A"
nipkow@23433
    95
shows "range Rep = A"
nipkow@23433
    96
proof -
nipkow@23433
    97
  from assms have A1: "!!x. Rep x : A"
nipkow@23433
    98
              and A2: "!!y. y : A ==> y = Rep(Abs y)"
nipkow@23433
    99
     by (auto simp add: type_definition_def)
nipkow@23433
   100
  have "range Rep <= A" using A1 by (auto simp add: image_def)
nipkow@23433
   101
  moreover have "A <= range Rep"
nipkow@23433
   102
  proof
nipkow@23433
   103
    fix x assume "x : A"
nipkow@23433
   104
    hence "x = Rep (Abs x)" by (rule A2)
nipkow@23433
   105
    thus "x : range Rep" by (auto simp add: image_def)
nipkow@23433
   106
  qed
nipkow@23433
   107
  ultimately show ?thesis ..
nipkow@23433
   108
qed
nipkow@23433
   109
haftmann@23247
   110
end
haftmann@23247
   111
wenzelm@11608
   112
use "Tools/typedef_package.ML"
haftmann@20426
   113
use "Tools/typecopy_package.ML"
haftmann@19459
   114
use "Tools/typedef_codegen.ML"
wenzelm@11608
   115
haftmann@20426
   116
setup {*
wenzelm@22846
   117
  TypecopyPackage.setup
haftmann@20426
   118
  #> TypedefCodegen.setup
haftmann@20426
   119
*}
berghofe@15260
   120
wenzelm@11608
   121
end