src/HOL/Predicate.thy
author wenzelm
Fri Mar 07 22:30:58 2014 +0100 (2014-03-07)
changeset 55990 41c6b99c5fb7
parent 55467 a5c9002bc54d
child 56154 f0a927235162
permissions -rw-r--r--
more antiquotations;
     1 (*  Title:      HOL/Predicate.thy
     2     Author:     Lukas Bulwahn and Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* Predicates as enumerations *}
     6 
     7 theory Predicate
     8 imports String
     9 begin
    10 
    11 subsection {* The type of predicate enumerations (a monad) *}
    12 
    13 datatype 'a pred = Pred "'a \<Rightarrow> bool"
    14 
    15 primrec eval :: "'a pred \<Rightarrow> 'a \<Rightarrow> bool" where
    16   eval_pred: "eval (Pred f) = f"
    17 
    18 lemma Pred_eval [simp]:
    19   "Pred (eval x) = x"
    20   by (cases x) simp
    21 
    22 lemma pred_eqI:
    23   "(\<And>w. eval P w \<longleftrightarrow> eval Q w) \<Longrightarrow> P = Q"
    24   by (cases P, cases Q) (auto simp add: fun_eq_iff)
    25 
    26 lemma pred_eq_iff:
    27   "P = Q \<Longrightarrow> (\<And>w. eval P w \<longleftrightarrow> eval Q w)"
    28   by (simp add: pred_eqI)
    29 
    30 instantiation pred :: (type) complete_lattice
    31 begin
    32 
    33 definition
    34   "P \<le> Q \<longleftrightarrow> eval P \<le> eval Q"
    35 
    36 definition
    37   "P < Q \<longleftrightarrow> eval P < eval Q"
    38 
    39 definition
    40   "\<bottom> = Pred \<bottom>"
    41 
    42 lemma eval_bot [simp]:
    43   "eval \<bottom>  = \<bottom>"
    44   by (simp add: bot_pred_def)
    45 
    46 definition
    47   "\<top> = Pred \<top>"
    48 
    49 lemma eval_top [simp]:
    50   "eval \<top>  = \<top>"
    51   by (simp add: top_pred_def)
    52 
    53 definition
    54   "P \<sqinter> Q = Pred (eval P \<sqinter> eval Q)"
    55 
    56 lemma eval_inf [simp]:
    57   "eval (P \<sqinter> Q) = eval P \<sqinter> eval Q"
    58   by (simp add: inf_pred_def)
    59 
    60 definition
    61   "P \<squnion> Q = Pred (eval P \<squnion> eval Q)"
    62 
    63 lemma eval_sup [simp]:
    64   "eval (P \<squnion> Q) = eval P \<squnion> eval Q"
    65   by (simp add: sup_pred_def)
    66 
    67 definition
    68   "\<Sqinter>A = Pred (INFI A eval)"
    69 
    70 lemma eval_Inf [simp]:
    71   "eval (\<Sqinter>A) = INFI A eval"
    72   by (simp add: Inf_pred_def)
    73 
    74 definition
    75   "\<Squnion>A = Pred (SUPR A eval)"
    76 
    77 lemma eval_Sup [simp]:
    78   "eval (\<Squnion>A) = SUPR A eval"
    79   by (simp add: Sup_pred_def)
    80 
    81 instance proof
    82 qed (auto intro!: pred_eqI simp add: less_eq_pred_def less_pred_def le_fun_def less_fun_def)
    83 
    84 end
    85 
    86 lemma eval_INFI [simp]:
    87   "eval (INFI A f) = INFI A (eval \<circ> f)"
    88   by (simp only: INF_def eval_Inf image_compose)
    89 
    90 lemma eval_SUPR [simp]:
    91   "eval (SUPR A f) = SUPR A (eval \<circ> f)"
    92   by (simp only: SUP_def eval_Sup image_compose)
    93 
    94 instantiation pred :: (type) complete_boolean_algebra
    95 begin
    96 
    97 definition
    98   "- P = Pred (- eval P)"
    99 
   100 lemma eval_compl [simp]:
   101   "eval (- P) = - eval P"
   102   by (simp add: uminus_pred_def)
   103 
   104 definition
   105   "P - Q = Pred (eval P - eval Q)"
   106 
   107 lemma eval_minus [simp]:
   108   "eval (P - Q) = eval P - eval Q"
   109   by (simp add: minus_pred_def)
   110 
   111 instance proof
   112 qed (auto intro!: pred_eqI)
   113 
   114 end
   115 
   116 definition single :: "'a \<Rightarrow> 'a pred" where
   117   "single x = Pred ((op =) x)"
   118 
   119 lemma eval_single [simp]:
   120   "eval (single x) = (op =) x"
   121   by (simp add: single_def)
   122 
   123 definition bind :: "'a pred \<Rightarrow> ('a \<Rightarrow> 'b pred) \<Rightarrow> 'b pred" (infixl "\<guillemotright>=" 70) where
   124   "P \<guillemotright>= f = (SUPR {x. eval P x} f)"
   125 
   126 lemma eval_bind [simp]:
   127   "eval (P \<guillemotright>= f) = eval (SUPR {x. eval P x} f)"
   128   by (simp add: bind_def)
   129 
   130 lemma bind_bind:
   131   "(P \<guillemotright>= Q) \<guillemotright>= R = P \<guillemotright>= (\<lambda>x. Q x \<guillemotright>= R)"
   132   by (rule pred_eqI) auto
   133 
   134 lemma bind_single:
   135   "P \<guillemotright>= single = P"
   136   by (rule pred_eqI) auto
   137 
   138 lemma single_bind:
   139   "single x \<guillemotright>= P = P x"
   140   by (rule pred_eqI) auto
   141 
   142 lemma bottom_bind:
   143   "\<bottom> \<guillemotright>= P = \<bottom>"
   144   by (rule pred_eqI) auto
   145 
   146 lemma sup_bind:
   147   "(P \<squnion> Q) \<guillemotright>= R = P \<guillemotright>= R \<squnion> Q \<guillemotright>= R"
   148   by (rule pred_eqI) auto
   149 
   150 lemma Sup_bind:
   151   "(\<Squnion>A \<guillemotright>= f) = \<Squnion>((\<lambda>x. x \<guillemotright>= f) ` A)"
   152   by (rule pred_eqI) auto
   153 
   154 lemma pred_iffI:
   155   assumes "\<And>x. eval A x \<Longrightarrow> eval B x"
   156   and "\<And>x. eval B x \<Longrightarrow> eval A x"
   157   shows "A = B"
   158   using assms by (auto intro: pred_eqI)
   159   
   160 lemma singleI: "eval (single x) x"
   161   by simp
   162 
   163 lemma singleI_unit: "eval (single ()) x"
   164   by simp
   165 
   166 lemma singleE: "eval (single x) y \<Longrightarrow> (y = x \<Longrightarrow> P) \<Longrightarrow> P"
   167   by simp
   168 
   169 lemma singleE': "eval (single x) y \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P"
   170   by simp
   171 
   172 lemma bindI: "eval P x \<Longrightarrow> eval (Q x) y \<Longrightarrow> eval (P \<guillemotright>= Q) y"
   173   by auto
   174 
   175 lemma bindE: "eval (R \<guillemotright>= Q) y \<Longrightarrow> (\<And>x. eval R x \<Longrightarrow> eval (Q x) y \<Longrightarrow> P) \<Longrightarrow> P"
   176   by auto
   177 
   178 lemma botE: "eval \<bottom> x \<Longrightarrow> P"
   179   by auto
   180 
   181 lemma supI1: "eval A x \<Longrightarrow> eval (A \<squnion> B) x"
   182   by auto
   183 
   184 lemma supI2: "eval B x \<Longrightarrow> eval (A \<squnion> B) x" 
   185   by auto
   186 
   187 lemma supE: "eval (A \<squnion> B) x \<Longrightarrow> (eval A x \<Longrightarrow> P) \<Longrightarrow> (eval B x \<Longrightarrow> P) \<Longrightarrow> P"
   188   by auto
   189 
   190 lemma single_not_bot [simp]:
   191   "single x \<noteq> \<bottom>"
   192   by (auto simp add: single_def bot_pred_def fun_eq_iff)
   193 
   194 lemma not_bot:
   195   assumes "A \<noteq> \<bottom>"
   196   obtains x where "eval A x"
   197   using assms by (cases A) (auto simp add: bot_pred_def)
   198 
   199 
   200 subsection {* Emptiness check and definite choice *}
   201 
   202 definition is_empty :: "'a pred \<Rightarrow> bool" where
   203   "is_empty A \<longleftrightarrow> A = \<bottom>"
   204 
   205 lemma is_empty_bot:
   206   "is_empty \<bottom>"
   207   by (simp add: is_empty_def)
   208 
   209 lemma not_is_empty_single:
   210   "\<not> is_empty (single x)"
   211   by (auto simp add: is_empty_def single_def bot_pred_def fun_eq_iff)
   212 
   213 lemma is_empty_sup:
   214   "is_empty (A \<squnion> B) \<longleftrightarrow> is_empty A \<and> is_empty B"
   215   by (auto simp add: is_empty_def)
   216 
   217 definition singleton :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a pred \<Rightarrow> 'a" where
   218   "singleton dfault A = (if \<exists>!x. eval A x then THE x. eval A x else dfault ())"
   219 
   220 lemma singleton_eqI:
   221   "\<exists>!x. eval A x \<Longrightarrow> eval A x \<Longrightarrow> singleton dfault A = x"
   222   by (auto simp add: singleton_def)
   223 
   224 lemma eval_singletonI:
   225   "\<exists>!x. eval A x \<Longrightarrow> eval A (singleton dfault A)"
   226 proof -
   227   assume assm: "\<exists>!x. eval A x"
   228   then obtain x where x: "eval A x" ..
   229   with assm have "singleton dfault A = x" by (rule singleton_eqI)
   230   with x show ?thesis by simp
   231 qed
   232 
   233 lemma single_singleton:
   234   "\<exists>!x. eval A x \<Longrightarrow> single (singleton dfault A) = A"
   235 proof -
   236   assume assm: "\<exists>!x. eval A x"
   237   then have "eval A (singleton dfault A)"
   238     by (rule eval_singletonI)
   239   moreover from assm have "\<And>x. eval A x \<Longrightarrow> singleton dfault A = x"
   240     by (rule singleton_eqI)
   241   ultimately have "eval (single (singleton dfault A)) = eval A"
   242     by (simp (no_asm_use) add: single_def fun_eq_iff) blast
   243   then have "\<And>x. eval (single (singleton dfault A)) x = eval A x"
   244     by simp
   245   then show ?thesis by (rule pred_eqI)
   246 qed
   247 
   248 lemma singleton_undefinedI:
   249   "\<not> (\<exists>!x. eval A x) \<Longrightarrow> singleton dfault A = dfault ()"
   250   by (simp add: singleton_def)
   251 
   252 lemma singleton_bot:
   253   "singleton dfault \<bottom> = dfault ()"
   254   by (auto simp add: bot_pred_def intro: singleton_undefinedI)
   255 
   256 lemma singleton_single:
   257   "singleton dfault (single x) = x"
   258   by (auto simp add: intro: singleton_eqI singleI elim: singleE)
   259 
   260 lemma singleton_sup_single_single:
   261   "singleton dfault (single x \<squnion> single y) = (if x = y then x else dfault ())"
   262 proof (cases "x = y")
   263   case True then show ?thesis by (simp add: singleton_single)
   264 next
   265   case False
   266   have "eval (single x \<squnion> single y) x"
   267     and "eval (single x \<squnion> single y) y"
   268   by (auto intro: supI1 supI2 singleI)
   269   with False have "\<not> (\<exists>!z. eval (single x \<squnion> single y) z)"
   270     by blast
   271   then have "singleton dfault (single x \<squnion> single y) = dfault ()"
   272     by (rule singleton_undefinedI)
   273   with False show ?thesis by simp
   274 qed
   275 
   276 lemma singleton_sup_aux:
   277   "singleton dfault (A \<squnion> B) = (if A = \<bottom> then singleton dfault B
   278     else if B = \<bottom> then singleton dfault A
   279     else singleton dfault
   280       (single (singleton dfault A) \<squnion> single (singleton dfault B)))"
   281 proof (cases "(\<exists>!x. eval A x) \<and> (\<exists>!y. eval B y)")
   282   case True then show ?thesis by (simp add: single_singleton)
   283 next
   284   case False
   285   from False have A_or_B:
   286     "singleton dfault A = dfault () \<or> singleton dfault B = dfault ()"
   287     by (auto intro!: singleton_undefinedI)
   288   then have rhs: "singleton dfault
   289     (single (singleton dfault A) \<squnion> single (singleton dfault B)) = dfault ()"
   290     by (auto simp add: singleton_sup_single_single singleton_single)
   291   from False have not_unique:
   292     "\<not> (\<exists>!x. eval A x) \<or> \<not> (\<exists>!y. eval B y)" by simp
   293   show ?thesis proof (cases "A \<noteq> \<bottom> \<and> B \<noteq> \<bottom>")
   294     case True
   295     then obtain a b where a: "eval A a" and b: "eval B b"
   296       by (blast elim: not_bot)
   297     with True not_unique have "\<not> (\<exists>!x. eval (A \<squnion> B) x)"
   298       by (auto simp add: sup_pred_def bot_pred_def)
   299     then have "singleton dfault (A \<squnion> B) = dfault ()" by (rule singleton_undefinedI)
   300     with True rhs show ?thesis by simp
   301   next
   302     case False then show ?thesis by auto
   303   qed
   304 qed
   305 
   306 lemma singleton_sup:
   307   "singleton dfault (A \<squnion> B) = (if A = \<bottom> then singleton dfault B
   308     else if B = \<bottom> then singleton dfault A
   309     else if singleton dfault A = singleton dfault B then singleton dfault A else dfault ())"
   310 using singleton_sup_aux [of dfault A B] by (simp only: singleton_sup_single_single)
   311 
   312 
   313 subsection {* Derived operations *}
   314 
   315 definition if_pred :: "bool \<Rightarrow> unit pred" where
   316   if_pred_eq: "if_pred b = (if b then single () else \<bottom>)"
   317 
   318 definition holds :: "unit pred \<Rightarrow> bool" where
   319   holds_eq: "holds P = eval P ()"
   320 
   321 definition not_pred :: "unit pred \<Rightarrow> unit pred" where
   322   not_pred_eq: "not_pred P = (if eval P () then \<bottom> else single ())"
   323 
   324 lemma if_predI: "P \<Longrightarrow> eval (if_pred P) ()"
   325   unfolding if_pred_eq by (auto intro: singleI)
   326 
   327 lemma if_predE: "eval (if_pred b) x \<Longrightarrow> (b \<Longrightarrow> x = () \<Longrightarrow> P) \<Longrightarrow> P"
   328   unfolding if_pred_eq by (cases b) (auto elim: botE)
   329 
   330 lemma not_predI: "\<not> P \<Longrightarrow> eval (not_pred (Pred (\<lambda>u. P))) ()"
   331   unfolding not_pred_eq eval_pred by (auto intro: singleI)
   332 
   333 lemma not_predI': "\<not> eval P () \<Longrightarrow> eval (not_pred P) ()"
   334   unfolding not_pred_eq by (auto intro: singleI)
   335 
   336 lemma not_predE: "eval (not_pred (Pred (\<lambda>u. P))) x \<Longrightarrow> (\<not> P \<Longrightarrow> thesis) \<Longrightarrow> thesis"
   337   unfolding not_pred_eq
   338   by (auto split: split_if_asm elim: botE)
   339 
   340 lemma not_predE': "eval (not_pred P) x \<Longrightarrow> (\<not> eval P x \<Longrightarrow> thesis) \<Longrightarrow> thesis"
   341   unfolding not_pred_eq
   342   by (auto split: split_if_asm elim: botE)
   343 lemma "f () = False \<or> f () = True"
   344 by simp
   345 
   346 lemma closure_of_bool_cases [no_atp]:
   347   fixes f :: "unit \<Rightarrow> bool"
   348   assumes "f = (\<lambda>u. False) \<Longrightarrow> P f"
   349   assumes "f = (\<lambda>u. True) \<Longrightarrow> P f"
   350   shows "P f"
   351 proof -
   352   have "f = (\<lambda>u. False) \<or> f = (\<lambda>u. True)"
   353     apply (cases "f ()")
   354     apply (rule disjI2)
   355     apply (rule ext)
   356     apply (simp add: unit_eq)
   357     apply (rule disjI1)
   358     apply (rule ext)
   359     apply (simp add: unit_eq)
   360     done
   361   from this assms show ?thesis by blast
   362 qed
   363 
   364 lemma unit_pred_cases:
   365   assumes "P \<bottom>"
   366   assumes "P (single ())"
   367   shows "P Q"
   368 using assms unfolding bot_pred_def bot_fun_def bot_bool_def empty_def single_def proof (cases Q)
   369   fix f
   370   assume "P (Pred (\<lambda>u. False))" "P (Pred (\<lambda>u. () = u))"
   371   then have "P (Pred f)" 
   372     by (cases _ f rule: closure_of_bool_cases) simp_all
   373   moreover assume "Q = Pred f"
   374   ultimately show "P Q" by simp
   375 qed
   376   
   377 lemma holds_if_pred:
   378   "holds (if_pred b) = b"
   379 unfolding if_pred_eq holds_eq
   380 by (cases b) (auto intro: singleI elim: botE)
   381 
   382 lemma if_pred_holds:
   383   "if_pred (holds P) = P"
   384 unfolding if_pred_eq holds_eq
   385 by (rule unit_pred_cases) (auto intro: singleI elim: botE)
   386 
   387 lemma is_empty_holds:
   388   "is_empty P \<longleftrightarrow> \<not> holds P"
   389 unfolding is_empty_def holds_eq
   390 by (rule unit_pred_cases) (auto elim: botE intro: singleI)
   391 
   392 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred \<Rightarrow> 'b pred" where
   393   "map f P = P \<guillemotright>= (single o f)"
   394 
   395 lemma eval_map [simp]:
   396   "eval (map f P) = (\<Squnion>x\<in>{x. eval P x}. (\<lambda>y. f x = y))"
   397   by (auto simp add: map_def comp_def)
   398 
   399 functor map: map
   400   by (rule ext, rule pred_eqI, auto)+
   401 
   402 
   403 subsection {* Implementation *}
   404 
   405 datatype 'a seq = Empty | Insert "'a" "'a pred" | Join "'a pred" "'a seq"
   406 
   407 primrec pred_of_seq :: "'a seq \<Rightarrow> 'a pred" where
   408   "pred_of_seq Empty = \<bottom>"
   409 | "pred_of_seq (Insert x P) = single x \<squnion> P"
   410 | "pred_of_seq (Join P xq) = P \<squnion> pred_of_seq xq"
   411 
   412 definition Seq :: "(unit \<Rightarrow> 'a seq) \<Rightarrow> 'a pred" where
   413   "Seq f = pred_of_seq (f ())"
   414 
   415 code_datatype Seq
   416 
   417 primrec member :: "'a seq \<Rightarrow> 'a \<Rightarrow> bool"  where
   418   "member Empty x \<longleftrightarrow> False"
   419 | "member (Insert y P) x \<longleftrightarrow> x = y \<or> eval P x"
   420 | "member (Join P xq) x \<longleftrightarrow> eval P x \<or> member xq x"
   421 
   422 lemma eval_member:
   423   "member xq = eval (pred_of_seq xq)"
   424 proof (induct xq)
   425   case Empty show ?case
   426   by (auto simp add: fun_eq_iff elim: botE)
   427 next
   428   case Insert show ?case
   429   by (auto simp add: fun_eq_iff elim: supE singleE intro: supI1 supI2 singleI)
   430 next
   431   case Join then show ?case
   432   by (auto simp add: fun_eq_iff elim: supE intro: supI1 supI2)
   433 qed
   434 
   435 lemma eval_code [(* FIXME declare simp *)code]: "eval (Seq f) = member (f ())"
   436   unfolding Seq_def by (rule sym, rule eval_member)
   437 
   438 lemma single_code [code]:
   439   "single x = Seq (\<lambda>u. Insert x \<bottom>)"
   440   unfolding Seq_def by simp
   441 
   442 primrec "apply" :: "('a \<Rightarrow> 'b pred) \<Rightarrow> 'a seq \<Rightarrow> 'b seq" where
   443   "apply f Empty = Empty"
   444 | "apply f (Insert x P) = Join (f x) (Join (P \<guillemotright>= f) Empty)"
   445 | "apply f (Join P xq) = Join (P \<guillemotright>= f) (apply f xq)"
   446 
   447 lemma apply_bind:
   448   "pred_of_seq (apply f xq) = pred_of_seq xq \<guillemotright>= f"
   449 proof (induct xq)
   450   case Empty show ?case
   451     by (simp add: bottom_bind)
   452 next
   453   case Insert show ?case
   454     by (simp add: single_bind sup_bind)
   455 next
   456   case Join then show ?case
   457     by (simp add: sup_bind)
   458 qed
   459   
   460 lemma bind_code [code]:
   461   "Seq g \<guillemotright>= f = Seq (\<lambda>u. apply f (g ()))"
   462   unfolding Seq_def by (rule sym, rule apply_bind)
   463 
   464 lemma bot_set_code [code]:
   465   "\<bottom> = Seq (\<lambda>u. Empty)"
   466   unfolding Seq_def by simp
   467 
   468 primrec adjunct :: "'a pred \<Rightarrow> 'a seq \<Rightarrow> 'a seq" where
   469   "adjunct P Empty = Join P Empty"
   470 | "adjunct P (Insert x Q) = Insert x (Q \<squnion> P)"
   471 | "adjunct P (Join Q xq) = Join Q (adjunct P xq)"
   472 
   473 lemma adjunct_sup:
   474   "pred_of_seq (adjunct P xq) = P \<squnion> pred_of_seq xq"
   475   by (induct xq) (simp_all add: sup_assoc sup_commute sup_left_commute)
   476 
   477 lemma sup_code [code]:
   478   "Seq f \<squnion> Seq g = Seq (\<lambda>u. case f ()
   479     of Empty \<Rightarrow> g ()
   480      | Insert x P \<Rightarrow> Insert x (P \<squnion> Seq g)
   481      | Join P xq \<Rightarrow> adjunct (Seq g) (Join P xq))"
   482 proof (cases "f ()")
   483   case Empty
   484   thus ?thesis
   485     unfolding Seq_def by (simp add: sup_commute [of "\<bottom>"])
   486 next
   487   case Insert
   488   thus ?thesis
   489     unfolding Seq_def by (simp add: sup_assoc)
   490 next
   491   case Join
   492   thus ?thesis
   493     unfolding Seq_def
   494     by (simp add: adjunct_sup sup_assoc sup_commute sup_left_commute)
   495 qed
   496 
   497 lemma [code]:
   498   "size (P :: 'a Predicate.pred) = 0" by (cases P) simp
   499 
   500 lemma [code]:
   501   "pred_size f P = 0" by (cases P) simp
   502 
   503 primrec contained :: "'a seq \<Rightarrow> 'a pred \<Rightarrow> bool" where
   504   "contained Empty Q \<longleftrightarrow> True"
   505 | "contained (Insert x P) Q \<longleftrightarrow> eval Q x \<and> P \<le> Q"
   506 | "contained (Join P xq) Q \<longleftrightarrow> P \<le> Q \<and> contained xq Q"
   507 
   508 lemma single_less_eq_eval:
   509   "single x \<le> P \<longleftrightarrow> eval P x"
   510   by (auto simp add: less_eq_pred_def le_fun_def)
   511 
   512 lemma contained_less_eq:
   513   "contained xq Q \<longleftrightarrow> pred_of_seq xq \<le> Q"
   514   by (induct xq) (simp_all add: single_less_eq_eval)
   515 
   516 lemma less_eq_pred_code [code]:
   517   "Seq f \<le> Q = (case f ()
   518    of Empty \<Rightarrow> True
   519     | Insert x P \<Rightarrow> eval Q x \<and> P \<le> Q
   520     | Join P xq \<Rightarrow> P \<le> Q \<and> contained xq Q)"
   521   by (cases "f ()")
   522     (simp_all add: Seq_def single_less_eq_eval contained_less_eq)
   523 
   524 lemma eq_pred_code [code]:
   525   fixes P Q :: "'a pred"
   526   shows "HOL.equal P Q \<longleftrightarrow> P \<le> Q \<and> Q \<le> P"
   527   by (auto simp add: equal)
   528 
   529 lemma [code nbe]:
   530   "HOL.equal (x :: 'a pred) x \<longleftrightarrow> True"
   531   by (fact equal_refl)
   532 
   533 lemma [code]:
   534   "case_pred f P = f (eval P)"
   535   by (cases P) simp
   536 
   537 lemma [code]:
   538   "rec_pred f P = f (eval P)"
   539   by (cases P) simp
   540 
   541 inductive eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where "eq x x"
   542 
   543 lemma eq_is_eq: "eq x y \<equiv> (x = y)"
   544   by (rule eq_reflection) (auto intro: eq.intros elim: eq.cases)
   545 
   546 primrec null :: "'a seq \<Rightarrow> bool" where
   547   "null Empty \<longleftrightarrow> True"
   548 | "null (Insert x P) \<longleftrightarrow> False"
   549 | "null (Join P xq) \<longleftrightarrow> is_empty P \<and> null xq"
   550 
   551 lemma null_is_empty:
   552   "null xq \<longleftrightarrow> is_empty (pred_of_seq xq)"
   553   by (induct xq) (simp_all add: is_empty_bot not_is_empty_single is_empty_sup)
   554 
   555 lemma is_empty_code [code]:
   556   "is_empty (Seq f) \<longleftrightarrow> null (f ())"
   557   by (simp add: null_is_empty Seq_def)
   558 
   559 primrec the_only :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a seq \<Rightarrow> 'a" where
   560   [code del]: "the_only dfault Empty = dfault ()"
   561 | "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 ())"
   562 | "the_only dfault (Join P xq) = (if is_empty P then the_only dfault xq else if null xq then singleton dfault P
   563        else let x = singleton dfault P; y = the_only dfault xq in
   564        if x = y then x else dfault ())"
   565 
   566 lemma the_only_singleton:
   567   "the_only dfault xq = singleton dfault (pred_of_seq xq)"
   568   by (induct xq)
   569     (auto simp add: singleton_bot singleton_single is_empty_def
   570     null_is_empty Let_def singleton_sup)
   571 
   572 lemma singleton_code [code]:
   573   "singleton dfault (Seq f) = (case f ()
   574    of Empty \<Rightarrow> dfault ()
   575     | Insert x P \<Rightarrow> if is_empty P then x
   576         else let y = singleton dfault P in
   577           if x = y then x else dfault ()
   578     | Join P xq \<Rightarrow> if is_empty P then the_only dfault xq
   579         else if null xq then singleton dfault P
   580         else let x = singleton dfault P; y = the_only dfault xq in
   581           if x = y then x else dfault ())"
   582   by (cases "f ()")
   583    (auto simp add: Seq_def the_only_singleton is_empty_def
   584       null_is_empty singleton_bot singleton_single singleton_sup Let_def)
   585 
   586 definition the :: "'a pred \<Rightarrow> 'a" where
   587   "the A = (THE x. eval A x)"
   588 
   589 lemma the_eqI:
   590   "(THE x. eval P x) = x \<Longrightarrow> the P = x"
   591   by (simp add: the_def)
   592 
   593 lemma the_eq [code]: "the A = singleton (\<lambda>x. Code.abort (STR ''not_unique'') (\<lambda>_. the A)) A"
   594   by (rule the_eqI) (simp add: singleton_def the_def)
   595 
   596 code_reflect Predicate
   597   datatypes pred = Seq and seq = Empty | Insert | Join
   598 
   599 ML {*
   600 signature PREDICATE =
   601 sig
   602   val anamorph: ('a -> ('b * 'a) option) -> int -> 'a -> 'b list * 'a
   603   datatype 'a pred = Seq of (unit -> 'a seq)
   604   and 'a seq = Empty | Insert of 'a * 'a pred | Join of 'a pred * 'a seq
   605   val map: ('a -> 'b) -> 'a pred -> 'b pred
   606   val yield: 'a pred -> ('a * 'a pred) option
   607   val yieldn: int -> 'a pred -> 'a list * 'a pred
   608 end;
   609 
   610 structure Predicate : PREDICATE =
   611 struct
   612 
   613 fun anamorph f k x =
   614  (if k = 0 then ([], x)
   615   else case f x
   616    of NONE => ([], x)
   617     | SOME (v, y) => let
   618         val k' = k - 1;
   619         val (vs, z) = anamorph f k' y
   620       in (v :: vs, z) end);
   621 
   622 datatype pred = datatype Predicate.pred
   623 datatype seq = datatype Predicate.seq
   624 
   625 fun map f = @{code Predicate.map} f;
   626 
   627 fun yield (Seq f) = next (f ())
   628 and next Empty = NONE
   629   | next (Insert (x, P)) = SOME (x, P)
   630   | next (Join (P, xq)) = (case yield P
   631      of NONE => next xq
   632       | SOME (x, Q) => SOME (x, Seq (fn _ => Join (Q, xq))));
   633 
   634 fun yieldn k = anamorph yield k;
   635 
   636 end;
   637 *}
   638 
   639 text {* Conversion from and to sets *}
   640 
   641 definition pred_of_set :: "'a set \<Rightarrow> 'a pred" where
   642   "pred_of_set = Pred \<circ> (\<lambda>A x. x \<in> A)"
   643 
   644 lemma eval_pred_of_set [simp]:
   645   "eval (pred_of_set A) x \<longleftrightarrow> x \<in>A"
   646   by (simp add: pred_of_set_def)
   647 
   648 definition set_of_pred :: "'a pred \<Rightarrow> 'a set" where
   649   "set_of_pred = Collect \<circ> eval"
   650 
   651 lemma member_set_of_pred [simp]:
   652   "x \<in> set_of_pred P \<longleftrightarrow> Predicate.eval P x"
   653   by (simp add: set_of_pred_def)
   654 
   655 definition set_of_seq :: "'a seq \<Rightarrow> 'a set" where
   656   "set_of_seq = set_of_pred \<circ> pred_of_seq"
   657 
   658 lemma member_set_of_seq [simp]:
   659   "x \<in> set_of_seq xq = Predicate.member xq x"
   660   by (simp add: set_of_seq_def eval_member)
   661 
   662 lemma of_pred_code [code]:
   663   "set_of_pred (Predicate.Seq f) = (case f () of
   664      Predicate.Empty \<Rightarrow> {}
   665    | Predicate.Insert x P \<Rightarrow> insert x (set_of_pred P)
   666    | Predicate.Join P xq \<Rightarrow> set_of_pred P \<union> set_of_seq xq)"
   667   by (auto split: seq.split simp add: eval_code)
   668 
   669 lemma of_seq_code [code]:
   670   "set_of_seq Predicate.Empty = {}"
   671   "set_of_seq (Predicate.Insert x P) = insert x (set_of_pred P)"
   672   "set_of_seq (Predicate.Join P xq) = set_of_pred P \<union> set_of_seq xq"
   673   by auto
   674 
   675 text {* Lazy Evaluation of an indexed function *}
   676 
   677 function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a Predicate.pred"
   678 where
   679   "iterate_upto f n m =
   680     Predicate.Seq (%u. if n > m then Predicate.Empty
   681      else Predicate.Insert (f n) (iterate_upto f (n + 1) m))"
   682 by pat_completeness auto
   683 
   684 termination by (relation "measure (%(f, n, m). nat_of_natural (m + 1 - n))")
   685   (auto simp add: less_natural_def)
   686 
   687 text {* Misc *}
   688 
   689 declare Inf_set_fold [where 'a = "'a Predicate.pred", code]
   690 declare Sup_set_fold [where 'a = "'a Predicate.pred", code]
   691 
   692 (* FIXME: better implement conversion by bisection *)
   693 
   694 lemma pred_of_set_fold_sup:
   695   assumes "finite A"
   696   shows "pred_of_set A = Finite_Set.fold sup bot (Predicate.single ` A)" (is "?lhs = ?rhs")
   697 proof (rule sym)
   698   interpret comp_fun_idem "sup :: 'a Predicate.pred \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a Predicate.pred"
   699     by (fact comp_fun_idem_sup)
   700   from `finite A` show "?rhs = ?lhs" by (induct A) (auto intro!: pred_eqI)
   701 qed
   702 
   703 lemma pred_of_set_set_fold_sup:
   704   "pred_of_set (set xs) = fold sup (List.map Predicate.single xs) bot"
   705 proof -
   706   interpret comp_fun_idem "sup :: 'a Predicate.pred \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a Predicate.pred"
   707     by (fact comp_fun_idem_sup)
   708   show ?thesis by (simp add: pred_of_set_fold_sup fold_set_fold [symmetric])
   709 qed
   710 
   711 lemma pred_of_set_set_foldr_sup [code]:
   712   "pred_of_set (set xs) = foldr sup (List.map Predicate.single xs) bot"
   713   by (simp add: pred_of_set_set_fold_sup ac_simps foldr_fold fun_eq_iff)
   714 
   715 no_notation
   716   bind (infixl "\<guillemotright>=" 70)
   717 
   718 hide_type (open) pred seq
   719 hide_const (open) Pred eval single bind is_empty singleton if_pred not_pred holds
   720   Empty Insert Join Seq member pred_of_seq "apply" adjunct null the_only eq map the
   721   iterate_upto
   722 hide_fact (open) null_def member_def
   723 
   724 end