src/HOL/Predicate.thy
author haftmann
Fri, 29 Jul 2011 19:47:55 +0200
changeset 44878 b5e7594061ce
parent 41798 efa734d9b221
child 44897 d5e28a49e16e
permissions -rw-r--r--
tuned proofs
berghofe@22259
     1
(*  Title:      HOL/Predicate.thy
haftmann@30328
     2
    Author:     Stefan Berghofer and Lukas Bulwahn and Florian Haftmann, TU Muenchen
berghofe@22259
     3
*)
berghofe@22259
     4
haftmann@30328
     5
header {* Predicates as relations and enumerations *}
berghofe@22259
     6
berghofe@22259
     7
theory Predicate
haftmann@23708
     8
imports Inductive Relation
berghofe@22259
     9
begin
berghofe@22259
    10
haftmann@30328
    11
notation
haftmann@41330
    12
  bot ("\<bottom>") and
haftmann@41330
    13
  top ("\<top>") and
haftmann@30328
    14
  inf (infixl "\<sqinter>" 70) and
haftmann@30328
    15
  sup (infixl "\<squnion>" 65) and
haftmann@30328
    16
  Inf ("\<Sqinter>_" [900] 900) and
haftmann@41330
    17
  Sup ("\<Squnion>_" [900] 900)
haftmann@30328
    18
haftmann@41328
    19
syntax (xsymbols)
haftmann@41330
    20
  "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
haftmann@41330
    21
  "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@41328
    22
  "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
haftmann@41328
    23
  "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@41328
    24
haftmann@30328
    25
haftmann@30328
    26
subsection {* Predicates as (complete) lattices *}
haftmann@30328
    27
haftmann@34065
    28
haftmann@34065
    29
text {*
haftmann@34065
    30
  Handy introduction and elimination rules for @{text "\<le>"}
haftmann@34065
    31
  on unary and binary predicates
haftmann@34065
    32
*}
haftmann@34065
    33
haftmann@34065
    34
lemma predicate1I:
haftmann@34065
    35
  assumes PQ: "\<And>x. P x \<Longrightarrow> Q x"
haftmann@34065
    36
  shows "P \<le> Q"
haftmann@34065
    37
  apply (rule le_funI)
haftmann@34065
    38
  apply (rule le_boolI)
haftmann@34065
    39
  apply (rule PQ)
haftmann@34065
    40
  apply assumption
haftmann@34065
    41
  done
haftmann@34065
    42
haftmann@34065
    43
lemma predicate1D [Pure.dest?, dest?]:
haftmann@34065
    44
  "P \<le> Q \<Longrightarrow> P x \<Longrightarrow> Q x"
haftmann@34065
    45
  apply (erule le_funE)
haftmann@34065
    46
  apply (erule le_boolE)
haftmann@34065
    47
  apply assumption+
haftmann@34065
    48
  done
haftmann@34065
    49
haftmann@34065
    50
lemma rev_predicate1D:
haftmann@34065
    51
  "P x ==> P <= Q ==> Q x"
haftmann@34065
    52
  by (rule predicate1D)
haftmann@34065
    53
haftmann@34065
    54
lemma predicate2I [Pure.intro!, intro!]:
haftmann@34065
    55
  assumes PQ: "\<And>x y. P x y \<Longrightarrow> Q x y"
haftmann@34065
    56
  shows "P \<le> Q"
haftmann@34065
    57
  apply (rule le_funI)+
haftmann@34065
    58
  apply (rule le_boolI)
haftmann@34065
    59
  apply (rule PQ)
haftmann@34065
    60
  apply assumption
haftmann@34065
    61
  done
haftmann@34065
    62
haftmann@34065
    63
lemma predicate2D [Pure.dest, dest]:
haftmann@34065
    64
  "P \<le> Q \<Longrightarrow> P x y \<Longrightarrow> Q x y"
haftmann@34065
    65
  apply (erule le_funE)+
haftmann@34065
    66
  apply (erule le_boolE)
haftmann@34065
    67
  apply assumption+
haftmann@34065
    68
  done
haftmann@34065
    69
haftmann@34065
    70
lemma rev_predicate2D:
haftmann@34065
    71
  "P x y ==> P <= Q ==> Q x y"
haftmann@34065
    72
  by (rule predicate2D)
haftmann@34065
    73
haftmann@34065
    74
haftmann@32779
    75
subsubsection {* Equality *}
berghofe@22259
    76
berghofe@26797
    77
lemma pred_equals_eq: "((\<lambda>x. x \<in> R) = (\<lambda>x. x \<in> S)) = (R = S)"
berghofe@26797
    78
  by (simp add: mem_def)
berghofe@22259
    79
berghofe@23741
    80
lemma pred_equals_eq2 [pred_set_conv]: "((\<lambda>x y. (x, y) \<in> R) = (\<lambda>x y. (x, y) \<in> S)) = (R = S)"
nipkow@39535
    81
  by (simp add: fun_eq_iff mem_def)
berghofe@22259
    82
haftmann@32779
    83
haftmann@32779
    84
subsubsection {* Order relation *}
haftmann@32779
    85
berghofe@26797
    86
lemma pred_subset_eq: "((\<lambda>x. x \<in> R) <= (\<lambda>x. x \<in> S)) = (R <= S)"
berghofe@26797
    87
  by (simp add: mem_def)
berghofe@22259
    88
berghofe@23741
    89
lemma pred_subset_eq2 [pred_set_conv]: "((\<lambda>x y. (x, y) \<in> R) <= (\<lambda>x y. (x, y) \<in> S)) = (R <= S)"
berghofe@22259
    90
  by fast
berghofe@22259
    91
berghofe@22259
    92
haftmann@30328
    93
subsubsection {* Top and bottom elements *}
berghofe@22259
    94
blanchet@38889
    95
lemma bot1E [no_atp, elim!]: "bot x \<Longrightarrow> P"
wenzelm@41798
    96
  by (simp add: bot_fun_def)
berghofe@22259
    97
berghofe@23741
    98
lemma bot2E [elim!]: "bot x y \<Longrightarrow> P"
wenzelm@41798
    99
  by (simp add: bot_fun_def)
berghofe@23741
   100
berghofe@23741
   101
lemma bot_empty_eq: "bot = (\<lambda>x. x \<in> {})"
nipkow@39535
   102
  by (auto simp add: fun_eq_iff)
berghofe@23741
   103
berghofe@23741
   104
lemma bot_empty_eq2: "bot = (\<lambda>x y. (x, y) \<in> {})"
nipkow@39535
   105
  by (auto simp add: fun_eq_iff)
berghofe@23741
   106
haftmann@41330
   107
lemma top1I [intro!]: "top x"
wenzelm@41798
   108
  by (simp add: top_fun_def)
haftmann@41330
   109
haftmann@41330
   110
lemma top2I [intro!]: "top x y"
wenzelm@41798
   111
  by (simp add: top_fun_def)
haftmann@41330
   112
haftmann@41330
   113
haftmann@41330
   114
subsubsection {* Binary intersection *}
haftmann@41330
   115
haftmann@41330
   116
lemma inf1I [intro!]: "A x ==> B x ==> inf A B x"
wenzelm@41798
   117
  by (simp add: inf_fun_def)
haftmann@41330
   118
haftmann@41330
   119
lemma inf2I [intro!]: "A x y ==> B x y ==> inf A B x y"
wenzelm@41798
   120
  by (simp add: inf_fun_def)
haftmann@41330
   121
haftmann@41330
   122
lemma inf1E [elim!]: "inf A B x ==> (A x ==> B x ==> P) ==> P"
wenzelm@41798
   123
  by (simp add: inf_fun_def)
haftmann@41330
   124
haftmann@41330
   125
lemma inf2E [elim!]: "inf A B x y ==> (A x y ==> B x y ==> P) ==> P"
wenzelm@41798
   126
  by (simp add: inf_fun_def)
haftmann@41330
   127
haftmann@41330
   128
lemma inf1D1: "inf A B x ==> A x"
wenzelm@41798
   129
  by (simp add: inf_fun_def)
haftmann@41330
   130
haftmann@41330
   131
lemma inf2D1: "inf A B x y ==> A x y"
wenzelm@41798
   132
  by (simp add: inf_fun_def)
haftmann@41330
   133
haftmann@41330
   134
lemma inf1D2: "inf A B x ==> B x"
wenzelm@41798
   135
  by (simp add: inf_fun_def)
haftmann@41330
   136
haftmann@41330
   137
lemma inf2D2: "inf A B x y ==> B x y"
wenzelm@41798
   138
  by (simp add: inf_fun_def)
haftmann@41330
   139
haftmann@41330
   140
lemma inf_Int_eq: "inf (\<lambda>x. x \<in> R) (\<lambda>x. x \<in> S) = (\<lambda>x. x \<in> R \<inter> S)"
wenzelm@41798
   141
  by (simp add: inf_fun_def mem_def)
