The Proofs and Programs Club @ RHUL

Programs track · follows FPIL

Level 2F Selection, as a Program

After Level 1 · Next: Level 3F

At a glance

You build
select, which keeps the rows of a table that satisfy a condition.
You prove
facts about selections of the club's tables, by computing.
What to learn first, and where
Learn toRead
define a function by cases on a list: empty, or a first row and the restFPIL 1.5 Datatypes and Patterns
write one function for tables of any row typeFPIL 1.6 Polymorphism
pass a function, such as a condition, as an argumentFPIL 1.3 Functions and Definitions

Milestones

Your ticks stay in this browser.

0 of 4 done

Theory, on paperConditions, selection, and selection on lists

A condition says yes or no to each row

Write \(\B = \{\true, \false\}\) for the two truth values.

Definition 2F.1 (Condition)

A condition on rows of \(D_1 \times D_2\) is a function \(p : D_1 \times D_2 \to \B\).

For example, on \(\rel{Member}\) and on \(\rel{Attended}\):

\[\mathit{cs}(n, d) = \true \iff d = \text{CS}, \qquad \mathit{late}(n, w) = \true \iff w \geq 2.\]

So \(\mathit{cs}(\text{Alan}, \text{CS}) = \true\) and \(\mathit{late}(\text{Ada}, 1) = \false\). A condition is a function like any other: a program can take it as an argument.

Definition 2F.2 (Both conditions)

\((p \wedge q)(t) = \true\) exactly when \(p(t) = \true\) and \(q(t) = \true\).

Selection keeps the rows that satisfy a condition

Definition 2F.3 (Selection)

For a table \(R\) and a condition \(p\), \(\ \sel{p}(R) = \{t \in R \mid p(t) = \true\}\).

\(\sel{\mathit{cs}}(\rel{Member})\)
\(\att{name}\)\(\att{dept}\)
AlanCS
GraceCS
\(\sel{\mathit{late}}(\rel{Attended})\)
\(\att{name}\)\(\att{week}\)
Alan2
Grace3

A selection never adds rows: \(\sel{p}(R) \subseteq R\). In SQL, a selection is a WHERE clause: \(\sel{\mathit{cs}}(\rel{Member})\) is SELECT * FROM Member WHERE dept = 'CS'. Read more in Abiteboul, Hull, Vianu, Foundations of Databases (1995), ch. 4: Conjunctive Queries.

On a list, selection is three equations

A table is a set; a program works on a list that represents it (Definition 1.2). On a list, selection looks at the first row, decides, and goes on with the rest.

Definition 2F.4 (Selection on lists)

For a condition \(p\):

\[\begin{aligned} \mathit{select}\ p\ [\,] &= [\,] \\ \mathit{select}\ p\ (t :: l) &= t :: \mathit{select}\ p\ l && \text{if } p(t) = \true \\ \mathit{select}\ p\ (t :: l) &= \mathit{select}\ p\ l && \text{if } p(t) = \false \end{aligned}\]

Here \(t :: l\) is the list with first row \(t\), then the list \(l\). Every non-empty list has this shape, so the equations cover every list. Each step applies one equation:

\[\begin{aligned} &\mathit{select}\ \mathit{late}\ [(\text{Ada}, 1), (\text{Alan}, 1), (\text{Alan}, 2), (\text{Grace}, 3)] \\ ={}& \mathit{select}\ \mathit{late}\ [(\text{Alan}, 1), (\text{Alan}, 2), (\text{Grace}, 3)] && \mathit{late}(\text{Ada}, 1) = \false \\ ={}& \mathit{select}\ \mathit{late}\ [(\text{Alan}, 2), (\text{Grace}, 3)] && \mathit{late}(\text{Alan}, 1) = \false \\ ={}& (\text{Alan}, 2) :: \mathit{select}\ \mathit{late}\ [(\text{Grace}, 3)] && \mathit{late}(\text{Alan}, 2) = \true \\ ={}& (\text{Alan}, 2) :: (\text{Grace}, 3) :: \mathit{select}\ \mathit{late}\ [\,] && \mathit{late}(\text{Grace}, 3) = \true \\ ={}& [(\text{Alan}, 2), (\text{Grace}, 3)]. \end{aligned}\]

This is exactly how Lean runs your program. The answer represents \(\sel{\mathit{late}}(\rel{Attended})\), as it should:

Fact 2F.5

If \(l\) represents \(R\) (Definition 1.2), then \(\mathit{select}\ p\ l\) represents \(\sel{p}(R)\).

Level 6 proves it, for every list. Here, you check it on examples.

Laws: two questions with the same answer

A law says that two ways of asking give the same answer, on every table. The first one:

Law 1

For every table \(R\) and conditions \(p\) and \(q\): \(\ \sel{p}(\sel{q}(R)) = \sel{p \wedge q}(R)\).

