| author | wenzelm | 
| Tue, 29 Aug 2017 11:08:42 +0200 | |
| changeset 66542 | 075bbb78d33c | 
| parent 66251 | cd935b7cb3fb | 
| child 67091 | 1393c2340eec | 
| 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 | |
| 66012 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 13 | datatype (plugins only: extraction) (dead 'a) pred = Pred (eval: "'a \<Rightarrow> bool") | 
| 30328 | 14 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 15 | lemma pred_eqI: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 16 | "(\<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 | 17 | by (cases P, cases Q) (auto simp add: fun_eq_iff) | 
| 30328 | 18 | |
| 46038 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 19 | lemma pred_eq_iff: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 20 | "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 | 21 | by (simp add: pred_eqI) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 22 | |
| 44033 | 23 | instantiation pred :: (type) complete_lattice | 
| 30328 | 24 | begin | 
| 25 | ||
| 26 | definition | |
| 27 | "P \<le> Q \<longleftrightarrow> eval P \<le> eval Q" | |
| 28 | ||
| 29 | definition | |
| 30 | "P < Q \<longleftrightarrow> eval P < eval Q" | |
| 31 | ||
| 32 | definition | |
| 33 | "\<bottom> = Pred \<bottom>" | |
| 34 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 35 | lemma eval_bot [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 36 | "eval \<bottom> = \<bottom>" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 37 | by (simp add: bot_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 38 | |
| 30328 | 39 | definition | 
| 40 | "\<top> = Pred \<top>" | |
| 41 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 42 | lemma eval_top [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 43 | "eval \<top> = \<top>" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 44 | by (simp add: top_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 45 | |
| 30328 | 46 | definition | 
| 47 | "P \<sqinter> Q = Pred (eval P \<sqinter> eval Q)" | |
| 48 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 49 | lemma eval_inf [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 50 | "eval (P \<sqinter> Q) = eval P \<sqinter> eval Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 51 | by (simp add: inf_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 52 | |
| 30328 | 53 | definition | 
| 54 | "P \<squnion> Q = Pred (eval P \<squnion> eval Q)" | |
| 55 | ||
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 56 | lemma eval_sup [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 57 | "eval (P \<squnion> Q) = eval P \<squnion> eval Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 58 | by (simp add: sup_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 59 | |
| 30328 | 60 | 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 | 61 | "\<Sqinter>A = Pred (INFIMUM A eval)" | 
| 30328 | 62 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 63 | 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 | 64 | "eval (\<Sqinter>A) = INFIMUM A eval" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 65 | by (simp add: Inf_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 | "\<Squnion>A = Pred (SUPREMUM A eval)" | 
| 30328 | 69 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 70 | 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 | 71 | "eval (\<Squnion>A) = SUPREMUM A eval" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 72 | by (simp add: Sup_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 73 | |
| 44033 | 74 | instance proof | 
| 44415 | 75 | qed (auto intro!: pred_eqI simp add: less_eq_pred_def less_pred_def le_fun_def less_fun_def) | 
| 44033 | 76 | |
| 77 | end | |
| 78 | ||
| 56212 
3253aaf73a01
consolidated theorem names containing INFI and SUPR: have INF and SUP instead uniformly
 haftmann parents: 
56166diff
changeset | 79 | 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 | 80 | "eval (INFIMUM A f) = INFIMUM A (eval \<circ> f)" | 
| 56166 | 81 | using eval_Inf [of "f ` A"] by simp | 
| 44033 | 82 | |
| 56212 
3253aaf73a01
consolidated theorem names containing INFI and SUPR: have INF and SUP instead uniformly
 haftmann parents: 
56166diff
changeset | 83 | 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 | 84 | "eval (SUPREMUM A f) = SUPREMUM A (eval \<circ> f)" | 
| 56166 | 85 | using eval_Sup [of "f ` A"] by simp | 
| 44033 | 86 | |
| 87 | instantiation pred :: (type) complete_boolean_algebra | |
| 88 | begin | |
| 89 | ||
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 90 | definition | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 91 | "- P = Pred (- eval P)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 92 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 93 | lemma eval_compl [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 94 | "eval (- P) = - eval P" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 95 | by (simp add: uminus_pred_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 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 - Q = Pred (eval P - eval Q)" | 
| 
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_minus [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 101 | "eval (P - Q) = eval P - eval Q" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 102 | by (simp add: minus_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 | instance proof | 
| 46884 | 105 | qed (auto intro!: pred_eqI) | 
| 30328 | 106 | |
| 22259 
476604be7d88
New theory for converting between predicates and sets.
 berghofe parents: diff
changeset | 107 | end | 
| 30328 | 108 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 109 | definition single :: "'a \<Rightarrow> 'a pred" where | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 110 | "single x = Pred ((op =) x)" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 111 | |
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 112 | lemma eval_single [simp]: | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 113 | "eval (single x) = (op =) x" | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 114 | by (simp add: single_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 115 | |
| 62026 | 116 | definition bind :: "'a pred \<Rightarrow> ('a \<Rightarrow> 'b pred) \<Rightarrow> 'b pred" (infixl "\<bind>" 70) where
 | 
| 117 |   "P \<bind> f = (SUPREMUM {x. eval P x} f)"
 | |
| 40616 
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_bind [simp]: | 
| 62026 | 120 |   "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 | 121 | by (simp add: bind_def) | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 122 | |
| 30328 | 123 | lemma bind_bind: | 
| 62026 | 124 | "(P \<bind> Q) \<bind> R = P \<bind> (\<lambda>x. Q x \<bind> R)" | 
| 46884 | 125 | by (rule pred_eqI) auto | 
| 30328 | 126 | |
| 127 | lemma bind_single: | |
| 62026 | 128 | "P \<bind> single = P" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 129 | by (rule pred_eqI) auto | 
| 30328 | 130 | |
| 131 | lemma single_bind: | |
| 62026 | 132 | "single x \<bind> P = P x" | 
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 133 | by (rule pred_eqI) auto | 
| 30328 | 134 | |
| 135 | lemma bottom_bind: | |
| 62026 | 136 | "\<bottom> \<bind> P = \<bottom>" | 
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 137 | by (rule pred_eqI) auto | 
| 30328 | 138 | |
| 139 | lemma sup_bind: | |
| 62026 | 140 | "(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 | 141 | by (rule pred_eqI) auto | 
| 30328 | 142 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 143 | lemma Sup_bind: | 
| 62026 | 144 | "(\<Squnion>A \<bind> f) = \<Squnion>((\<lambda>x. x \<bind> f) ` A)" | 
| 46884 | 145 | by (rule pred_eqI) auto | 
| 30328 | 146 | |
| 147 | lemma pred_iffI: | |
| 148 | assumes "\<And>x. eval A x \<Longrightarrow> eval B x" | |
| 149 | and "\<And>x. eval B x \<Longrightarrow> eval A x" | |
| 150 | shows "A = B" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 151 | using assms by (auto intro: pred_eqI) | 
| 30328 | 152 | |
| 153 | lemma singleI: "eval (single x) x" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 154 | by simp | 
| 30328 | 155 | |
| 156 | lemma singleI_unit: "eval (single ()) x" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 157 | by simp | 
| 30328 | 158 | |
| 159 | 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 | 160 | by simp | 
| 30328 | 161 | |
| 162 | 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 | 163 | by simp | 
| 30328 | 164 | |
| 62026 | 165 | 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 | 166 | by auto | 
| 30328 | 167 | |
| 62026 | 168 | 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 | 169 | by auto | 
| 30328 | 170 | |
| 171 | lemma botE: "eval \<bottom> x \<Longrightarrow> P" | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 172 | by auto | 
| 30328 | 173 | |
| 174 | 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 | 175 | by auto | 
| 30328 | 176 | |
| 177 | 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 | 178 | by auto | 
| 30328 | 179 | |
| 180 | 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 | 181 | by auto | 
| 30328 | 182 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 183 | lemma single_not_bot [simp]: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 184 | "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 | 185 | 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 | 186 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 187 | lemma not_bot: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 188 | assumes "A \<noteq> \<bottom>" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 189 | obtains x where "eval A x" | 
| 45970 
b6d0cff57d96
adjusted to set/pred distinction by means of type constructor `set`
 haftmann parents: 
45630diff
changeset | 190 | 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 | 191 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 192 | |
| 60758 | 193 | subsection \<open>Emptiness check and definite choice\<close> | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 194 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 195 | definition is_empty :: "'a pred \<Rightarrow> bool" where | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 196 | "is_empty A \<longleftrightarrow> A = \<bottom>" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 197 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 198 | lemma is_empty_bot: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 199 | "is_empty \<bottom>" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 200 | by (simp add: is_empty_def) | 
| 
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 | lemma not_is_empty_single: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 203 | "\<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 | 204 | 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 | 205 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 206 | lemma is_empty_sup: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 207 | "is_empty (A \<squnion> B) \<longleftrightarrow> is_empty A \<and> is_empty B" | 
| 36008 | 208 | by (auto simp add: is_empty_def) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 209 | |
| 40616 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 210 | definition singleton :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a pred \<Rightarrow> 'a" where | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 211 | "singleton default A = (if \<exists>!x. eval A x then THE x. eval A x else default ())" for default | 
| 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 singleton_eqI: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 214 | "\<exists>!x. eval A x \<Longrightarrow> eval A x \<Longrightarrow> singleton default A = x" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 215 | by (auto simp add: singleton_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 216 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 217 | lemma eval_singletonI: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 218 | "\<exists>!x. eval A x \<Longrightarrow> eval A (singleton default A)" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 219 | proof - | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 220 | assume assm: "\<exists>!x. eval A x" | 
| 53374 
a14d2a854c02
tuned proofs -- clarified flow of facts wrt. calculation;
 wenzelm parents: 
51143diff
changeset | 221 | then obtain x where x: "eval A x" .. | 
| 60166 | 222 | 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 | 223 | with x show ?thesis by simp | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 224 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 225 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 226 | lemma single_singleton: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 227 | "\<exists>!x. eval A x \<Longrightarrow> single (singleton default A) = A" for default | 
| 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" | 
| 60166 | 230 | then have "eval A (singleton default A)" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 231 | by (rule eval_singletonI) | 
| 60166 | 232 | 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 | 233 | by (rule singleton_eqI) | 
| 60166 | 234 | 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 | 235 | by (simp (no_asm_use) add: single_def fun_eq_iff) blast | 
| 60166 | 236 | 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 | 237 | by simp | 
| 
c5ee1e06d795
eval simp rules for predicate type, simplify primitive proofs
 haftmann parents: 
39302diff
changeset | 238 | then show ?thesis by (rule pred_eqI) | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 239 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 240 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 241 | lemma singleton_undefinedI: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 242 | "\<not> (\<exists>!x. eval A x) \<Longrightarrow> singleton default A = default ()" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 243 | by (simp add: singleton_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 244 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 245 | lemma singleton_bot: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 246 | "singleton default \<bottom> = default ()" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 247 | by (auto simp add: bot_pred_def intro: singleton_undefinedI) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 248 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 249 | lemma singleton_single: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 250 | "singleton default (single x) = x" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 251 | by (auto simp add: intro: singleton_eqI singleI elim: singleE) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 252 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 253 | lemma singleton_sup_single_single: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 254 | "singleton default (single x \<squnion> single y) = (if x = y then x else default ())" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 255 | proof (cases "x = y") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 256 | case True then show ?thesis by (simp add: singleton_single) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 257 | next | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 258 | case False | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 259 | have "eval (single x \<squnion> single y) x" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 260 | and "eval (single x \<squnion> single y) y" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 261 | by (auto intro: supI1 supI2 singleI) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 262 | 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 | 263 | by blast | 
| 60166 | 264 | then have "singleton default (single x \<squnion> single y) = default ()" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 265 | by (rule singleton_undefinedI) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 266 | with False show ?thesis by simp | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 267 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 268 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 269 | lemma singleton_sup_aux: | 
| 60166 | 270 | "singleton default (A \<squnion> B) = (if A = \<bottom> then singleton default B | 
| 271 | else if B = \<bottom> then singleton default A | |
| 272 | else singleton default | |
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 273 | (single (singleton default A) \<squnion> single (singleton default B)))" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 274 | 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 | 275 | case True then show ?thesis by (simp add: single_singleton) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 276 | next | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 277 | case False | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 278 | from False have A_or_B: | 
| 60166 | 279 | "singleton default A = default () \<or> singleton default B = default ()" | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 280 | by (auto intro!: singleton_undefinedI) | 
| 60166 | 281 | then have rhs: "singleton default | 
| 282 | (single (singleton default A) \<squnion> single (singleton default B)) = default ()" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 283 | by (auto simp add: singleton_sup_single_single singleton_single) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 284 | from False have not_unique: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 285 | "\<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 | 286 | show ?thesis proof (cases "A \<noteq> \<bottom> \<and> B \<noteq> \<bottom>") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 287 | case True | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 288 | 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 | 289 | by (blast elim: not_bot) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 290 | 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 | 291 | by (auto simp add: sup_pred_def bot_pred_def) | 
| 60166 | 292 | 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 | 293 | with True rhs show ?thesis by simp | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 294 | next | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 295 | case False then show ?thesis by auto | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 296 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 297 | qed | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 298 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 299 | lemma singleton_sup: | 
| 60166 | 300 | "singleton default (A \<squnion> B) = (if A = \<bottom> then singleton default B | 
| 301 | else if B = \<bottom> then singleton default A | |
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 302 | else if singleton default A = singleton default B then singleton default A else default ())" for default | 
| 60166 | 303 | 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 | 304 | |
| 30328 | 305 | |
| 60758 | 306 | subsection \<open>Derived operations\<close> | 
| 30328 | 307 | |
| 308 | definition if_pred :: "bool \<Rightarrow> unit pred" where | |
| 309 | if_pred_eq: "if_pred b = (if b then single () else \<bottom>)" | |
| 310 | ||
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 311 | definition holds :: "unit pred \<Rightarrow> bool" where | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 312 | holds_eq: "holds P = eval P ()" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 313 | |
| 30328 | 314 | definition not_pred :: "unit pred \<Rightarrow> unit pred" where | 
| 315 | not_pred_eq: "not_pred P = (if eval P () then \<bottom> else single ())" | |
| 316 | ||
| 317 | lemma if_predI: "P \<Longrightarrow> eval (if_pred P) ()" | |
| 318 | unfolding if_pred_eq by (auto intro: singleI) | |
| 319 | ||
| 320 | lemma if_predE: "eval (if_pred b) x \<Longrightarrow> (b \<Longrightarrow> x = () \<Longrightarrow> P) \<Longrightarrow> P" | |
| 321 | unfolding if_pred_eq by (cases b) (auto elim: botE) | |
| 322 | ||
| 323 | lemma not_predI: "\<not> P \<Longrightarrow> eval (not_pred (Pred (\<lambda>u. P))) ()" | |
| 66012 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 324 | unfolding not_pred_eq by (auto intro: singleI) | 
| 30328 | 325 | |
| 326 | lemma not_predI': "\<not> eval P () \<Longrightarrow> eval (not_pred P) ()" | |
| 327 | unfolding not_pred_eq by (auto intro: singleI) | |
| 328 | ||
| 329 | lemma not_predE: "eval (not_pred (Pred (\<lambda>u. P))) x \<Longrightarrow> (\<not> P \<Longrightarrow> thesis) \<Longrightarrow> thesis" | |
| 330 | unfolding not_pred_eq | |
| 62390 | 331 | by (auto split: if_split_asm elim: botE) | 
| 30328 | 332 | |
| 333 | lemma not_predE': "eval (not_pred P) x \<Longrightarrow> (\<not> eval P x \<Longrightarrow> thesis) \<Longrightarrow> thesis" | |
| 334 | unfolding not_pred_eq | |
| 62390 | 335 | 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 | 336 | lemma "f () = False \<or> f () = True" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 337 | by simp | 
| 30328 | 338 | |
| 37549 | 339 | lemma closure_of_bool_cases [no_atp]: | 
| 44007 | 340 | fixes f :: "unit \<Rightarrow> bool" | 
| 341 | assumes "f = (\<lambda>u. False) \<Longrightarrow> P f" | |
| 342 | assumes "f = (\<lambda>u. True) \<Longrightarrow> P f" | |
| 343 | shows "P f" | |
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 344 | proof - | 
| 44007 | 345 | 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 | 346 | apply (cases "f ()") | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 347 | apply (rule disjI2) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 348 | apply (rule ext) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 349 | apply (simp add: unit_eq) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 350 | apply (rule disjI1) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 351 | apply (rule ext) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 352 | apply (simp add: unit_eq) | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 353 | done | 
| 41550 | 354 | 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 | 355 | qed | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 356 | |
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 357 | lemma unit_pred_cases: | 
| 44007 | 358 | assumes "P \<bottom>" | 
| 359 | assumes "P (single ())" | |
| 360 | shows "P Q" | |
| 44415 | 361 | using assms unfolding bot_pred_def bot_fun_def bot_bool_def empty_def single_def proof (cases Q) | 
| 44007 | 362 | fix f | 
| 363 | assume "P (Pred (\<lambda>u. False))" "P (Pred (\<lambda>u. () = u))" | |
| 364 | then have "P (Pred f)" | |
| 365 | by (cases _ f rule: closure_of_bool_cases) simp_all | |
| 366 | moreover assume "Q = Pred f" | |
| 367 | ultimately show "P Q" by simp | |
| 368 | qed | |
| 369 | ||
| 33754 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 370 | lemma holds_if_pred: | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 371 | "holds (if_pred b) = b" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 372 | unfolding if_pred_eq holds_eq | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 373 | 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 | 374 | |
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 375 | lemma if_pred_holds: | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 376 | "if_pred (holds P) = P" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 377 | unfolding if_pred_eq holds_eq | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 378 | 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 | 379 | |
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 380 | lemma is_empty_holds: | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 381 | "is_empty P \<longleftrightarrow> \<not> holds P" | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 382 | unfolding is_empty_def holds_eq | 
| 
f2957bd46faf
adding derived constant Predicate.holds to Predicate theory; adopting the predicate compiler
 bulwahn parents: 
33622diff
changeset | 383 | by (rule unit_pred_cases) (auto elim: botE intro: singleI) | 
| 30328 | 384 | |
| 41311 | 385 | definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pred \<Rightarrow> 'b pred" where
 | 
| 62026 | 386 | "map f P = P \<bind> (single o f)" | 
| 41311 | 387 | |
| 388 | lemma eval_map [simp]: | |
| 44363 | 389 |   "eval (map f P) = (\<Squnion>x\<in>{x. eval P x}. (\<lambda>y. f x = y))"
 | 
| 44415 | 390 | by (auto simp add: map_def comp_def) | 
| 41311 | 391 | |
| 55467 
a5c9002bc54d
renamed 'enriched_type' to more informative 'functor' (following the renaming of enriched type constructors to bounded natural functors)
 blanchet parents: 
55416diff
changeset | 392 | functor map: map | 
| 44363 | 393 | by (rule ext, rule pred_eqI, auto)+ | 
| 41311 | 394 | |
| 395 | ||
| 60758 | 396 | subsection \<open>Implementation\<close> | 
| 30328 | 397 | |
| 58350 
919149921e46
added 'extraction' plugins -- this might help 'HOL-Proofs'
 blanchet parents: 
58334diff
changeset | 398 | datatype (plugins only: code extraction) (dead 'a) seq = | 
| 58334 | 399 | Empty | 
| 400 | | Insert "'a" "'a pred" | |
| 401 | | Join "'a pred" "'a seq" | |
| 30328 | 402 | |
| 403 | primrec pred_of_seq :: "'a seq \<Rightarrow> 'a pred" where | |
| 44414 | 404 | "pred_of_seq Empty = \<bottom>" | 
| 405 | | "pred_of_seq (Insert x P) = single x \<squnion> P" | |
| 406 | | "pred_of_seq (Join P xq) = P \<squnion> pred_of_seq xq" | |
| 30328 | 407 | |
| 408 | definition Seq :: "(unit \<Rightarrow> 'a seq) \<Rightarrow> 'a pred" where | |
| 409 | "Seq f = pred_of_seq (f ())" | |
| 410 | ||
| 411 | code_datatype Seq | |
| 412 | ||
| 413 | primrec member :: "'a seq \<Rightarrow> 'a \<Rightarrow> bool" where | |
| 414 | "member Empty x \<longleftrightarrow> False" | |
| 44414 | 415 | | "member (Insert y P) x \<longleftrightarrow> x = y \<or> eval P x" | 
| 416 | | "member (Join P xq) x \<longleftrightarrow> eval P x \<or> member xq x" | |
| 30328 | 417 | |
| 418 | lemma eval_member: | |
| 419 | "member xq = eval (pred_of_seq xq)" | |
| 420 | proof (induct xq) | |
| 421 | 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 | 422 | by (auto simp add: fun_eq_iff elim: botE) | 
| 30328 | 423 | next | 
| 424 | 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 | 425 | by (auto simp add: fun_eq_iff elim: supE singleE intro: supI1 supI2 singleI) | 
| 30328 | 426 | next | 
| 427 | 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 | 428 | by (auto simp add: fun_eq_iff elim: supE intro: supI1 supI2) | 
| 30328 | 429 | qed | 
| 430 | ||
| 46038 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 431 | lemma eval_code [(* FIXME declare simp *)code]: "eval (Seq f) = member (f ())" | 
| 30328 | 432 | unfolding Seq_def by (rule sym, rule eval_member) | 
| 433 | ||
| 434 | lemma single_code [code]: | |
| 435 | "single x = Seq (\<lambda>u. Insert x \<bottom>)" | |
| 436 | unfolding Seq_def by simp | |
| 437 | ||
| 41080 | 438 | primrec "apply" :: "('a \<Rightarrow> 'b pred) \<Rightarrow> 'a seq \<Rightarrow> 'b seq" where
 | 
| 44415 | 439 | "apply f Empty = Empty" | 
| 62026 | 440 | | "apply f (Insert x P) = Join (f x) (Join (P \<bind> f) Empty)" | 
| 441 | | "apply f (Join P xq) = Join (P \<bind> f) (apply f xq)" | |
| 30328 | 442 | |
| 443 | lemma apply_bind: | |
| 62026 | 444 | "pred_of_seq (apply f xq) = pred_of_seq xq \<bind> f" | 
| 30328 | 445 | proof (induct xq) | 
| 446 | case Empty show ?case | |
| 447 | by (simp add: bottom_bind) | |
| 448 | next | |
| 449 | case Insert show ?case | |
| 450 | by (simp add: single_bind sup_bind) | |
| 451 | next | |
| 452 | case Join then show ?case | |
| 453 | by (simp add: sup_bind) | |
| 454 | qed | |
| 455 | ||
| 456 | lemma bind_code [code]: | |
| 62026 | 457 | "Seq g \<bind> f = Seq (\<lambda>u. apply f (g ()))" | 
| 30328 | 458 | unfolding Seq_def by (rule sym, rule apply_bind) | 
| 459 | ||
| 460 | lemma bot_set_code [code]: | |
| 461 | "\<bottom> = Seq (\<lambda>u. Empty)" | |
| 462 | unfolding Seq_def by simp | |
| 463 | ||
| 30376 | 464 | primrec adjunct :: "'a pred \<Rightarrow> 'a seq \<Rightarrow> 'a seq" where | 
| 44415 | 465 | "adjunct P Empty = Join P Empty" | 
| 466 | | "adjunct P (Insert x Q) = Insert x (Q \<squnion> P)" | |
| 467 | | "adjunct P (Join Q xq) = Join Q (adjunct P xq)" | |
| 30376 | 468 | |
| 469 | lemma adjunct_sup: | |
| 470 | "pred_of_seq (adjunct P xq) = P \<squnion> pred_of_seq xq" | |
| 471 | by (induct xq) (simp_all add: sup_assoc sup_commute sup_left_commute) | |
| 472 | ||
| 30328 | 473 | lemma sup_code [code]: | 
| 474 | "Seq f \<squnion> Seq g = Seq (\<lambda>u. case f () | |
| 475 | of Empty \<Rightarrow> g () | |
| 476 | | Insert x P \<Rightarrow> Insert x (P \<squnion> Seq g) | |
| 30376 | 477 | | Join P xq \<Rightarrow> adjunct (Seq g) (Join P xq))" | 
| 30328 | 478 | proof (cases "f ()") | 
| 479 | case Empty | |
| 480 | thus ?thesis | |
| 34007 
aea892559fc5
tuned lattices theory fragements; generlized some lemmas from sets to lattices
 haftmann parents: 
33988diff
changeset | 481 | unfolding Seq_def by (simp add: sup_commute [of "\<bottom>"]) | 
| 30328 | 482 | next | 
| 483 | case Insert | |
| 484 | thus ?thesis | |
| 485 | unfolding Seq_def by (simp add: sup_assoc) | |
| 486 | next | |
| 487 | case Join | |
| 488 | thus ?thesis | |
| 30376 | 489 | unfolding Seq_def | 
| 490 | by (simp add: adjunct_sup sup_assoc sup_commute sup_left_commute) | |
| 30328 | 491 | qed | 
| 492 | ||
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 493 | primrec contained :: "'a seq \<Rightarrow> 'a pred \<Rightarrow> bool" where | 
| 44415 | 494 | "contained Empty Q \<longleftrightarrow> True" | 
| 495 | | "contained (Insert x P) Q \<longleftrightarrow> eval Q x \<and> P \<le> Q" | |
| 496 | | "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 | 497 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 498 | lemma single_less_eq_eval: | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 499 | "single x \<le> P \<longleftrightarrow> eval P x" | 
| 44415 | 500 | 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 | 501 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 502 | lemma contained_less_eq: | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 503 | "contained xq Q \<longleftrightarrow> pred_of_seq xq \<le> Q" | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 504 | by (induct xq) (simp_all add: single_less_eq_eval) | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 505 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 506 | lemma less_eq_pred_code [code]: | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 507 | "Seq f \<le> Q = (case f () | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 508 | of Empty \<Rightarrow> True | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 509 | | 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 | 510 | | 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 | 511 | by (cases "f ()") | 
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 512 | (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 | 513 | |
| 66012 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 514 | instantiation pred :: (type) equal | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 515 | begin | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 516 | |
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 517 | definition equal_pred | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 518 | where [simp]: "HOL.equal P Q \<longleftrightarrow> P = (Q :: 'a pred)" | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 519 | |
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 520 | instance by standard simp | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 521 | |
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 522 | end | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 523 | |
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 524 | lemma [code]: | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 525 | "HOL.equal P Q \<longleftrightarrow> P \<le> Q \<and> Q \<le> P" for P Q :: "'a pred" | 
| 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 526 | by auto | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 527 | |
| 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 528 | lemma [code nbe]: | 
| 66012 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 529 | "HOL.equal P P \<longleftrightarrow> True" for P :: "'a pred" | 
| 38857 
97775f3e8722
renamed class/constant eq to equal; tuned some instantiations
 haftmann parents: 
38651diff
changeset | 530 | by (fact equal_refl) | 
| 30430 
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 [code]: | 
| 55416 | 533 | "case_pred f P = f (eval P)" | 
| 66012 
59bf29d2b3a1
modernized (code) setup for enumeration predicates
 haftmann parents: 
62390diff
changeset | 534 | by (fact pred.case_eq_if) | 
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 535 | |
| 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 536 | lemma [code]: | 
| 55416 | 537 | "rec_pred f P = f (eval P)" | 
| 30430 
42ea5d85edcc
explicit code equations for some rarely used pred operations
 haftmann parents: 
30378diff
changeset | 538 | by (cases P) simp | 
| 30328 | 539 | |
| 31105 
95f66b234086
added general preprocessing of equality in predicates for code generation
 bulwahn parents: 
30430diff
changeset | 540 | 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 | 541 | |
| 
95f66b234086
added general preprocessing of equality in predicates for code generation
 bulwahn parents: 
30430diff
changeset | 542 | lemma eq_is_eq: "eq x y \<equiv> (x = y)" | 
| 31108 | 543 | by (rule eq_reflection) (auto intro: eq.intros elim: eq.cases) | 
| 30948 | 544 | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 545 | primrec null :: "'a seq \<Rightarrow> bool" where | 
| 44415 | 546 | "null Empty \<longleftrightarrow> True" | 
| 547 | | "null (Insert x P) \<longleftrightarrow> False" | |
| 548 | | "null (Join P xq) \<longleftrightarrow> is_empty P \<and> null xq" | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 549 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 550 | lemma null_is_empty: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 551 | "null xq \<longleftrightarrow> is_empty (pred_of_seq xq)" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 552 | 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 | 553 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 554 | lemma is_empty_code [code]: | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 555 | "is_empty (Seq f) \<longleftrightarrow> null (f ())" | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 556 | by (simp add: null_is_empty Seq_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 557 | |
| 33111 | 558 | primrec the_only :: "(unit \<Rightarrow> 'a) \<Rightarrow> 'a seq \<Rightarrow> 'a" where | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 559 | "the_only default Empty = default ()" for default | 
| 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 560 | | "the_only default (Insert x P) = | 
| 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 561 | (if is_empty P then x else let y = singleton default P in if x = y then x else default ())" for default | 
| 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 562 | | "the_only default (Join P xq) = | 
| 60166 | 563 | (if is_empty P then the_only default xq else if null xq then singleton default P | 
| 564 | else let x = singleton default P; y = the_only default xq in | |
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 565 | if x = y then x else default ())" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 566 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 567 | lemma the_only_singleton: | 
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 568 | "the_only default xq = singleton default (pred_of_seq xq)" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 569 | by (induct xq) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 570 | (auto simp add: singleton_bot singleton_single is_empty_def | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 571 | null_is_empty Let_def singleton_sup) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 572 | |
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 573 | lemma singleton_code [code]: | 
| 60166 | 574 | "singleton default (Seq f) = | 
| 575 | (case f () of | |
| 576 | Empty \<Rightarrow> default () | |
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 577 | | Insert x P \<Rightarrow> if is_empty P then x | 
| 60166 | 578 | else let y = singleton default P in | 
| 579 | if x = y then x else default () | |
| 580 | | Join P xq \<Rightarrow> if is_empty P then the_only default xq | |
| 581 | else if null xq then singleton default P | |
| 582 | else let x = singleton default P; y = the_only default xq in | |
| 66251 
cd935b7cb3fb
proper concept of code declaration wrt. atomicity and Isar declarations
 haftmann parents: 
66012diff
changeset | 583 | if x = y then x else default ())" for default | 
| 32578 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 584 | by (cases "f ()") | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 585 | (auto simp add: Seq_def the_only_singleton is_empty_def | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 586 | null_is_empty singleton_bot singleton_single singleton_sup Let_def) | 
| 
22117a76f943
added emptiness check predicate and singleton projection
 haftmann parents: 
32372diff
changeset | 587 | |
| 44414 | 588 | definition the :: "'a pred \<Rightarrow> 'a" where | 
| 37767 | 589 | "the A = (THE x. eval A x)" | 
| 33111 | 590 | |
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 591 | lemma the_eqI: | 
| 41080 | 592 | "(THE x. eval P x) = x \<Longrightarrow> the P = x" | 
| 40674 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 593 | by (simp add: the_def) | 
| 
54dbe6a1c349
adhere established Collect/mem convention more closely
 haftmann parents: 
40616diff
changeset | 594 | |
| 53943 | 595 | lemma the_eq [code]: "the A = singleton (\<lambda>x. Code.abort (STR ''not_unique'') (\<lambda>_. the A)) A" | 
| 596 | by (rule the_eqI) (simp add: singleton_def the_def) | |
| 33110 | 597 | |
| 36531 
19f6e3b0d9b6
code_reflect: specify module name directly after keyword
 haftmann parents: 
36513diff
changeset | 598 | code_reflect Predicate | 
| 36513 | 599 | datatypes pred = Seq and seq = Empty | Insert | Join | 
| 600 | ||
| 60758 | 601 | ML \<open> | 
| 30948 | 602 | signature PREDICATE = | 
| 603 | sig | |
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 604 |   val anamorph: ('a -> ('b * 'a) option) -> int -> 'a -> 'b list * 'a
 | 
| 30948 | 605 | datatype 'a pred = Seq of (unit -> 'a seq) | 
| 606 | 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 | 607 |   val map: ('a -> 'b) -> 'a pred -> 'b pred
 | 
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 608 |   val yield: 'a pred -> ('a * 'a pred) option
 | 
| 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 609 | val yieldn: int -> 'a pred -> 'a list * 'a pred | 
| 30948 | 610 | end; | 
| 611 | ||
| 612 | structure Predicate : PREDICATE = | |
| 613 | struct | |
| 614 | ||
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 615 | fun anamorph f k x = | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 616 | (if k = 0 then ([], x) | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 617 | else case f x | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 618 | of NONE => ([], x) | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 619 | | SOME (v, y) => let | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 620 | val k' = k - 1; | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 621 | val (vs, z) = anamorph f k' y | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 622 | in (v :: vs, z) end); | 
| 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 623 | |
| 36513 | 624 | datatype pred = datatype Predicate.pred | 
| 625 | datatype seq = datatype Predicate.seq | |
| 626 | ||
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 627 | fun map f = @{code Predicate.map} f;
 | 
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 628 | |
| 36513 | 629 | fun yield (Seq f) = next (f ()) | 
| 630 | and next Empty = NONE | |
| 631 | | next (Insert (x, P)) = SOME (x, P) | |
| 632 | | next (Join (P, xq)) = (case yield P | |
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 633 | of NONE => next xq | 
| 36513 | 634 | | SOME (x, Q) => SOME (x, Seq (fn _ => Join (Q, xq)))); | 
| 30959 
458e55fd0a33
fixed compilation of predicate types in ML environment
 haftmann parents: 
30948diff
changeset | 635 | |
| 51126 
df86080de4cb
reform of predicate compiler / quickcheck theories:
 haftmann parents: 
51112diff
changeset | 636 | fun yieldn k = anamorph yield k; | 
| 30948 | 637 | |
| 638 | end; | |
| 60758 | 639 | \<close> | 
| 30948 | 640 | |
| 60758 | 641 | 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 | 642 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 643 | 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 | 644 | "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 | 645 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 646 | lemma eval_pred_of_set [simp]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 647 | "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 | 648 | by (simp add: pred_of_set_def) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 649 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 650 | 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 | 651 | "set_of_pred = Collect \<circ> eval" | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 652 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 653 | lemma member_set_of_pred [simp]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 654 | "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 | 655 | by (simp add: set_of_pred_def) | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 656 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 657 | 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 | 658 | "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 | 659 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 660 | lemma member_set_of_seq [simp]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 661 | "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 | 662 | 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 | 663 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 664 | lemma of_pred_code [code]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 665 | "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 | 666 |      Predicate.Empty \<Rightarrow> {}
 | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 667 | | 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 | 668 | | 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 | 669 | 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 | 670 | |
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 671 | lemma of_seq_code [code]: | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 672 |   "set_of_seq Predicate.Empty = {}"
 | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 673 | "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 | 674 | "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 | 675 | by auto | 
| 
bb2f7488a0f1
conversions from sets to predicates and vice versa; extensionality on predicates
 haftmann parents: 
45970diff
changeset | 676 | |
| 60758 | 677 | 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 | 678 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
51126diff
changeset | 679 | 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 | 680 | 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 | 681 | "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 | 682 | 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 | 683 | 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 | 684 | 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 | 685 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
51126diff
changeset | 686 | 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 | 687 | (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 | 688 | |
| 60758 | 689 | 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 | 690 | |
| 47399 | 691 | declare Inf_set_fold [where 'a = "'a Predicate.pred", code] | 
| 692 | 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 | 693 | |
| 
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 | (* 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 | 695 | |
| 
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 | 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 | 697 | 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 | 698 | 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 | 699 | 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 | 700 | 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 | 701 | by (fact comp_fun_idem_sup) | 
| 60758 | 702 | 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 | 703 | 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 | 704 | |
| 
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 | 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 | 706 | "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 | 707 | 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 | 708 | 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 | 709 | 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 | 710 | 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 | 711 | 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 | 712 | |
| 
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 | 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 | 714 | "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 | 715 | 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 | 716 | |
| 30328 | 717 | no_notation | 
| 62026 | 718 | bind (infixl "\<bind>" 70) | 
| 30328 | 719 | |
| 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 | 720 | 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 | 721 | hide_const (open) Pred eval single bind is_empty singleton if_pred not_pred holds | 
| 53943 | 722 | 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 | 723 | 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 | 724 | hide_fact (open) null_def member_def | 
| 30328 | 725 | |
| 726 | end |