The Proofs and Programs Club @ RHUL

Programs track · follows FPIL

Level 4F Difference and Joins, by Comparing Rows

After Level 3F · Next: Level 5F

At a glance

You build
dedup, diff and inter; and joinOn, a join, as a selection of a product.
You prove
which sessions had nobody, and the size of a join, by computing.
What to learn first, and where
Learn toRead
write functions for every type whose values can be comparedFPIL 3.2 Type Classes and Polymorphism
compare values with ==, and test membership in a listFPIL 3.5.3 Equality and Ordering

Milestones

Your ticks stay in this browser.

0 of 4 done

Theory, on paperEqual rows, difference, and joins

Comparing rows needs an equality test

Selection, projection, product and union never compare two rows. Removing repeats, difference and joins do, so a program needs a way to tell whether two values are equal.

Definition 4F.1 (Equality test)

An equality test on \(D\) is a function \(\mathit{eq} : D \times D \to \B\) with \(\mathit{eq}(t, s) = \true\) exactly when \(t = s\).

Definition 4F.2 (Membership, duplicates)

With \(\mathit{eq}\):

\[\begin{aligned} \mathit{contains}\ [\,]\ t &= \false \\ \mathit{contains}\ (s :: l)\ t &= \mathit{eq}(s, t) \text{ or } \mathit{contains}\ l\ t \\ \mathit{dedup}\ [\,] &= [\,] \\ \mathit{dedup}\ (t :: l) &= \mathit{dedup}\ l && \text{if } \mathit{contains}\ l\ t = \true \\ \mathit{dedup}\ (t :: l) &= t :: \mathit{dedup}\ l && \text{if } \mathit{contains}\ l\ t = \false \end{aligned}\]

So \(\mathit{contains}\ l\ t = \true\) exactly when \(t\) is in the table \(l\) represents. \(\mathit{dedup}\) drops a row when another copy comes later, so it keeps the last copy of each row:

\[\mathit{dedup}\ [1, 1, 2, 3] = \mathit{dedup}\ [1, 2, 3] = 1 :: \mathit{dedup}\ [2, 3] = \cdots = [1, 2, 3].\]

In Lean, == is the equality test, from the type class BEq (FPIL 3.5.3 Equality and Ordering).

Difference removes the rows of another table

Definition 4F.3 (Difference, intersection)

For tables \(R\) and \(S\) of the same rows: \(R - S = \{t \in R \mid t \notin S\}\) and \(R \cap S = \{t \in R \mid t \in S\}\).

Which sessions had nobody? With one-column tables:

\[\pi_1[\rel{Session}] - \pi_2[\rel{Attended}] = \{1, 2, 3, 4\} - \{1, 2, 3\} = \{4\}.\]

On lists, a difference is a selection (Level 2F), with the condition "not in \(m\)":

\[\mathit{diff}\ l\ m = \mathit{select}\ (t \mapsto \text{not } \mathit{contains}\ m\ t)\ l.\]

In SQL: EXCEPT. Read more in Abiteboul, Hull, Vianu, Foundations of Databases (1995), ch. 5: Adding Negation: Algebra and Calculus.

Fact 4F.4

\(R \cap S = R - (R - S)\).

So intersection needs no program of its own. Level 3T proves Fact 4F.4, and finds that one half of it needs classical logic.

A join is a selection of a product

Definition 4F.5 (Join)

For functions \(k_1\) on the rows of \(R\) and \(k_2\) on the rows of \(S\) (their keys): \(\ R \join_{k_1 = k_2} S = \{(t, s) \in R \times S \mid k_1(t) = k_2(s)\}\).

It is a selection of the product (Level 2F and Level 3F), with the condition \(\mathit{eq}(k_1(t), k_2(s))\). Joining on the week, with \(k_1 = \pi_2\) and \(k_2 = \pi_1\):

\(\rel{Attended} \join_{\pi_2 = \pi_1} \rel{Session}\)
row of \(\rel{Attended}\)row of \(\rel{Session}\)
(Ada, 1)(1, Lists)
(Alan, 1)(1, Lists)
(Alan, 2)(2, Proofs)
(Grace, 3)(3, Types)

Of the 16 pairs in the product, the join keeps the 4 whose weeks agree. In SQL: Attended a JOIN Session s ON a.week = s.week. Joins are how a database answers questions that need two tables, such as "who came to the session on Proofs?".

Questions

Question 4F.A Two differences

Compute \(\pi_1[\rel{Session}] - \pi_2[\rel{Attended}]\) and \(\pi_2[\rel{Attended}] - \pi_1[\rel{Session}]\).

Answer

\(\{4\}\), and \(\emptyset\): every week someone attended had a session.

Question 4F.B Is difference symmetric?

Is \(R - S = S - R\) for every \(R\) and \(S\)?

