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