src/HOL/Typedef.thy
author wenzelm
Mon Sep 29 10:58:01 2008 +0200 (2008-09-29)
changeset 28394 b9c8e3a12a98
parent 28084 a05ca48ef263
child 28965 1de908189869
permissions -rw-r--r--
LocalTheory.exit_global;
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
huffman@27295
    93
lemma Rep_range: "range Rep = A"
huffman@24269
    94
proof
huffman@24269
    95
  show "range Rep <= A" using Rep by (auto simp add: image_def)
huffman@24269
    96
  show "A <= range Rep"
nipkow@23433
    97
  proof
nipkow@23433
    98
    fix x assume "x : A"
huffman@24269
    99
    hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
huffman@24269
   100
    thus "x : range Rep" by (rule range_eqI)
nipkow@23433
   101
  qed
nipkow@23433
   102
qed
nipkow@23433
   103
huffman@27295
   104
lemma Abs_image: "Abs ` A = UNIV"
huffman@27295
   105
proof
huffman@27295
   106
  show "Abs ` A <= UNIV" by (rule subset_UNIV)
huffman@27295
   107
next
huffman@27295
   108
  show "UNIV <= Abs ` A"
huffman@27295
   109
  proof
huffman@27295
   110
    fix x
huffman@27295
   111
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
huffman@27295
   112
    moreover have "Rep x : A" by (rule Rep)
huffman@27295
   113
    ultimately show "x : Abs ` A" by (rule image_eqI)
huffman@27295
   114
  qed
huffman@27295
   115
qed
huffman@27295
   116
haftmann@23247
   117
end
haftmann@23247
   118
wenzelm@11608
   119
use "Tools/typedef_package.ML"
haftmann@20426
   120
use "Tools/typecopy_package.ML"
haftmann@19459
   121
use "Tools/typedef_codegen.ML"
wenzelm@11608
   122
haftmann@20426
   123
setup {*
haftmann@25535
   124
  TypedefPackage.setup
haftmann@25535
   125
  #> TypecopyPackage.setup
haftmann@20426
   126
  #> TypedefCodegen.setup
haftmann@20426
   127
*}
berghofe@15260
   128
haftmann@26151
   129
text {* This class is just a workaround for classes without parameters;
haftmann@26151
   130
  it shall disappear as soon as possible. *}
haftmann@26151
   131
haftmann@26151
   132
class itself = type + 
haftmann@26151
   133
  fixes itself :: "'a itself"
haftmann@26151
   134
haftmann@26151
   135
setup {*
haftmann@26151
   136
let fun add_itself tyco thy =
haftmann@26151
   137
  let
haftmann@26151
   138
    val vs = Name.names Name.context "'a"
haftmann@26151
   139
      (replicate (Sign.arity_number thy tyco) @{sort type});
haftmann@26151
   140
    val ty = Type (tyco, map TFree vs);
haftmann@26151
   141
    val lhs = Const (@{const_name itself}, Term.itselfT ty);
haftmann@26151
   142
    val rhs = Logic.mk_type ty;
haftmann@26151
   143
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
haftmann@26151
   144
  in
haftmann@26151
   145
    thy
haftmann@26151
   146
    |> TheoryTarget.instantiation ([tyco], vs, @{sort itself})
haftmann@26151
   147
    |> `(fn lthy => Syntax.check_term lthy eq)
wenzelm@28084
   148
    |-> (fn eq => Specification.definition (NONE, (Attrib.no_binding, eq)))
haftmann@26151
   149
    |> snd
haftmann@26151
   150
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
wenzelm@28394
   151
    |> LocalTheory.exit_global
haftmann@26151
   152
  end
haftmann@26151
   153
in TypedefPackage.interpretation add_itself end
haftmann@26151
   154
*}
haftmann@26151
   155
haftmann@26151
   156
instantiation bool :: itself
haftmann@26151
   157
begin
haftmann@26151
   158
haftmann@26151
   159
definition "itself = TYPE(bool)"
haftmann@26151
   160
haftmann@26151
   161
instance ..
haftmann@26151
   162
wenzelm@11608
   163
end
haftmann@26151
   164
haftmann@26151
   165
instantiation "fun" :: ("type", "type") itself
haftmann@26151
   166
begin
haftmann@26151
   167
haftmann@26151
   168
definition "itself = TYPE('a \<Rightarrow> 'b)"
haftmann@26151
   169
haftmann@26151
   170
instance ..
haftmann@26151
   171
haftmann@26151
   172
end
haftmann@26151
   173
haftmann@26151
   174
hide (open) const itself
haftmann@26151
   175
haftmann@26151
   176
end