Answer

No: Question 4F.A gives \(\{4\} \neq \emptyset\). In fact \(R - S\) and \(S - R\) never share a row.

Question 4F.C Join on the name

Compute \(\rel{Member} \join_{\pi_1 = \pi_1} \rel{Attended}\).

Answer

Four pairs: \(((\text{Ada}, \text{Maths}), (\text{Ada}, 1))\), \(((\text{Alan}, \text{CS}), (\text{Alan}, 1))\), \(((\text{Alan}, \text{CS}), (\text{Alan}, 2))\) and \(((\text{Grace}, \text{CS}), (\text{Grace}, 3))\).

Question 4F.D A broken equality test

Suppose \(\mathit{eq}(t, s) = \true\) for all \(t\) and \(s\), which breaks Definition 4F.1. What do \(\mathit{dedup}\) and \(\mathit{diff}\) compute?

Answer

\(\mathit{contains}\ l\ t = \true\) whenever \(l\) is not empty. So \(\mathit{dedup}\ l\) keeps only the last row of \(l\), and \(\mathit{diff}\ l\ m = [\,]\) whenever \(m\) is not empty. The programs are only as correct as the equality test: Level 6 assumes a lawful one.

Question 4F.E Intersection, two ways

Check Fact 4F.4 on \(R = \{1, 2, 3\}\) and \(S = \{2, 3, 4\}\).

Answer

\(R - S = \{1\}\), so \(R - (R - S) = \{2, 3\} = R \cap S\).

Lean, on your computerBuild dedup, diff, inter and joinOn

Your turn: build dedup

Leandedup.lean
def dedup {α : Type} [BEq α] : List α → List α
  | [] => sorry
  | row :: rest => sorry   -- hint: rest.contains row

example : dedup (project (fun row => row.2) attended) = [1, 2, 3] := rfl

Exercise 4F.1 In Lean Build dedup

Complete dedup with Definition 4F.2, and prove the example.

Hint

if rest.contains row then … else …, with dedup rest in both branches.

Your turn: build diff and inter

Leandiff.lean
def diff {α : Type} [BEq α] (r s : List α) : List α := sorry
def inter {α : Type} [BEq α] (r s : List α) : List α := sorry

-- Which sessions had nobody?
example : diff (project (fun s => s.1) session)
               (project (fun a => a.2) attended) = [4] := rfl

Exercise 4F.2 In Lean Build diff

diff: a selection, with s.contains row.

Hint

select (fun row => !(s.contains row)) r. ! is "not" on Bool.

Exercise 4F.3 In Lean Build inter

inter: from diff alone (Fact 4F.4). Then prove the example.

Hint

diff r (diff r s). Check it: #eval inter [1, 2, 3] [2, 3, 4] should print [2, 3].

Your turn: derive a join from select and product

Leanjoin.lean
def joinOn {α β κ : Type} [BEq κ] (k₁ : α → κ) (k₂ : β → κ)
    (r : List α) (s : List β) : List (α × β) :=
  sorry

-- Attended ⋈ Session, on the week
example : (joinOn (fun a => a.2) (fun s => s.1) attended session).length = 4 :=
  rfl

Exercise 4F.4 In Lean Build joinOn

Complete joinOn: select the pairs of product r s whose keys are equal. Then prove the example.

Hint

A pair x of the product has x.1 from r and x.2 from s: compare k₁ x.1 with k₂ x.2.

More exercises

Exercise 4F.5 In Lean Join on the name

#eval Question 4F.C: join member and attended on the name. You should see four pairs.

Hint

joinOn (fun m => m.1) (fun a => a.1) member attended.

Exercise 4F.6 In Lean Who came to the session on Proofs?

Answer it with joinOn, select and project, in one expression. #eval it: the answer is ["Alan"].

Hint 1

Join attended and session on the week; keep the pairs whose session's topic is "Proofs"; then project each pair to the name.

Hint 2

In a pair x of the join, the topic is x.2.2 and the name is x.1.1.

Exercise 4F.7 In Lean Who never came?

Compute the names of members who attended no session, with diff and project. What do you get, and why?

Answer

diff (project (fun m => m.1) member) (project (fun a => a.1) attended) is []: every member attended at least once.

Exercise 4F.8 Stretch A semijoin

The semijoin keeps the rows of \(R\) that have at least one partner in \(S\), without pairing them. Define it, and find the members who attended in week 2:

Lean
def semijoin {α β κ : Type} [BEq κ] (k₁ : α → κ) (k₂ : β → κ)
    (r : List α) (s : List β) : List α :=
  sorry
Hint

select (fun a => s.any (fun b => k₁ a == k₂ b)) r. Then use it on member and the week-2 rows of attended.

Next

  1. Level 5F Named columns, queries

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