src/HOL/Quotient_Examples/Quotient_Message.thy
author blanchet
Wed Feb 12 08:35:57 2014 +0100 (2014-02-12)
changeset 55415 05f5fdb8d093
parent 47304 a0d97d919f01
child 58305 57752a91eec4
permissions -rw-r--r--
renamed 'nat_{case,rec}' to '{case,rec}_nat'
kaliszyk@36524
     1
(*  Title:      HOL/Quotient_Examples/Quotient_Message.thy
kaliszyk@36524
     2
    Author:     Christian Urban
kaliszyk@36524
     3
kaliszyk@36524
     4
Message datatype, based on an older version by Larry Paulson.
kaliszyk@36524
     5
*)
wenzelm@41467
     6
kaliszyk@36524
     7
theory Quotient_Message
bulwahn@45535
     8
imports Main "~~/src/HOL/Library/Quotient_Syntax"
kaliszyk@35222
     9
begin
kaliszyk@35222
    10
kaliszyk@35222
    11
subsection{*Defining the Free Algebra*}
kaliszyk@35222
    12
kaliszyk@35222
    13
datatype
kaliszyk@35222
    14
  freemsg = NONCE  nat
kaliszyk@35222
    15
        | MPAIR  freemsg freemsg
kaliszyk@36524
    16
        | CRYPT  nat freemsg
kaliszyk@35222
    17
        | DECRYPT  nat freemsg
kaliszyk@35222
    18
kaliszyk@36524
    19
inductive
kaliszyk@35222
    20
  msgrel::"freemsg \<Rightarrow> freemsg \<Rightarrow> bool" (infixl "\<sim>" 50)
kaliszyk@36524
    21
where
kaliszyk@35222
    22
  CD:    "CRYPT K (DECRYPT K X) \<sim> X"
kaliszyk@35222
    23
| DC:    "DECRYPT K (CRYPT K X) \<sim> X"
kaliszyk@35222
    24
| NONCE: "NONCE N \<sim> NONCE N"
kaliszyk@35222
    25
| MPAIR: "\<lbrakk>X \<sim> X'; Y \<sim> Y'\<rbrakk> \<Longrightarrow> MPAIR X Y \<sim> MPAIR X' Y'"
kaliszyk@35222
    26
| CRYPT: "X \<sim> X' \<Longrightarrow> CRYPT K X \<sim> CRYPT K X'"
kaliszyk@35222
    27
| DECRYPT: "X \<sim> X' \<Longrightarrow> DECRYPT K X \<sim> DECRYPT K X'"
kaliszyk@35222
    28
| SYM:   "X \<sim> Y \<Longrightarrow> Y \<sim> X"
kaliszyk@35222
    29
| TRANS: "\<lbrakk>X \<sim> Y; Y \<sim> Z\<rbrakk> \<Longrightarrow> X \<sim> Z"
kaliszyk@35222
    30
kaliszyk@35222
    31
lemmas msgrel.intros[intro]
kaliszyk@35222
    32
kaliszyk@35222
    33
text{*Proving that it is an equivalence relation*}
kaliszyk@35222
    34
kaliszyk@35222
    35
lemma msgrel_refl: "X \<sim> X"
urbanc@37594
    36
by (induct X) (auto intro: msgrel.intros)
kaliszyk@35222
    37
kaliszyk@35222
    38
theorem equiv_msgrel: "equivp msgrel"
kaliszyk@35222
    39
proof (rule equivpI)
haftmann@40823
    40
  show "reflp msgrel" by (rule reflpI) (simp add: msgrel_refl)
haftmann@40823
    41
  show "symp msgrel" by (rule sympI) (blast intro: msgrel.SYM)
haftmann@40823
    42
  show "transp msgrel" by (rule transpI) (blast intro: msgrel.TRANS)
kaliszyk@35222
    43
qed
kaliszyk@35222
    44
kaliszyk@35222
    45
subsection{*Some Functions on the Free Algebra*}
kaliszyk@35222
    46
kaliszyk@35222
    47
subsubsection{*The Set of Nonces*}
kaliszyk@35222
    48
