src/HOL/Probability/ex/Dining_Cryptographers.thy
changeset 38656 d5d342611edb
parent 36624 25153c08655e
child 39099 5c714fd55b1e
equal deleted inserted replaced
38655:5001ed24e129 38656:d5d342611edb
     1 theory Dining_Cryptographers
     1 theory Dining_Cryptographers
     2 imports Information
     2 imports Information
     3 begin
     3 begin
     4 
     4 
     5 lemma finite_information_spaceI:
     5 lemma finite_information_spaceI:
     6   "\<lbrakk> finite_measure_space M ; measure M (space M) = 1 ; 1 < b \<rbrakk> \<Longrightarrow> finite_information_space M b"
     6   "\<lbrakk> finite_measure_space M \<mu> ; \<mu> (space M) = 1 ; 1 < b \<rbrakk> \<Longrightarrow> finite_information_space M \<mu> b"
     7   unfolding finite_information_space_def finite_measure_space_def finite_measure_space_axioms_def
     7   unfolding finite_information_space_def finite_measure_space_def finite_measure_space_axioms_def
     8     finite_prob_space_def prob_space_def prob_space_axioms_def finite_information_space_axioms_def
     8     finite_prob_space_def prob_space_def prob_space_axioms_def finite_information_space_axioms_def
     9   by auto
     9   by auto
    10 
    10 
    11 locale finite_space =
    11 locale finite_space =
    12   fixes S :: "'a set"
    12   fixes S :: "'a set"
    13   assumes finite[simp]: "finite S"
    13   assumes finite[simp]: "finite S"
    14   and not_empty[simp]: "S \<noteq> {}"
    14   and not_empty[simp]: "S \<noteq> {}"
    15 
    15 
    16 definition (in finite_space) "M = \<lparr> space = S, sets = Pow S, measure = (\<lambda>s. real (card s) / real (card S)) \<rparr>"
    16 definition (in finite_space) "M = \<lparr> space = S, sets = Pow S \<rparr>"
       
    17 definition (in finite_space) \<mu>_def[simp]: "\<mu> A = (of_nat (card A) / of_nat (card S) :: pinfreal)"
    17 
    18 
    18 lemma (in finite_space)
    19 lemma (in finite_space)
    19   shows space_M[simp]: "space M = S"
    20   shows space_M[simp]: "space M = S"
    20   and sets_M[simp]: "sets M = Pow S"
    21   and sets_M[simp]: "sets M = Pow S"
    21   and measure_M[simp]: "measure M s = real (card s) / real (card S)"
       
    22   by (simp_all add: M_def)
    22   by (simp_all add: M_def)
    23 
    23 
    24 sublocale finite_space \<subseteq> finite_information_space M 2
    24 sublocale finite_space \<subseteq> finite_information_space M \<mu> 2
    25 proof (rule finite_information_spaceI)
    25 proof (rule finite_information_spaceI)
    26   let ?measure = "\<lambda>s::'a set. real (card s) / real (card S)"
    26   let ?measure = "\<lambda>s::'a set. real (card s) / real (card S)"
    27 
    27 
    28   show "finite_measure_space M"
    28   show "finite_measure_space M \<mu>"
    29   proof (rule finite_Pow_additivity_sufficient, simp_all)
    29   proof (rule finite_Pow_additivity_sufficient, simp_all)
    30     show "positive M (measure M)"
    30     show "positive \<mu>" by (simp add: positive_def)
    31       by (simp add: positive_def le_divide_eq)
    31 
    32 
    32     show "additive M \<mu>"
    33     show "additive M (measure M)"
    33       by (simp add: additive_def inverse_eq_divide field_simps Real_real
    34     proof (simp add: additive_def, safe)
    34                     divide_le_0_iff zero_le_divide_iff
    35       fix x y assume "x \<subseteq> S" and "y \<subseteq> S" and "x \<inter> y = {}"
    35                     card_Un_disjoint finite_subset[OF _ finite])
    36       with this(1,2)[THEN finite_subset]
       
    37       have "card (x \<union> y) = card x + card y"
       
    38         by (simp add: card_Un_disjoint)
       
    39       thus "?measure (x \<union> y) = ?measure x + ?measure y"
       
    40         by (cases "card S = 0") (simp_all add: field_simps)
       
    41     qed
       
    42   qed
    36   qed
    43 qed simp_all
    37 qed simp_all
    44 
    38 
    45 lemma set_of_list_extend:
    39 lemma set_of_list_extend:
    46   "{xs. length xs = Suc n \<and> (\<forall>x\<in>set xs. x \<in> A)} =
    40   "{xs. length xs = Suc n \<and> (\<forall>x\<in>set xs. x \<in> A)} =
   506   let ?dIP = "distribution (\<lambda>x. (inversion x, payer x))"
   500   let ?dIP = "distribution (\<lambda>x. (inversion x, payer x))"
   507   let ?dP = "distribution payer"
   501   let ?dP = "distribution payer"
   508   let ?dI = "distribution inversion"
   502   let ?dI = "distribution inversion"
   509 
   503 
   510   { have "\<H>(inversion|payer) =
   504   { have "\<H>(inversion|payer) =
   511         - (\<Sum>x\<in>inversion`dc_crypto. (\<Sum>z\<in>Some ` {0..<n}. ?dIP {(x, z)} * log 2 (?dIP {(x, z)} / ?dP {z})))"
   505         - (\<Sum>x\<in>inversion`dc_crypto. (\<Sum>z\<in>Some ` {0..<n}. real (?dIP {(x, z)}) * log 2 (real (?dIP {(x, z)}) / real (?dP {z}))))"
   512       unfolding conditional_entropy_eq
   506       unfolding conditional_entropy_eq
   513       by (simp add: image_payer_dc_crypto setsum_Sigma)
   507       by (simp add: image_payer_dc_crypto setsum_Sigma)
   514     also have "... =
   508     also have "... =
   515         - (\<Sum>x\<in>inversion`dc_crypto. (\<Sum>z\<in>Some ` {0..<n}. 2 / (real n * 2^n) * (1 - real n)))"
   509         - (\<Sum>x\<in>inversion`dc_crypto. (\<Sum>z\<in>Some ` {0..<n}. 2 / (real n * 2^n) * (1 - real n)))"
   516       unfolding neg_equal_iff_equal
   510       unfolding neg_equal_iff_equal
   520           {dc \<in> dc_crypto. payer dc = Some (the z) \<and> inversion dc = x}"
   514           {dc \<in> dc_crypto. payer dc = Some (the z) \<and> inversion dc = x}"
   521         by (auto simp add: payer_def)
   515         by (auto simp add: payer_def)
   522       moreover from x z obtain i where "z = Some i" and "i < n" by auto
   516       moreover from x z obtain i where "z = Some i" and "i < n" by auto
   523       moreover from x have "length x = n" by (auto simp: inversion_def_raw dc_crypto)
   517       moreover from x have "length x = n" by (auto simp: inversion_def_raw dc_crypto)
   524       ultimately
   518       ultimately
   525       have "?dIP {(x, z)} = 2 / (real n * 2^n)" using x
   519       have "real (?dIP {(x, z)}) = 2 / (real n * 2^n)" using x
   526         by (simp add: distribution_def card_dc_crypto card_payer_and_inversion)
   520         by (simp add: distribution_def card_dc_crypto card_payer_and_inversion
       
   521                       inverse_eq_divide mult_le_0_iff zero_le_mult_iff power_le_zero_eq)
   527       moreover
   522       moreover
   528       from z have "payer -` {z} \<inter> dc_crypto = {z} \<times> {xs. length xs = n}"
   523       from z have "payer -` {z} \<inter> dc_crypto = {z} \<times> {xs. length xs = n}"
   529         by (auto simp: dc_crypto payer_def)
   524         by (auto simp: dc_crypto payer_def)
   530       hence "card (payer -` {z} \<inter> dc_crypto) = 2^n"
   525       hence "card (payer -` {z} \<inter> dc_crypto) = 2^n"
   531         using card_list_length[where A="UNIV::bool set"]
   526         using card_list_length[where A="UNIV::bool set"]
   532         by (simp add: card_cartesian_product_singleton)
   527         by (simp add: card_cartesian_product_singleton)
   533       hence "?dP {z} = 1 / real n"
   528       hence "real (?dP {z}) = 1 / real n"
   534         by (simp add: distribution_def card_dc_crypto)
   529         by (simp add: distribution_def card_dc_crypto field_simps inverse_eq_divide
       
   530                       mult_le_0_iff zero_le_mult_iff power_le_zero_eq)
   535       ultimately
   531       ultimately
   536       show "?dIP {(x,z)} * log 2 (?dIP {(x,z)} / ?dP {z}) =
   532       show "real (?dIP {(x,z)}) * log 2 (real (?dIP {(x,z)}) / real (?dP {z})) =
   537        2 / (real n * 2^n) * (1 - real n)"
   533        2 / (real n * 2^n) * (1 - real n)"
   538         by (simp add: field_simps log_divide log_nat_power[of 2])
   534         by (simp add: field_simps log_divide log_nat_power[of 2])
   539     qed
   535     qed
   540     also have "... = real n - 1"
   536     also have "... = real n - 1"
   541       using n finite_space
   537       using n finite_space
   542       by (simp add: card_image_inversion card_image[OF inj_Some] field_simps real_eq_of_nat[symmetric])
   538       by (simp add: card_image_inversion card_image[OF inj_Some] field_simps real_eq_of_nat[symmetric])
   543     finally have "\<H>(inversion|payer) = real n - 1" . }
   539     finally have "\<H>(inversion|payer) = real n - 1" . }
   544   moreover
   540   moreover
   545   { have "\<H>(inversion) = - (\<Sum>x \<in> inversion`dc_crypto. ?dI {x} * log 2 (?dI {x}))"
   541   { have "\<H>(inversion) = - (\<Sum>x \<in> inversion`dc_crypto. real (?dI {x}) * log 2 (real (?dI {x})))"
   546       unfolding entropy_eq by simp
   542       unfolding entropy_eq by simp
   547     also have "... = - (\<Sum>x \<in> inversion`dc_crypto. 2 * (1 - real n) / 2^n)"
   543     also have "... = - (\<Sum>x \<in> inversion`dc_crypto. 2 * (1 - real n) / 2^n)"
   548       unfolding neg_equal_iff_equal
   544       unfolding neg_equal_iff_equal
   549     proof (rule setsum_cong[OF refl])
   545     proof (rule setsum_cong[OF refl])
   550       fix x assume x_inv: "x \<in> inversion ` dc_crypto"
   546       fix x assume x_inv: "x \<in> inversion ` dc_crypto"
   551       hence "length x = n" by (auto simp: inversion_def_raw dc_crypto)
   547       hence "length x = n" by (auto simp: inversion_def_raw dc_crypto)
   552       moreover have "inversion -` {x} \<inter> dc_crypto = {dc \<in> dc_crypto. inversion dc = x}" by auto
   548       moreover have "inversion -` {x} \<inter> dc_crypto = {dc \<in> dc_crypto. inversion dc = x}" by auto
   553       ultimately have "?dI {x} = 2 / 2^n" using `0 < n`
   549       ultimately have "?dI {x} = 2 / 2^n" using `0 < n`
   554         by (simp add: distribution_def card_inversion[OF x_inv] card_dc_crypto)
   550         by (simp add: distribution_def card_inversion[OF x_inv] card_dc_crypto
   555       thus "?dI {x} * log 2 (?dI {x}) = 2 * (1 - real n) / 2^n"
   551                       mult_le_0_iff zero_le_mult_iff power_le_zero_eq)
   556         by (simp add: log_divide log_nat_power)
   552       thus "real (?dI {x}) * log 2 (real (?dI {x})) = 2 * (1 - real n) / 2^n"
       
   553         by (simp add: log_divide log_nat_power power_le_zero_eq inverse_eq_divide)
   557     qed
   554     qed
   558     also have "... = real n - 1"
   555     also have "... = real n - 1"
   559       by (simp add: card_image_inversion real_of_nat_def[symmetric] field_simps)
   556       by (simp add: card_image_inversion real_of_nat_def[symmetric] field_simps)
   560     finally have "\<H>(inversion) = real n - 1" .
   557     finally have "\<H>(inversion) = real n - 1" .
   561   }
   558   }