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