src/HOL/Quotient_Examples/Lift_Fun.thy
changeset 63167 0909deb8059b
parent 58889 5b7a9633cfa8
child 66453 cc19f7ca2ed6
equal deleted inserted replaced
63166:143f58bb34f9 63167:0909deb8059b
     1 (*  Title:      HOL/Quotient_Examples/Lift_Fun.thy
     1 (*  Title:      HOL/Quotient_Examples/Lift_Fun.thy
     2     Author:     Ondrej Kuncar
     2     Author:     Ondrej Kuncar
     3 *)
     3 *)
     4 
     4 
     5 section {* Example of lifting definitions with contravariant or co/contravariant type variables *}
     5 section \<open>Example of lifting definitions with contravariant or co/contravariant type variables\<close>
     6 
     6 
     7 
     7 
     8 theory Lift_Fun
     8 theory Lift_Fun
     9 imports Main "~~/src/HOL/Library/Quotient_Syntax"
     9 imports Main "~~/src/HOL/Library/Quotient_Syntax"
    10 begin
    10 begin
    11 
    11 
    12 text {* This file is meant as a test case. 
    12 text \<open>This file is meant as a test case. 
    13   It contains examples of lifting definitions with quotients that have contravariant 
    13   It contains examples of lifting definitions with quotients that have contravariant 
    14   type variables or type variables which are covariant and contravariant in the same time. *}
    14   type variables or type variables which are covariant and contravariant in the same time.\<close>
    15 
    15 
    16 subsection {* Contravariant type variables *}
    16 subsection \<open>Contravariant type variables\<close>
    17 
    17 
    18 text {* 'a is a contravariant type variable and we are able to map over this variable
    18 text \<open>'a is a contravariant type variable and we are able to map over this variable
    19   in the following four definitions. This example is based on HOL/Fun.thy. *}
    19   in the following four definitions. This example is based on HOL/Fun.thy.\<close>
    20 
    20 
    21 quotient_type
    21 quotient_type
    22 ('a, 'b) fun' (infixr "\<rightarrow>" 55) = "'a \<Rightarrow> 'b" / "op =" 
    22 ('a, 'b) fun' (infixr "\<rightarrow>" 55) = "'a \<Rightarrow> 'b" / "op =" 
    23   by (simp add: identity_equivp)
    23   by (simp add: identity_equivp)
    24 
    24 
    34 quotient_definition "inj_on' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> bool" is inj_on done
    34 quotient_definition "inj_on' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> bool" is inj_on done
    35 
    35 
    36 quotient_definition "bij_betw' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> 'b set \<rightarrow> bool" is bij_betw done
    36 quotient_definition "bij_betw' :: ('a \<rightarrow> 'b) \<rightarrow> 'a set \<rightarrow> 'b set \<rightarrow> bool" is bij_betw done
    37 
    37 
    38 
    38 
    39 subsection {* Co/Contravariant type variables *} 
    39 subsection \<open>Co/Contravariant type variables\<close> 
    40 
    40 
    41 text {* 'a is a covariant and contravariant type variable in the same time.
    41 text \<open>'a is a covariant and contravariant type variable in the same time.
    42   The following example is a bit artificial. We haven't had a natural one yet. *}
    42   The following example is a bit artificial. We haven't had a natural one yet.\<close>
    43 
    43 
    44 quotient_type 'a endofun = "'a \<Rightarrow> 'a" / "op =" by (simp add: identity_equivp)
    44 quotient_type 'a endofun = "'a \<Rightarrow> 'a" / "op =" by (simp add: identity_equivp)
    45 
    45 
    46 definition map_endofun' :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> ('a => 'a) \<Rightarrow> ('b => 'b)"
    46 definition map_endofun' :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> ('a => 'a) \<Rightarrow> ('b => 'b)"
    47   where "map_endofun' f g e = map_fun g f e"
    47   where "map_endofun' f g e = map_fun g f e"
    48 
    48 
    49 quotient_definition "map_endofun :: ('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a endofun \<Rightarrow> 'b endofun" is
    49 quotient_definition "map_endofun :: ('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a endofun \<Rightarrow> 'b endofun" is
    50   map_endofun' done
    50   map_endofun' done
    51 
    51 
    52 text {* Registration of the map function for 'a endofun. *}
    52 text \<open>Registration of the map function for 'a endofun.\<close>
    53 
    53 
    54 functor map_endofun : map_endofun
    54 functor map_endofun : map_endofun
    55 proof -
    55 proof -
    56   have "\<forall> x. abs_endofun (rep_endofun x) = x" using Quotient3_endofun by (auto simp: Quotient3_def)
    56   have "\<forall> x. abs_endofun (rep_endofun x) = x" using Quotient3_endofun by (auto simp: Quotient3_def)
    57   then show "map_endofun id id = id" 
    57   then show "map_endofun id id = id" 
    61     Quotient3_rep_abs[of "(op =)" abs_endofun rep_endofun] by blast
    61     Quotient3_rep_abs[of "(op =)" abs_endofun rep_endofun] by blast
    62   show "\<And>f g h i. map_endofun f g \<circ> map_endofun h i = map_endofun (f \<circ> h) (i \<circ> g)"
    62   show "\<And>f g h i. map_endofun f g \<circ> map_endofun h i = map_endofun (f \<circ> h) (i \<circ> g)"
    63     by (auto simp: map_endofun_def map_endofun'_def map_fun_def fun_eq_iff) (simp add: a o_assoc) 
    63     by (auto simp: map_endofun_def map_endofun'_def map_fun_def fun_eq_iff) (simp add: a o_assoc) 
    64 qed
    64 qed
    65 
    65 
    66 text {* Relator for 'a endofun. *}
    66 text \<open>Relator for 'a endofun.\<close>
    67 
    67 
    68 definition
    68 definition
    69   rel_endofun' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> bool" 
    69   rel_endofun' :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('b \<Rightarrow> 'b) \<Rightarrow> bool" 
    70 where
    70 where
    71   "rel_endofun' R = (\<lambda>f g. (R ===> R) f g)"
    71   "rel_endofun' R = (\<lambda>f g. (R ===> R) f g)"
   108 term  endofun_id_id
   108 term  endofun_id_id
   109 thm  endofun_id_id_def
   109 thm  endofun_id_id_def
   110 
   110 
   111 quotient_type 'a endofun' = "'a endofun" / "op =" by (simp add: identity_equivp)
   111 quotient_type 'a endofun' = "'a endofun" / "op =" by (simp add: identity_equivp)
   112 
   112 
   113 text {* We have to map "'a endofun" to "('a endofun') endofun", i.e., mapping (lifting)
   113 text \<open>We have to map "'a endofun" to "('a endofun') endofun", i.e., mapping (lifting)
   114   over a type variable which is a covariant and contravariant type variable. *}
   114   over a type variable which is a covariant and contravariant type variable.\<close>
   115 
   115 
   116 quotient_definition "endofun'_id_id :: ('a endofun') endofun'" is endofun_id_id done
   116 quotient_definition "endofun'_id_id :: ('a endofun') endofun'" is endofun_id_id done
   117 
   117 
   118 term  endofun'_id_id
   118 term  endofun'_id_id
   119 thm  endofun'_id_id_def
   119 thm  endofun'_id_id_def