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