| author | fleury <Mathias.Fleury@mpi-inf.mpg.de> | 
| Sun, 18 Sep 2016 11:31:19 +0200 | |
| changeset 63908 | ca41b6670904 | 
| parent 62390 | 842917225d56 | 
| child 66012 | 59bf29d2b3a1 | 
| permissions | -rw-r--r-- | 
| 22259 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 1 | (* Title: HOL/Predicate.thy | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 2 | Author: Lukas Bulwahn and Florian Haftmann, TU Muenchen | 
| 22259 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 3 | *) | 
| 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 4 | |
| 60758 | 5 | section \<open>Predicates as enumerations\<close> | 
| 22259 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 6 | |
| 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 7 | theory Predicate | 
| 53943 | 8 | imports String | 
| 22259 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 9 | begin | 
| 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 10 | |
| 60758 | 11 | subsection \<open>The type of predicate enumerations (a monad)\<close> | 
| 30328 | 12 | |
| 58350 
919149921e46
added 'extraction' plugins -- this might help 'HOL-Proofs'
 blanchet parents: 
58334diff
changeset | 13 | datatype (plugins only: code extraction) (dead 'a) pred = Pred "'a \<Rightarrow> bool" | 
| 30328 | 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 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 22 | lemma pred_eqI: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 23 | "(\<And>w. eval P w \<longleftrightarrow> eval Q w) \<Longrightarrow> P = Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 24 | by (cases P, cases Q) (auto simp add: fun_eq_iff) | 
| 30328 | 25 | |
| 46038 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 26 | lemma pred_eq_iff: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 27 | "P = Q \<Longrightarrow> (\<And>w. eval P w \<longleftrightarrow> eval Q w)" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 28 | by (simp add: pred_eqI) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 29 | |
| 44033 | 30 | instantiation pred :: (type) complete_lattice | 
| 30328 | 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 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 42 | lemma eval_bot [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 43 | "eval \<bottom> = \<bottom>" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 44 | by (simp add: bot_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 45 | |
| 30328 | 46 | definition | 
| 47 | "\<top> = Pred \<top>" | |
| 48 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 49 | lemma eval_top [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 50 | "eval \<top> = \<top>" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 51 | by (simp add: top_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 52 | |
| 30328 | 53 | definition | 
| 54 | "P \<sqinter> Q = Pred (eval P \<sqinter> eval Q)" | |
| 55 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 56 | lemma eval_inf [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 57 | "eval (P \<sqinter> Q) = eval P \<sqinter> eval Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 58 | by (simp add: inf_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 59 | |
| 30328 | 60 | definition | 
| 61 | "P \<squnion> Q = Pred (eval P \<squnion> eval Q)" | |
| 62 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 63 | lemma eval_sup [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 64 | "eval (P \<squnion> Q) = eval P \<squnion> eval Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 65 | by (simp add: sup_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 66 | |
| 30328 | 67 | definition | 
| 56218 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 haftmann parents: 
56212diff
changeset | 68 | "\<Sqinter>A = Pred (INFIMUM A eval)" | 
| 30328 | 69 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 70 | lemma eval_Inf [simp]: | 
| 56218 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 haftmann parents: 
56212diff
changeset | 71 | "eval (\<Sqinter>A) = INFIMUM A eval" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 72 | by (simp add: Inf_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 73 | |
| 30328 | 74 | definition | 
| 56218 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 haftmann parents: 
56212diff
changeset | 75 | "\<Squnion>A = Pred (SUPREMUM A eval)" | 
| 30328 | 76 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 77 | lemma eval_Sup [simp]: | 
| 56218 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 haftmann parents: 
56212diff
changeset | 78 | "eval (\<Squnion>A) = SUPREMUM A eval" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 79 | by (simp add: Sup_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 80 | |
| 44033 | 81 | instance proof | 
| 44415 | 82 | qed (auto intro!: pred_eqI simp add: less_eq_pred_def less_pred_def le_fun_def less_fun_def) | 
| 44033 | 83 | |
| 84 | end | |
| 85 | ||
| 56212 
3253aaf73a01
consolidated theorem names containing INFI and SUPR: have INF and SUP instead uniformly
 haftmann parents: 
56166diff
changeset | 86 | lemma eval_INF [simp]: | 
| 56218 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 haftmann parents: 
56212diff
changeset | 87 | "eval (INFIMUM A f) = INFIMUM A (eval \<circ> f)" | 
| 56166 | 88 | using eval_Inf [of "f ` A"] by simp | 
| 44033 | 89 | |
| 56212 
3253aaf73a01
consolidated theorem names containing INFI and SUPR: have INF and SUP instead uniformly
 haftmann parents: 
56166diff
changeset | 90 | lemma eval_SUP [simp]: | 
| 56218 
1c3f1f2431f9
elongated INFI and SUPR, to reduced risk of confusing theorems names in the future while still being consistent with INTER and UNION
 haftmann parents: 
56212diff
changeset | 91 | "eval (SUPREMUM A f) = SUPREMUM A (eval \<circ> f)" | 
| 56166 | 92 | using eval_Sup [of "f ` A"] by simp | 
| 44033 | 93 | |
| 94 | instantiation pred :: (type) complete_boolean_algebra | |
| 95 | begin | |
| 96 | ||
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 97 | definition | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 98 | "- P = Pred (- eval P)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 99 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 100 | lemma eval_compl [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 101 | "eval (- P) = - eval P" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 102 | by (simp add: uminus_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 103 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 104 | definition | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 105 | "P - Q = Pred (eval P - eval Q)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 106 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 107 | lemma eval_minus [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 108 | "eval (P - Q) = eval P - eval Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 109 | by (simp add: minus_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 110 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 111 | instance proof | 
| 46884 | 112 | qed (auto intro!: pred_eqI) | 
| 30328 | 113 | |
| 22259 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 114 | end | 
| 30328 | 115 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 116 | definition single :: "'a \<Rightarrow> 'a pred" where | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 117 | "single x = Pred ((op =) x)" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 118 | |
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 119 | lemma eval_single [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 120 | "eval (single x) = (op =) x" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 121 | by (simp add: single_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 122 | |
| 62026 | 123 | definition bind :: "'a pred \<Rightarrow> ('a \<Rightarrow> 'b pred) \<Rightarrow> 'b pred" (infixl "\<bind>" 70) where
 | 
| 124 |   "P \<bind> f = (SUPREMUM {x. eval P x} f)"
 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 125 | |
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 126 | lemma eval_bind [simp]: | 
| 62026 | 127 |   "eval (P \<bind> f) = eval (SUPREMUM {x. eval P x} f)"
 | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 128 | by (simp add: bind_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 129 | |
| 30328 | 130 | lemma bind_bind: | 
| 62026 | 131 | "(P \<bind> Q) \<bind> R = P \<bind> (\<lambda>x. Q x \<bind> R)" | 
| 46884 | 132 | by (rule pred_eqI) auto | 
| 30328 | 133 | |
| 134 | lemma bind_single: | |
| 62026 | 135 | "P \<bind> single = P" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 136 | by (rule pred_eqI) auto | 
| 30328 | 137 | |
| 138 | lemma single_bind: | |
| 62026 | 139 | "single x \<bind> P = P x" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 140 | by (rule pred_eqI) auto | 
| 30328 | 141 | |
| 142 | lemma bottom_bind: | |
| 62026 | 143 | "\<bottom> \<bind> P = \<bottom>" | 
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 144 | by (rule pred_eqI) auto | 
| 30328 | 145 | |
| 146 | lemma sup_bind: | |
| 62026 | 147 | "(P \<squnion> Q) \<bind> R = P \<bind> R \<squnion> Q \<bind> R" | 
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 148 | by (rule pred_eqI) auto | 
| 30328 | 149 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 150 | lemma Sup_bind: | 
| 62026 | 151 | "(\<Squnion>A \<bind> f) = \<Squnion>((\<lambda>x. x \<bind> f) ` A)" | 
| 46884 | 152 | by (rule pred_eqI) auto | 
| 30328 | 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" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 158 | using assms by (auto intro: pred_eqI) | 
| 30328 | 159 | |
| 160 | lemma singleI: "eval (single x) x" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 161 | by simp | 
| 30328 | 162 | |
| 163 | lemma singleI_unit: "eval (single ()) x" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 164 | by simp | 
| 30328 | 165 | |
| 166 | lemma singleE: "eval (single x) y \<Longrightarrow> (y = x \<Longrightarrow> P) \<Longrightarrow> P" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 167 | by simp | 
| 30328 | 168 | |
| 169 | lemma singleE': "eval (single x) y \<Longrightarrow> (x = y \<Longrightarrow> P) \<Longrightarrow> P" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 170 | by simp | 
| 30328 | 171 | |
| 62026 | 172 | lemma bindI: "eval P x \<Longrightarrow> eval (Q x) y \<Longrightarrow> eval (P \<bind> Q) y" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 173 | by auto | 
| 30328 | 174 | |
| 62026 | 175 | lemma bindE: "eval (R \<bind> Q) y \<Longrightarrow> (\<And>x. eval R x \<Longrightarrow> eval (Q x) y \<Longrightarrow> P) \<Longrightarrow> P" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 176 | by auto | 
| 30328 | 177 | |
| 178 | lemma botE: "eval \<bottom> x \<Longrightarrow> P" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 179 | by auto | 
| 30328 | 180 | |
| 181 | lemma supI1: "eval A x \<Longrightarrow> eval (A \<squnion> B) x" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 182 | by auto | 
| 30328 | 183 | |
| 184 | lemma supI2: "eval B x \<Longrightarrow> eval (A \<squnion> B) x" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 185 | by auto | 
| 30328 | 186 | |
| 187 | lemma supE: "eval (A \<squnion> B) x \<Longrightarrow> (eval A x \<Longrightarrow> P) \<Longrightarrow> (eval B x \<Longrightarrow> P) \<Longrightarrow> P" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 188 | by auto | 
| 30328 | 189 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 190 | lemma single_not_bot [simp]: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 191 | "single x \<noteq> \<bottom>" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 192 | by (auto simp add: single_def bot_pred_def fun_eq_iff) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 193 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 194 | lemma not_bot: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 195 | assumes "A \<noteq> \<bottom>" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 196 | obtains x where "eval A x" | 
| 45970 
b6d0cff57d96
adjusted to set/pred distinction by means of type constructor `set`
 haftmann parents: 
45630diff
changeset | 197 | using assms by (cases A) (auto simp add: bot_pred_def) | 
| 
b6d0cff57d96
adjusted to set/pred distinction by means of type constructor `set`
 haftmann parents: 
45630diff
changeset | 198 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 199 | |
| 60758 | 200 | subsection \<open>Emptiness check and definite choice\<close> | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 201 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 202 | definition is_empty :: "'a pred \<Rightarrow> bool" where | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 203 | "is_empty A \<longleftrightarrow> A = \<bottom>" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 204 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 205 | lemma is_empty_bot: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 206 | "is_empty \<bottom>" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 207 | by (simp add: is_empty_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 208 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 209 | lemma not_is_empty_single: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 210 | "\<not> is_empty (single x)" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 211 | by (auto simp add: is_empty_def single_def bot_pred_def fun_eq_iff) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 212 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 213 | lemma is_empty_sup: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 214 | "is_empty (A \<squnion> B) \<longleftrightarrow> is_empty A \<and> is_empty B" | 
| 36008 | 215 | by (auto simp add: is_empty_def) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 216 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 217 | definition singleton :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a pred \<Rightarrow> 'a" where | 
| 60166 | 218 | "\<And>default. singleton default A = (if \<exists>!x. eval A x then THE x. eval A x else default ())" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 219 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 220 | lemma singleton_eqI: | 
| 60166 | 221 | fixes default | 
| 222 | shows "\<exists>!x. eval A x \<Longrightarrow> eval A x \<Longrightarrow> singleton default A = x" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 223 | by (auto simp add: singleton_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 224 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 225 | lemma eval_singletonI: | 
| 60166 | 226 | fixes default | 
| 227 | shows "\<exists>!x. eval A x \<Longrightarrow> eval A (singleton default A)" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 228 | proof - | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 229 | assume assm: "\<exists>!x. eval A x" | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
51143diff
changeset | 230 | then obtain x where x: "eval A x" .. | 
| 60166 | 231 | with assm have "singleton default A = x" by (rule singleton_eqI) | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
51143diff
changeset | 232 | with x show ?thesis by simp | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 233 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 234 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 235 | lemma single_singleton: | 
| 60166 | 236 | fixes default | 
| 237 | shows "\<exists>!x. eval A x \<Longrightarrow> single (singleton default A) = A" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 238 | proof - | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 239 | assume assm: "\<exists>!x. eval A x" | 
| 60166 | 240 | then have "eval A (singleton default A)" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 241 | by (rule eval_singletonI) | 
| 60166 | 242 | moreover from assm have "\<And>x. eval A x \<Longrightarrow> singleton default A = x" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 243 | by (rule singleton_eqI) | 
| 60166 | 244 | ultimately have "eval (single (singleton default A)) = eval A" | 
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 245 | by (simp (no_asm_use) add: single_def fun_eq_iff) blast | 
| 60166 | 246 | then have "\<And>x. eval (single (singleton default A)) x = eval A x" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 247 | by simp | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 248 | then show ?thesis by (rule pred_eqI) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 249 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 250 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 251 | lemma singleton_undefinedI: | 
| 60166 | 252 | fixes default | 
| 253 | shows "\<not> (\<exists>!x. eval A x) \<Longrightarrow> singleton default A = default ()" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 254 | by (simp add: singleton_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 255 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 256 | lemma singleton_bot: | 
| 60166 | 257 | fixes default | 
| 258 | shows "singleton default \<bottom> = default ()" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 259 | by (auto simp add: bot_pred_def intro: singleton_undefinedI) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 260 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 261 | lemma singleton_single: | 
| 60166 | 262 | fixes default | 
| 263 | shows "singleton default (single x) = x" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 264 | by (auto simp add: intro: singleton_eqI singleI elim: singleE) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 265 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 266 | lemma singleton_sup_single_single: | 
| 60166 | 267 | fixes default | 
| 268 | shows "singleton default (single x \<squnion> single y) = (if x = y then x else default ())" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 269 | proof (cases "x = y") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 270 | case True then show ?thesis by (simp add: singleton_single) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 271 | next | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 272 | case False | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 273 | have "eval (single x \<squnion> single y) x" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 274 | and "eval (single x \<squnion> single y) y" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 275 | by (auto intro: supI1 supI2 singleI) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 276 | with False have "\<not> (\<exists>!z. eval (single x \<squnion> single y) z)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 277 | by blast | 
| 60166 | 278 | then have "singleton default (single x \<squnion> single y) = default ()" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 279 | by (rule singleton_undefinedI) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 280 | with False show ?thesis by simp | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 281 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 282 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 283 | lemma singleton_sup_aux: | 
| 60166 | 284 | fixes default | 
| 285 | shows | |
| 286 | "singleton default (A \<squnion> B) = (if A = \<bottom> then singleton default B | |
| 287 | else if B = \<bottom> then singleton default A | |
| 288 | else singleton default | |
| 289 | (single (singleton default A) \<squnion> single (singleton default B)))" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 290 | proof (cases "(\<exists>!x. eval A x) \<and> (\<exists>!y. eval B y)") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 291 | case True then show ?thesis by (simp add: single_singleton) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 292 | next | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 293 | case False | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 294 | from False have A_or_B: | 
| 60166 | 295 | "singleton default A = default () \<or> singleton default B = default ()" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 296 | by (auto intro!: singleton_undefinedI) | 
| 60166 | 297 | then have rhs: "singleton default | 
| 298 | (single (singleton default A) \<squnion> single (singleton default B)) = default ()" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 299 | by (auto simp add: singleton_sup_single_single singleton_single) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 300 | from False have not_unique: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 301 | "\<not> (\<exists>!x. eval A x) \<or> \<not> (\<exists>!y. eval B y)" by simp | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 302 | show ?thesis proof (cases "A \<noteq> \<bottom> \<and> B \<noteq> \<bottom>") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 303 | case True | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 304 | then obtain a b where a: "eval A a" and b: "eval B b" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 305 | by (blast elim: not_bot) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 306 | with True not_unique have "\<not> (\<exists>!x. eval (A \<squnion> B) x)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 307 | by (auto simp add: sup_pred_def bot_pred_def) | 
| 60166 | 308 | then have "singleton default (A \<squnion> B) = default ()" by (rule singleton_undefinedI) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 309 | with True rhs show ?thesis by simp | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 310 | next | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 311 | case False then show ?thesis by auto | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 312 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 313 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 314 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 315 | lemma singleton_sup: | 
| 60166 | 316 | fixes default | 
| 317 | shows | |
| 318 | "singleton default (A \<squnion> B) = (if A = \<bottom> then singleton default B | |
| 319 | else if B = \<bottom> then singleton default A | |
| 320 | else if singleton default A = singleton default B then singleton default A else default ())" | |
| 321 | using singleton_sup_aux [of default A B] by (simp only: singleton_sup_single_single) | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 322 | |
| 30328 | 323 | |
| 60758 | 324 | subsection \<open>Derived operations\<close> | 
| 30328 | 325 | |
| 326 | definition if_pred :: "bool \<Rightarrow> unit pred" where | |
| 327 | if_pred_eq: "if_pred b = (if b then single () else \<bottom>)" | |
| 328 | ||
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 329 | definition holds :: "unit pred \<Rightarrow> bool" where | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 330 | holds_eq: "holds P = eval P ()" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 331 | |
| 30328 | 332 | definition not_pred :: "unit pred \<Rightarrow> unit pred" where | 
| 333 | not_pred_eq: "not_pred P = (if eval P () then \<bottom> else single ())" | |
| 334 | ||
| 335 | lemma if_predI: "P \<Longrightarrow> eval (if_pred P) ()" | |
| 336 | unfolding if_pred_eq by (auto intro: singleI) | |
| 337 | ||
| 338 | lemma if_predE: "eval (if_pred b) x \<Longrightarrow> (b \<Longrightarrow> x = () \<Longrightarrow> P) \<Longrightarrow> P" | |
| 339 | unfolding if_pred_eq by (cases b) (auto elim: botE) | |
| 340 | ||
| 341 | lemma not_predI: "\<not> P \<Longrightarrow> eval (not_pred (Pred (\<lambda>u. P))) ()" | |
| 342 | unfolding not_pred_eq eval_pred by (auto intro: singleI) | |
| 343 | ||
| 344 | lemma not_predI': "\<not> eval P () \<Longrightarrow> eval (not_pred P) ()" | |
| 345 | unfolding not_pred_eq by (auto intro: singleI) | |
| 346 | ||
| 347 | lemma not_predE: "eval (not_pred (Pred (\<lambda>u. P))) x \<Longrightarrow> (\<not> P \<Longrightarrow> thesis) \<Longrightarrow> thesis" | |
| 348 | unfolding not_pred_eq | |
| 62390 | 349 | by (auto split: if_split_asm elim: botE) | 
| 30328 | 350 | |
| 351 | lemma not_predE': "eval (not_pred P) x \<Longrightarrow> (\<not> eval P x \<Longrightarrow> thesis) \<Longrightarrow> thesis" | |
| 352 | unfolding not_pred_eq | |
| 62390 | 353 | by (auto split: if_split_asm elim: botE) | 
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 354 | lemma "f () = False \<or> f () = True" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 355 | by simp | 
| 30328 | 356 | |
| 37549 | 357 | lemma closure_of_bool_cases [no_atp]: | 
| 44007 | 358 | fixes f :: "unit \<Rightarrow> bool" | 
| 359 | assumes "f = (\<lambda>u. False) \<Longrightarrow> P f" | |
| 360 | assumes "f = (\<lambda>u. True) \<Longrightarrow> P f" | |
| 361 | shows "P f" | |
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 362 | proof - | 
| 44007 | 363 | have "f = (\<lambda>u. False) \<or> f = (\<lambda>u. True)" | 
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 364 | apply (cases "f ()") | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 365 | apply (rule disjI2) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 366 | apply (rule ext) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 367 | apply (simp add: unit_eq) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 368 | apply (rule disjI1) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 369 | apply (rule ext) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 370 | apply (simp add: unit_eq) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 371 | done | 
| 41550 | 372 | from this assms show ?thesis by blast | 
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 373 | qed | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 374 | |
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 375 | lemma unit_pred_cases: | 
| 44007 | 376 | assumes "P \<bottom>" | 
| 377 | assumes "P (single ())" | |
| 378 | shows "P Q" | |
| 44415 | 379 | using assms unfolding bot_pred_def bot_fun_def bot_bool_def empty_def single_def proof (cases Q) | 
| 44007 | 380 | fix f | 
| 381 | assume "P (Pred (\<lambda>u. False))" "P (Pred (\<lambda>u. () = u))" | |
| 382 | then have "P (Pred f)" | |
| 383 | by (cases _ f rule: closure_of_bool_cases) simp_all | |
| 384 | moreover assume "Q = Pred f" | |
| 385 | ultimately show "P Q" by simp | |
| 386 | qed | |
| 387 | ||
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 388 | lemma holds_if_pred: | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 389 | "holds (if_pred b) = b" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 390 | unfolding if_pred_eq holds_eq | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 391 | by (cases b) (auto intro: singleI elim: botE) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 392 | |
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 393 | lemma if_pred_holds: | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 394 | "if_pred (holds P) = P" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 395 | unfolding if_pred_eq holds_eq | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 396 | by (rule unit_pred_cases) (auto intro: singleI elim: botE) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 397 | |
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 398 | lemma is_empty_holds: | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 399 | "is_empty P \<longleftrightarrow> \<not> holds P" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 400 | unfolding is_empty_def holds_eq | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 401 | by (rule unit_pred_cases) (auto elim: botE intro: singleI) | 
| 30328 | 402 | |
| 41311 | 403 | definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred \<Rightarrow> 'b pred" where
 | 
| 62026 | 404 | "map f P = P \<bind> (single o f)" | 
| 41311 | 405 | |
| 406 | lemma eval_map [simp]: | |
| 44363 | 407 |   "eval (map f P) = (\<Squnion>x\<in>{x. eval P x}. (\<lambda>y. f x = y))"
 | 
| 44415 | 408 | by (auto simp add: map_def comp_def) | 
| 41311 | 409 | |
| 55467 
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
 blanchet parents: 
55416diff
changeset | 410 | functor map: map | 
| 44363 | 411 | by (rule ext, rule pred_eqI, auto)+ | 
| 41311 | 412 | |
| 413 | ||
| 60758 | 414 | subsection \<open>Implementation\<close> | 
| 30328 | 415 | |
| 58350 
919149921e46
added 'extraction' plugins -- this might help 'HOL-Proofs'
 blanchet parents: 
58334diff
changeset | 416 | datatype (plugins only: code extraction) (dead 'a) seq = | 
| 58334 | 417 | Empty | 
| 418 | | Insert "'a" "'a pred" | |
| 419 | | Join "'a pred" "'a seq" | |
| 30328 | 420 | |
| 421 | primrec pred_of_seq :: "'a seq \<Rightarrow> 'a pred" where | |
| 44414 | 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" | |
| 30328 | 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" | |
| 44414 | 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" | |
| 30328 | 435 | |
| 436 | lemma eval_member: | |
| 437 | "member xq = eval (pred_of_seq xq)" | |
| 438 | proof (induct xq) | |
| 439 | case Empty show ?case | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 440 | by (auto simp add: fun_eq_iff elim: botE) | 
| 30328 | 441 | next | 
| 442 | case Insert show ?case | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 443 | by (auto simp add: fun_eq_iff elim: supE singleE intro: supI1 supI2 singleI) | 
| 30328 | 444 | next | 
| 445 | case Join then show ?case | |
| 39302 
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
 nipkow parents: 
39198diff
changeset | 446 | by (auto simp add: fun_eq_iff elim: supE intro: supI1 supI2) | 
| 30328 | 447 | qed | 
| 448 | ||
| 46038 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 449 | lemma eval_code [(* FIXME declare simp *)code]: "eval (Seq f) = member (f ())" | 
| 30328 | 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 | ||
| 41080 | 456 | primrec "apply" :: "('a \<Rightarrow> 'b pred) \<Rightarrow> 'a seq \<Rightarrow> 'b seq" where
 | 
| 44415 | 457 | "apply f Empty = Empty" | 
| 62026 | 458 | | "apply f (Insert x P) = Join (f x) (Join (P \<bind> f) Empty)" | 
| 459 | | "apply f (Join P xq) = Join (P \<bind> f) (apply f xq)" | |
| 30328 | 460 | |
| 461 | lemma apply_bind: | |
| 62026 | 462 | "pred_of_seq (apply f xq) = pred_of_seq xq \<bind> f" | 
| 30328 | 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]: | |
| 62026 | 475 | "Seq g \<bind> f = Seq (\<lambda>u. apply f (g ()))" | 
| 30328 | 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 | ||
| 30376 | 482 | primrec adjunct :: "'a pred \<Rightarrow> 'a seq \<Rightarrow> 'a seq" where | 
| 44415 | 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)" | |
| 30376 | 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 | ||
| 30328 | 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) | |
| 30376 | 495 | | Join P xq \<Rightarrow> adjunct (Seq g) (Join P xq))" | 
| 30328 | 496 | proof (cases "f ()") | 
| 497 | case Empty | |
| 498 | thus ?thesis | |
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33988diff
changeset | 499 | unfolding Seq_def by (simp add: sup_commute [of "\<bottom>"]) | 
| 30328 | 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 | |
| 30376 | 507 | unfolding Seq_def | 
| 508 | by (simp add: adjunct_sup sup_assoc sup_commute sup_left_commute) | |
| 30328 | 509 | qed | 
| 510 | ||
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 511 | primrec contained :: "'a seq \<Rightarrow> 'a pred \<Rightarrow> bool" where | 
| 44415 | 512 | "contained Empty Q \<longleftrightarrow> True" | 
| 513 | | "contained (Insert x P) Q \<longleftrightarrow> eval Q x \<and> P \<le> Q" | |
| 514 | | "contained (Join P xq) Q \<longleftrightarrow> P \<le> Q \<and> contained xq Q" | |
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 515 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 516 | lemma single_less_eq_eval: | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 517 | "single x \<le> P \<longleftrightarrow> eval P x" | 
| 44415 | 518 | by (auto simp add: less_eq_pred_def le_fun_def) | 
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 519 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 520 | lemma contained_less_eq: | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 521 | "contained xq Q \<longleftrightarrow> pred_of_seq xq \<le> Q" | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 522 | by (induct xq) (simp_all add: single_less_eq_eval) | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 523 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 524 | lemma less_eq_pred_code [code]: | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 525 | "Seq f \<le> Q = (case f () | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 526 | of Empty \<Rightarrow> True | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 527 | | Insert x P \<Rightarrow> eval Q x \<and> P \<le> Q | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 528 | | Join P xq \<Rightarrow> P \<le> Q \<and> contained xq Q)" | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 529 | by (cases "f ()") | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 530 | (simp_all add: Seq_def single_less_eq_eval contained_less_eq) | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 531 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 532 | lemma eq_pred_code [code]: | 
| 31133 | 533 | fixes P Q :: "'a pred" | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 534 | shows "HOL.equal P Q \<longleftrightarrow> P \<le> Q \<and> Q \<le> P" | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 535 | by (auto simp add: equal) | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 536 | |
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 537 | lemma [code nbe]: | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 538 | "HOL.equal (x :: 'a pred) x \<longleftrightarrow> True" | 
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 539 | by (fact equal_refl) | 
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 540 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 541 | lemma [code]: | 
| 55416 | 542 | "case_pred f P = f (eval P)" | 
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 543 | by (cases P) simp | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 544 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 545 | lemma [code]: | 
| 55416 | 546 | "rec_pred f P = f (eval P)" | 
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 547 | by (cases P) simp | 
| 30328 | 548 | |
| 31105 
95f66b234086
added general preprocessing of equality in predicates for code generation
 bulwahn parents: 
30430diff
changeset | 549 | inductive eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" where "eq x x" | 
| 
95f66b234086
added general preprocessing of equality in predicates for code generation
 bulwahn parents: 
30430diff
changeset | 550 | |
| 
95f66b234086
added general preprocessing of equality in predicates for code generation
 bulwahn parents: 
30430diff
changeset | 551 | lemma eq_is_eq: "eq x y \<equiv> (x = y)" | 
| 31108 | 552 | by (rule eq_reflection) (auto intro: eq.intros elim: eq.cases) | 
| 30948 | 553 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 554 | primrec null :: "'a seq \<Rightarrow> bool" where | 
| 44415 | 555 | "null Empty \<longleftrightarrow> True" | 
| 556 | | "null (Insert x P) \<longleftrightarrow> False" | |
| 557 | | "null (Join P xq) \<longleftrightarrow> is_empty P \<and> null xq" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 558 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 559 | lemma null_is_empty: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 560 | "null xq \<longleftrightarrow> is_empty (pred_of_seq xq)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 561 | by (induct xq) (simp_all add: is_empty_bot not_is_empty_single is_empty_sup) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 562 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 563 | lemma is_empty_code [code]: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 564 | "is_empty (Seq f) \<longleftrightarrow> null (f ())" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 565 | by (simp add: null_is_empty Seq_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 566 | |
| 33111 | 567 | primrec the_only :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a seq \<Rightarrow> 'a" where | 
| 60166 | 568 | [code del]: "\<And>default. the_only default Empty = default ()" | 
| 569 | | "\<And>default. the_only default (Insert x P) = | |
| 570 | (if is_empty P then x else let y = singleton default P in if x = y then x else default ())" | |
| 571 | | "\<And>default. the_only default (Join P xq) = | |
| 572 | (if is_empty P then the_only default xq else if null xq then singleton default P | |
| 573 | else let x = singleton default P; y = the_only default xq in | |
| 574 | if x = y then x else default ())" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 575 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 576 | lemma the_only_singleton: | 
| 60166 | 577 | fixes default | 
| 578 | shows "the_only default xq = singleton default (pred_of_seq xq)" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 579 | by (induct xq) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 580 | (auto simp add: singleton_bot singleton_single is_empty_def | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 581 | null_is_empty Let_def singleton_sup) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 582 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 583 | lemma singleton_code [code]: | 
| 60166 | 584 | fixes default | 
| 585 | shows | |
| 586 | "singleton default (Seq f) = | |
| 587 | (case f () of | |
| 588 | Empty \<Rightarrow> default () | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 589 | | Insert x P \<Rightarrow> if is_empty P then x | 
| 60166 | 590 | else let y = singleton default P in | 
| 591 | if x = y then x else default () | |
| 592 | | Join P xq \<Rightarrow> if is_empty P then the_only default xq | |
| 593 | else if null xq then singleton default P | |
| 594 | else let x = singleton default P; y = the_only default xq in | |
| 595 | if x = y then x else default ())" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 596 | by (cases "f ()") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 597 | (auto simp add: Seq_def the_only_singleton is_empty_def | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 598 | null_is_empty singleton_bot singleton_single singleton_sup Let_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 599 | |
| 44414 | 600 | definition the :: "'a pred \<Rightarrow> 'a" where | 
| 37767 | 601 | "the A = (THE x. eval A x)" | 
| 33111 | 602 | |
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 603 | lemma the_eqI: | 
| 41080 | 604 | "(THE x. eval P x) = x \<Longrightarrow> the P = x" | 
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 605 | by (simp add: the_def) | 
| 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 606 | |
| 53943 | 607 | lemma the_eq [code]: "the A = singleton (\<lambda>x. Code.abort (STR ''not_unique'') (\<lambda>_. the A)) A" | 
| 608 | by (rule the_eqI) (simp add: singleton_def the_def) | |
| 33110 | 609 | |
| 36531 
19f6e3b0d9b6
code_reflect: specify module name directly after keyword
 haftmann parents: 
36513diff
changeset | 610 | code_reflect Predicate | 
| 36513 | 611 | datatypes pred = Seq and seq = Empty | Insert | Join | 
| 612 | ||
| 60758 | 613 | ML \<open> | 
| 30948 | 614 | signature PREDICATE = | 
| 615 | sig | |
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 616 |   val anamorph: ('a -> ('b * 'a) option) -> int -> 'a -> 'b list * 'a
 | 
| 30948 | 617 | datatype 'a pred = Seq of (unit -> 'a seq) | 
| 618 | and 'a seq = Empty | Insert of 'a * 'a pred | Join of 'a pred * 'a seq | |
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 619 |   val map: ('a -> 'b) -> 'a pred -> 'b pred
 | 
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 620 |   val yield: 'a pred -> ('a * 'a pred) option
 | 
| 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 621 | val yieldn: int -> 'a pred -> 'a list * 'a pred | 
| 30948 | 622 | end; | 
| 623 | ||
| 624 | structure Predicate : PREDICATE = | |
| 625 | struct | |
| 626 | ||
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 627 | fun anamorph f k x = | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 628 | (if k = 0 then ([], x) | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 629 | else case f x | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 630 | of NONE => ([], x) | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 631 | | SOME (v, y) => let | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 632 | val k' = k - 1; | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 633 | val (vs, z) = anamorph f k' y | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 634 | in (v :: vs, z) end); | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 635 | |
| 36513 | 636 | datatype pred = datatype Predicate.pred | 
| 637 | datatype seq = datatype Predicate.seq | |
| 638 | ||
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 639 | fun map f = @{code Predicate.map} f;
 | 
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 640 | |
| 36513 | 641 | fun yield (Seq f) = next (f ()) | 
| 642 | and next Empty = NONE | |
| 643 | | next (Insert (x, P)) = SOME (x, P) | |
| 644 | | next (Join (P, xq)) = (case yield P | |
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 645 | of NONE => next xq | 
| 36513 | 646 | | SOME (x, Q) => SOME (x, Seq (fn _ => Join (Q, xq)))); | 
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 647 | |
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 648 | fun yieldn k = anamorph yield k; | 
| 30948 | 649 | |
| 650 | end; | |
| 60758 | 651 | \<close> | 
| 30948 | 652 | |
| 60758 | 653 | text \<open>Conversion from and to sets\<close> | 
| 46038 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 654 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 655 | definition pred_of_set :: "'a set \<Rightarrow> 'a pred" where | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 656 | "pred_of_set = Pred \<circ> (\<lambda>A x. x \<in> A)" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 657 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 658 | lemma eval_pred_of_set [simp]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 659 | "eval (pred_of_set A) x \<longleftrightarrow> x \<in>A" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 660 | by (simp add: pred_of_set_def) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 661 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 662 | definition set_of_pred :: "'a pred \<Rightarrow> 'a set" where | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 663 | "set_of_pred = Collect \<circ> eval" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 664 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 665 | lemma member_set_of_pred [simp]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 666 | "x \<in> set_of_pred P \<longleftrightarrow> Predicate.eval P x" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 667 | by (simp add: set_of_pred_def) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 668 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 669 | definition set_of_seq :: "'a seq \<Rightarrow> 'a set" where | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 670 | "set_of_seq = set_of_pred \<circ> pred_of_seq" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 671 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 672 | lemma member_set_of_seq [simp]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 673 | "x \<in> set_of_seq xq = Predicate.member xq x" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 674 | by (simp add: set_of_seq_def eval_member) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 675 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 676 | lemma of_pred_code [code]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 677 | "set_of_pred (Predicate.Seq f) = (case f () of | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 678 |      Predicate.Empty \<Rightarrow> {}
 | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 679 | | Predicate.Insert x P \<Rightarrow> insert x (set_of_pred P) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 680 | | Predicate.Join P xq \<Rightarrow> set_of_pred P \<union> set_of_seq xq)" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 681 | by (auto split: seq.split simp add: eval_code) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 682 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 683 | lemma of_seq_code [code]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 684 |   "set_of_seq Predicate.Empty = {}"
 | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 685 | "set_of_seq (Predicate.Insert x P) = insert x (set_of_pred P)" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 686 | "set_of_seq (Predicate.Join P xq) = set_of_pred P \<union> set_of_seq xq" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 687 | by auto | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 688 | |
| 60758 | 689 | text \<open>Lazy Evaluation of an indexed function\<close> | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 690 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
51126diff
changeset | 691 | function iterate_upto :: "(natural \<Rightarrow> 'a) \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> 'a Predicate.pred" | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 692 | where | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 693 | "iterate_upto f n m = | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 694 | Predicate.Seq (%u. if n > m then Predicate.Empty | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 695 | else Predicate.Insert (f n) (iterate_upto f (n + 1) m))" | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 696 | by pat_completeness auto | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 697 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
51126diff
changeset | 698 | termination by (relation "measure (%(f, n, m). nat_of_natural (m + 1 - n))") | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
51126diff
changeset | 699 | (auto simp add: less_natural_def) | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 700 | |
| 60758 | 701 | text \<open>Misc\<close> | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 702 | |
| 47399 | 703 | declare Inf_set_fold [where 'a = "'a Predicate.pred", code] | 
| 704 | declare Sup_set_fold [where 'a = "'a Predicate.pred", code] | |
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 705 | |
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 706 | (* FIXME: better implement conversion by bisection *) | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 707 | |
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 708 | lemma pred_of_set_fold_sup: | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 709 | assumes "finite A" | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 710 | shows "pred_of_set A = Finite_Set.fold sup bot (Predicate.single ` A)" (is "?lhs = ?rhs") | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 711 | proof (rule sym) | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 712 | interpret comp_fun_idem "sup :: 'a Predicate.pred \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a Predicate.pred" | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 713 | by (fact comp_fun_idem_sup) | 
| 60758 | 714 | from \<open>finite A\<close> show "?rhs = ?lhs" by (induct A) (auto intro!: pred_eqI) | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 715 | qed | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 716 | |
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 717 | lemma pred_of_set_set_fold_sup: | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 718 | "pred_of_set (set xs) = fold sup (List.map Predicate.single xs) bot" | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 719 | proof - | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 720 | interpret comp_fun_idem "sup :: 'a Predicate.pred \<Rightarrow> 'a Predicate.pred \<Rightarrow> 'a Predicate.pred" | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 721 | by (fact comp_fun_idem_sup) | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 722 | show ?thesis by (simp add: pred_of_set_fold_sup fold_set_fold [symmetric]) | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 723 | qed | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 724 | |
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 725 | lemma pred_of_set_set_foldr_sup [code]: | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 726 | "pred_of_set (set xs) = foldr sup (List.map Predicate.single xs) bot" | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 727 | by (simp add: pred_of_set_set_fold_sup ac_simps foldr_fold fun_eq_iff) | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 728 | |
| 30328 | 729 | no_notation | 
| 62026 | 730 | bind (infixl "\<bind>" 70) | 
| 30328 | 731 | |
| 36176 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 wenzelm parents: 
36008diff
changeset | 732 | hide_type (open) pred seq | 
| 
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
 wenzelm parents: 
36008diff
changeset | 733 | hide_const (open) Pred eval single bind is_empty singleton if_pred not_pred holds | 
| 53943 | 734 | Empty Insert Join Seq member pred_of_seq "apply" adjunct null the_only eq map the | 
| 46664 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 735 | iterate_upto | 
| 
1f6c140f9c72
moved predicate relations and conversion rules between set and predicate relations from Predicate.thy to Relation.thy; moved Predicate.thy upwards in theory hierarchy
 haftmann parents: 
46638diff
changeset | 736 | hide_fact (open) null_def member_def | 
| 30328 | 737 | |
| 738 | end |