haftmann@40823
    49
primrec
kaliszyk@35222
    50
  freenonces :: "freemsg \<Rightarrow> nat set"
kaliszyk@35222
    51
where
kaliszyk@35222
    52
  "freenonces (NONCE N) = {N}"
kaliszyk@35222
    53
| "freenonces (MPAIR X Y) = freenonces X \<union> freenonces Y"
kaliszyk@35222
    54
| "freenonces (CRYPT K X) = freenonces X"
kaliszyk@35222
    55
| "freenonces (DECRYPT K X) = freenonces X"
kaliszyk@35222
    56
kaliszyk@36524
    57
theorem msgrel_imp_eq_freenonces:
kaliszyk@35222
    58
  assumes a: "U \<sim> V"
kaliszyk@35222
    59
  shows "freenonces U = freenonces V"
kaliszyk@36524
    60
  using a by (induct) (auto)
kaliszyk@35222
    61
kaliszyk@35222
    62
subsubsection{*The Left Projection*}
kaliszyk@35222
    63
kaliszyk@35222
    64
text{*A function to return the left part of the top pair in a message.  It will
kaliszyk@35222
    65
be lifted to the initial algrebra, to serve as an example of that process.*}
haftmann@40823
    66
primrec
kaliszyk@35222
    67
  freeleft :: "freemsg \<Rightarrow> freemsg"
kaliszyk@35222
    68
where
kaliszyk@35222
    69
  "freeleft (NONCE N) = NONCE N"
kaliszyk@35222
    70
| "freeleft (MPAIR X Y) = X"
kaliszyk@35222
    71
| "freeleft (CRYPT K X) = freeleft X"
kaliszyk@35222
    72
| "freeleft (DECRYPT K X) = freeleft X"
kaliszyk@35222
    73
kaliszyk@35222
    74
text{*This theorem lets us prove that the left function respects the
kaliszyk@35222
    75
equivalence relation.  It also helps us prove that MPair
kaliszyk@35222
    76
  (the abstract constructor) is injective*}
kaliszyk@35222
    77
lemma msgrel_imp_eqv_freeleft_aux:
kaliszyk@35222
    78
  shows "freeleft U \<sim> freeleft U"
haftmann@40823
    79
  by (fact msgrel_refl)
kaliszyk@35222
    80
kaliszyk@35222
    81
theorem msgrel_imp_eqv_freeleft:
kaliszyk@36524
    82
  assumes a: "U \<sim> V"
kaliszyk@35222
    83
  shows "freeleft U \<sim> freeleft V"
kaliszyk@35222
    84
  using a
kaliszyk@35222
    85
  by (induct) (auto intro: msgrel_imp_eqv_freeleft_aux)
kaliszyk@35222
    86
kaliszyk@35222
    87
subsubsection{*The Right Projection*}
kaliszyk@35222
    88
kaliszyk@35222
    89
text{*A function to return the right part of the top pair in a message.*}
haftmann@40823
    90
primrec
kaliszyk@35222
    91
  freeright :: "freemsg \<Rightarrow> freemsg"
kaliszyk@35222
    92
where
kaliszyk@35222
    93
  "freeright (NONCE N) = NONCE N"
kaliszyk@35222
    94
| "freeright (MPAIR X Y) = Y"
kaliszyk@35222
    95
| "freeright (CRYPT K X) = freeright X"
kaliszyk@35222
    96
| "freeright (DECRYPT K X) = freeright X"
kaliszyk@35222
    97
kaliszyk@35222
    98
text{*This theorem lets us prove that the right function respects the
kaliszyk@35222
    99
equivalence relation.  It also helps us prove that MPair
kaliszyk@35222
   100
  (the abstract constructor) is injective*}
kaliszyk@35222
   101
lemma msgrel_imp_eqv_freeright_aux:
kaliszyk@35222
   102
  shows "freeright U \<sim> freeright U"
haftmann@40823
   103
  by (fact msgrel_refl)
kaliszyk@35222
   104
kaliszyk@35222
   105
theorem msgrel_imp_eqv_freeright:
kaliszyk@36524
   106
  assumes a: "U \<sim> V"
