src/HOL/Typedef.thy
author haftmann
Wed Jan 21 23:40:23 2009 +0100 (2009-01-21)
changeset 29608 564ea783ace8
parent 29056 dc08e3990c77
child 29797 08ef36ed2f8a
permissions -rw-r--r--
no base sort in class import
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@20426
    10
  ("Tools/typedef_package.ML")
haftmann@20426
    11
  ("Tools/typecopy_package.ML")
haftmann@20426
    12
  ("Tools/typedef_codegen.ML")
nipkow@15131
    13
begin
wenzelm@11608
    14
haftmann@23247
    15
ML {*
haftmann@23247
    16
structure HOL = struct val thy = theory "HOL" end;
haftmann@23247
    17
*}  -- "belongs to theory HOL"
haftmann@23247
    18
wenzelm@13412
    19
locale type_definition =
wenzelm@13412
    20
  fixes Rep and Abs and A
wenzelm@13412
    21
  assumes Rep: "Rep x \<in> A"
wenzelm@13412
    22
    and Rep_inverse: "Abs (Rep x) = x"
wenzelm@13412
    23
    and Abs_inverse: "y \<in> A ==> Rep (Abs y) = y"
wenzelm@13412
    24
  -- {* This will be axiomatized for each typedef! *}
haftmann@23247
    25
begin
wenzelm@11608
    26
haftmann@23247
    27
lemma Rep_inject:
wenzelm@13412
    28
  "(Rep x = Rep y) = (x = y)"
wenzelm@13412
    29
proof
wenzelm@13412
    30
  assume "Rep x = Rep y"
haftmann@23710
    31
  then have "Abs (Rep x) = Abs (Rep y)" by (simp only:)
haftmann@23710
    32
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
haftmann@23710
    33
  moreover have "Abs (Rep y) = y" by (rule Rep_inverse)
haftmann@23710
    34
  ultimately show "x = y" by simp
wenzelm@13412
    35
next
wenzelm@13412
    36
  assume "x = y"
wenzelm@13412
    37
  thus "Rep x = Rep y" by (simp only:)
wenzelm@13412
    38
qed
wenzelm@11608
    39
haftmann@23247
    40
lemma Abs_inject:
wenzelm@13412
    41
  assumes x: "x \<in> A" and y: "y \<in> A"
wenzelm@13412
    42
  shows "(Abs x = Abs y) = (x = y)"
wenzelm@13412
    43
proof
wenzelm@13412
    44
  assume "Abs x = Abs y"
haftmann@23710
    45
  then have "Rep (Abs x) = Rep (Abs y)" by (simp only:)
haftmann@23710
    46
  moreover from x have "Rep (Abs x) = x" by (rule Abs_inverse)
haftmann@23710
    47
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
haftmann@23710
    48
  ultimately show "x = y" by simp
wenzelm@13412
    49
next
wenzelm@13412
    50
  assume "x = y"
wenzelm@13412
    51
  thus "Abs x = Abs y" by (simp only:)
wenzelm@11608
    52
qed
wenzelm@11608
    53
haftmann@23247
    54
lemma Rep_cases [cases set]:
wenzelm@13412
    55
  assumes y: "y \<in> A"
wenzelm@13412
    56
    and hyp: "!!x. y = Rep x ==> P"
wenzelm@13412
    57
  shows P
wenzelm@13412
    58
proof (rule hyp)
wenzelm@13412
    59
  from y have "Rep (Abs y) = y" by (rule Abs_inverse)
wenzelm@13412
    60
  thus "y = Rep (Abs y)" ..
wenzelm@11608
    61
qed
wenzelm@11608
    62
haftmann@23247
    63
lemma Abs_cases [cases type]:
wenzelm@13412
    64
  assumes r: "!!y. x = Abs y ==> y \<in> A ==> P"
wenzelm@13412
    65
  shows P
wenzelm@13412
    66
proof (rule r)
wenzelm@13412
    67
  have "Abs (Rep x) = x" by (rule Rep_inverse)
wenzelm@13412
    68
  thus "x = Abs (Rep x)" ..
wenzelm@13412
    69
  show "Rep x \<in> A" by (rule Rep)
wenzelm@11608
    70
qed
wenzelm@11608
    71
haftmann@23247
    72
lemma Rep_induct [induct set]:
wenzelm@13412
    73
  assumes y: "y \<in> A"
wenzelm@13412
    74
    and hyp: "!!x. P (Rep x)"
wenzelm@13412
    75
  shows "P y"
wenzelm@11608
    76
proof -
wenzelm@13412
    77
  have "P (Rep (Abs y))" by (rule hyp)
haftmann@23710
    78
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
haftmann@23710
    79
  ultimately show "P y" by simp
