|
| 1 | += Rel : Properties of Relations |
| 2 | + |
| 3 | +> module Rel |
| 4 | + |
| 5 | +This short (and optional) chapter develops some basic definitions and a few |
| 6 | +theorems about binary relations in Idris. The key definitions are repeated where |
| 7 | +they are actually used (in the `Smallstep` chapter), so readers who are already |
| 8 | +comfortable with these ideas can safely skim or skip this chapter. However, |
| 9 | +relations are also a good source of exercises for developing facility with |
| 10 | +Idris's basic reasoning facilities, so it may be useful to look at this material |
| 11 | +just after the `IndProp` chapter. |
| 12 | + |
| 13 | +Require Export IndProp. |
| 14 | + |
| 15 | +A binary _relation_ on a set \idr{t} is a family of propositions parameterized |
| 16 | +by two elements of \idr{t} — i.e., a proposition about pairs of elements of |
| 17 | +\idr{t}. |
| 18 | + |
| 19 | +> Relation : Type -> Type |
| 20 | +> Relation t = t -> t -> Type |
| 21 | + |
| 22 | +\todo[inline]{Edit} |
| 23 | + |
| 24 | +Confusingly, the Idris standard library hijacks the generic term "relation" for |
| 25 | +this specific instance of the idea. To maintain consistency with the library, we |
| 26 | +will do the same. So, henceforth the Idris identifier `relation` will always |
| 27 | +refer to a binary relation between some set and itself, whereas the English word |
| 28 | +"relation" can refer either to the specific Idris concept or the more general |
| 29 | +concept of a relation between any number of possibly different sets. The context |
| 30 | +of the discussion should always make clear which is meant. |
| 31 | + |
| 32 | +\todo[inline]{Called \idr{LTE} in \idr{Prelude.Nat}, but defined via induction |
| 33 | +from zero there} |
| 34 | + |
| 35 | +An example relation on \idr{Nat} is \idr{Le}, the less-than-or-equal-to |
| 36 | +relation, which we usually write \idr{n1 <= n2}. |
| 37 | + |
| 38 | +\todo[inline]{Copied from `IndProp`} |
| 39 | + |
| 40 | +> data Le : (n : Nat) -> Nat -> Type where |
| 41 | +> Le_n : Le n n |
| 42 | +> Le_S : Le n m -> Le n (S m) |
| 43 | +> |
| 44 | +> syntax [m] "<='" [n] = Le m n |
| 45 | + |
| 46 | +```idris |
| 47 | +λΠ> the (Relation Nat) Le |
| 48 | +Le : Nat -> Nat -> Type |
| 49 | +``` |
| 50 | + |
| 51 | +\todo[inline]{Edit, it probably doesn't matter in Idris} |
| 52 | + |
| 53 | +(Why did we write it this way instead of starting with \idr{data Le : Relation |
| 54 | +Nat ...}? Because we wanted to put the first \idr{Nat} to the left of the |
| 55 | +\idr{:}, which makes Idris generate a somewhat nicer induction principle for |
| 56 | +reasoning about \idr{<='}.) |
| 57 | +
|
| 58 | +
|
| 59 | +== Basic Properties |
| 60 | +
|
| 61 | +As anyone knows who has taken an undergraduate discrete math course, there is a |
| 62 | +lot to be said about relations in general, including ways of classifying |
| 63 | +relations (as reflexive, transitive, etc.), theorems that can be proved |
| 64 | +generically about certain sorts of relations, constructions that build one |
| 65 | +relation from another, etc. For example... |
| 66 | +
|
| 67 | +
|
| 68 | +=== Partial Functions |
| 69 | +
|
| 70 | +A relation \idr{r} on a set \idr{t} is a partial function if, for every \idr{x}, |
| 71 | +there is at most one \idr{y} such that \idr{r x y} — i.e., \idr{r x y1} and |
| 72 | +\idr{r x y2} together imply \idr{y1 = y2}. |
| 73 | +
|
| 74 | +> Partial_function : (r : Relation t) -> Type |
| 75 | +> Partial_function {t} r = (x, y1, y2 : t) -> r x y1 -> r x y2 -> y1 = y2 |
| 76 | +
|
| 77 | +For example, the \idr{Next_nat} relation defined earlier is a partial function. |
| 78 | +
|
| 79 | +> data Next_nat : (n : Nat) -> Nat -> Type where |
| 80 | +> NN : Next_nat n (S n) |
| 81 | +
|
| 82 | +```idris |
| 83 | +λΠ> the (Relation Nat) Next_nat |
| 84 | +Next_nat : Nat -> Nat -> Type |
| 85 | +``` |
| 86 | +
|
| 87 | +> next_nat_partial_function : Partial_function Next_nat |
| 88 | +> next_nat_partial_function x (S x) (S x) NN NN = Refl |
| 89 | +
|
| 90 | +However, the \idr{<='} relation on numbers is not a partial function. (Assume, |
| 91 | +for a contradiction, that \idr{<='} is a partial function. But then, since |
| 92 | +\idr{0 <=' 0} and \idr{0 <=' 1}, it follows that \idr{0 = 1}. This is nonsense, |
| 93 | +so our assumption was contradictory.) |
| 94 | +
|
| 95 | +> le_not_a_partial_function : Not (Partial_function Le) |
| 96 | +> le_not_a_partial_function f = ?le_not_a_partial_function_rhs |
| 97 | +
|
| 98 | +
|
| 99 | +==== Exercise: 2 stars, optional |
| 100 | +
|
| 101 | +Show that the total_relation defined in earlier is not a partial function. |
| 102 | +
|
| 103 | +(* FILL IN HERE *) |
| 104 | +
|
| 105 | +$\square$ |
| 106 | +
|
| 107 | +
|
| 108 | +==== Exercise: 2 stars, optional |
| 109 | +
|
| 110 | +Show that the empty_relation that we defined earlier is a partial function. |
| 111 | +
|
| 112 | +(* FILL IN HERE *) |
| 113 | +
|
| 114 | +$\square$ |
| 115 | +
|
| 116 | +
|
| 117 | +=== Reflexive Relations |
| 118 | +
|
| 119 | +A reflexive relation on a set X is one for which every element of X is related |
| 120 | +to itself. |
| 121 | +
|
| 122 | +Definition reflexive {X: Type} (R: relation X) := |
| 123 | + ∀a : X, R a a. |
| 124 | +
|
| 125 | +Theorem le_reflexive : |
| 126 | + reflexive le. |
| 127 | +
|
| 128 | +
|
| 129 | +=== Transitive Relations |
| 130 | +
|
| 131 | +A relation R is transitive if R a c holds whenever R a b and R b c do. |
| 132 | +
|
| 133 | +Definition transitive {X: Type} (R: relation X) := |
| 134 | + ∀a b c : X, (R a b) -> (R b c) -> (R a c). |
| 135 | +
|
| 136 | +Theorem le_trans : |
| 137 | + transitive le. |
| 138 | +
|
| 139 | +Theorem lt_trans: |
| 140 | + transitive lt. |
| 141 | +
|
| 142 | +
|
| 143 | +==== Exercise: 2 stars, optional |
| 144 | +
|
| 145 | +We can also prove lt_trans more laboriously by induction, without using |
| 146 | +le_trans. Do this. |
| 147 | +
|
| 148 | +Theorem lt_trans' : |
| 149 | + transitive lt. |
| 150 | +Proof. |
| 151 | + (* Prove this by induction on evidence that m is less than o. *) |
| 152 | + unfold lt. unfold transitive. |
| 153 | + intros n m o Hnm Hmo. |
| 154 | + induction Hmo as [| m' Hm'o]. |
| 155 | + (* FILL IN HERE *) Admitted. |
| 156 | + |
| 157 | +$\square$ |
| 158 | + |
| 159 | + |
| 160 | +==== Exercise: 2 stars, optional |
| 161 | + |
| 162 | +Prove the same thing again by induction on o. |
| 163 | + |
| 164 | +Theorem lt_trans'' : |
| 165 | + transitive lt. |
| 166 | + |
| 167 | +$\square$ |
| 168 | + |
| 169 | +The transitivity of le, in turn, can be used to prove some facts that will be |
| 170 | +useful later (e.g., for the proof of antisymmetry below)... |
| 171 | + |
| 172 | +Theorem le_Sn_le : ∀n m, S n ≤ m -> n ≤ m. |
| 173 | + |
| 174 | +==== Exercise: 1 star, optional |
| 175 | + |
| 176 | +Theorem le_S_n : ∀n m, |
| 177 | + (S n ≤ S m) -> (n ≤ m). |
| 178 | +Proof. |
| 179 | + (* FILL IN HERE *) Admitted. |
| 180 | + |
| 181 | +$\square$ |
| 182 | + |
| 183 | + |
| 184 | +==== Exercise: 2 stars, optional (le_Sn_n_inf) |
| 185 | + |
| 186 | +Provide an informal proof of the following theorem: |
| 187 | + |
| 188 | +Theorem: For every n, ¬ (S n ≤ n) |
| 189 | + |
| 190 | +A formal proof of this is an optional exercise below, but try writing an |
| 191 | +informal proof without doing the formal proof first. |
| 192 | + |
| 193 | +Proof: (* FILL IN HERE *) |
| 194 | + |
| 195 | +$\square$ |
| 196 | + |
| 197 | + |
| 198 | +==== Exercise: 1 star, optional |
| 199 | + |
| 200 | +Theorem le_Sn_n : ∀n, |
| 201 | + ¬ (S n ≤ n). |
| 202 | +Proof. |
| 203 | + (* FILL IN HERE *) Admitted. |
| 204 | + |
| 205 | +$\square$ |
| 206 | + |
| 207 | +Reflexivity and transitivity are the main concepts we'll need for later |
| 208 | +chapters, but, for a bit of additional practice working with relations in Idris, |
| 209 | +let's look at a few other common ones... |
| 210 | + |
| 211 | + |
| 212 | +=== Symmetric and Antisymmetric Relations |
| 213 | + |
| 214 | +A relation R is symmetric if R a b implies R b a. |
| 215 | + |
| 216 | +Definition symmetric {X: Type} (R: relation X) := |
| 217 | + ∀a b : X, (R a b) -> (R b a). |
| 218 | + |
| 219 | + |
| 220 | +==== Exercise: 2 stars, optional |
| 221 | + |
| 222 | +Theorem le_not_symmetric : |
| 223 | + ¬ (symmetric le). |
| 224 | +Proof. |
| 225 | + (* FILL IN HERE *) Admitted. |
| 226 | +$\square$ |
| 227 | +A relation R is antisymmetric if R a b and R b a together imply a = b — that is, if the only "cycles" in R are trivial ones. |
| 228 | + |
| 229 | +Definition antisymmetric {X: Type} (R: relation X) := |
| 230 | + ∀a b : X, (R a b) -> (R b a) -> a = b. |
| 231 | + |
| 232 | + |
| 233 | +==== Exercise: 2 stars, optional |
| 234 | + |
| 235 | +Theorem le_antisymmetric : |
| 236 | + antisymmetric le. |
| 237 | +Proof. |
| 238 | + (* FILL IN HERE *) Admitted. |
| 239 | +$\square$ |
| 240 | + |
| 241 | + |
| 242 | +==== Exercise: 2 stars, optional |
| 243 | + |
| 244 | +Theorem le_step : ∀n m p, |
| 245 | + n < m -> |
| 246 | + m ≤ S p -> |
| 247 | + n ≤ p. |
| 248 | +Proof. |
| 249 | + (* FILL IN HERE *) Admitted. |
| 250 | +$\square$ |
| 251 | + |
| 252 | + |
| 253 | +=== Equivalence Relations |
| 254 | + |
| 255 | +A relation is an equivalence if it's reflexive, symmetric, and transitive. |
| 256 | + |
| 257 | +Definition equivalence {X:Type} (R: relation X) := |
| 258 | + (reflexive R) ∧ (symmetric R) ∧ (transitive R). |
| 259 | + |
| 260 | + |
| 261 | +=== Partial Orders and Preorders |
| 262 | + |
| 263 | +A relation is a partial order when it's reflexive, anti-symmetric, and |
| 264 | +transitive. In the Idris standard library it's called just "order" for short. |
| 265 | + |
| 266 | +Definition order {X:Type} (R: relation X) := |
| 267 | + (reflexive R) ∧ (antisymmetric R) ∧ (transitive R). |
| 268 | + |
| 269 | +A preorder is almost like a partial order, but doesn't have to be antisymmetric. |
| 270 | + |
| 271 | +Definition preorder {X:Type} (R: relation X) := |
| 272 | + (reflexive R) ∧ (transitive R). |
| 273 | + |
| 274 | +Theorem le_order : |
| 275 | + order le. |
| 276 | + |
| 277 | + |
| 278 | +== Reflexive, Transitive Closure |
| 279 | + |
| 280 | +The reflexive, transitive closure of a relation R is the smallest relation that |
| 281 | +contains R and that is both reflexive and transitive. Formally, it is defined |
| 282 | +like this in the Relations module of the Idris standard library: |
| 283 | + |
| 284 | +Inductive clos_refl_trans {A: Type} (R: relation A) : relation A := |
| 285 | + | rt_step : ∀x y, R x y -> clos_refl_trans R x y |
| 286 | + | rt_refl : ∀x, clos_refl_trans R x x |
| 287 | + | rt_trans : ∀x y z, |
| 288 | + clos_refl_trans R x y -> |
| 289 | + clos_refl_trans R y z -> |
| 290 | + clos_refl_trans R x z. |
| 291 | + |
| 292 | +For example, the reflexive and transitive closure of the next_nat relation |
| 293 | +coincides with the le relation. |
| 294 | + |
| 295 | +Theorem next_nat_closure_is_le : ∀n m, |
| 296 | + (n ≤ m) <-> ((clos_refl_trans next_nat) n m). |
| 297 | + |
| 298 | +The above definition of reflexive, transitive closure is natural: it says, |
| 299 | +explicitly, that the reflexive and transitive closure of R is the least relation |
| 300 | +that includes R and that is closed under rules of reflexivity and transitivity. |
| 301 | +But it turns out that this definition is not very convenient for doing proofs, |
| 302 | +since the "nondeterminism" of the rt_trans rule can sometimes lead to tricky |
| 303 | +inductions. Here is a more useful definition: |
| 304 | + |
| 305 | +Inductive clos_refl_trans_1n {A : Type} |
| 306 | + (R : relation A) (x : A) |
| 307 | + : A -> Type := |
| 308 | + | rt1n_refl : clos_refl_trans_1n R x x |
| 309 | + | rt1n_trans (y z : A) : |
| 310 | + R x y -> clos_refl_trans_1n R y z -> |
| 311 | + clos_refl_trans_1n R x z. |
| 312 | + |
| 313 | +Our new definition of reflexive, transitive closure "bundles" the rt_step and |
| 314 | +rt_trans rules into the single rule step. The left-hand premise of this step is |
| 315 | +a single use of R, leading to a much simpler induction principle. |
| 316 | + |
| 317 | +Before we go on, we should check that the two definitions do indeed define the |
| 318 | +same relation... |
| 319 | + |
| 320 | +First, we prove two lemmas showing that clos_refl_trans_1n mimics the behavior |
| 321 | +of the two "missing" clos_refl_trans constructors. |
| 322 | + |
| 323 | +Lemma rsc_R : ∀(X:Type) (R:relation X) (x y : X), |
| 324 | + R x y -> clos_refl_trans_1n R x y. |
| 325 | + |
| 326 | + |
| 327 | +==== Exercise: 2 stars, optional (rsc_trans) |
| 328 | + |
| 329 | +Lemma rsc_trans : |
| 330 | + ∀(X:Type) (R: relation X) (x y z : X), |
| 331 | + clos_refl_trans_1n R x y -> |
| 332 | + clos_refl_trans_1n R y z -> |
| 333 | + clos_refl_trans_1n R x z. |
| 334 | +Proof. |
| 335 | + (* FILL IN HERE *) Admitted. |
| 336 | +$\square$ |
| 337 | + |
| 338 | +Then we use these facts to prove that the two definitions of reflexive, transitive closure do indeed define the same relation. |
| 339 | + |
| 340 | + |
| 341 | +==== Exercise: 3 stars, optional (rtc_rsc_coincide) |
| 342 | + |
| 343 | +Theorem rtc_rsc_coincide : |
| 344 | + ∀(X:Type) (R: relation X) (x y : X), |
| 345 | + clos_refl_trans R x y <-> clos_refl_trans_1n R x y. |
| 346 | +Proof. |
| 347 | + (* FILL IN HERE *) Admitted. |
| 348 | + |
| 349 | +$\square$ |
0 commit comments