src/HOL/Typedef.thy
author wenzelm
Tue Jul 20 23:09:49 2010 +0200 (2010-07-20)
changeset 37863 7f113caabcf4
parent 31723 f5cafe803b55
child 38393 7c045c03598f
permissions -rw-r--r--
discontinued pervasive val theory = Thy_Info.get_theory -- prefer antiquotations in most situations;
     1 (*  Title:      HOL/Typedef.thy
     2     Author:     Markus Wenzel, TU Munich
     3 *)
     4 
     5 header {* HOL type definitions *}
     6 
     7 theory Typedef
     8 imports Set
     9 uses
    10   ("Tools/typedef.ML")
    11   ("Tools/typecopy.ML")
    12   ("Tools/typedef_codegen.ML")
    13 begin
    14 
    15 ML {*
    16 structure HOL = struct val thy = @{theory HOL} end;
    17 *}  -- "belongs to theory HOL"
    18 
    19 locale type_definition =
    20   fixes Rep and Abs and A
    21   assumes Rep: "Rep x \<in> A"
    22     and Rep_inverse: "Abs (Rep x) = x"
    23     and Abs_inverse: "y \<in> A ==> Rep (Abs y) = y"
    24   -- {* This will be axiomatized for each typedef! *}
    25 begin
    26 
    27 lemma Rep_inject:
    28   "(Rep x = Rep y) = (x = y)"
    29 proof
    30   assume "Rep x = Rep y"
    31   then have "Abs (Rep x) = Abs (Rep y)" by (simp only:)
    32   moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
    33   moreover have "Abs (Rep y) = y" by (rule Rep_inverse)
    34   ultimately show "x = y" by simp
    35 next
    36   assume "x = y"
    37   thus "Rep x = Rep y" by (simp only:)
    38 qed
    39 
    40 lemma Abs_inject:
    41   assumes x: "x \<in> A" and y: "y \<in> A"
    42   shows "(Abs x = Abs y) = (x = y)"
    43 proof
    44   assume "Abs x = Abs y"
    45   then have "Rep (Abs x) = Rep (Abs y)" by (simp only:)
    46   moreover from x have "Rep (Abs x) = x" by (rule Abs_inverse)
    47   moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
    48   ultimately show "x = y" by simp
    49 next
    50   assume "x = y"
    51   thus "Abs x = Abs y" by (simp only:)
    52 qed
    53 
    54 lemma Rep_cases [cases set]:
    55   assumes y: "y \<in> A"
    56     and hyp: "!!x. y = Rep x ==> P"
    57   shows P
    58 proof (rule hyp)
    59   from y have "Rep (Abs y) = y" by (rule Abs_inverse)
    60   thus "y = Rep (Abs y)" ..
    61 qed
    62 
    63 lemma Abs_cases [cases type]:
    64   assumes r: "!!y. x = Abs y ==> y \<in> A ==> P"
    65   shows P
    66 proof (rule r)
    67   have "Abs (Rep x) = x" by (rule Rep_inverse)
    68   thus "x = Abs (Rep x)" ..
    69   show "Rep x \<in> A" by (rule Rep)
    70 qed
    71 
    72 lemma Rep_induct [induct set]:
    73   assumes y: "y \<in> A"
    74     and hyp: "!!x. P (Rep x)"
    75   shows "P y"
    76 proof -
    77   have "P (Rep (Abs y))" by (rule hyp)
    78   moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse)
    79   ultimately show "P y" by simp
    80 qed
    81 
    82 lemma Abs_induct [induct type]:
    83   assumes r: "!!y. y \<in> A ==> P (Abs y)"
    84   shows "P x"
    85 proof -
    86   have "Rep x \<in> A" by (rule Rep)
    87   then have "P (Abs (Rep x))" by (rule r)
    88   moreover have "Abs (Rep x) = x" by (rule Rep_inverse)
    89   ultimately show "P x" by simp
    90 qed
    91 
    92 lemma Rep_range: "range Rep = A"
    93 proof
    94   show "range Rep <= A" using Rep by (auto simp add: image_def)
    95   show "A <= range Rep"
    96   proof
    97     fix x assume "x : A"
    98     hence "x = Rep (Abs x)" by (rule Abs_inverse [symmetric])
    99     thus "x : range Rep" by (rule range_eqI)
   100   qed
   101 qed
   102 
   103 lemma Abs_image: "Abs ` A = UNIV"
   104 proof
   105   show "Abs ` A <= UNIV" by (rule subset_UNIV)
   106 next
   107   show "UNIV <= Abs ` A"
   108   proof
   109     fix x
   110     have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
   111     moreover have "Rep x : A" by (rule Rep)
   112     ultimately show "x : Abs ` A" by (rule image_eqI)
   113   qed
   114 qed
   115 
   116 end
   117 
   118 use "Tools/typedef.ML" setup Typedef.setup
   119 use "Tools/typecopy.ML" setup Typecopy.setup
   120 use "Tools/typedef_codegen.ML" setup TypedefCodegen.setup
   121 
   122 end