haftmann@41330
   142
haftmann@41330
   143
lemma inf_Int_eq2 [pred_set_conv]: "inf (\<lambda>x y. (x, y) \<in> R) (\<lambda>x y. (x, y) \<in> S) = (\<lambda>x y. (x, y) \<in> R \<inter> S)"
wenzelm@41798
   144
  by (simp add: inf_fun_def mem_def)
haftmann@41330
   145
berghofe@23741
   146
haftmann@30328
   147
subsubsection {* Binary union *}
berghofe@22259
   148
haftmann@32883
   149
lemma sup1I1 [elim?]: "A x \<Longrightarrow> sup A B x"
wenzelm@41798
   150
  by (simp add: sup_fun_def)
berghofe@22259
   151
haftmann@32883
   152
lemma sup2I1 [elim?]: "A x y \<Longrightarrow> sup A B x y"
wenzelm@41798
   153
  by (simp add: sup_fun_def)
berghofe@22259
   154
berghofe@23741
   155
lemma sup1I2 [elim?]: "B x \<Longrightarrow> sup A B x"
wenzelm@41798
   156
  by (simp add: sup_fun_def)
berghofe@22259
   157
berghofe@23741
   158
lemma sup2I2 [elim?]: "B x y \<Longrightarrow> sup A B x y"
wenzelm@41798
   159
  by (simp add: sup_fun_def)
haftmann@32883
   160
haftmann@32883
   161
lemma sup1E [elim!]: "sup A B x ==> (A x ==> P) ==> (B x ==> P) ==> P"
wenzelm@41798
   162
  by (simp add: sup_fun_def) iprover
haftmann@32883
   163
haftmann@32883
   164
lemma sup2E [elim!]: "sup A B x y ==> (A x y ==> P) ==> (B x y ==> P) ==> P"
wenzelm@41798
   165
  by (simp add: sup_fun_def) iprover
berghofe@22259
   166
berghofe@22259
   167
text {*
berghofe@22259
   168
  \medskip Classical introduction rule: no commitment to @{text A} vs
berghofe@22259
   169
  @{text B}.
berghofe@22259
   170
*}
berghofe@22259
   171
haftmann@22422
   172
lemma sup1CI [intro!]: "(~ B x ==> A x) ==> sup A B x"
wenzelm@41798
   173
  by (auto simp add: sup_fun_def)
berghofe@22259
   174
haftmann@22422
   175
lemma sup2CI [intro!]: "(~ B x y ==> A x y) ==> sup A B x y"
wenzelm@41798
   176
  by (auto simp add: sup_fun_def)
berghofe@22259
   177
haftmann@32883
   178
lemma sup_Un_eq: "sup (\<lambda>x. x \<in> R) (\<lambda>x. x \<in> S) = (\<lambda>x. x \<in> R \<union> S)"
wenzelm@41798
   179
  by (simp add: sup_fun_def mem_def)
berghofe@22259
   180
haftmann@32883
   181
lemma sup_Un_eq2 [pred_set_conv]: "sup (\<lambda>x y. (x, y) \<in> R) (\<lambda>x y. (x, y) \<in> S) = (\<lambda>x y. (x, y) \<in> R \<union> S)"
wenzelm@41798
   182
  by (simp add: sup_fun_def mem_def)
berghofe@22259
   183
berghofe@22259
   184
haftmann@30328
   185
subsubsection {* Intersections of families *}
berghofe@22430
   186
haftmann@32601
   187
lemma INF1_iff: "(INF x:A. B x) b = (ALL x:A. B x b)"
haftmann@41328
   188
  by (simp add: INFI_apply)
berghofe@22430
   189
haftmann@32601
   190
lemma INF2_iff: "(INF x:A. B x) b c = (ALL x:A. B x b c)"
haftmann@41328
   191
  by (simp add: INFI_apply)
berghofe@22430
   192
berghofe@22430
   193
lemma INF1_I [intro!]: "(!!x. x : A ==> B x b) ==> (INF x:A. B x) b"
haftmann@41328
   194
  by (auto simp add: INFI_apply)
berghofe@22430
   195
berghofe@22430
   196
lemma INF2_I [intro!]: "(!!x. x : A ==> B x b c) ==> (INF x:A. B x) b c"
haftmann@41328
   197
  by (auto simp add: INFI_apply)
berghofe@22430
   198
berghofe@22430
   199
lemma INF1_D [elim]: "(INF x:A. B x) b ==> a : A ==> B a b"
haftmann@41328
   200
  by (auto simp add: INFI_apply)
berghofe@22430
   201
berghofe@22430
   202
lemma INF2_D [elim]: "(INF x:A. B x) b c ==> a : A ==> B a b c"
haftmann@41328
   203
  by (auto simp add: INFI_apply)
berghofe@22430
   204
berghofe@22430
   205
lemma INF1_E [elim]: "(INF x:A. B x) b ==> (B a b ==> R) ==> (a ~: A ==> R) ==> R"
haftmann@41328
   206
  by (auto simp add: INFI_apply)
berghofe@22430
   207
berghofe@22430
   208
lemma INF2_E [elim]: "(INF x:A. B x) b c ==> (B a b c ==> R) ==> (a ~: A ==> R) ==> R"
haftmann@41328
   209
  by (auto simp add: INFI_apply)
berghofe@22259
   210
berghofe@23741
   211
lemma INF_INT_eq: "(INF i. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (INT i. r i))"
haftmann@41328
   212
  by (simp add: INFI_apply fun_eq_iff)
berghofe@23741
   213
berghofe@23741
   214
lemma INF_INT_eq2: "(INF i. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (INT i. r i))"
haftmann@41328
   215
  by (simp add: INFI_apply fun_eq_iff)
berghofe@23741
   216
berghofe@22259
   217
haftmann@41330
   218
subsubsection {* Unions of families *}
haftmann@41330
   219
haftmann@41330
   220
lemma SUP1_iff: "(SUP x:A. B x) b = (EX x:A. B x b)"
haftmann@41330
   221
  by (simp add: SUPR_apply)
haftmann@41330
   222
haftmann@41330
   223
lemma SUP2_iff: "(SUP x:A. B x) b c = (EX x:A. B x b c)"
haftmann@41330
   224
  by (simp add: SUPR_apply)
haftmann@41330
   225
haftmann@41330
   226
lemma SUP1_I [intro]: "a : A ==> B a b ==> (SUP x:A. B x) b"
haftmann@41330
   227
  by (auto simp add: SUPR_apply)
haftmann@41330
   228
haftmann@41330
   229
lemma SUP2_I [intro]: "a : A ==> B a b c ==> (SUP x:A. B x) b c"
haftmann@41330
   230
  by (auto simp add: SUPR_apply)
haftmann@41330
   231
haftmann@41330
   232
lemma SUP1_E [elim!]: "(SUP x:A. B x) b ==> (!!x. x : A ==> B x b ==> R) ==> R"
haftmann@41330
   233
  by (auto simp add: SUPR_apply)
haftmann@41330
   234
haftmann@41330
   235
lemma SUP2_E [elim!]: "(SUP x:A. B x) b c ==> (!!x. x : A ==> B x b c ==> R) ==> R"
haftmann@41330
   236
  by (auto simp add: SUPR_apply)
haftmann@41330
   237
haftmann@41330
   238
lemma SUP_UN_eq: "(SUP i. (\<lambda>x. x \<in> r i)) = (\<lambda>x. x \<in> (UN i. r i))"
haftmann@41330
   239
  by (simp add: SUPR_apply fun_eq_iff)
haftmann@41330
   240
haftmann@41330
   241
lemma SUP_UN_eq2: "(SUP i. (\<lambda>x y. (x, y) \<in> r i)) = (\<lambda>x y. (x, y) \<in> (UN i. r i))"
haftmann@41330
   242
  by (simp add: SUPR_apply fun_eq_iff)
haftmann@41330
   243
haftmann@41330
   244
haftmann@30328
   245
subsection {* Predicates as relations *}
haftmann@30328
   246
haftmann@30328
   247
subsubsection {* Composition  *}
berghofe@22259
   248
berghofe@23741
   249
inductive
krauss@32231
   250
  pred_comp  :: "['a => 'b => bool, 'b => 'c => bool] => 'a => 'c => bool"
berghofe@22259
   251
    (infixr "OO" 75)
krauss@32231
   252
  for r :: "'a => 'b => bool" and s :: "'b => 'c => bool"
berghofe@22259
   253
where
krauss@32231
   254
  pred_compI [intro]: "r a b ==> s b c ==> (r OO s) a c"
berghofe@22259
   255
berghofe@23741
   256
inductive_cases pred_compE [elim!]: "(r OO s) a c"
berghofe@22259
   257
berghofe@22259
   258
