src/HOL/Typedef.thy
changeset 11608 c760ea8154ee
child 11654 53d18ab990f6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Typedef.thy	Thu Sep 27 22:26:00 2001 +0200
     1.3 @@ -0,0 +1,139 @@
     1.4 +(*  Title:      HOL/Typedef.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Markus Wenzel, TU Munich
     1.7 +
     1.8 +Misc set-theory lemmas and HOL type definitions.
     1.9 +*)
    1.10 +
    1.11 +theory Typedef = Set
    1.12 +files "subset.ML" "equalities.ML" "mono.ML"
    1.13 +  "Tools/induct_attrib.ML" ("Tools/typedef_package.ML"):
    1.14 +
    1.15 +(** belongs to theory Ord **)
    1.16 +  
    1.17 +theorems linorder_cases [case_names less equal greater] =
    1.18 +  linorder_less_split
    1.19 +
    1.20 +(* Courtesy of Stephan Merz *)
    1.21 +lemma Least_mono: 
    1.22 +  "mono (f::'a::order => 'b::order) ==> EX x:S. ALL y:S. x <= y
    1.23 +    ==> (LEAST y. y : f ` S) = f (LEAST x. x : S)"
    1.24 +  apply clarify
    1.25 +  apply (erule_tac P = "%x. x : S" in LeastI2)
    1.26 +   apply fast
    1.27 +  apply (rule LeastI2)
    1.28 +  apply (auto elim: monoD intro!: order_antisym)
    1.29 +  done
    1.30 +
    1.31 +
    1.32 +(*belongs to theory Set*)
    1.33 +setup Rulify.setup
    1.34 +
    1.35 +
    1.36 +section {* HOL type definitions *}
    1.37 +
    1.38 +constdefs
    1.39 +  type_definition :: "('a => 'b) => ('b => 'a) => 'b set => bool"
    1.40 +  "type_definition Rep Abs A ==
    1.41 +    (\<forall>x. Rep x \<in> A) \<and>
    1.42 +    (\<forall>x. Abs (Rep x) = x) \<and>
    1.43 +    (\<forall>y \<in> A. Rep (Abs y) = y)"
    1.44 +  -- {* This will be stated as an axiom for each typedef! *}
    1.45 +
    1.46 +lemma type_definitionI [intro]:
    1.47 +  "(!!x. Rep x \<in> A) ==>
    1.48 +    (!!x. Abs (Rep x) = x) ==>
    1.49 +    (!!y. y \<in> A ==> Rep (Abs y) = y) ==>
    1.50 +    type_definition Rep Abs A"
    1.51 +  by (unfold type_definition_def) blast
    1.52 +
    1.53 +theorem Rep: "type_definition Rep Abs A ==> Rep x \<in> A"
    1.54 +  by (unfold type_definition_def) blast
    1.55 +
    1.56 +theorem Rep_inverse: "type_definition Rep Abs A ==> Abs (Rep x) = x"
    1.57 +  by (unfold type_definition_def) blast
    1.58 +
    1.59 +theorem Abs_inverse: "type_definition Rep Abs A ==> y \<in> A ==> Rep (Abs y) = y"
    1.60 +  by (unfold type_definition_def) blast
    1.61 +
    1.62 +theorem Rep_inject: "type_definition Rep Abs A ==> (Rep x = Rep y) = (x = y)"
    1.63 +proof -
    1.64 +  assume tydef: "type_definition Rep Abs A"
    1.65 +  show ?thesis
    1.66 +  proof
    1.67 +    assume "Rep x = Rep y"
    1.68 +    hence "Abs (Rep x) = Abs (Rep y)" by (simp only:)
    1.69 +    thus "x = y" by (simp only: Rep_inverse [OF tydef])
    1.70 +  next
    1.71 +    assume "x = y"
    1.72 +    thus "Rep x = Rep y" by simp
    1.73 +  qed
    1.74 +qed
    1.75 +
    1.76 +theorem Abs_inject:
    1.77 +  "type_definition Rep Abs A ==> x \<in> A ==> y \<in> A ==> (Abs x = Abs y) = (x = y)"
    1.78 +proof -
    1.79 +  assume tydef: "type_definition Rep Abs A"
    1.80 +  assume x: "x \<in> A" and y: "y \<in> A"
    1.81 +  show ?thesis
    1.82 +  proof
    1.83 +    assume "Abs x = Abs y"
    1.84 +    hence "Rep (Abs x) = Rep (Abs y)" by simp
    1.85 +    moreover from x have "Rep (Abs x) = x" by (rule Abs_inverse [OF tydef])
    1.86 +    moreover from y have "Rep (Abs y) = y" by (rule Abs_inverse [OF tydef])
    1.87 +    ultimately show "x = y" by (simp only:)
    1.88 +  next
    1.89 +    assume "x = y"
    1.90 +    thus "Abs x = Abs y" by simp
    1.91 +  qed
    1.92 +qed
    1.93 +
    1.94 +theorem Rep_cases:
    1.95 +  "type_definition Rep Abs A ==> y \<in> A ==> (!!x. y = Rep x ==> P) ==> P"
    1.96 +proof -
    1.97 +  assume tydef: "type_definition Rep Abs A"
    1.98 +  assume y: "y \<in> A" and r: "(!!x. y = Rep x ==> P)"
    1.99 +  show P
   1.100 +  proof (rule r)
   1.101 +    from y have "Rep (Abs y) = y" by (rule Abs_inverse [OF tydef])
   1.102 +    thus "y = Rep (Abs y)" ..
   1.103 +  qed
   1.104 +qed
   1.105 +
   1.106 +theorem Abs_cases:
   1.107 +  "type_definition Rep Abs A ==> (!!y. x = Abs y ==> y \<in> A ==> P) ==> P"
   1.108 +proof -
   1.109 +  assume tydef: "type_definition Rep Abs A"
   1.110 +  assume r: "!!y. x = Abs y ==> y \<in> A ==> P"
   1.111 +  show P
   1.112 +  proof (rule r)
   1.113 +    have "Abs (Rep x) = x" by (rule Rep_inverse [OF tydef])
   1.114 +    thus "x = Abs (Rep x)" ..
   1.115 +    show "Rep x \<in> A" by (rule Rep [OF tydef])
   1.116 +  qed
   1.117 +qed
   1.118 +
   1.119 +theorem Rep_induct:
   1.120 +  "type_definition Rep Abs A ==> y \<in> A ==> (!!x. P (Rep x)) ==> P y"
   1.121 +proof -
   1.122 +  assume tydef: "type_definition Rep Abs A"
   1.123 +  assume "!!x. P (Rep x)" hence "P (Rep (Abs y))" .
   1.124 +  moreover assume "y \<in> A" hence "Rep (Abs y) = y" by (rule Abs_inverse [OF tydef])
   1.125 +  ultimately show "P y" by (simp only:)
   1.126 +qed
   1.127 +
   1.128 +theorem Abs_induct:
   1.129 +  "type_definition Rep Abs A ==> (!!y. y \<in> A ==> P (Abs y)) ==> P x"
   1.130 +proof -
   1.131 +  assume tydef: "type_definition Rep Abs A"
   1.132 +  assume r: "!!y. y \<in> A ==> P (Abs y)"
   1.133 +  have "Rep x \<in> A" by (rule Rep [OF tydef])
   1.134 +  hence "P (Abs (Rep x))" by (rule r)
   1.135 +  moreover have "Abs (Rep x) = x" by (rule Rep_inverse [OF tydef])
   1.136 +  ultimately show "P x" by (simp only:)
   1.137 +qed
   1.138 +
   1.139 +setup InductAttrib.setup
   1.140 +use "Tools/typedef_package.ML"
   1.141 +
   1.142 +end