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