lemma pred_comp_rel_comp_eq [pred_set_conv]:
berghofe@23741
   259
  "((\<lambda>x y. (x, y) \<in> r) OO (\<lambda>x y. (x, y) \<in> s)) = (\<lambda>x y. (x, y) \<in> r O s)"
wenzelm@41798
   260
  by (auto simp add: fun_eq_iff)
berghofe@22259
   261
berghofe@22259
   262
haftmann@30328
   263
subsubsection {* Converse *}
berghofe@22259
   264
berghofe@23741
   265
inductive
berghofe@22259
   266
  conversep :: "('a => 'b => bool) => 'b => 'a => bool"
berghofe@22259
   267
    ("(_^--1)" [1000] 1000)
berghofe@22259
   268
  for r :: "'a => 'b => bool"
berghofe@22259
   269
where
berghofe@22259
   270
  conversepI: "r a b ==> r^--1 b a"
berghofe@22259
   271
berghofe@22259
   272
notation (xsymbols)
berghofe@22259
   273
  conversep  ("(_\<inverse>\<inverse>)" [1000] 1000)
berghofe@22259
   274
berghofe@22259
   275
lemma conversepD:
berghofe@22259
   276
  assumes ab: "r^--1 a b"
berghofe@22259
   277
  shows "r b a" using ab
berghofe@22259
   278
  by cases simp
berghofe@22259
   279
berghofe@22259
   280
lemma conversep_iff [iff]: "r^--1 a b = r b a"
berghofe@22259
   281
  by (iprover intro: conversepI dest: conversepD)
berghofe@22259
   282
berghofe@22259
   283
lemma conversep_converse_eq [pred_set_conv]:
berghofe@23741
   284
  "(\<lambda>x y. (x, y) \<in> r)^--1 = (\<lambda>x y. (x, y) \<in> r^-1)"
nipkow@39535
   285
  by (auto simp add: fun_eq_iff)
berghofe@22259
   286
berghofe@22259
   287
lemma conversep_conversep [simp]: "(r^--1)^--1 = r"
berghofe@22259
   288
  by (iprover intro: order_antisym conversepI dest: conversepD)
berghofe@22259
   289
berghofe@22259
   290
lemma converse_pred_comp: "(r OO s)^--1 = s^--1 OO r^--1"
berghofe@22259
   291
  by (iprover intro: order_antisym conversepI pred_compI
berghofe@22259
   292
    elim: pred_compE dest: conversepD)
berghofe@22259
   293
haftmann@22422
   294
lemma converse_meet: "(inf r s)^--1 = inf r^--1 s^--1"
wenzelm@41798
   295
  by (simp add: inf_fun_def) (iprover intro: conversepI ext dest: conversepD)
berghofe@22259
   296
haftmann@22422
   297
lemma converse_join: "(sup r s)^--1 = sup r^--1 s^--1"
wenzelm@41798
   298
  by (simp add: sup_fun_def) (iprover intro: conversepI ext dest: conversepD)
berghofe@22259
   299
berghofe@22259
   300
lemma conversep_noteq [simp]: "(op ~=)^--1 = op ~="
nipkow@39535
   301
  by (auto simp add: fun_eq_iff)
berghofe@22259
   302
berghofe@22259
   303
lemma conversep_eq [simp]: "(op =)^--1 = op ="
nipkow@39535
   304
  by (auto simp add: fun_eq_iff)
berghofe@22259
   305
berghofe@22259
   306
haftmann@30328
   307
subsubsection {* Domain *}
berghofe@22259
   308
berghofe@23741
   309
inductive
berghofe@22259
   310
  DomainP :: "('a => 'b => bool) => 'a => bool"
berghofe@22259
   311
  for r :: "'a => 'b => bool"
berghofe@22259
   312
where
berghofe@22259
   313
  DomainPI [intro]: "r a b ==> DomainP r a"
berghofe@22259
   314
berghofe@23741
   315
inductive_cases DomainPE [elim!]: "DomainP r a"
berghofe@22259
   316
berghofe@23741
   317
lemma DomainP_Domain_eq [pred_set_conv]: "DomainP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Domain r)"
berghofe@26797
   318
  by (blast intro!: Orderings.order_antisym predicate1I)
berghofe@22259
   319
berghofe@22259
   320
haftmann@30328
   321
subsubsection {* Range *}
berghofe@22259
   322
berghofe@23741
   323
inductive
berghofe@22259
   324
  RangeP :: "('a => 'b => bool) => 'b => bool"
berghofe@22259
   325
  for r :: "'a => 'b => bool"
berghofe@22259
   326
where
berghofe@22259
   327
  RangePI [intro]: "r a b ==> RangeP r b"
berghofe@22259
   328
berghofe@23741
   329
inductive_cases RangePE [elim!]: "RangeP r b"
berghofe@22259
   330
berghofe@23741
   331
lemma RangeP_Range_eq [pred_set_conv]: "RangeP (\<lambda>x y. (x, y) \<in> r) = (\<lambda>x. x \<in> Range r)"
berghofe@26797
   332
  by (blast intro!: Orderings.order_antisym predicate1I)
berghofe@22259
   333
berghofe@22259
   334
haftmann@30328
   335
subsubsection {* Inverse image *}
berghofe@22259
   336
berghofe@22259
   337
definition
berghofe@22259
   338
  inv_imagep :: "('b => 'b => bool) => ('a => 'b) => 'a => 'a => bool" where
berghofe@22259
   339
  "inv_imagep r f == %x y. r (f x) (f y)"
berghofe@22259
   340
berghofe@23741
   341
lemma [pred_set_conv]: "inv_imagep (\<lambda>x y. (x, y) \<in> r) f = (\<lambda>x y. (x, y) \<in> inv_image r f)"
berghofe@22259
   342
  by (simp add: inv_image_def inv_imagep_def)
berghofe@22259
   343
berghofe@22259
   344
lemma in_inv_imagep [simp]: "inv_imagep r f x y = r (f x) (f y)"
berghofe@22259
   345
  by (simp add: inv_imagep_def)
berghofe@22259
   346
berghofe@22259
   347
haftmann@30328
   348
subsubsection {* Powerset *}
berghofe@23741
   349
berghofe@23741
   350
definition Powp :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool" where
berghofe@23741
   351
  "Powp A == \<lambda>B. \<forall>x \<in> B. A x"
berghofe@23741
   352
berghofe@23741
   353
lemma Powp_Pow_eq [pred_set_conv]: "Powp (\<lambda>x. x \<in> A) = (\<lambda>x. x \<in> Pow A)"
nipkow@39535
   354
  by (auto simp add: Powp_def fun_eq_iff)
berghofe@23741
   355
berghofe@26797
   356
lemmas Powp_mono [mono] = Pow_mono [to_pred pred_subset_eq]
berghofe@26797
   357
berghofe@23741
   358
haftmann@30328
   359
subsubsection {* Properties of relations *}
berghofe@22259
   360
berghofe@22259
   361
abbreviation antisymP :: "('a => 'a => bool) => bool" where
berghofe@23741
   362
  "antisymP r == antisym {(x, y). r x y}"
berghofe@22259
   363
berghofe@22259
   364
abbreviation transP :: "('a => 'a => bool) => bool" where
berghofe@23741
   365
  "transP r == trans {(x, y). r x y}"
berghofe@22259
   366
berghofe@22259
   367
abbreviation single_valuedP :: "('a => 'b => bool) => bool" where
berghofe@23741
   368
  "single_valuedP r == single_valued {(x, y). r x y}"
berghofe@22259
   369
haftmann@41061
   370
(*FIXME inconsistencies: abbreviations vs. definitions, suffix `P` vs. suffix `p`*)
haftmann@41061
   371
haftmann@41061
   372
definition reflp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@41061
   373
  "reflp r \<longleftrightarrow> refl {(x, y). r x y}"
haftmann@41061
   374
haftmann@41061
   375
definition symp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@41061
   376
  "symp r \<longleftrightarrow> sym {(x, y). r x y}"
haftmann@41061
   377
haftmann@41061
   378
definition transp :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool" where
haftmann@41061
   379
  "transp r \<longleftrightarrow> trans {(x, y). r x y}"
haftmann@41061
   380
haftmann@41061
   381
lemma reflpI:
haftmann@41061
   382
  "(\<And>x. r x x) \<Longrightarrow> reflp r"
haftmann@41061
   383
  by (auto intro: refl_onI simp add: reflp_def)
haftmann@41061
   384
haftmann@41061
   385
lemma reflpE:
haftmann@41061
   386
  assumes "reflp r"
haftmann@41061
   387
  obtains "r x x"
haftmann@41061
   388
  using assms by (auto dest: refl_onD simp add: reflp_def)
haftmann@41061
   389
haftmann@41061
   390
lemma sympI:
haftmann@41061
   391
  "(\<And>x y. r x y \<Longrightarrow> r y x) \<Longrightarrow> symp r"
haftmann@41061
   392
  by (auto intro: symI simp add: symp_def)