kaliszyk@35222
   107
  shows "freeright U \<sim> freeright V"
kaliszyk@35222
   108
  using a
kaliszyk@35222
   109
  by (induct) (auto intro: msgrel_imp_eqv_freeright_aux)
kaliszyk@35222
   110
kaliszyk@35222
   111
subsubsection{*The Discriminator for Constructors*}
kaliszyk@35222
   112
kaliszyk@35222
   113
text{*A function to distinguish nonces, mpairs and encryptions*}
haftmann@40823
   114
primrec
kaliszyk@35222
   115
  freediscrim :: "freemsg \<Rightarrow> int"
kaliszyk@35222
   116
where
kaliszyk@35222
   117
   "freediscrim (NONCE N) = 0"
kaliszyk@35222
   118
 | "freediscrim (MPAIR X Y) = 1"
kaliszyk@35222
   119
 | "freediscrim (CRYPT K X) = freediscrim X + 2"
kaliszyk@35222
   120
 | "freediscrim (DECRYPT K X) = freediscrim X - 2"
kaliszyk@35222
   121
kaliszyk@35222
   122
text{*This theorem helps us prove @{term "Nonce N \<noteq> MPair X Y"}*}
kaliszyk@35222
   123
theorem msgrel_imp_eq_freediscrim:
kaliszyk@35222
   124
  assumes a: "U \<sim> V"
kaliszyk@35222
   125
  shows "freediscrim U = freediscrim V"
kaliszyk@35222
   126
  using a by (induct) (auto)
kaliszyk@35222
   127
kaliszyk@35222
   128
subsection{*The Initial Algebra: A Quotiented Message Type*}
kaliszyk@35222
   129
kaliszyk@35222
   130
quotient_type msg = freemsg / msgrel
kaliszyk@35222
   131
  by (rule equiv_msgrel)
kaliszyk@35222
   132
kaliszyk@35222
   133
text{*The abstract message constructors*}
kaliszyk@35222
   134
kaliszyk@35222
   135
quotient_definition
kaliszyk@35222
   136
  "Nonce :: nat \<Rightarrow> msg"
kaliszyk@35222
   137
is
kaliszyk@35222
   138
  "NONCE"
kuncar@47092
   139
done
kaliszyk@35222
   140
kaliszyk@35222
   141
quotient_definition
kaliszyk@35222
   142
  "MPair :: msg \<Rightarrow> msg \<Rightarrow> msg"
kaliszyk@35222
   143
is
kaliszyk@35222
   144
  "MPAIR"
kuncar@47092
   145
by (rule MPAIR)
kaliszyk@35222
   146
kaliszyk@35222
   147
quotient_definition
kaliszyk@35222
   148
  "Crypt :: nat \<Rightarrow> msg \<Rightarrow> msg"
kaliszyk@35222
   149
is
kaliszyk@35222
   150
  "CRYPT"
kuncar@47092
   151
by (rule CRYPT)
kaliszyk@35222
   152
kaliszyk@35222
   153
quotient_definition
kaliszyk@35222
   154
  "Decrypt :: nat \<Rightarrow> msg \<Rightarrow> msg"
kaliszyk@35222
   155
is
kaliszyk@35222
   156
  "DECRYPT"
kuncar@47092
   157
by (rule DECRYPT)
kaliszyk@35222
   158
kaliszyk@35222
   159
text{*Establishing these two equations is the point of the whole exercise*}
kaliszyk@36524
   160
theorem CD_eq [simp]:
kaliszyk@35222
   161
  shows "Crypt K (Decrypt K X) = X"
kaliszyk@35222
   162
  by (lifting CD)
kaliszyk@35222
   163
kaliszyk@36524
   164
theorem DC_eq [simp]:
kaliszyk@35222
   165
  shows "Decrypt K (Crypt K X) = X"
kaliszyk@35222
   166
  by (lifting DC)
kaliszyk@35222
   167
kaliszyk@35222
   168
subsection{*The Abstract Function to Return the Set of Nonces*}
kaliszyk@35222
   169
