Theory Cardinality

theory Cardinality
imports Phantom_Type
(*  Title:      HOL/Library/Cardinality.thy
Author: Brian Huffman, Andreas Lochbihler

header {* Cardinality of types *}

theory Cardinality
imports Phantom_Type

subsection {* Preliminary lemmas *}
(* These should be moved elsewhere *)

lemma (in type_definition) univ:
"UNIV = Abs ` A"
show "Abs ` A ⊆ UNIV" by (rule subset_UNIV)
show "UNIV ⊆ Abs ` A"
fix x :: 'b
have "x = Abs (Rep x)" by (rule Rep_inverse [symmetric])
moreover have "Rep x ∈ A" by (rule Rep)
ultimately show "x ∈ Abs ` A" by (rule image_eqI)

lemma (in type_definition) card: "card (UNIV :: 'b set) = card A"
by (simp add: univ card_image inj_on_def Abs_inject)

lemma finite_range_Some: "finite (range (Some :: 'a => 'a option)) = finite (UNIV :: 'a set)"
by(auto dest: finite_imageD intro: inj_Some)

lemma infinite_literal: "¬ finite (UNIV :: String.literal set)"
proof -
have "inj STR" by(auto intro: injI)
thus ?thesis
by(auto simp add: type_definition.univ[OF type_definition_literal] infinite_UNIV_listI dest: finite_imageD)

subsection {* Cardinalities of types *}

syntax "_type_card" :: "type => nat" ("(1CARD/(1'(_')))")

translations "CARD('t)" => "CONST card (CONST UNIV :: 't set)"

print_translation {*
fun card_univ_tr' ctxt [Const (@{const_syntax UNIV}, Type (_, [T]))] =
Syntax.const @{syntax_const "_type_card"} $ Syntax_Phases.term_of_typ ctxt T
in [(@{const_syntax card}, card_univ_tr')] end

lemma card_prod [simp]: "CARD('a × 'b) = CARD('a) * CARD('b)"
unfolding UNIV_Times_UNIV [symmetric] by (simp only: card_cartesian_product)

lemma card_UNIV_sum: "CARD('a + 'b) = (if CARD('a) ≠ 0 ∧ CARD('b) ≠ 0 then CARD('a) + CARD('b) else 0)"
unfolding UNIV_Plus_UNIV[symmetric]
by(auto simp add: card_eq_0_iff card_Plus simp del: UNIV_Plus_UNIV)

lemma card_sum [simp]: "CARD('a + 'b) = CARD('a::finite) + CARD('b::finite)"
by(simp add: card_UNIV_sum)

lemma card_UNIV_option: "CARD('a option) = (if CARD('a) = 0 then 0 else CARD('a) + 1)"
proof -
have "(None :: 'a option) ∉ range Some" by clarsimp
thus ?thesis
by (simp add: UNIV_option_conv card_eq_0_iff finite_range_Some card_image)

lemma card_option [simp]: "CARD('a option) = Suc CARD('a::finite)"
by(simp add: card_UNIV_option)

lemma card_UNIV_set: "CARD('a set) = (if CARD('a) = 0 then 0 else 2 ^ CARD('a))"
by(simp add: Pow_UNIV[symmetric] card_eq_0_iff card_Pow del: Pow_UNIV)

lemma card_set [simp]: "CARD('a set) = 2 ^ CARD('a::finite)"
by(simp add: card_UNIV_set)

lemma card_nat [simp]: "CARD(nat) = 0"
by (simp add: card_eq_0_iff)

lemma card_fun: "CARD('a => 'b) = (if CARD('a) ≠ 0 ∧ CARD('b) ≠ 0 ∨ CARD('b) = 1 then CARD('b) ^ CARD('a) else 0)"
proof -
{ assume "0 < CARD('a)" and "0 < CARD('b)"
hence fina: "finite (UNIV :: 'a set)" and finb: "finite (UNIV :: 'b set)"
by(simp_all only: card_ge_0_finite)
from finite_distinct_list[OF finb] obtain bs
where bs: "set bs = (UNIV :: 'b set)" and distb: "distinct bs" by blast
from finite_distinct_list[OF fina] obtain as
where as: "set as = (UNIV :: 'a set)" and dista: "distinct as" by blast
have cb: "CARD('b) = length bs"
unfolding bs[symmetric] distinct_card[OF distb] ..
have ca: "CARD('a) = length as"
unfolding as[symmetric] distinct_card[OF dista] ..
let ?xs = "map (λys. the o map_of (zip as ys)) (List.n_lists (length as) bs)"
have "UNIV = set ?xs"
proof(rule UNIV_eq_I)
fix f :: "'a => 'b"
from as have "f = the o map_of (zip as (map f as))"
by(auto simp add: map_of_zip_map)
thus "f ∈ set ?xs" using bs by(auto simp add: set_n_lists)
moreover have "distinct ?xs" unfolding distinct_map
proof(intro conjI distinct_n_lists distb inj_onI)
fix xs ys :: "'b list"
assume xs: "xs ∈ set (List.n_lists (length as) bs)"
and ys: "ys ∈ set (List.n_lists (length as) bs)"
and eq: "the o map_of (zip as xs) = the o map_of (zip as ys)"
from xs ys have [simp]: "length xs = length as" "length ys = length as"
by(simp_all add: length_n_lists_elem)
have "map_of (zip as xs) = map_of (zip as ys)"
fix x
from as bs have "∃y. map_of (zip as xs) x = Some y" "∃y. map_of (zip as ys) x = Some y"
by(simp_all add: map_of_zip_is_Some[symmetric])
with eq show "map_of (zip as xs) x = map_of (zip as ys) x"
by(auto dest: fun_cong[where x=x])
with dista show "xs = ys" by(simp add: map_of_zip_inject)
hence "card (set ?xs) = length ?xs" by(simp only: distinct_card)
moreover have "length ?xs = length bs ^ length as" by(simp add: length_n_lists)
ultimately have "CARD('a => 'b) = CARD('b) ^ CARD('a)" using cb ca by simp }
moreover {
assume cb: "CARD('b) = 1"
then obtain b where b: "UNIV = {b :: 'b}" by(auto simp add: card_Suc_eq)
have eq: "UNIV = {λx :: 'a. b ::'b}"
proof(rule UNIV_eq_I)
fix x :: "'a => 'b"
{ fix y
have "x y ∈ UNIV" ..
hence "x y = b" unfolding b by simp }
thus "x ∈ {λx. b}" by(auto)
have "CARD('a => 'b) = 1" unfolding eq by simp }
ultimately show ?thesis
by(auto simp del: One_nat_def)(auto simp add: card_eq_0_iff dest: finite_fun_UNIVD2 finite_fun_UNIVD1)

corollary finite_UNIV_fun:
"finite (UNIV :: ('a => 'b) set) <->
finite (UNIV :: 'a set) ∧ finite (UNIV :: 'b set) ∨ CARD('b) = 1"

(is "?lhs <-> ?rhs")
proof -
have "?lhs <-> CARD('a => 'b) > 0" by(simp add: card_gt_0_iff)
also have "… <-> CARD('a) > 0 ∧ CARD('b) > 0 ∨ CARD('b) = 1"
by(simp add: card_fun)
also have "… = ?rhs" by(simp add: card_gt_0_iff)
finally show ?thesis .

lemma card_nibble: "CARD(nibble) = 16"
unfolding UNIV_nibble by simp

lemma card_UNIV_char: "CARD(char) = 256"
proof -
have "inj (λ(x, y). Char x y)" by(auto intro: injI)
thus ?thesis unfolding UNIV_char by(simp add: card_image card_nibble)

lemma card_literal: "CARD(String.literal) = 0"
by(simp add: card_eq_0_iff infinite_literal)

subsection {* Classes with at least 1 and 2 *}

text {* Class finite already captures "at least 1" *}

lemma zero_less_card_finite [simp]: "0 < CARD('a::finite)"
unfolding neq0_conv [symmetric] by simp

lemma one_le_card_finite [simp]: "Suc 0 ≤ CARD('a::finite)"
by (simp add: less_Suc_eq_le [symmetric])

text {* Class for cardinality "at least 2" *}

class card2 = finite +
assumes two_le_card: "2 ≤ CARD('a)"

lemma one_less_card: "Suc 0 < CARD('a::card2)"
using two_le_card [where 'a='a] by simp

lemma one_less_int_card: "1 < int CARD('a::card2)"
using one_less_card [where 'a='a] by simp

subsection {* A type class for deciding finiteness of types *}

type_synonym 'a finite_UNIV = "('a, bool) phantom"

class finite_UNIV =
fixes finite_UNIV :: "('a, bool) phantom"
assumes finite_UNIV: "finite_UNIV = Phantom('a) (finite (UNIV :: 'a set))"

lemma finite_UNIV_code [code_unfold]:
"finite (UNIV :: 'a :: finite_UNIV set)
<-> of_phantom (finite_UNIV :: 'a finite_UNIV)"

by(simp add: finite_UNIV)

subsection {* A type class for computing the cardinality of types *}

definition is_list_UNIV :: "'a list => bool"
where "is_list_UNIV xs = (let c = CARD('a) in if c = 0 then False else size (remdups xs) = c)"

lemma is_list_UNIV_iff: "is_list_UNIV xs <-> set xs = UNIV"
by(auto simp add: is_list_UNIV_def Let_def card_eq_0_iff List.card_set[symmetric]
dest: subst[where P="finite", OF _ finite_set] card_eq_UNIV_imp_eq_UNIV)

type_synonym 'a card_UNIV = "('a, nat) phantom"

class card_UNIV = finite_UNIV +
fixes card_UNIV :: "'a card_UNIV"
assumes card_UNIV: "card_UNIV = Phantom('a) CARD('a)"

subsection {* Instantiations for @{text "card_UNIV"} *}

instantiation nat :: card_UNIV begin
definition "finite_UNIV = Phantom(nat) False"
definition "card_UNIV = Phantom(nat) 0"
instance by intro_classes (simp_all add: finite_UNIV_nat_def card_UNIV_nat_def)

instantiation int :: card_UNIV begin
definition "finite_UNIV = Phantom(int) False"
definition "card_UNIV = Phantom(int) 0"
instance by intro_classes (simp_all add: card_UNIV_int_def finite_UNIV_int_def infinite_UNIV_int)

instantiation natural :: card_UNIV begin
definition "finite_UNIV = Phantom(natural) False"
definition "card_UNIV = Phantom(natural) 0"
instance proof
qed (auto simp add: finite_UNIV_natural_def card_UNIV_natural_def card_eq_0_iff
type_definition.univ [OF type_definition_natural] natural_eq_iff
dest!: finite_imageD intro: inj_onI)

instantiation integer :: card_UNIV begin
definition "finite_UNIV = Phantom(integer) False"
definition "card_UNIV = Phantom(integer) 0"
instance proof
qed (auto simp add: finite_UNIV_integer_def card_UNIV_integer_def card_eq_0_iff
type_definition.univ [OF type_definition_integer] infinite_UNIV_int
dest!: finite_imageD intro: inj_onI)

instantiation list :: (type) card_UNIV begin
definition "finite_UNIV = Phantom('a list) False"
definition "card_UNIV = Phantom('a list) 0"
instance by intro_classes (simp_all add: card_UNIV_list_def finite_UNIV_list_def infinite_UNIV_listI)

instantiation unit :: card_UNIV begin
definition "finite_UNIV = Phantom(unit) True"
definition "card_UNIV = Phantom(unit) 1"
instance by intro_classes (simp_all add: card_UNIV_unit_def finite_UNIV_unit_def)

instantiation bool :: card_UNIV begin
definition "finite_UNIV = Phantom(bool) True"
definition "card_UNIV = Phantom(bool) 2"
instance by(intro_classes)(simp_all add: card_UNIV_bool_def finite_UNIV_bool_def)

instantiation nibble :: card_UNIV begin
definition "finite_UNIV = Phantom(nibble) True"
definition "card_UNIV = Phantom(nibble) 16"
instance by(intro_classes)(simp_all add: card_UNIV_nibble_def card_nibble finite_UNIV_nibble_def)

instantiation char :: card_UNIV begin
definition "finite_UNIV = Phantom(char) True"
definition "card_UNIV = Phantom(char) 256"
instance by intro_classes (simp_all add: card_UNIV_char_def card_UNIV_char finite_UNIV_char_def)

instantiation prod :: (finite_UNIV, finite_UNIV) finite_UNIV begin
definition "finite_UNIV = Phantom('a × 'b)
(of_phantom (finite_UNIV :: 'a finite_UNIV) ∧ of_phantom (finite_UNIV :: 'b finite_UNIV))"

instance by intro_classes (simp add: finite_UNIV_prod_def finite_UNIV finite_prod)

instantiation prod :: (card_UNIV, card_UNIV) card_UNIV begin
definition "card_UNIV = Phantom('a × 'b)
(of_phantom (card_UNIV :: 'a card_UNIV) * of_phantom (card_UNIV :: 'b card_UNIV))"

instance by intro_classes (simp add: card_UNIV_prod_def card_UNIV)

instantiation sum :: (finite_UNIV, finite_UNIV) finite_UNIV begin
definition "finite_UNIV = Phantom('a + 'b)
(of_phantom (finite_UNIV :: 'a finite_UNIV) ∧ of_phantom (finite_UNIV :: 'b finite_UNIV))"

by intro_classes (simp add: UNIV_Plus_UNIV[symmetric] finite_UNIV_sum_def finite_UNIV del: UNIV_Plus_UNIV)

instantiation sum :: (card_UNIV, card_UNIV) card_UNIV begin
definition "card_UNIV = Phantom('a + 'b)
(let ca = of_phantom (card_UNIV :: 'a card_UNIV);
cb = of_phantom (card_UNIV :: 'b card_UNIV)
in if ca ≠ 0 ∧ cb ≠ 0 then ca + cb else 0)"

instance by intro_classes (auto simp add: card_UNIV_sum_def card_UNIV card_UNIV_sum)

instantiation "fun" :: (finite_UNIV, card_UNIV) finite_UNIV begin
definition "finite_UNIV = Phantom('a => 'b)
(let cb = of_phantom (card_UNIV :: 'b card_UNIV)
in cb = 1 ∨ of_phantom (finite_UNIV :: 'a finite_UNIV) ∧ cb ≠ 0)"

by intro_classes (auto simp add: finite_UNIV_fun_def Let_def card_UNIV finite_UNIV finite_UNIV_fun card_gt_0_iff)

instantiation "fun" :: (card_UNIV, card_UNIV) card_UNIV begin
definition "card_UNIV = Phantom('a => 'b)
(let ca = of_phantom (card_UNIV :: 'a card_UNIV);
cb = of_phantom (card_UNIV :: 'b card_UNIV)
in if ca ≠ 0 ∧ cb ≠ 0 ∨ cb = 1 then cb ^ ca else 0)"

instance by intro_classes (simp add: card_UNIV_fun_def card_UNIV Let_def card_fun)

instantiation option :: (finite_UNIV) finite_UNIV begin
definition "finite_UNIV = Phantom('a option) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
instance by intro_classes (simp add: finite_UNIV_option_def finite_UNIV)

instantiation option :: (card_UNIV) card_UNIV begin
definition "card_UNIV = Phantom('a option)
(let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c ≠ 0 then Suc c else 0)"

instance by intro_classes (simp add: card_UNIV_option_def card_UNIV card_UNIV_option)

instantiation String.literal :: card_UNIV begin
definition "finite_UNIV = Phantom(String.literal) False"
definition "card_UNIV = Phantom(String.literal) 0"
by intro_classes (simp_all add: card_UNIV_literal_def finite_UNIV_literal_def infinite_literal card_literal)

instantiation set :: (finite_UNIV) finite_UNIV begin
definition "finite_UNIV = Phantom('a set) (of_phantom (finite_UNIV :: 'a finite_UNIV))"
instance by intro_classes (simp add: finite_UNIV_set_def finite_UNIV Finite_Set.finite_set)

instantiation set :: (card_UNIV) card_UNIV begin
definition "card_UNIV = Phantom('a set)
(let c = of_phantom (card_UNIV :: 'a card_UNIV) in if c = 0 then 0 else 2 ^ c)"

instance by intro_classes (simp add: card_UNIV_set_def card_UNIV_set card_UNIV)

lemma UNIV_finite_1: "UNIV = set [finite_1.a1]"
by(auto intro: finite_1.exhaust)

lemma UNIV_finite_2: "UNIV = set [finite_2.a1, finite_2.a2]"
by(auto intro: finite_2.exhaust)

lemma UNIV_finite_3: "UNIV = set [finite_3.a1, finite_3.a2, finite_3.a3]"
by(auto intro: finite_3.exhaust)

lemma UNIV_finite_4: "UNIV = set [finite_4.a1, finite_4.a2, finite_4.a3, finite_4.a4]"
by(auto intro: finite_4.exhaust)

lemma UNIV_finite_5:
"UNIV = set [finite_5.a1, finite_5.a2, finite_5.a3, finite_5.a4, finite_5.a5]"
by(auto intro: finite_5.exhaust)

instantiation Enum.finite_1 :: card_UNIV begin
definition "finite_UNIV = Phantom(Enum.finite_1) True"
definition "card_UNIV = Phantom(Enum.finite_1) 1"
by intro_classes (simp_all add: UNIV_finite_1 card_UNIV_finite_1_def finite_UNIV_finite_1_def)

instantiation Enum.finite_2 :: card_UNIV begin
definition "finite_UNIV = Phantom(Enum.finite_2) True"
definition "card_UNIV = Phantom(Enum.finite_2) 2"
by intro_classes (simp_all add: UNIV_finite_2 card_UNIV_finite_2_def finite_UNIV_finite_2_def)

instantiation Enum.finite_3 :: card_UNIV begin
definition "finite_UNIV = Phantom(Enum.finite_3) True"
definition "card_UNIV = Phantom(Enum.finite_3) 3"
by intro_classes (simp_all add: UNIV_finite_3 card_UNIV_finite_3_def finite_UNIV_finite_3_def)

instantiation Enum.finite_4 :: card_UNIV begin
definition "finite_UNIV = Phantom(Enum.finite_4) True"
definition "card_UNIV = Phantom(Enum.finite_4) 4"
by intro_classes (simp_all add: UNIV_finite_4 card_UNIV_finite_4_def finite_UNIV_finite_4_def)

instantiation Enum.finite_5 :: card_UNIV begin
definition "finite_UNIV = Phantom(Enum.finite_5) True"
definition "card_UNIV = Phantom(Enum.finite_5) 5"
by intro_classes (simp_all add: UNIV_finite_5 card_UNIV_finite_5_def finite_UNIV_finite_5_def)

subsection {* Code setup for sets *}

text {*
Implement @{term "CARD('a)"} via @{term card_UNIV} and provide
implementations for @{term "finite"}, @{term "card"}, @{term "op ⊆"},
and @{term "op ="}if the calling context already provides @{class finite_UNIV}
and @{class card_UNIV} instances. If we implemented the latter
always via @{term card_UNIV}, we would require instances of essentially all
element types, i.e., a lot of instantiation proofs and -- at run time --
possibly slow dictionary constructions.

definition card_UNIV' :: "'a card_UNIV"
where [code del]: "card_UNIV' = Phantom('a) CARD('a)"

lemma CARD_code [code_unfold]:
"CARD('a) = of_phantom (card_UNIV' :: 'a card_UNIV)"
by(simp add: card_UNIV'_def)

lemma card_UNIV'_code [code]:
"card_UNIV' = card_UNIV"
by(simp add: card_UNIV card_UNIV'_def)

hide_const (open) card_UNIV'

lemma card_Compl:
"finite A ==> card (- A) = card (UNIV :: 'a set) - card (A :: 'a set)"
by (metis Compl_eq_Diff_UNIV card_Diff_subset top_greatest)

context fixes xs :: "'a :: finite_UNIV list"

definition finite' :: "'a set => bool"
where [simp, code del, code_abbrev]: "finite' = finite"

lemma finite'_code [code]:
"finite' (set xs) <-> True"
"finite' (List.coset xs) <-> of_phantom (finite_UNIV :: 'a finite_UNIV)"
by(simp_all add: card_gt_0_iff finite_UNIV)


context fixes xs :: "'a :: card_UNIV list"

definition card' :: "'a set => nat"
where [simp, code del, code_abbrev]: "card' = card"

lemma card'_code [code]:
"card' (set xs) = length (remdups xs)"
"card' (List.coset xs) = of_phantom (card_UNIV :: 'a card_UNIV) - length (remdups xs)"
by(simp_all add: List.card_set card_Compl card_UNIV)

definition subset' :: "'a set => 'a set => bool"
where [simp, code del, code_abbrev]: "subset' = op ⊆"

lemma subset'_code [code]:
"subset' A (List.coset ys) <-> (∀y ∈ set ys. y ∉ A)"
"subset' (set ys) B <-> (∀y ∈ set ys. y ∈ B)"
"subset' (List.coset xs) (set ys) <-> (let n = CARD('a) in n > 0 ∧ card(set (xs @ ys)) = n)"
by(auto simp add: Let_def card_gt_0_iff dest: card_eq_UNIV_imp_eq_UNIV intro: arg_cong[where f=card])
(metis finite_compl finite_set rev_finite_subset)

definition eq_set :: "'a set => 'a set => bool"
where [simp, code del, code_abbrev]: "eq_set = op ="

lemma eq_set_code [code]:
fixes ys
defines "rhs ≡
let n = CARD('a)
in if n = 0 then False else
let xs' = remdups xs; ys' = remdups ys
in length xs' + length ys' = n ∧ (∀x ∈ set xs'. x ∉ set ys') ∧ (∀y ∈ set ys'. y ∉ set xs')"

shows "eq_set (List.coset xs) (set ys) <-> rhs" (is ?thesis1)
and "eq_set (set ys) (List.coset xs) <-> rhs" (is ?thesis2)
and "eq_set (set xs) (set ys) <-> (∀x ∈ set xs. x ∈ set ys) ∧ (∀y ∈ set ys. y ∈ set xs)" (is ?thesis3)
and "eq_set (List.coset xs) (List.coset ys) <-> (∀x ∈ set xs. x ∈ set ys) ∧ (∀y ∈ set ys. y ∈ set xs)" (is ?thesis4)
proof -
show ?thesis1 (is "?lhs <-> ?rhs")
assume ?lhs thus ?rhs
by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_Un_Int[where A="set xs" and B="- set xs"] card_UNIV Compl_partition card_gt_0_iff dest: sym)(metis finite_compl finite_set)
assume ?rhs
moreover have "[| ∀y∈set xs. y ∉ set ys; ∀x∈set ys. x ∉ set xs |] ==> set xs ∩ set ys = {}" by blast
ultimately show ?lhs
by(auto simp add: rhs_def Let_def List.card_set[symmetric] card_UNIV card_gt_0_iff card_Un_Int[where A="set xs" and B="set ys"] dest: card_eq_UNIV_imp_eq_UNIV split: split_if_asm)
thus ?thesis2 unfolding eq_set_def by blast
show ?thesis3 ?thesis4 unfolding eq_set_def List.coset_def by blast+


text {*
Provide more informative exceptions than Match for non-rewritten cases.
If generated code raises one these exceptions, then a code equation calls
the mentioned operator for an element type that is not an instance of
@{class card_UNIV} and is therefore not implemented via @{term card_UNIV}.
Constrain the element type with sort @{class card_UNIV} to change this.

lemma card_coset_error [code]:
"card (List.coset xs) =
Code.abort (STR ''card (List.coset _) requires type class instance card_UNIV'')
(λ_. card (List.coset xs))"


lemma coset_subseteq_set_code [code]:
"List.coset xs ⊆ set ys <->
(if xs = [] ∧ ys = [] then False
else Code.abort
(STR ''subset_eq (List.coset _) (List.set _) requires type class instance card_UNIV'')
(λ_. List.coset xs ⊆ set ys))"

by simp

notepad begin -- "test code setup"
have "List.coset [True] = set [False] ∧
List.coset [] ⊆ List.set [True, False] ∧
finite (List.coset [True])"

by eval

hide_const (open) card' finite' subset' eq_set