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