haftmann@41061
   393
haftmann@41061
   394
lemma sympE:
haftmann@41061
   395
  assumes "symp r" and "r x y"
haftmann@41061
   396
  obtains "r y x"
haftmann@41061
   397
  using assms by (auto dest: symD simp add: symp_def)
haftmann@41061
   398
haftmann@41061
   399
lemma transpI:
haftmann@41061
   400
  "(\<And>x y z. r x y \<Longrightarrow> r y z \<Longrightarrow> r x z) \<Longrightarrow> transp r"
haftmann@41061
   401
  by (auto intro: transI simp add: transp_def)
haftmann@41061
   402
  
haftmann@41061
   403
lemma transpE:
haftmann@41061
   404
  assumes "transp r" and "r x y" and "r y z"
haftmann@41061
   405
  obtains "r x z"
haftmann@41061
   406
  using assms by (auto dest: transD simp add: transp_def)
haftmann@41061
   407
haftmann@30328
   408
haftmann@30328
   409
subsection {* Predicates as enumerations *}
haftmann@30328
   410
haftmann@30328
   411
subsubsection {* The type of predicate enumerations (a monad) *}
haftmann@30328
   412
haftmann@30328
   413
datatype 'a pred = Pred "'a \<Rightarrow> bool"
haftmann@30328
   414
haftmann@30328
   415
primrec eval :: "'a pred \<Rightarrow> 'a \<Rightarrow> bool" where
haftmann@30328
   416
  eval_pred: "eval (Pred f) = f"
haftmann@30328
   417
haftmann@30328
   418
lemma Pred_eval [simp]:
haftmann@30328
   419
  "Pred (eval x) = x"
haftmann@30328
   420
  by (cases x) simp
haftmann@30328
   421
haftmann@40864
   422
lemma pred_eqI:
haftmann@40864
   423
  "(\<And>w. eval P w \<longleftrightarrow> eval Q w) \<Longrightarrow> P = Q"
haftmann@40864
   424
  by (cases P, cases Q) (auto simp add: fun_eq_iff)
haftmann@30328
   425
haftmann@40864
   426
lemma eval_mem [simp]:
haftmann@40864
   427
  "x \<in> eval P \<longleftrightarrow> eval P x"
haftmann@40864
   428
  by (simp add: mem_def)
haftmann@30328
   429
haftmann@40864
   430
lemma eq_mem [simp]:
haftmann@40864
   431
  "x \<in> (op =) y \<longleftrightarrow> x = y"
haftmann@40864
   432
  by (auto simp add: mem_def)
haftmann@30328
   433
haftmann@32578
   434
instantiation pred :: (type) "{complete_lattice, boolean_algebra}"
haftmann@30328
   435
begin
haftmann@30328
   436
haftmann@30328
   437
definition
haftmann@30328
   438
  "P \<le> Q \<longleftrightarrow> eval P \<le> eval Q"
haftmann@30328
   439
haftmann@30328
   440
definition
haftmann@30328
   441
  "P < Q \<longleftrightarrow> eval P < eval Q"
haftmann@30328
   442
haftmann@30328
   443
definition
haftmann@30328
   444
  "\<bottom> = Pred \<bottom>"
haftmann@30328
   445
haftmann@40864
   446
lemma eval_bot [simp]:
haftmann@40864
   447
  "eval \<bottom>  = \<bottom>"
haftmann@40864
   448
  by (simp add: bot_pred_def)
haftmann@40864
   449
haftmann@30328
   450
definition
haftmann@30328
   451
  "\<top> = Pred \<top>"
haftmann@30328
   452
haftmann@40864
   453
lemma eval_top [simp]:
haftmann@40864
   454
  "eval \<top>  = \<top>"
haftmann@40864
   455
  by (simp add: top_pred_def)
haftmann@40864
   456
haftmann@30328
   457
definition
haftmann@30328
   458
  "P \<sqinter> Q = Pred (eval P \<sqinter> eval Q)"
haftmann@30328
   459
haftmann@40864
   460
lemma eval_inf [simp]:
haftmann@40864
   461
  "eval (P \<sqinter> Q) = eval P \<sqinter> eval Q"
haftmann@40864
   462
  by (simp add: inf_pred_def)
haftmann@40864
   463
haftmann@30328
   464
definition
haftmann@30328
   465
  "P \<squnion> Q = Pred (eval P \<squnion> eval Q)"
haftmann@30328
   466
haftmann@40864
   467
lemma eval_sup [simp]:
haftmann@40864
   468
  "eval (P \<squnion> Q) = eval P \<squnion> eval Q"
haftmann@40864
   469
  by (simp add: sup_pred_def)
haftmann@40864
   470
haftmann@30328
   471
definition
haftmann@37767
   472
  "\<Sqinter>A = Pred (INFI A eval)"
haftmann@30328
   473
haftmann@40864
   474
lemma eval_Inf [simp]:
haftmann@40864
   475
  "eval (\<Sqinter>A) = INFI A eval"
haftmann@40864
   476
  by (simp add: Inf_pred_def)
haftmann@40864
   477
haftmann@30328
   478
definition
haftmann@37767
   479
  "\<Squnion>A = Pred (SUPR A eval)"
haftmann@30328
   480
haftmann@40864
   481
lemma eval_Sup [simp]:
haftmann@40864
   482
  "eval (\<Squnion>A) = SUPR A eval"
haftmann@40864
   483
  by (simp add: Sup_pred_def)
haftmann@40864
   484
haftmann@32578
   485
definition
haftmann@32578
   486
  "- P = Pred (- eval P)"
haftmann@32578
   487
haftmann@40864
   488
lemma eval_compl [simp]:
haftmann@40864
   489
  "eval (- P) = - eval P"
haftmann@40864
   490
  by (simp add: uminus_pred_def)
haftmann@40864
   491
haftmann@32578
   492
definition
haftmann@32578
   493
  "P - Q = Pred (eval P - eval Q)"
haftmann@32578
   494
haftmann@40864
   495
lemma eval_minus [simp]:
haftmann@40864
   496
  "eval (P - Q) = eval P - eval Q"
haftmann@40864
   497
  by (simp add: minus_pred_def)
haftmann@40864
   498
haftmann@32578
   499
instance proof
haftmann@41328
   500
qed (auto intro!: pred_eqI simp add: less_eq_pred_def less_pred_def uminus_apply minus_apply)
haftmann@30328
   501
berghofe@22259
   502
end
haftmann@30328
   503
haftmann@40864
   504
lemma eval_INFI [simp]:
haftmann@40864
   505
  "eval (INFI A f) = INFI A (eval \<circ> f)"
haftmann@40864
   506
  by (unfold INFI_def) simp
haftmann@40864
   507
haftmann@40864
   508
lemma eval_SUPR [simp]:
haftmann@40864
   509
  "eval (SUPR A f) = SUPR A (eval \<circ> f)"
haftmann@40864
   510
  by (unfold SUPR_def) simp
haftmann@40864
   511
haftmann@40864
   512
definition single :: "'a \<Rightarrow> 'a pred" where
haftmann@40864
   513
  "single x = Pred ((op =) x)"
haftmann@40864
   514
haftmann@40864
   515
lemma eval_single [simp]:
haftmann@40864
   516
  "eval (single x) = (op =) x"
haftmann@40864
   517
  by (simp add: single_def)
haftmann@40864
   518
haftmann@40864
   519
definition bind :: "'a pred \<Rightarrow> ('a \<Rightarrow> 'b pred) \<Rightarrow> 'b pred" (infixl "\<guillemotright>=" 70) where
haftmann@41328
   520
  "P \<guillemotright>= f = (SUPR {x. eval P x} f)"
haftmann@40864
   521
haftmann@40864
   522
lemma eval_bind [simp]:
haftmann@41328
   523
  "eval (P \<guillemotright>= f) = eval (SUPR {x. eval P x} f)"
haftmann@40864
   524
  by (simp add: bind_def)
haftmann@40864
   525
haftmann@30328
   526
lemma bind_bind:
haftmann@30328
   527
  "(P \<guillemotright>= Q) \<guillemotright>= R = P \<guillemotright>= (\<lambda>x. Q x \<guillemotright>= R)"
haftmann@40922
   528
  by (rule pred_eqI) auto
haftmann@30328
   529
haftmann@30328
   530
lemma bind_single:
haftmann@30328
   531
  "P \<guillemotright>= single = P"
haftmann@40864
   532
  by (rule pred_eqI) auto
haftmann@30328
   533
haftmann@30328
   534
lemma single_bind:
haftmann@30328
   535
  "single x \<guillemotright>= P = P x"
haftmann@40864
   536
  by (rule pred_eqI) auto
haftmann@30328
   537
haftmann@30328
   538
lemma bottom_bind:
haftmann@30328
   539
  "\<bottom> \<guillemotright>= P = \<bottom>"
