src/HOL/Typedef.thy
author ballarin
Fri, 19 Dec 2008 16:39:23 +0100
changeset 29249 4dc278c8dc59
parent 29056 dc08e3990c77
child 29608 564ea783ace8
permissions -rw-r--r--
All logics ported to new locales.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Typedef.thy
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
     2
    Author:     Markus Wenzel, TU Munich
11743
wenzelm
parents: 11659
diff changeset
     3
*)
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
     4
11979
0a3dace545c5 converted theory "Set";
wenzelm
parents: 11770
diff changeset
     5
header {* HOL type definitions *}
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
     6
15131
c69542757a4d New theory header syntax.
nipkow
parents: 13421
diff changeset
     7
theory Typedef
15140
322485b816ac import -> imports
nipkow
parents: 15131
diff changeset
     8
imports Set
20426
9ffea7a8b31c added typecopy_package
haftmann
parents: 19459
diff changeset
     9
uses
9ffea7a8b31c added typecopy_package
haftmann
parents: 19459
diff changeset
    10
  ("Tools/typedef_package.ML")
9ffea7a8b31c added typecopy_package
haftmann
parents: 19459
diff changeset
    11
  ("Tools/typecopy_package.ML")
9ffea7a8b31c added typecopy_package
haftmann
parents: 19459
diff changeset
    12
  ("Tools/typedef_codegen.ML")
15131
c69542757a4d New theory header syntax.
nipkow
parents: 13421
diff changeset
    13
begin
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    14
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    15
ML {*
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    16
structure HOL = struct val thy = theory "HOL" end;
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    17
*}  -- "belongs to theory HOL"
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    18
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    19
locale type_definition =
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    20
  fixes Rep and Abs and A
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    21
  assumes Rep: "Rep x \<in> A"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    22
    and Rep_inverse: "Abs (Rep x) = x"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    23
    and Abs_inverse: "y \<in> A ==> Rep (Abs y) = y"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    24
  -- {* This will be axiomatized for each typedef! *}
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    25
begin
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    26
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    27
lemma Rep_inject:
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    28
  "(Rep x = Rep y) = (x = y)"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    29
proof
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    30
  assume "Rep x = Rep y"
23710
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    31
  then have "Abs (Rep x) = Abs (Rep y)" by (simp only:)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    32
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    33
  moreover have "Abs (Rep y) = y" by (rule Rep_inverse)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    34
  ultimately show "x = y" by simp
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    35
next
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    36
  assume "x = y"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    37
  thus "Rep x = Rep y" by (simp only:)
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    38
qed
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    39
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    40
lemma Abs_inject:
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    41
  assumes x: "x \<in> A" and y: "y \<in> A"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    42
  shows "(Abs x = Abs y) = (x = y)"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    43
proof
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    44
  assume "Abs x = Abs y"
23710
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    45
  then have "Rep (Abs x) = Rep (Abs y)" by (simp only:)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    46
  moreover from x have "Rep (Abs x) = x" by (rule Abs_inverse)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    47
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    48
  ultimately show "x = y" by simp
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    49
next
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    50
  assume "x = y"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    51
  thus "Abs x = Abs y" by (simp only:)
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    52
qed
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    53
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    54
lemma Rep_cases [cases set]:
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    55
  assumes y: "y \<in> A"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    56
    and hyp: "!!x. y = Rep x ==> P"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    57
  shows P
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    58
proof (rule hyp)
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    59
  from y have "Rep (Abs y) = y" by (rule Abs_inverse)
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    60
  thus "y = Rep (Abs y)" ..
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    61
qed
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    62
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    63
lemma Abs_cases [cases type]:
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    64
  assumes r: "!!y. x = Abs y ==> y \<in> A ==> P"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    65
  shows P
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    66
proof (rule r)
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    67
  have "Abs (Rep x) = x" by (rule Rep_inverse)
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    68
  thus "x = Abs (Rep x)" ..
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    69
  show "Rep x \<in> A" by (rule Rep)
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    70
qed
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    71
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    72
lemma Rep_induct [induct set]:
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    73
  assumes y: "y \<in> A"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    74
    and hyp: "!!x. P (Rep x)"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    75
  shows "P y"
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    76
proof -
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    77
  have "P (Rep (Abs y))" by (rule hyp)
23710
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    78
  moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    79
  ultimately show "P y" by simp
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    80
qed
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    81
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
    82
lemma Abs_induct [induct type]:
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    83
  assumes r: "!!y. y \<in> A ==> P (Abs y)"
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    84
  shows "P x"
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    85
proof -
13412
666137b488a4 predicate defs via locales;
wenzelm
parents: 12023
diff changeset
    86
  have "Rep x \<in> A" by (rule Rep)
23710
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    87
  then have "P (Abs (Rep x))" by (rule r)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    88
  moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