kaliszyk@35222
   170
quotient_definition
kaliszyk@35222
   171
   "nonces:: msg \<Rightarrow> nat set"
kaliszyk@35222
   172
is
kaliszyk@35222
   173
  "freenonces"
kuncar@47092
   174
by (rule msgrel_imp_eq_freenonces)
kaliszyk@35222
   175
kaliszyk@35222
   176
text{*Now prove the four equations for @{term nonces}*}
kaliszyk@35222
   177
kaliszyk@36524
   178
lemma nonces_Nonce [simp]:
kaliszyk@35222
   179
  shows "nonces (Nonce N) = {N}"
kaliszyk@35222
   180
  by (lifting freenonces.simps(1))
kaliszyk@36524
   181
kaliszyk@36524
   182
lemma nonces_MPair [simp]:
kaliszyk@35222
   183
  shows "nonces (MPair X Y) = nonces X \<union> nonces Y"
kaliszyk@35222
   184
  by (lifting freenonces.simps(2))
kaliszyk@35222
   185
kaliszyk@36524
   186
lemma nonces_Crypt [simp]:
kaliszyk@35222
   187
  shows "nonces (Crypt K X) = nonces X"
kaliszyk@35222
   188
  by (lifting freenonces.simps(3))
kaliszyk@35222
   189
kaliszyk@36524
   190
lemma nonces_Decrypt [simp]:
kaliszyk@35222
   191
  shows "nonces (Decrypt K X) = nonces X"
kaliszyk@35222
   192
  by (lifting freenonces.simps(4))
kaliszyk@35222
   193
kaliszyk@35222
   194
subsection{*The Abstract Function to Return the Left Part*}
kaliszyk@35222
   195
kaliszyk@35222
   196
quotient_definition
kaliszyk@35222
   197
  "left:: msg \<Rightarrow> msg"
kaliszyk@35222
   198
is
kaliszyk@35222
   199
  "freeleft"
kuncar@47092
   200
by (rule msgrel_imp_eqv_freeleft)
kaliszyk@35222
   201
kaliszyk@36524
   202
lemma left_Nonce [simp]:
kaliszyk@35222
   203
  shows "left (Nonce N) = Nonce N"
kaliszyk@35222
   204
  by (lifting freeleft.simps(1))
kaliszyk@35222
   205
kaliszyk@36524
   206
lemma left_MPair [simp]:
kaliszyk@35222
   207
  shows "left (MPair X Y) = X"
kaliszyk@35222
   208
  by (lifting freeleft.simps(2))
kaliszyk@35222
   209
kaliszyk@36524
   210
lemma left_Crypt [simp]:
kaliszyk@35222
   211
  shows "left (Crypt K X) = left X"
kaliszyk@35222
   212
  by (lifting freeleft.simps(3))
kaliszyk@35222
   213
kaliszyk@36524
   214
lemma left_Decrypt [simp]:
kaliszyk@35222
   215
  shows "left (Decrypt K X) = left X"
kaliszyk@35222
   216
  by (lifting freeleft.simps(4))
kaliszyk@35222
   217
kaliszyk@35222
   218
subsection{*The Abstract Function to Return the Right Part*}
kaliszyk@35222
   219
kaliszyk@35222
   220
quotient_definition
kaliszyk@35222
   221
  "right:: msg \<Rightarrow> msg"
kaliszyk@35222
   222
is
kaliszyk@35222
   223
  "freeright"
kuncar@47092
   224
by (rule msgrel_imp_eqv_freeright)
kaliszyk@35222
   225
kaliszyk@35222
   226
text{*Now prove the four equations for @{term right}*}
kaliszyk@35222
   227
kaliszyk@36524
   228
lemma right_Nonce [simp]:
kaliszyk@35222
   229
  shows "right (Nonce N) = Nonce N"
kaliszyk@35222
   230
  by (lifting freeright.simps(1))
kaliszyk@35222
   231
kaliszyk@36524
   232
lemma right_MPair [simp]:
kaliszyk@35222
   233
  shows "right (MPair X Y) = Y"
kaliszyk@35222
   234
  by (lifting freeright.simps(2))