haftmann@40922
   540
  by (rule pred_eqI) auto
haftmann@30328
   541
haftmann@30328
   542
lemma sup_bind:
haftmann@30328
   543
  "(P \<squnion> Q) \<guillemotright>= R = P \<guillemotright>= R \<squnion> Q \<guillemotright>= R"
haftmann@40922
   544
  by (rule pred_eqI) auto
haftmann@30328
   545
haftmann@40864
   546
lemma Sup_bind:
haftmann@40864
   547
  "(\<Squnion>A \<guillemotright>= f) = \<Squnion>((\<lambda>x. x \<guillemotright>= f) ` A)"
haftmann@40922
   548
  by (rule pred_eqI) auto
haftmann@30328
   549
haftmann@30328
   550
lemma pred_iffI:
haftmann@30328
   551
  assumes "\<And>x. eval A x \<Longrightarrow> eval B x"
haftmann@30328
   552
  and "\<And>x. eval B x \<Longrightarrow> eval A x"
haftmann@30328
   553
  shows "A = B"
haftmann@40864
   554
  using assms by (auto intro: pred_eqI)
haftmann@30328
   555
  
haftmann@30328
   556
lemma singleI: "eval (single x) x"
haftmann@40864
   557
  by simp
haftmann@30328
   558
haftmann@30328
   559
lemma singleI_unit: "eval (single ()) x"
haftmann@40864
   560
  by simp
haftmann@30328
   561
haftmann@30328
   562
lemma singleE: "eval (single x) y \<Longrightarrow> (y = x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40864
   563
  by simp
haftmann@30328
   564
haftmann@30328
   565
lemma singleE': "eval (single x) y \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40864
   566
  by simp
haftmann@30328
   567
haftmann@30328
   568
lemma bindI: "eval P x \<Longrightarrow> eval (Q x) y \<Longrightarrow> eval (P \<guillemotright>= Q) y"
haftmann@40864
   569
  by auto
haftmann@30328
   570
haftmann@30328
   571
lemma bindE: "eval (R \<guillemotright>= Q) y \<Longrightarrow> (\<And>x. eval R x \<Longrightarrow> eval (Q x) y \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40864
   572
  by auto
haftmann@30328
   573
haftmann@30328
   574
lemma botE: "eval \<bottom> x \<Longrightarrow> P"
haftmann@40864
   575
  by auto
haftmann@30328
   576
haftmann@30328
   577
lemma supI1: "eval A x \<Longrightarrow> eval (A \<squnion> B) x"
haftmann@40864
   578
  by auto
haftmann@30328
   579
haftmann@30328
   580
lemma supI2: "eval B x \<Longrightarrow> eval (A \<squnion> B) x" 
haftmann@40864
   581
  by auto
haftmann@30328
   582
haftmann@30328
   583
lemma supE: "eval (A \<squnion> B) x \<Longrightarrow> (eval A x \<Longrightarrow> P) \<Longrightarrow> (eval B x \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@40864
   584
  by auto
haftmann@30328
   585
haftmann@32578
   586
lemma single_not_bot [simp]:
haftmann@32578
   587
  "single x \<noteq> \<bottom>"
nipkow@39535
   588
  by (auto simp add: single_def bot_pred_def fun_eq_iff)
haftmann@32578
   589
haftmann@32578
   590
lemma not_bot:
haftmann@32578
   591
  assumes "A \<noteq> \<bottom>"
haftmann@32578
   592
  obtains x where "eval A x"
haftmann@40864
   593
  using assms by (cases A)
haftmann@40864
   594
    (auto simp add: bot_pred_def, auto simp add: mem_def)
haftmann@32578
   595
  
haftmann@32578
   596
haftmann@32578
   597
subsubsection {* Emptiness check and definite choice *}
haftmann@32578
   598
haftmann@32578
   599
definition is_empty :: "'a pred \<Rightarrow> bool" where
haftmann@32578
   600
  "is_empty A \<longleftrightarrow> A = \<bottom>"
haftmann@32578
   601
haftmann@32578
   602
lemma is_empty_bot:
haftmann@32578
   603
  "is_empty \<bottom>"
haftmann@32578
   604
  by (simp add: is_empty_def)
haftmann@32578
   605
haftmann@32578
   606
lemma not_is_empty_single:
haftmann@32578
   607
  "\<not> is_empty (single x)"
nipkow@39535
   608
  by (auto simp add: is_empty_def single_def bot_pred_def fun_eq_iff)
haftmann@32578
   609
haftmann@32578
   610
lemma is_empty_sup:
haftmann@32578
   611
  "is_empty (A \<squnion> B) \<longleftrightarrow> is_empty A \<and> is_empty B"
huffman@36008
   612
  by (auto simp add: is_empty_def)
haftmann@32578
   613
haftmann@40864
   614
definition singleton :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a pred \<Rightarrow> 'a" where
bulwahn@33111
   615
  "singleton dfault A = (if \<exists>!x. eval A x then THE x. eval A x else dfault ())"
haftmann@32578
   616
haftmann@32578
   617
lemma singleton_eqI:
bulwahn@33110
   618
  "\<exists>!x. eval A x \<Longrightarrow> eval A x \<Longrightarrow> singleton dfault A = x"
haftmann@32578
   619
  by (auto simp add: singleton_def)
haftmann@32578
   620
haftmann@32578
   621
lemma eval_singletonI:
bulwahn@33110
   622
  "\<exists>!x. eval A x \<Longrightarrow> eval A (singleton dfault A)"
haftmann@32578
   623
proof -
haftmann@32578
   624
  assume assm: "\<exists>!x. eval A x"
haftmann@32578
   625
  then obtain x where "eval A x" ..
bulwahn@33110
   626
  moreover with assm have "singleton dfault A = x" by (rule singleton_eqI)
haftmann@32578
   627
  ultimately show ?thesis by simp 
haftmann@32578
   628
qed
haftmann@32578
   629
haftmann@32578
   630
lemma single_singleton:
bulwahn@33110
   631
  "\<exists>!x. eval A x \<Longrightarrow> single (singleton dfault A) = A"
haftmann@32578
   632
proof -
haftmann@32578
   633
  assume assm: "\<exists>!x. eval A x"
bulwahn@33110
   634
  then have "eval A (singleton dfault A)"
haftmann@32578
   635
    by (rule eval_singletonI)
bulwahn@33110
   636
  moreover from assm have "\<And>x. eval A x \<Longrightarrow> singleton dfault A = x"
haftmann@32578
   637
    by (rule singleton_eqI)
bulwahn@33110
   638
  ultimately have "eval (single (singleton dfault A)) = eval A"
nipkow@39535
   639
    by (simp (no_asm_use) add: single_def fun_eq_iff) blast
haftmann@40864
   640
  then have "\<And>x. eval (single (singleton dfault A)) x = eval A x"
haftmann@40864
   641
    by simp
haftmann@40864
   642
  then show ?thesis by (rule pred_eqI)
haftmann@32578
   643
qed
haftmann@32578
   644
haftmann@32578
   645
lemma singleton_undefinedI:
bulwahn@33111
   646
  "\<not> (\<exists>!x. eval A x) \<Longrightarrow> singleton dfault A = dfault ()"
haftmann@32578
   647
  by (simp add: singleton_def)
haftmann@32578
   648
haftmann@32578
   649
lemma singleton_bot:
bulwahn@33111
   650
  "singleton dfault \<bottom> = dfault ()"
haftmann@32578
   651
  by (auto simp add: bot_pred_def intro: singleton_undefinedI)
haftmann@32578
   652
haftmann@32578
   653
lemma singleton_single:
bulwahn@33110
   654
  "singleton dfault (single x) = x"
haftmann@32578
   655
  by (auto simp add: intro: singleton_eqI singleI elim: singleE)
haftmann@32578
   656
haftmann@32578
   657
lemma singleton_sup_single_single:
bulwahn@33111
   658
  "singleton dfault (single x \<squnion> single y) = (if x = y then x else dfault ())"
haftmann@32578
   659
proof (cases "x = y")
haftmann@32578
   660
  case True then show ?thesis by (simp add: singleton_single)
haftmann@32578
   661
next
haftmann@32578
   662
  case False
haftmann@32578
   663
  have "eval (single x \<squnion> single y) x"
haftmann@32578
   664
    and "eval (single x \<squnion> single y) y"
haftmann@32578
   665
  by (auto intro: supI1 supI2 singleI)
haftmann@32578
   666
  with False have "\<not> (\<exists>!z. eval (single x \<squnion> single y) z)"
haftmann@32578
   667
    by blast
bulwahn@33111
   668
  then have "singleton dfault (single x \<squnion> single y) = dfault ()"
haftmann@32578
   669
    by (rule singleton_undefinedI)
haftmann@32578
   670
  with False show ?thesis by simp
haftmann@32578
   671
qed
haftmann@32578
   672
haftmann@32578
   673