a8ac2305eaf2 removed proof dependency on transitivity theorems
haftmann
parents: 23433
diff changeset
    89
  ultimately show "P x" by simp
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    90
qed
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
    91
27295
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
    92
lemma Rep_range: "range Rep = A"
24269
4b2aac7669b3 remove redundant assumption from Rep_range lemma
huffman
parents: 23710
diff changeset
    93
proof
4b2aac7669b3 remove redundant assumption from Rep_range lemma
huffman
parents: 23710
diff changeset
    94
  show "range Rep <= A" using Rep by (auto simp add: image_def)
4b2aac7669b3 remove redundant assumption from Rep_range lemma
huffman
parents: 23710
diff changeset
    95
  show "A <= range Rep"
23433
c2c10abd2a1e added lemmas
nipkow
parents: 23247
diff changeset
    96
  proof
c2c10abd2a1e added lemmas
nipkow
parents: 23247
diff changeset
    97
    fix x assume "x : A"
24269
4b2aac7669b3 remove redundant assumption from Rep_range lemma
huffman
parents: 23710
diff changeset
    98
    hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
4b2aac7669b3 remove redundant assumption from Rep_range lemma
huffman
parents: 23710
diff changeset
    99
    thus "x : range Rep" by (rule range_eqI)
23433
c2c10abd2a1e added lemmas
nipkow
parents: 23247
diff changeset
   100
  qed
c2c10abd2a1e added lemmas
nipkow
parents: 23247
diff changeset
   101
qed
c2c10abd2a1e added lemmas
nipkow
parents: 23247
diff changeset
   102
27295
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   103
lemma Abs_image: "Abs ` A = UNIV"
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   104
proof
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   105
  show "Abs ` A <= UNIV" by (rule subset_UNIV)
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   106
next
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   107
  show "UNIV <= Abs ` A"
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   108
  proof
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   109
    fix x
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   110
    have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   111
    moreover have "Rep x : A" by (rule Rep)
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   112
    ultimately show "x : Abs ` A" by (rule image_eqI)
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   113
  qed
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   114
qed
cfe5244301dd add lemma Abs_image
huffman
parents: 26802
diff changeset
   115
23247
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
   116
end
b99dce43d252 merged Code_Generator.thy into HOL.thy
haftmann
parents: 22846
diff changeset
   117
29056
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 28965
diff changeset
   118
use "Tools/typedef_package.ML" setup TypedefPackage.setup
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 28965
diff changeset
   119
use "Tools/typecopy_package.ML" setup TypecopyPackage.setup
dc08e3990c77 misc tuning and modernisation;
wenzelm
parents: 28965
diff changeset
   120
use "Tools/typedef_codegen.ML" setup TypedefCodegen.setup
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
   121
15260
a12e999a0113 Added setup for code generator.
berghofe
parents: 15140
diff changeset
   122
26151
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   123
text {* This class is just a workaround for classes without parameters;
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   124
  it shall disappear as soon as possible. *}
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   125
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   126
class itself = type + 
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   127
  fixes itself :: "'a itself"
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   128
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   129
setup {*
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   130
let fun add_itself tyco thy =
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   131
  let
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   132
    val vs = Name.names Name.context "'a"
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   133
      (replicate (Sign.arity_number thy tyco) @{sort type});
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   134
    val ty = Type (tyco, map TFree vs);
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   135
    val lhs = Const (@{const_name itself}, Term.itselfT ty);
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   136
    val rhs = Logic.mk_type ty;
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   137
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   138
  in
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   139
    thy
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   140
    |> TheoryTarget.instantiation ([tyco], vs, @{sort itself})
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   141
    |> `(fn lthy => Syntax.check_term lthy eq)
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28394
diff changeset
   142
    |-> (fn eq => Specification.definition (NONE, (Attrib.empty_binding, eq)))
26151
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   143
    |> snd
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   144
    |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
28394
b9c8e3a12a98 LocalTheory.exit_global;
wenzelm
parents: 28084
diff changeset
   145
    |> LocalTheory.exit_global
26151
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   146
  end
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   147
in TypedefPackage.interpretation add_itself end
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   148
*}
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   149
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   150
instantiation bool :: itself
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   151
begin
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   152
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   153
definition "itself = TYPE(bool)"
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   154
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   155
instance ..
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   156
11608
c760ea8154ee renamed theory "subset" to "Typedef";
wenzelm
parents:
diff changeset
   157
end
26151
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   158
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   159
instantiation "fun" :: ("type", "type") itself
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   160
begin
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   161
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   162
definition "itself = TYPE('a \<Rightarrow> 'b)"
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   163
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   164
instance ..
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   165
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   166
end
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   167
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   168
hide (open) const itself
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   169
4a9b8f15ce7f class itself works around a problem with class interpretation in class finite
haftmann
parents: 25535
diff changeset
   170
end