kaliszyk@35222
   235
kaliszyk@36524
   236
lemma right_Crypt [simp]:
kaliszyk@35222
   237
  shows "right (Crypt K X) = right X"
kaliszyk@35222
   238
  by (lifting freeright.simps(3))
kaliszyk@35222
   239
kaliszyk@36524
   240
lemma right_Decrypt [simp]:
kaliszyk@35222
   241
  shows "right (Decrypt K X) = right X"
kaliszyk@35222
   242
  by (lifting freeright.simps(4))
kaliszyk@35222
   243
kaliszyk@35222
   244
subsection{*Injectivity Properties of Some Constructors*}
kaliszyk@35222
   245
kaliszyk@35222
   246
text{*Can also be proved using the function @{term nonces}*}
kaliszyk@36524
   247
lemma Nonce_Nonce_eq [iff]:
kaliszyk@35222
   248
  shows "(Nonce m = Nonce n) = (m = n)"
kaliszyk@35222
   249
proof
kaliszyk@35222
   250
  assume "Nonce m = Nonce n"
urbanc@37594
   251
  then show "m = n" 
urbanc@37594
   252
    by (descending) (drule msgrel_imp_eq_freenonces, simp)
kaliszyk@35222
   253
next
kaliszyk@36524
   254
  assume "m = n"
kaliszyk@35222
   255
  then show "Nonce m = Nonce n" by simp
kaliszyk@35222
   256
qed
kaliszyk@35222
   257
kaliszyk@36524
   258
lemma MPair_imp_eq_left:
kaliszyk@36524
   259
  assumes eq: "MPair X Y = MPair X' Y'"
kaliszyk@35222
   260
  shows "X = X'"
urbanc@37594
   261
  using eq 
urbanc@37594
   262
  by (descending) (drule msgrel_imp_eqv_freeleft, simp)
kaliszyk@35222
   263
kaliszyk@36524
   264
lemma MPair_imp_eq_right:
kaliszyk@35222
   265
  shows "MPair X Y = MPair X' Y' \<Longrightarrow> Y = Y'"
urbanc@37594
   266
  by (descending) (drule msgrel_imp_eqv_freeright, simp)
kaliszyk@35222
   267
kaliszyk@36524
   268
theorem MPair_MPair_eq [iff]:
kaliszyk@36524
   269
  shows "(MPair X Y = MPair X' Y') = (X=X' & Y=Y')"
kaliszyk@35222
   270
  by (blast dest: MPair_imp_eq_left MPair_imp_eq_right)
kaliszyk@35222
   271
kaliszyk@36524
   272
theorem Nonce_neq_MPair [iff]:
kaliszyk@35222
   273
  shows "Nonce N \<noteq> MPair X Y"
urbanc@37594
   274
  by (descending) (auto dest: msgrel_imp_eq_freediscrim)
kaliszyk@35222
   275
kaliszyk@35222
   276
text{*Example suggested by a referee*}
kaliszyk@35222
   277
kaliszyk@36524
   278
theorem Crypt_Nonce_neq_Nonce:
kaliszyk@35222
   279
  shows "Crypt K (Nonce M) \<noteq> Nonce N"
urbanc@37594
   280
  by (descending) (auto dest: msgrel_imp_eq_freediscrim) 
kaliszyk@35222
   281
kaliszyk@35222
   282
text{*...and many similar results*}
kaliszyk@35222
   283
kaliszyk@36524
   284
theorem Crypt2_Nonce_neq_Nonce:
kaliszyk@35222
   285
  shows "Crypt K (Crypt K' (Nonce M)) \<noteq> Nonce N"
urbanc@37594
   286
  by (descending) (auto dest: msgrel_imp_eq_freediscrim)
kaliszyk@35222
   287
kaliszyk@36524
   288
theorem Crypt_Crypt_eq [iff]:
kaliszyk@36524
   289
  shows "(Crypt K X = Crypt K X') = (X=X')"
kaliszyk@35222
   290
proof
kaliszyk@35222
   291
  assume "Crypt K X = Crypt K X'"
