src/HOL/Typedef.thy
author berghofe
Wed May 07 10:56:50 2008 +0200 (2008-05-07)
changeset 26802 9eede540a5e8
parent 26151 4a9b8f15ce7f
child 27295 cfe5244301dd
permissions -rw-r--r--
Deleted instantiation "set :: (type) itself".
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 {*
haftmann@25535
   112
  TypedefPackage.setup
haftmann@25535
   113
  #> TypecopyPackage.setup
haftmann@20426
   114
  #> TypedefCodegen.setup
haftmann@20426
   115
*}
berghofe@15260
   116
haftmann@26151
   117
text {* This class is just a workaround for classes without parameters;
haftmann@26151
   118
  it shall disappear as soon as possible. *}
haftmann@26151
   119
haftmann@26151
   120
class itself = type + 
haftmann@26151
   121
  fixes itself :: "'a itself"
haftmann@26151
   122
haftmann@26151
   123
setup {*
haftmann@26151
   124
let fun add_itself tyco thy =
haftmann@26151
   125
  let
haftmann@26151
   126
    val vs = Name.names Name.context "'a"
haftmann@26151
   127
      (replicate (Sign.arity_number thy tyco) @{sort type});
haftmann@26151
   128
    val ty = Type (tyco, map TFree vs);
haftmann@26151
   129
    val lhs = Const (@{const_name itself}, Term.itselfT ty);
haftmann@26151
   130
    val rhs = Logic.mk_type ty;
haftmann@26151
   131
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@26151
   132
  in
haftmann@26151
   133
    thy
haftmann@26151
   134
    |> TheoryTarget.instantiation ([tyco], vs, @{sort itself})
haftmann@26151
   135
    |> `(fn lthy => Syntax.check_term lthy eq)
haftmann@26151
   136
    |-> (fn eq => Specification.definition (NONE, (("", []), eq)))
haftmann@26151
   137
    |> snd
haftmann@26151
   138
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
haftmann@26151
   139
    |> LocalTheory.exit
haftmann@26151
   140
    |> ProofContext.theory_of
haftmann@26151
   141
  end
haftmann@26151
   142
in TypedefPackage.interpretation add_itself end
haftmann@26151
   143
*}
haftmann@26151
   144
haftmann@26151
   145
instantiation bool :: itself
haftmann@26151
   146
begin
haftmann@26151
   147
haftmann@26151
   148
definition "itself = TYPE(bool)"
haftmann@26151
   149
haftmann@26151
   150
instance ..
haftmann@26151
   151
wenzelm@11608
   152
end
haftmann@26151
   153
haftmann@26151
   154
instantiation "fun" :: ("type", "type") itself
haftmann@26151
   155
begin
haftmann@26151
   156
haftmann@26151
   157
definition "itself = TYPE('a \<Rightarrow> 'b)"
haftmann@26151
   158
haftmann@26151
   159
instance ..
haftmann@26151
   160
haftmann@26151
   161
end
haftmann@26151
   162
haftmann@26151
   163
hide (open) const itself
haftmann@26151
   164
haftmann@26151
   165
end