wenzelm@11608
    80
qed
wenzelm@11608
    81
haftmann@23247
    82
lemma Abs_induct [induct type]:
wenzelm@13412
    83
  assumes r: "!!y. y \<in> A ==> P (Abs y)"
wenzelm@13412
    84
  shows "P x"
wenzelm@11608
    85
proof -
wenzelm@13412
    86
  have "Rep x \<in> A" by (rule Rep)
haftmann@23710
    87
  then have "P (Abs (Rep x))" by (rule r)
haftmann@23710
    88
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
haftmann@23710
    89
  ultimately show "P x" by simp
wenzelm@11608
    90
qed
wenzelm@11608
    91
huffman@27295
    92
lemma Rep_range: "range Rep = A"
huffman@24269
    93
proof
huffman@24269
    94
  show "range Rep <= A" using Rep by (auto simp add: image_def)
huffman@24269
    95
  show "A <= range Rep"
nipkow@23433
    96
  proof
nipkow@23433
    97
    fix x assume "x : A"
huffman@24269
    98
    hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
huffman@24269
    99
    thus "x : range Rep" by (rule range_eqI)
nipkow@23433
   100
  qed
nipkow@23433
   101
qed
nipkow@23433
   102
huffman@27295
   103
lemma Abs_image: "Abs ` A = UNIV"
huffman@27295
   104
proof
huffman@27295
   105
  show "Abs ` A <= UNIV" by (rule subset_UNIV)
huffman@27295
   106
next
huffman@27295
   107
  show "UNIV <= Abs ` A"
huffman@27295
   108
  proof
huffman@27295
   109
    fix x
huffman@27295
   110
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
huffman@27295
   111
    moreover have "Rep x : A" by (rule Rep)
huffman@27295
   112
    ultimately show "x : Abs ` A" by (rule image_eqI)
huffman@27295
   113
  qed
huffman@27295
   114
qed
huffman@27295
   115
haftmann@23247
   116
end
haftmann@23247
   117
wenzelm@29056
   118
use "Tools/typedef_package.ML" setup TypedefPackage.setup
wenzelm@29056
   119
use "Tools/typecopy_package.ML" setup TypecopyPackage.setup
wenzelm@29056
   120
use "Tools/typedef_codegen.ML" setup TypedefCodegen.setup
wenzelm@11608
   121
berghofe@15260
   122
haftmann@26151
   123
text {* This class is just a workaround for classes without parameters;
haftmann@26151
   124
  it shall disappear as soon as possible. *}
haftmann@26151
   125
haftmann@29608
   126
class itself = 
haftmann@26151
   127
  fixes itself :: "'a itself"
haftmann@26151
   128
haftmann@26151
   129
setup {*
haftmann@26151
   130
let fun add_itself tyco thy =
haftmann@26151
   131
  let
haftmann@26151
   132
    val vs = Name.names Name.context "'a"
haftmann@26151
   133
      (replicate (Sign.arity_number thy tyco) @{sort type});
haftmann@26151
   134
    val ty = Type (tyco, map TFree vs);
haftmann@26151
   135
    val lhs = Const (@{const_name itself}, Term.itselfT ty);
haftmann@26151
   136
    val rhs = Logic.mk_type ty;
haftmann@26151
   137
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@26151
   138
  in
haftmann@26151
   139
    thy
haftmann@26151
   140
    |> TheoryTarget.instantiation ([tyco], vs, @{sort itself})
haftmann@26151
   141
    |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@28965
   142
    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
haftmann@26151
   143
    |> snd
haftmann@26151
   144
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
wenzelm@28394
   145
    |> LocalTheory.exit_global
haftmann@26151
   146
  end
haftmann@26151
   147
in TypedefPackage.interpretation add_itself end
haftmann@26151
   148
*}
haftmann@26151
   149
haftmann@26151
   150
instantiation bool :: itself
haftmann@26151
   151
begin
haftmann@26151
   152
haftmann@26151
   153
definition "itself = TYPE(bool)"
haftmann@26151
   154
haftmann@26151
   155
instance ..
haftmann@26151
   156
wenzelm@11608
   157
end
haftmann@26151
   158
haftmann@26151
   159
instantiation "fun" :: ("type", "type") itself
haftmann@26151
   160
begin
haftmann@26151
   161
haftmann@26151
   162
definition "itself = TYPE('a \<Rightarrow> 'b)"
haftmann@26151
   163
haftmann@26151
   164
instance ..
haftmann@26151
   165
haftmann@26151
   166
end
haftmann@26151
   167
haftmann@26151
   168
hide (open) const itself
haftmann@26151
   169
haftmann@26151
   170
end