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