kaliszyk@35222
   292
  hence "Decrypt K (Crypt K X) = Decrypt K (Crypt K X')" by simp
kaliszyk@35222
   293
  thus "X = X'" by simp
kaliszyk@35222
   294
next
kaliszyk@35222
   295
  assume "X = X'"
kaliszyk@35222
   296
  thus "Crypt K X = Crypt K X'" by simp
kaliszyk@35222
   297
qed
kaliszyk@35222
   298
kaliszyk@36524
   299
theorem Decrypt_Decrypt_eq [iff]:
kaliszyk@36524
   300
  shows "(Decrypt K X = Decrypt K X') = (X=X')"
kaliszyk@35222
   301
proof
kaliszyk@35222
   302
  assume "Decrypt K X = Decrypt K X'"
kaliszyk@35222
   303
  hence "Crypt K (Decrypt K X) = Crypt K (Decrypt K X')" by simp
kaliszyk@35222
   304
  thus "X = X'" by simp
kaliszyk@35222
   305
next
kaliszyk@35222
   306
  assume "X = X'"
kaliszyk@35222
   307
  thus "Decrypt K X = Decrypt K X'" by simp
kaliszyk@35222
   308
qed
kaliszyk@35222
   309
kaliszyk@35222
   310
lemma msg_induct [case_names Nonce MPair Crypt Decrypt, cases type: msg]:
kaliszyk@35222
   311
  assumes N: "\<And>N. P (Nonce N)"
kaliszyk@35222
   312
      and M: "\<And>X Y. \<lbrakk>P X; P Y\<rbrakk> \<Longrightarrow> P (MPair X Y)"
kaliszyk@35222
   313
      and C: "\<And>K X. P X \<Longrightarrow> P (Crypt K X)"
kaliszyk@35222
   314
      and D: "\<And>K X. P X \<Longrightarrow> P (Decrypt K X)"
kaliszyk@35222
   315
  shows "P msg"
urbanc@47304
   316
  using N M C D 
urbanc@47304
   317
  by (descending) (auto intro: freemsg.induct)
urbanc@47304
   318
kaliszyk@35222
   319
kaliszyk@35222
   320
subsection{*The Abstract Discriminator*}
kaliszyk@35222
   321
kaliszyk@35222
   322
text{*However, as @{text Crypt_Nonce_neq_Nonce} above illustrates, we don't
kaliszyk@35222
   323
need this function in order to prove discrimination theorems.*}
kaliszyk@35222
   324
kaliszyk@35222
   325
quotient_definition
kaliszyk@35222
   326
  "discrim:: msg \<Rightarrow> int"
kaliszyk@35222
   327
is
kaliszyk@35222
   328
  "freediscrim"
kuncar@47092
   329
by (rule msgrel_imp_eq_freediscrim)
kaliszyk@35222
   330
kaliszyk@35222
   331
text{*Now prove the four equations for @{term discrim}*}
kaliszyk@35222
   332
kaliszyk@36524
   333
lemma discrim_Nonce [simp]:
kaliszyk@35222
   334
  shows "discrim (Nonce N) = 0"
kaliszyk@35222
   335
  by (lifting freediscrim.simps(1))
kaliszyk@35222
   336
kaliszyk@36524
   337
lemma discrim_MPair [simp]:
kaliszyk@35222
   338
  shows "discrim (MPair X Y) = 1"
kaliszyk@35222
   339
  by (lifting freediscrim.simps(2))
kaliszyk@35222
   340
kaliszyk@36524
   341
lemma discrim_Crypt [simp]:
kaliszyk@35222
   342
  shows "discrim (Crypt K X) = discrim X + 2"
kaliszyk@35222
   343
  by (lifting freediscrim.simps(3))
kaliszyk@35222
   344
kaliszyk@36524
   345
lemma discrim_Decrypt [simp]:
kaliszyk@35222
   346
  shows "discrim (Decrypt K X) = discrim X - 2"
kaliszyk@35222
   347
  by (lifting freediscrim.simps(4))
kaliszyk@35222
   348
kaliszyk@35222
   349
end
kaliszyk@35222
   350