lemma singleton_sup_aux:
bulwahn@33110
   674
  "singleton dfault (A \<squnion> B) = (if A = \<bottom> then singleton dfault B
bulwahn@33110
   675
    else if B = \<bottom> then singleton dfault A
bulwahn@33110
   676
    else singleton dfault
bulwahn@33110
   677
      (single (singleton dfault A) \<squnion> single (singleton dfault B)))"
haftmann@32578
   678
proof (cases "(\<exists>!x. eval A x) \<and> (\<exists>!y. eval B y)")
haftmann@32578
   679
  case True then show ?thesis by (simp add: single_singleton)
haftmann@32578
   680
next
haftmann@32578
   681
  case False
haftmann@32578
   682
  from False have A_or_B:
bulwahn@33111
   683
    "singleton dfault A = dfault () \<or> singleton dfault B = dfault ()"
haftmann@32578
   684
    by (auto intro!: singleton_undefinedI)
bulwahn@33110
   685
  then have rhs: "singleton dfault
bulwahn@33111
   686
    (single (singleton dfault A) \<squnion> single (singleton dfault B)) = dfault ()"
haftmann@32578
   687
    by (auto simp add: singleton_sup_single_single singleton_single)
haftmann@32578
   688
  from False have not_unique:
haftmann@32578
   689
    "\<not> (\<exists>!x. eval A x) \<or> \<not> (\<exists>!y. eval B y)" by simp
haftmann@32578
   690
  show ?thesis proof (cases "A \<noteq> \<bottom> \<and> B \<noteq> \<bottom>")
haftmann@32578
   691
    case True
haftmann@32578
   692
    then obtain a b where a: "eval A a" and b: "eval B b"
haftmann@32578
   693
      by (blast elim: not_bot)
haftmann@32578
   694
    with True not_unique have "\<not> (\<exists>!x. eval (A \<squnion> B) x)"
haftmann@32578
   695
      by (auto simp add: sup_pred_def bot_pred_def)
bulwahn@33111
   696
    then have "singleton dfault (A \<squnion> B) = dfault ()" by (rule singleton_undefinedI)
haftmann@32578
   697
    with True rhs show ?thesis by simp
haftmann@32578
   698
  next
haftmann@32578
   699
    case False then show ?thesis by auto
haftmann@32578
   700
  qed
haftmann@32578
   701
qed
haftmann@32578
   702
haftmann@32578
   703
lemma singleton_sup:
bulwahn@33110
   704
  "singleton dfault (A \<squnion> B) = (if A = \<bottom> then singleton dfault B
bulwahn@33110
   705
    else if B = \<bottom> then singleton dfault A
bulwahn@33111
   706
    else if singleton dfault A = singleton dfault B then singleton dfault A else dfault ())"
bulwahn@33110
   707
using singleton_sup_aux [of dfault A B] by (simp only: singleton_sup_single_single)
haftmann@32578
   708
haftmann@30328
   709
haftmann@30328
   710
subsubsection {* Derived operations *}
haftmann@30328
   711
haftmann@30328
   712
definition if_pred :: "bool \<Rightarrow> unit pred" where
haftmann@30328
   713
  if_pred_eq: "if_pred b = (if b then single () else \<bottom>)"
haftmann@30328
   714
bulwahn@33754
   715
definition holds :: "unit pred \<Rightarrow> bool" where
bulwahn@33754
   716
  holds_eq: "holds P = eval P ()"
bulwahn@33754
   717
haftmann@30328
   718
definition not_pred :: "unit pred \<Rightarrow> unit pred" where
haftmann@30328
   719
  not_pred_eq: "not_pred P = (if eval P () then \<bottom> else single ())"
haftmann@30328
   720
haftmann@30328
   721
lemma if_predI: "P \<Longrightarrow> eval (if_pred P) ()"
haftmann@30328
   722
  unfolding if_pred_eq by (auto intro: singleI)
haftmann@30328
   723
haftmann@30328
   724
lemma if_predE: "eval (if_pred b) x \<Longrightarrow> (b \<Longrightarrow> x = () \<Longrightarrow> P) \<Longrightarrow> P"
haftmann@30328
   725
  unfolding if_pred_eq by (cases b) (auto elim: botE)
haftmann@30328
   726
haftmann@30328
   727
lemma not_predI: "\<not> P \<Longrightarrow> eval (not_pred (Pred (\<lambda>u. P))) ()"
haftmann@30328
   728
  unfolding not_pred_eq eval_pred by (auto intro: singleI)
haftmann@30328
   729
haftmann@30328
   730
lemma not_predI': "\<not> eval P () \<Longrightarrow> eval (not_pred P) ()"
haftmann@30328
   731
  unfolding not_pred_eq by (auto intro: singleI)
haftmann@30328
   732
haftmann@30328
   733
lemma not_predE: "eval (not_pred (Pred (\<lambda>u. P))) x \<Longrightarrow> (\<not> P \<Longrightarrow> thesis) \<Longrightarrow> thesis"
haftmann@30328
   734
  unfolding not_pred_eq
haftmann@30328
   735
  by (auto split: split_if_asm elim: botE)
haftmann@30328
   736
haftmann@30328
   737
lemma not_predE': "eval (not_pred P) x \<Longrightarrow> (\<not> eval P x \<Longrightarrow> thesis) \<Longrightarrow> thesis"
haftmann@30328
   738
  unfolding not_pred_eq
haftmann@30328
   739
  by (auto split: split_if_asm elim: botE)
bulwahn@33754
   740
lemma "f () = False \<or> f () = True"
bulwahn@33754
   741
by simp
haftmann@30328
   742
blanchet@37545
   743
lemma closure_of_bool_cases [no_atp]:
haftmann@44878
   744
  fixes f :: "unit \<Rightarrow> bool"
haftmann@44878
   745
  assumes "f = (\<lambda>u. False) \<Longrightarrow> P f"
haftmann@44878
   746
  assumes "f = (\<lambda>u. True) \<Longrightarrow> P f"
haftmann@44878
   747
  shows "P f"
bulwahn@33754
   748
proof -
haftmann@44878
   749
  have "f = (\<lambda>u. False) \<or> f = (\<lambda>u. True)"
bulwahn@33754
   750
    apply (cases "f ()")
bulwahn@33754
   751
    apply (rule disjI2)
bulwahn@33754
   752
    apply (rule ext)
bulwahn@33754
   753
    apply (simp add: unit_eq)
bulwahn@33754
   754
    apply (rule disjI1)
bulwahn@33754
   755
    apply (rule ext)
bulwahn@33754
   756
    apply (simp add: unit_eq)
bulwahn@33754
   757
    done
wenzelm@41798
   758
  from this assms show ?thesis by blast
bulwahn@33754
   759
qed
bulwahn@33754
   760
bulwahn@33754
   761
lemma unit_pred_cases:
haftmann@44878
   762
  assumes "P \<bottom>"
haftmann@44878
   763
  assumes "P (single ())"
haftmann@44878
   764
  shows "P Q"
haftmann@44878
   765
using assms unfolding bot_pred_def Collect_def empty_def single_def proof (cases Q)
haftmann@44878
   766
  fix f
haftmann@44878
   767
  assume "P (Pred (\<lambda>u. False))" "P (Pred (\<lambda>u. () = u))"
haftmann@44878
   768
  then have "P (Pred f)" 
haftmann@44878
   769
    by (cases _ f rule: closure_of_bool_cases) simp_all
haftmann@44878
   770
  moreover assume "Q = Pred f"
haftmann@44878
   771
  ultimately show "P Q" by simp
haftmann@44878
   772
qed
haftmann@44878
   773
  
bulwahn@33754
   774
lemma holds_if_pred:
bulwahn@33754
   775
  "holds (if_pred b) = b"
bulwahn@33754
   776
unfolding if_pred_eq holds_eq
bulwahn@33754
   777
by (cases b) (auto intro: singleI elim: botE)
bulwahn@33754
   778
bulwahn@33754
   779
lemma if_pred_holds:
bulwahn@33754
   780
  "if_pred (holds P) = P"
bulwahn@33754
   781
unfolding if_pred_eq holds_eq
bulwahn@33754
   782
by (rule unit_pred_cases) (auto intro: singleI elim: botE)
bulwahn@33754
   783
bulwahn@33754
   784
lemma is_empty_holds:
bulwahn@33754
   785
  "is_empty P \<longleftrightarrow> \<not> holds P"
bulwahn@33754
   786
unfolding is_empty_def holds_eq
bulwahn@33754
   787
by (rule unit_pred_cases) (auto elim: botE intro: singleI)
haftmann@30328
   788
haftmann@41559
   789
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred \<Rightarrow> 'b pred" where
haftmann@41559
   790
  "map f P = P \<guillemotright>= (single o f)"
haftmann@41559
   791
haftmann@41559
   792
