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