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