src/HOL/Predicate.thy
author haftmann
Fri Nov 27 08:41:10 2009 +0100 (2009-11-27)
changeset 33963 977b94b64905
parent 33754 f2957bd46faf
child 33988 901001414358
permissions -rw-r--r--
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
     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 :: "(unit => '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 holds :: "unit pred \<Rightarrow> bool" where
   574   holds_eq: "holds P = eval P ()"
   575 
   576 definition not_pred :: "unit pred \<Rightarrow> unit pred" where
   577   not_pred_eq: "not_pred P = (if eval P () then \<bottom> else single ())"
   578 
   579 lemma if_predI: "P \<Longrightarrow> eval (if_pred P) ()"
   580   unfolding if_pred_eq by (auto intro: singleI)
   581 
   582 lemma if_predE: "eval (if_pred b) x \<Longrightarrow> (b \<Longrightarrow> x = () \<Longrightarrow> P) \<Longrightarrow> P"
   583   unfolding if_pred_eq by (cases b) (auto elim: botE)
   584 
   585 lemma not_predI: "\<not> P \<Longrightarrow> eval (not_pred (Pred (\<lambda>u. P))) ()"
   586   unfolding not_pred_eq eval_pred by (auto intro: singleI)
   587 
   588 lemma not_predI': "\<not> eval P () \<Longrightarrow> eval (not_pred P) ()"
   589   unfolding not_pred_eq by (auto intro: singleI)
   590 
   591 lemma not_predE: "eval (not_pred (Pred (\<lambda>u. P))) x \<Longrightarrow> (\<not> P \<Longrightarrow> thesis) \<Longrightarrow> thesis"
   592   unfolding not_pred_eq
   593   by (auto split: split_if_asm elim: botE)
   594 
   595 lemma not_predE': "eval (not_pred P) x \<Longrightarrow> (\<not> eval P x \<Longrightarrow> thesis) \<Longrightarrow> thesis"
   596   unfolding not_pred_eq
   597   by (auto split: split_if_asm elim: botE)
   598 lemma "f () = False \<or> f () = True"
   599 by simp
   600 
   601 lemma closure_of_bool_cases:
   602 assumes "(f :: unit \<Rightarrow> bool) = (%u. False) \<Longrightarrow> P f"
   603 assumes "f = (%u. True) \<Longrightarrow> P f"
   604 shows "P f"
   605 proof -
   606   have "f = (%u. False) \<or> f = (%u. True)"
   607     apply (cases "f ()")
   608     apply (rule disjI2)
   609     apply (rule ext)
   610     apply (simp add: unit_eq)
   611     apply (rule disjI1)
   612     apply (rule ext)
   613     apply (simp add: unit_eq)
   614     done
   615   from this prems show ?thesis by blast
   616 qed
   617 
   618 lemma unit_pred_cases:
   619 assumes "P \<bottom>"
   620 assumes "P (single ())"
   621 shows "P Q"
   622 using assms
   623 unfolding bot_pred_def Collect_def empty_def single_def
   624 apply (cases Q)
   625 apply simp
   626 apply (rule_tac f="fun" in closure_of_bool_cases)
   627 apply auto
   628 apply (subgoal_tac "(%x. () = x) = (%x. True)") 
   629 apply auto
   630 done
   631 
   632 lemma holds_if_pred:
   633   "holds (if_pred b) = b"
   634 unfolding if_pred_eq holds_eq
   635 by (cases b) (auto intro: singleI elim: botE)
   636 
   637 lemma if_pred_holds:
   638   "if_pred (holds P) = P"
   639 unfolding if_pred_eq holds_eq
   640 by (rule unit_pred_cases) (auto intro: singleI elim: botE)
   641 
   642 lemma is_empty_holds:
   643   "is_empty P \<longleftrightarrow> \<not> holds P"
   644 unfolding is_empty_def holds_eq
   645 by (rule unit_pred_cases) (auto elim: botE intro: singleI)
   646 
   647 subsubsection {* Implementation *}
   648 
   649 datatype 'a seq = Empty | Insert "'a" "'a pred" | Join "'a pred" "'a seq"
   650 
   651 primrec pred_of_seq :: "'a seq \<Rightarrow> 'a pred" where
   652     "pred_of_seq Empty = \<bottom>"
   653   | "pred_of_seq (Insert x P) = single x \<squnion> P"
   654   | "pred_of_seq (Join P xq) = P \<squnion> pred_of_seq xq"
   655 
   656 definition Seq :: "(unit \<Rightarrow> 'a seq) \<Rightarrow> 'a pred" where
   657   "Seq f = pred_of_seq (f ())"
   658 
   659 code_datatype Seq
   660 
   661 primrec member :: "'a seq \<Rightarrow> 'a \<Rightarrow> bool"  where
   662   "member Empty x \<longleftrightarrow> False"
   663   | "member (Insert y P) x \<longleftrightarrow> x = y \<or> eval P x"
   664   | "member (Join P xq) x \<longleftrightarrow> eval P x \<or> member xq x"
   665 
   666 lemma eval_member:
   667   "member xq = eval (pred_of_seq xq)"
   668 proof (induct xq)
   669   case Empty show ?case
   670   by (auto simp add: expand_fun_eq elim: botE)
   671 next
   672   case Insert show ?case
   673   by (auto simp add: expand_fun_eq elim: supE singleE intro: supI1 supI2 singleI)
   674 next
   675   case Join then show ?case
   676   by (auto simp add: expand_fun_eq elim: supE intro: supI1 supI2)
   677 qed
   678 
   679 lemma eval_code [code]: "eval (Seq f) = member (f ())"
   680   unfolding Seq_def by (rule sym, rule eval_member)
   681 
   682 lemma single_code [code]:
   683   "single x = Seq (\<lambda>u. Insert x \<bottom>)"
   684   unfolding Seq_def by simp
   685 
   686 primrec "apply" :: "('a \<Rightarrow> 'b Predicate.pred) \<Rightarrow> 'a seq \<Rightarrow> 'b seq" where
   687     "apply f Empty = Empty"
   688   | "apply f (Insert x P) = Join (f x) (Join (P \<guillemotright>= f) Empty)"
   689   | "apply f (Join P xq) = Join (P \<guillemotright>= f) (apply f xq)"
   690 
   691 lemma apply_bind:
   692   "pred_of_seq (apply f xq) = pred_of_seq xq \<guillemotright>= f"
   693 proof (induct xq)
   694   case Empty show ?case
   695     by (simp add: bottom_bind)
   696 next
   697   case Insert show ?case
   698     by (simp add: single_bind sup_bind)
   699 next
   700   case Join then show ?case
   701     by (simp add: sup_bind)
   702 qed
   703   
   704 lemma bind_code [code]:
   705   "Seq g \<guillemotright>= f = Seq (\<lambda>u. apply f (g ()))"
   706   unfolding Seq_def by (rule sym, rule apply_bind)
   707 
   708 lemma bot_set_code [code]:
   709   "\<bottom> = Seq (\<lambda>u. Empty)"
   710   unfolding Seq_def by simp
   711 
   712 primrec adjunct :: "'a pred \<Rightarrow> 'a seq \<Rightarrow> 'a seq" where
   713     "adjunct P Empty = Join P Empty"
   714   | "adjunct P (Insert x Q) = Insert x (Q \<squnion> P)"
   715   | "adjunct P (Join Q xq) = Join Q (adjunct P xq)"
   716 
   717 lemma adjunct_sup:
   718   "pred_of_seq (adjunct P xq) = P \<squnion> pred_of_seq xq"
   719   by (induct xq) (simp_all add: sup_assoc sup_commute sup_left_commute)
   720 
   721 lemma sup_code [code]:
   722   "Seq f \<squnion> Seq g = Seq (\<lambda>u. case f ()
   723     of Empty \<Rightarrow> g ()
   724      | Insert x P \<Rightarrow> Insert x (P \<squnion> Seq g)
   725      | Join P xq \<Rightarrow> adjunct (Seq g) (Join P xq))"
   726 proof (cases "f ()")
   727   case Empty
   728   thus ?thesis
   729     unfolding Seq_def by (simp add: sup_commute [of "\<bottom>"]  sup_bot)
   730 next
   731   case Insert
   732   thus ?thesis
   733     unfolding Seq_def by (simp add: sup_assoc)
   734 next
   735   case Join
   736   thus ?thesis
   737     unfolding Seq_def
   738     by (simp add: adjunct_sup sup_assoc sup_commute sup_left_commute)
   739 qed
   740 
   741 primrec contained :: "'a seq \<Rightarrow> 'a pred \<Rightarrow> bool" where
   742     "contained Empty Q \<longleftrightarrow> True"
   743   | "contained (Insert x P) Q \<longleftrightarrow> eval Q x \<and> P \<le> Q"
   744   | "contained (Join P xq) Q \<longleftrightarrow> P \<le> Q \<and> contained xq Q"
   745 
   746 lemma single_less_eq_eval:
   747   "single x \<le> P \<longleftrightarrow> eval P x"
   748   by (auto simp add: single_def less_eq_pred_def mem_def)
   749 
   750 lemma contained_less_eq:
   751   "contained xq Q \<longleftrightarrow> pred_of_seq xq \<le> Q"
   752   by (induct xq) (simp_all add: single_less_eq_eval)
   753 
   754 lemma less_eq_pred_code [code]:
   755   "Seq f \<le> Q = (case f ()
   756    of Empty \<Rightarrow> True
   757     | Insert x P \<Rightarrow> eval Q x \<and> P \<le> Q
   758     | Join P xq \<Rightarrow> P \<le> Q \<and> contained xq Q)"
   759   by (cases "f ()")
   760     (simp_all add: Seq_def single_less_eq_eval contained_less_eq)
   761 
   762 lemma eq_pred_code [code]:
   763   fixes P Q :: "'a pred"
   764   shows "eq_class.eq P Q \<longleftrightarrow> P \<le> Q \<and> Q \<le> P"
   765   unfolding eq by auto
   766 
   767 lemma [code]:
   768   "pred_case f P = f (eval P)"
   769   by (cases P) simp
   770 
   771 lemma [code]:
   772   "pred_rec f P = f (eval P)"
   773   by (cases P) simp
   774 
   775 inductive eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where "eq x x"
   776 
   777 lemma eq_is_eq: "eq x y \<equiv> (x = y)"
   778   by (rule eq_reflection) (auto intro: eq.intros elim: eq.cases)
   779 
   780 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred \<Rightarrow> 'b pred" where
   781   "map f P = P \<guillemotright>= (single o f)"
   782 
   783 primrec null :: "'a seq \<Rightarrow> bool" where
   784     "null Empty \<longleftrightarrow> True"
   785   | "null (Insert x P) \<longleftrightarrow> False"
   786   | "null (Join P xq) \<longleftrightarrow> is_empty P \<and> null xq"
   787 
   788 lemma null_is_empty:
   789   "null xq \<longleftrightarrow> is_empty (pred_of_seq xq)"
   790   by (induct xq) (simp_all add: is_empty_bot not_is_empty_single is_empty_sup)
   791 
   792 lemma is_empty_code [code]:
   793   "is_empty (Seq f) \<longleftrightarrow> null (f ())"
   794   by (simp add: null_is_empty Seq_def)
   795 
   796 primrec the_only :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a seq \<Rightarrow> 'a" where
   797   [code del]: "the_only dfault Empty = dfault ()"
   798   | "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 ())"
   799   | "the_only dfault (Join P xq) = (if is_empty P then the_only dfault xq else if null xq then singleton dfault P
   800        else let x = singleton dfault P; y = the_only dfault xq in
   801        if x = y then x else dfault ())"
   802 
   803 lemma the_only_singleton:
   804   "the_only dfault xq = singleton dfault (pred_of_seq xq)"
   805   by (induct xq)
   806     (auto simp add: singleton_bot singleton_single is_empty_def
   807     null_is_empty Let_def singleton_sup)
   808 
   809 lemma singleton_code [code]:
   810   "singleton dfault (Seq f) = (case f ()
   811    of Empty \<Rightarrow> dfault ()
   812     | Insert x P \<Rightarrow> if is_empty P then x
   813         else let y = singleton dfault P in
   814           if x = y then x else dfault ()
   815     | Join P xq \<Rightarrow> if is_empty P then the_only dfault xq
   816         else if null xq then singleton dfault P
   817         else let x = singleton dfault P; y = the_only dfault xq in
   818           if x = y then x else dfault ())"
   819   by (cases "f ()")
   820    (auto simp add: Seq_def the_only_singleton is_empty_def
   821       null_is_empty singleton_bot singleton_single singleton_sup Let_def)
   822 
   823 definition not_unique :: "'a pred => 'a"
   824 where
   825   [code del]: "not_unique A = (THE x. eval A x)"
   826 
   827 definition the :: "'a pred => 'a"
   828 where
   829   [code del]: "the A = (THE x. eval A x)"
   830 
   831 lemma the_eq[code]: "the A = singleton (\<lambda>x. not_unique A) A"
   832 by (auto simp add: the_def singleton_def not_unique_def)
   833 
   834 ML {*
   835 signature PREDICATE =
   836 sig
   837   datatype 'a pred = Seq of (unit -> 'a seq)
   838   and 'a seq = Empty | Insert of 'a * 'a pred | Join of 'a pred * 'a seq
   839   val yield: 'a pred -> ('a * 'a pred) option
   840   val yieldn: int -> 'a pred -> 'a list * 'a pred
   841   val map: ('a -> 'b) -> 'a pred -> 'b pred
   842 end;
   843 
   844 structure Predicate : PREDICATE =
   845 struct
   846 
   847 @{code_datatype pred = Seq};
   848 @{code_datatype seq = Empty | Insert | Join};
   849 
   850 fun yield (@{code Seq} f) = next (f ())
   851 and next @{code Empty} = NONE
   852   | next (@{code Insert} (x, P)) = SOME (x, P)
   853   | next (@{code Join} (P, xq)) = (case yield P
   854      of NONE => next xq
   855       | SOME (x, Q) => SOME (x, @{code Seq} (fn _ => @{code Join} (Q, xq))));
   856 
   857 fun anamorph f k x = (if k = 0 then ([], x)
   858   else case f x
   859    of NONE => ([], x)
   860     | SOME (v, y) => let
   861         val (vs, z) = anamorph f (k - 1) y
   862       in (v :: vs, z) end);
   863 
   864 fun yieldn P = anamorph yield P;
   865 
   866 fun map f = @{code map} f;
   867 
   868 end;
   869 *}
   870 
   871 code_reserved Eval Predicate
   872 
   873 code_type pred and seq
   874   (Eval "_/ Predicate.pred" and "_/ Predicate.seq")
   875 
   876 code_const Seq and Empty and Insert and Join
   877   (Eval "Predicate.Seq" and "Predicate.Empty" and "Predicate.Insert/ (_,/ _)" and "Predicate.Join/ (_,/ _)")
   878 
   879 code_abort not_unique
   880 
   881 no_notation
   882   inf (infixl "\<sqinter>" 70) and
   883   sup (infixl "\<squnion>" 65) and
   884   Inf ("\<Sqinter>_" [900] 900) and
   885   Sup ("\<Squnion>_" [900] 900) and
   886   top ("\<top>") and
   887   bot ("\<bottom>") and
   888   bind (infixl "\<guillemotright>=" 70)
   889 
   890 hide (open) type pred seq
   891 hide (open) const Pred eval single bind is_empty singleton if_pred not_pred holds
   892   Empty Insert Join Seq member pred_of_seq "apply" adjunct null the_only eq map not_unique the
   893 
   894 end