lemma eval_map [simp]:
haftmann@41559
   793
  "eval (map f P) = image f (eval P)"
haftmann@41559
   794
  by (auto simp add: map_def)
haftmann@41559
   795
haftmann@41752
   796
enriched_type map: map
haftmann@41620
   797
  by (auto intro!: pred_eqI simp add: fun_eq_iff image_compose)
haftmann@41559
   798
haftmann@41559
   799
haftmann@30328
   800
subsubsection {* Implementation *}
haftmann@30328
   801
haftmann@30328
   802
datatype 'a seq = Empty | Insert "'a" "'a pred" | Join "'a pred" "'a seq"
haftmann@30328
   803
haftmann@30328
   804
primrec pred_of_seq :: "'a seq \<Rightarrow> 'a pred" where
haftmann@30328
   805
    "pred_of_seq Empty = \<bottom>"
haftmann@30328
   806
  | "pred_of_seq (Insert x P) = single x \<squnion> P"
haftmann@30328
   807
  | "pred_of_seq (Join P xq) = P \<squnion> pred_of_seq xq"
haftmann@30328
   808
haftmann@30328
   809
definition Seq :: "(unit \<Rightarrow> 'a seq) \<Rightarrow> 'a pred" where
haftmann@30328
   810
  "Seq f = pred_of_seq (f ())"
haftmann@30328
   811
haftmann@30328
   812
code_datatype Seq
haftmann@30328
   813
haftmann@30328
   814
primrec member :: "'a seq \<Rightarrow> 'a \<Rightarrow> bool"  where
haftmann@30328
   815
  "member Empty x \<longleftrightarrow> False"
haftmann@30328
   816
  | "member (Insert y P) x \<longleftrightarrow> x = y \<or> eval P x"
haftmann@30328
   817
  | "member (Join P xq) x \<longleftrightarrow> eval P x \<or> member xq x"
haftmann@30328
   818
haftmann@30328
   819
lemma eval_member:
haftmann@30328
   820
  "member xq = eval (pred_of_seq xq)"
haftmann@30328
   821
proof (induct xq)
haftmann@30328
   822
  case Empty show ?case
nipkow@39535
   823
  by (auto simp add: fun_eq_iff elim: botE)
haftmann@30328
   824
next
haftmann@30328
   825
  case Insert show ?case
nipkow@39535
   826
  by (auto simp add: fun_eq_iff elim: supE singleE intro: supI1 supI2 singleI)
haftmann@30328
   827
next
haftmann@30328
   828
  case Join then show ?case
nipkow@39535
   829
  by (auto simp add: fun_eq_iff elim: supE intro: supI1 supI2)
haftmann@30328
   830
qed
haftmann@30328
   831
haftmann@30328
   832
lemma eval_code [code]: "eval (Seq f) = member (f ())"
haftmann@30328
   833
  unfolding Seq_def by (rule sym, rule eval_member)
haftmann@30328
   834
haftmann@30328
   835
lemma single_code [code]:
haftmann@30328
   836
  "single x = Seq (\<lambda>u. Insert x \<bottom>)"
haftmann@30328
   837
  unfolding Seq_def by simp
haftmann@30328
   838
haftmann@41328
   839
primrec "apply" :: "('a \<Rightarrow> 'b pred) \<Rightarrow> 'a seq \<Rightarrow> 'b seq" where
haftmann@30328
   840
    "apply f Empty = Empty"
haftmann@30328
   841
  | "apply f (Insert x P) = Join (f x) (Join (P \<guillemotright>= f) Empty)"
haftmann@30328
   842
  | "apply f (Join P xq) = Join (P \<guillemotright>= f) (apply f xq)"
haftmann@30328
   843
haftmann@30328
   844
lemma apply_bind:
haftmann@30328
   845
  "pred_of_seq (apply f xq) = pred_of_seq xq \<guillemotright>= f"
haftmann@30328
   846
proof (induct xq)
haftmann@30328
   847
  case Empty show ?case
haftmann@30328
   848
    by (simp add: bottom_bind)
haftmann@30328
   849
next
haftmann@30328
   850
  case Insert show ?case
haftmann@30328
   851
    by (simp add: single_bind sup_bind)
haftmann@30328
   852
next
haftmann@30328
   853
  case Join then show ?case
haftmann@30328
   854
    by (simp add: sup_bind)
haftmann@30328
   855
qed
haftmann@30328
   856
  
haftmann@30328
   857
lemma bind_code [code]:
haftmann@30328
   858
  "Seq g \<guillemotright>= f = Seq (\<lambda>u. apply f (g ()))"
haftmann@30328
   859
  unfolding Seq_def by (rule sym, rule apply_bind)
haftmann@30328
   860
haftmann@30328
   861
lemma bot_set_code [code]:
haftmann@30328
   862
  "\<bottom> = Seq (\<lambda>u. Empty)"
haftmann@30328
   863
  unfolding Seq_def by simp
haftmann@30328
   864
haftmann@30376
   865
primrec adjunct :: "'a pred \<Rightarrow> 'a seq \<Rightarrow> 'a seq" where
haftmann@30376
   866
    "adjunct P Empty = Join P Empty"
haftmann@30376
   867
  | "adjunct P (Insert x Q) = Insert x (Q \<squnion> P)"
haftmann@30376
   868
  | "adjunct P (Join Q xq) = Join Q (adjunct P xq)"
haftmann@30376
   869
haftmann@30376
   870
lemma adjunct_sup:
haftmann@30376
   871
  "pred_of_seq (adjunct P xq) = P \<squnion> pred_of_seq xq"
haftmann@30376
   872
  by (induct xq) (simp_all add: sup_assoc sup_commute sup_left_commute)
haftmann@30376
   873
haftmann@30328
   874
lemma sup_code [code]:
haftmann@30328
   875
  "Seq f \<squnion> Seq g = Seq (\<lambda>u. case f ()
haftmann@30328
   876
    of Empty \<Rightarrow> g ()
haftmann@30328
   877
     | Insert x P \<Rightarrow> Insert x (P \<squnion> Seq g)
haftmann@30376
   878
     | Join P xq \<Rightarrow> adjunct (Seq g) (Join P xq))"
haftmann@30328
   879
proof (cases "f ()")
haftmann@30328
   880
  case Empty
haftmann@30328
   881
  thus ?thesis
haftmann@33998
   882
    unfolding Seq_def by (simp add: sup_commute [of "\<bottom>"])
haftmann@30328
   883
next
haftmann@30328
   884
  case Insert
haftmann@30328
   885
  thus ?thesis
haftmann@30328
   886
    unfolding Seq_def by (simp add: sup_assoc)
haftmann@30328
   887
next
haftmann@30328
   888
  case Join
haftmann@30328
   889
  thus ?thesis
haftmann@30376
   890
    unfolding Seq_def
haftmann@30376
   891
    by (simp add: adjunct_sup sup_assoc sup_commute sup_left_commute)
haftmann@30328
   892
qed
haftmann@30328
   893
haftmann@30430
   894
primrec contained :: "'a seq \<Rightarrow> 'a pred \<Rightarrow> bool" where
haftmann@30430
   895
    "contained Empty Q \<longleftrightarrow> True"
haftmann@30430
   896
  | "contained (Insert x P) Q \<longleftrightarrow> eval Q x \<and> P \<le> Q"
haftmann@30430
   897
  | "contained (Join P xq) Q \<longleftrightarrow> P \<le> Q \<and> contained xq Q"
haftmann@30430
   898
haftmann@30430
   899
lemma single_less_eq_eval:
haftmann@30430
   900
  "single x \<le> P \<longleftrightarrow> eval P x"
haftmann@30430
   901
  by (auto simp add: single_def less_eq_pred_def mem_def)
haftmann@30430
   902
haftmann@30430
   903
lemma contained_less_eq:
haftmann@30430
   904
  "contained xq Q \<longleftrightarrow> pred_of_seq xq \<le> Q"
haftmann@30430
   905
  by (induct xq) (simp_all add: single_less_eq_eval)
haftmann@30430
   906
haftmann@30430
   907
lemma less_eq_pred_code [code]:
haftmann@30430
   908
  "Seq f \<le> Q = (case f ()
haftmann@30430
   909
   of Empty \<Rightarrow> True
haftmann@30430
   910
    | Insert x P \<Rightarrow> eval Q x \<and> P \<le> Q
haftmann@30430
   911
    | Join P xq \<Rightarrow> P \<le> Q \<and> contained xq Q)"
haftmann@30430
   912
  by (cases "f ()")
haftmann@30430
   913
    (simp_all add: Seq_def single_less_eq_eval contained_less_eq)
haftmann@30430
   914
haftmann@30430
   915
lemma eq_pred_code [code]:
haftmann@31133
   916
  fixes P Q :: "'a pred"
haftmann@39086
   917
  shows "HOL.equal P Q \<longleftrightarrow> P \<le> Q \<and> Q \<le> P"