Two selections, one after the other, are one selection with both conditions. In this track you check laws on examples, by computing; Level 2T proves law 1 for every table. Checking is useful, since it catches false laws, but it is not proving: an example is one table, and a law speaks of all of them.

Questions

Question 2F.A Compute a selection

With \(\mathit{early}(n, w) = \true \iff w \leq 1\), what is \(\sel{\mathit{early}}(\rel{Attended})\)?

Answer

\(\{(\text{Ada}, 1), (\text{Alan}, 1)\}\).

Question 2F.B Extreme conditions

What is \(\sel{p}(R)\) when \(p(t) = \true\) for every row \(t\)? And when \(p(t) = \false\) for every row?

Answer

\(R\) itself, and the empty table \(\emptyset\).

Question 2F.C Step by step

Compute \(\mathit{select}\ \mathit{late}\ [(\text{Grace}, 3), (\text{Ada}, 1)]\) with Definition 2F.4, one equation per step.

Answer

\(\mathit{select}\ \mathit{late}\ [(\text{Grace}, 3), (\text{Ada}, 1)] = (\text{Grace}, 3) :: \mathit{select}\ \mathit{late}\ [(\text{Ada}, 1)] = (\text{Grace}, 3) :: \mathit{select}\ \mathit{late}\ [\,] = [(\text{Grace}, 3)]\).

Question 2F.D Never more rows

Why is \(|\sel{p}(R)| \leq |R|\), for every \(R\) and \(p\)?

Answer

Because \(\sel{p}(R) \subseteq R\): a subset of a finite set has at most as many elements.

Question 2F.E Checking is not proving

Law 1 holds on the example of Exercise 2F.4 below. Does that prove law 1? What would a single example show?

Answer

No: it shows law 1 for one table and two conditions. A single example can only disprove a law, by giving two different answers. To prove law 1, you show it for every \(R\), \(p\) and \(q\): Level 2T does.

Lean, on your computerBuild select, and run it

Your turn: build select

Add this to Club.lean, after the tables of Level 1:

Leanselect.lean
def select {α : Type} (p : α → Bool) : List α → List α
  | [] => sorry
  | row :: rest => sorry

-- Test: should print [("Ada", 1), ("Alan", 1)]
#eval select (fun row => row.2 == 1) attended

Exercise 2F.1 In Lean Build select

Replace the sorrys with Definition 2F.4. Then run the test.

Hint 1

For []: selecting from no rows gives no rows.

Hint 2

For row :: rest, the two cases of Definition 2F.4 become one if p row then … else …. Each branch calls select p rest on the rest of the list.

Exercise 2F.2 In Lean A fact, by computing

Prove that select keeps 2 rows of week 1:

Lean
example : (select (fun row => row.2 == 1) attended).length = 2 := rfl
Hint

If Lean rejects rfl, first check with #eval what the left side computes to.

Exercise 2F.3 In Lean The CS members

#eval the CS members, \(\sel{\mathit{cs}}(\rel{Member})\). You should see [("Alan", "CS"), ("Grace", "CS")].

Hint

fun row => row.2 == "CS", applied to member.

Law 1, checked by Lean on an example

Leanlaw.lean
-- Law 1, on one example: two selections are one, with both conditions
example : select (fun r => r.2 ≤ 2) (select (fun r => r.2 ≥ 2) attended)
        = select (fun r => r.2 ≥ 2 && r.2 ≤ 2) attended := rfl

Exercise 2F.4 In Lean Your own example

Check law 1 on another example of your own: other conditions, or another table.

Exercise 2F.5 In Lean Does the order matter?

Is \(\sel{p}(\sel{q}(R)) = \sel{q}(\sel{p}(R))\)? Check it in Lean on an example. Then try to find an example where it fails.

Answer

You will not find one: it is law 2, and Level 2T proves it. Even as lists, both sides keep the same rows in the same order.

More exercises

Exercise 2F.6 In Lean Count the rows that satisfy a condition

Define count, with select and .length:

Lean
def count {α : Type} (p : α → Bool) (l : List α) : Nat := sorry

example : count (fun r => r.2 ≥ 2) attended = 2 := rfl
Hint

(select p l).length.

Exercise 2F.7 In Lean Both conditions, as a function

Write Definition 2F.2 as a function, and use it to restate the example of law 1:

Lean
def both {α : Type} (p q : α → Bool) : α → Bool := sorry
Hint

fun r => p r && q r.

Exercise 2F.8 Stretch Lean already knows select

Lean's library calls it List.filter. Check that they agree on an example:

Lean
example : select (fun r => r.2 ≥ 2) attended
        = attended.filter (fun r => r.2 ≥ 2) := rfl

They agree on every list, too: proving that needs induction, which is Level 5T.

Next

  1. Level 3F Projection, product, union

Stuck, or curious? Ask us on Discord, or come along any Thursday.