haftmann@39086
   918
  by (auto simp add: equal)
haftmann@39086
   919
haftmann@39086
   920
lemma [code nbe]:
haftmann@39086
   921
  "HOL.equal (x :: 'a pred) x \<longleftrightarrow> True"
haftmann@39086
   922
  by (fact equal_refl)
haftmann@30430
   923
haftmann@30430
   924
lemma [code]:
haftmann@30430
   925
  "pred_case f P = f (eval P)"
haftmann@30430
   926
  by (cases P) simp
haftmann@30430
   927
haftmann@30430
   928
lemma [code]:
haftmann@30430
   929
  "pred_rec f P = f (eval P)"
haftmann@30430
   930
  by (cases P) simp
haftmann@30328
   931
bulwahn@31105
   932
inductive eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where "eq x x"
bulwahn@31105
   933
bulwahn@31105
   934
lemma eq_is_eq: "eq x y \<equiv> (x = y)"
haftmann@31108
   935
  by (rule eq_reflection) (auto intro: eq.intros elim: eq.cases)
haftmann@30948
   936
haftmann@32578
   937
primrec null :: "'a seq \<Rightarrow> bool" where
haftmann@32578
   938
    "null Empty \<longleftrightarrow> True"
haftmann@32578
   939
  | "null (Insert x P) \<longleftrightarrow> False"
haftmann@32578
   940
  | "null (Join P xq) \<longleftrightarrow> is_empty P \<and> null xq"
haftmann@32578
   941
haftmann@32578
   942
lemma null_is_empty:
haftmann@32578
   943
  "null xq \<longleftrightarrow> is_empty (pred_of_seq xq)"
haftmann@32578
   944
  by (induct xq) (simp_all add: is_empty_bot not_is_empty_single is_empty_sup)
haftmann@32578
   945
haftmann@32578
   946
lemma is_empty_code [code]:
haftmann@32578
   947
  "is_empty (Seq f) \<longleftrightarrow> null (f ())"
haftmann@32578
   948
  by (simp add: null_is_empty Seq_def)
haftmann@32578
   949
bulwahn@33111
   950
primrec the_only :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a seq \<Rightarrow> 'a" where
bulwahn@33111
   951
  [code del]: "the_only dfault Empty = dfault ()"
bulwahn@33111
   952
  | "the_only dfault (Insert x P) = (if is_empty P then x else let y = singleton dfault P in if x = y then x else dfault ())"
bulwahn@33110
   953
  | "the_only dfault (Join P xq) = (if is_empty P then the_only dfault xq else if null xq then singleton dfault P
bulwahn@33110
   954
       else let x = singleton dfault P; y = the_only dfault xq in
bulwahn@33111
   955
       if x = y then x else dfault ())"
haftmann@32578
   956
haftmann@32578
   957
lemma the_only_singleton:
bulwahn@33110
   958
  "the_only dfault xq = singleton dfault (pred_of_seq xq)"
haftmann@32578
   959
  by (induct xq)
haftmann@32578
   960
    (auto simp add: singleton_bot singleton_single is_empty_def
haftmann@32578
   961
    null_is_empty Let_def singleton_sup)
haftmann@32578
   962
haftmann@32578
   963
lemma singleton_code [code]:
bulwahn@33110
   964
  "singleton dfault (Seq f) = (case f ()
bulwahn@33111
   965
   of Empty \<Rightarrow> dfault ()
haftmann@32578
   966
    | Insert x P \<Rightarrow> if is_empty P then x
bulwahn@33110
   967
        else let y = singleton dfault P in
bulwahn@33111
   968
          if x = y then x else dfault ()
bulwahn@33110
   969
    | Join P xq \<Rightarrow> if is_empty P then the_only dfault xq
bulwahn@33110
   970
        else if null xq then singleton dfault P
bulwahn@33110
   971
        else let x = singleton dfault P; y = the_only dfault xq in
bulwahn@33111
   972
          if x = y then x else dfault ())"
haftmann@32578
   973
  by (cases "f ()")
haftmann@32578
   974
   (auto simp add: Seq_def the_only_singleton is_empty_def
haftmann@32578
   975
      null_is_empty singleton_bot singleton_single singleton_sup Let_def)
haftmann@32578
   976
bulwahn@33110
   977
definition not_unique :: "'a pred => 'a"
bulwahn@33110
   978
where
bulwahn@33111
   979
  [code del]: "not_unique A = (THE x. eval A x)"
bulwahn@33110
   980
bulwahn@33111
   981
definition the :: "'a pred => 'a"
bulwahn@33111
   982
where
haftmann@37767
   983
  "the A = (THE x. eval A x)"
bulwahn@33111
   984
haftmann@40922
   985
lemma the_eqI:
haftmann@41328
   986
  "(THE x. eval P x) = x \<Longrightarrow> the P = x"
haftmann@40922
   987
  by (simp add: the_def)
haftmann@40922
   988
haftmann@40922
   989
lemma the_eq [code]: "the A = singleton (\<lambda>x. not_unique A) A"
haftmann@40922
   990
  by (rule the_eqI) (simp add: singleton_def not_unique_def)
bulwahn@33110
   991
haftmann@33985
   992
code_abort not_unique
haftmann@33985
   993
haftmann@36531
   994
code_reflect Predicate
haftmann@36506
   995
  datatypes pred = Seq and seq = Empty | Insert | Join
haftmann@36506
   996
  functions map
haftmann@36506
   997
haftmann@30948
   998
ML {*
haftmann@30948
   999
signature PREDICATE =
haftmann@30948
  1000
sig
haftmann@30948
  1001
  datatype 'a pred = Seq of (unit -> 'a seq)
haftmann@30948
  1002
  and 'a seq = Empty | Insert of 'a * 'a pred | Join of 'a pred * 'a seq
haftmann@30959
  1003
  val yield: 'a pred -> ('a * 'a pred) option
haftmann@30959
  1004
  val yieldn: int -> 'a pred -> 'a list * 'a pred
haftmann@31222
  1005
  val map: ('a -> 'b) -> 'a pred -> 'b pred
haftmann@30948
  1006
end;
haftmann@30948
  1007
haftmann@30948
  1008
structure Predicate : PREDICATE =
haftmann@30948
  1009
struct
haftmann@30948
  1010
haftmann@36506
  1011
datatype pred = datatype Predicate.pred
haftmann@36506
  1012
datatype seq = datatype Predicate.seq
haftmann@30959
  1013
haftmann@36506
  1014
fun map f = Predicate.map f;
haftmann@36506
  1015
haftmann@36506
  1016
fun yield (Seq f) = next (f ())
haftmann@36506
  1017
and next Empty = NONE
haftmann@36506
  1018
  | next (Insert (x, P)) = SOME (x, P)
haftmann@36506
  1019
  | next (Join (P, xq)) = (case yield P
haftmann@30959
  1020
     of NONE => next xq
haftmann@36506
  1021
      | SOME (x, Q) => SOME (x, Seq (fn _ => Join (Q, xq))));
haftmann@30959
  1022
haftmann@30959
  1023
fun anamorph f k x = (if k = 0 then ([], x)
haftmann@30959
  1024
  else case f x
haftmann@30959
  1025
   of NONE => ([], x)
haftmann@30959
  1026
    | SOME (v, y) => let
haftmann@30959
  1027
        val (vs, z) = anamorph f (k - 1) y
haftmann@33607
  1028
      in (v :: vs, z) end);
haftmann@30959
  1029
haftmann@30959
  1030
fun yieldn P = anamorph yield P;
haftmann@30948
  1031
haftmann@30948
  1032
end;
haftmann@30948
  1033
*}
haftmann@30948
  1034
haftmann@30328
  1035
no_notation
haftmann@41330
  1036
  bot ("\<bottom>") and
haftmann@41330
  1037
  top ("\<top>") and
haftmann@30328
  1038
  inf (infixl "\<sqinter>" 70) and
haftmann@30328
  1039
  sup (infixl "\<squnion>" 65) and
haftmann@30328
  1040
  Inf ("\<Sqinter>_" [900] 900) and
haftmann@30328
  1041
  Sup ("\<Squnion>_" [900] 900) and
haftmann@30328
  1042
  bind (infixl "\<guillemotright>=" 70)
haftmann@30328
  1043
haftmann@41328
  1044
no_syntax (xsymbols)
haftmann@41330
  1045
  "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
haftmann@41330
  1046
  "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@41328
  1047
  "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
haftmann@41328
  1048
  "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
haftmann@41328
  1049
wenzelm@36176
  1050
hide_type (open) pred seq
wenzelm@36176
  1051
hide_const (open) Pred eval single bind is_empty singleton if_pred not_pred holds
bulwahn@33111
  1052
  Empty Insert Join Seq member pred_of_seq "apply" adjunct null the_only eq map not_unique the
haftmann@30328
  1053
haftmann@30328
  1054
end