Pattern and Copattern matching Anton Setzer Swansea University, - - PowerPoint PPT Presentation

pattern and copattern matching
SMART_READER_LITE
LIVE PREVIEW

Pattern and Copattern matching Anton Setzer Swansea University, - - PowerPoint PPT Presentation

Pattern and Copattern matching Anton Setzer Swansea University, Swansea UK Leeds Logic Seminar, 13 May 2015 Anton Setzer (Swansea) Pattern and Copattern matching 1/ 46 Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity


slide-1
SLIDE 1

Pattern and Copattern matching

Anton Setzer Swansea University, Swansea UK Leeds Logic Seminar, 13 May 2015

Anton Setzer (Swansea) Pattern and Copattern matching 1/ 46

slide-2
SLIDE 2

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 2/ 46

slide-3
SLIDE 3

Iteration, Recursion, Induction

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 3/ 46

slide-4
SLIDE 4

Iteration, Recursion, Induction

N as an Initial Algebra

◮ N is initial algebra of the functor F(X) = 1 + X ◮

F(N) = 1 + N 0 + S

✲ N

F(A) = 1 + A F(g) = 1 + g

f ′

✲ A

∃! g

f ′ can be decomposed as f ′ = a + f

Anton Setzer (Swansea) Pattern and Copattern matching 4/ 46

slide-5
SLIDE 5

Iteration, Recursion, Induction

Unique Iteration

1 + N 0 + S ✲ N 1 + A 1 + g

a + f ✲ A ∃! g

Unique existence of g means unique iteration: Given a : A and f : A → A there exists a unique g : N → A g 0 = a g (S n) = f (g n) i.e g (Sn 0) = f n a

Anton Setzer (Swansea) Pattern and Copattern matching 5/ 46

slide-6
SLIDE 6

Iteration, Recursion, Induction

Unique Recursion

◮ From the principle of unique iteration we can prove the principle of

unique (primitive) recursion: Given a : A and f : N → A → A there exists a unique g : N → A g 0 = a g (S n) = f n (g n)

Anton Setzer (Swansea) Pattern and Copattern matching 6/ 46

slide-7
SLIDE 7

Iteration, Recursion, Induction

Induction

◮ From the principle of unique iteration we can prove the principle of

induction: Assume A : N → Set, a : A 0 and f : (n : N) → A n → A (S n) There exists a unique g : (n : N) → A n g 0 = a g (S n) = f n (g n)

◮ Using induction we can prove that if we have two solutions for a

iteration or recursion principle, they are pointwise equal, i.e. uniqueness of iteration and recursion.

Anton Setzer (Swansea) Pattern and Copattern matching 7/ 46

slide-8
SLIDE 8

Iteration, Recursion, Induction

Pattern Matching

◮ The above means that we can define

g : (n : N) → A n g 0 = a for some a : A g (S n) = a′ for some a′ : A depending on n where in the second case we can use the recursion hypothesis or induction hypothesis g n.

◮ This means we can define g n by pattern matching on n : N.

Anton Setzer (Swansea) Pattern and Copattern matching 8/ 46

slide-9
SLIDE 9

Iteration, Recursion, Induction

Iteration, Recursion, Induction

Theorem

Assume N : Set, 0 : N, S : N → N. The following are equivalent

◮ The principle of unique iteration. ◮ The principle of unique recursion. ◮ The principle of unique induction. ◮ The principle of induction.

Anton Setzer (Swansea) Pattern and Copattern matching 9/ 46

slide-10
SLIDE 10

Coiteration, Corecursion

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 10/ 46

slide-11
SLIDE 11

Coiteration, Corecursion

Streams as a Final Coalgebra

◮ Dual of + is ×, so we use for clarity a functor using product rather

than disjoint union:

◮ Stream is the final coalgebra of F(X) = N × X

X f

✲ N × X

= F(X) Stream ∃!g

head × tail✲ N × Stream id × g = F(g)

= F(Stream)

◮ We can decompose f as

f = f0 × f1

Anton Setzer (Swansea) Pattern and Copattern matching 11/ 46

slide-12
SLIDE 12

Coiteration, Corecursion

Unique Coiteration

X f0 × f1 ✲ N × X Stream ∃!g

head × tail

✲ N × Stream

id × g

This corresponds to the principle of unique coiteration: There exists a unique g : A → Stream head (g x) = f0 x tail (g x) = g (f1 x)

Anton Setzer (Swansea) Pattern and Copattern matching 12/ 46

slide-13
SLIDE 13

Coiteration, Corecursion

Unique Coiteration

◮ We had:

head (g x)) = f0 x tail (g x) = g (f1 x)

◮ By choosing f0, f1 we can define g : X → Stream s.t.

head (g x) = n for some n : N depending on x tail (g x) = g x′ for some x′ : X depending on x

Anton Setzer (Swansea) Pattern and Copattern matching 13/ 46

slide-14
SLIDE 14

Coiteration, Corecursion

Unique Corecursion

◮ From unique coiteration we can derive unique corecursion:

There exists a unique g : A → Stream head (g x) = n for some n : N depending on x tail (g x) = g x′ for some x′ : X depending on x

  • r

= s for some s : Stream depending on x

◮ This means we can define g x by copattern matching

Anton Setzer (Swansea) Pattern and Copattern matching 14/ 46

slide-15
SLIDE 15

Coiteration, Corecursion

Examples

◮ We can define

cons : (N × Stream) → Stream head (cons(n, s)) = n tail (cons(n, s)) = s Note: cons not primitive but defined by corecursion inc : N → Stream head (inc n) = n tail (inc n) = inc (n + 1)

Anton Setzer (Swansea) Pattern and Copattern matching 15/ 46

slide-16
SLIDE 16

Coiteration, Corecursion

Examples

inc′ : N → Stream head (inc′(n)) = n tail (inc′(n)) = inc′′(n + 1) inc′′ : N → Stream head (inc′′(n)) = n tail (inc′′(n)) = inc′(n + 1)

Anton Setzer (Swansea) Pattern and Copattern matching 16/ 46

slide-17
SLIDE 17

Bisimilarity and Coinduction

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 17/ 46

slide-18
SLIDE 18

Bisimilarity and Coinduction

Bisimilarity

◮ Bisimilarity ∼ on Streams is an indexed final coalgebra. ◮ Consider the category SetStream×Stream of binary relations

ϕ : Stream × Stream → Set

◮ Let

F∼ : SetStream×Stream → SetStream×Stream F∼(ϕ, (s, s′)) = (head s = head s′) × ϕ (tail s, tail s′)

Anton Setzer (Swansea) Pattern and Copattern matching 18/ 46

slide-19
SLIDE 19

Bisimilarity and Coinduction

Bisimilarity

◮ That ∼ is a F∼ coalgebra means there exist

elim∼ : (s, s′ : Stream) → s ∼ s′ → (head s = head s′) × (tail s ∼ tail s′) i.e. s ∼ s′ → (head s = head s′) ∧ ((tail s) ∼ (tail s′))

◮ Let elim0 ∼ and elim1 ∼ the two components of elim∼,

elim0

: (s, s′ : Stream) → s ∼ s′ → head s = head s′ elim1

: (s, s′ : Stream) → s ∼ s′ → tail s ∼ tail s′ and hide the first two arguments of elimi

∼.

Anton Setzer (Swansea) Pattern and Copattern matching 19/ 46

slide-20
SLIDE 20

Bisimilarity and Coinduction

Bisimilarity

◮ That ∼ is a final F∼-coalgebra means that it is the largest such

relation: ϕ (s, s′) f

✲ head s = head s′ ∧ ϕ (tail s, tail s′)

s ∼ s′ ∃!g

elim∼

✲ head s = head s′ ∧ (tail s) ∼ (tail s′)

id ∧ g

◮ This means that

∀s, s′.ϕ (s, s′) → head s = head s′ ∧ ϕ (tail s, tail s′) then ∀s, s′.ϕ (s, s′) → s ∼ s′

Anton Setzer (Swansea) Pattern and Copattern matching 20/ 46

slide-21
SLIDE 21

Bisimilarity and Coinduction

Bisimilarity

◮ So we have

s ∼ s′ → head s = head s′ ∧ (tail s) ∼ (tail s′) and if ∀s, s′.ϕ (s, s′) → head s = head s′ ∧ ϕ (tail s, tail s′) then ∀s, s′.ϕ (s, s′) → s ∼ s′

Anton Setzer (Swansea) Pattern and Copattern matching 21/ 46

slide-22
SLIDE 22

Bisimilarity and Coinduction

Corecursive Proof of Bisimilarity

◮ Because ∼ is a final coalgebra we can compute proofs of it by

corecursion:

◮ We can define

f : (s, s′ : Stream) → ϕ s s′ → s ∼ s′ elim0

∼ (f s s′ x)

= an element of head s = head s′ elim0

∼ (f s s′ x)

= an element of (tail s) ∼ (tail s′) where in the last line we can use

◮ either a proof of tail s ∼ tail s′ defined before ◮ or use the corecursion hypothesis f (tail s) (tail s′) x′ for some

x′ : ϕ (tail s) (tail s′)

Anton Setzer (Swansea) Pattern and Copattern matching 22/ 46

slide-23
SLIDE 23

Bisimilarity and Coinduction

Coinduction

Theorem

Assume Stream : Set, head : Stream → N, tail : Stream → Stream. The following are equivalent

◮ The principle of unique coiteration. ◮ The principle of unique corecursion. ◮ The principle of iteration together with the principle that bisimilarity

∼ implies equality ∀s, s′ : Stream.s ∼ s′ → s = s′ Because of the possibility of defining elements of s ∼ s′ the latter can be considered as a principle of coinduction.

Anton Setzer (Swansea) Pattern and Copattern matching 23/ 46

slide-24
SLIDE 24

Bisimilarity and Coinduction

Principle of Coinduction

◮ Let ϕ : Stream → Stream → Set. ◮ We can prove

∀s, s′ : Stream.ϕ s s′ → s = s′ by showing ∀s, s′ : Stream.ϕ s s′ → head s = head s′ ∀s, s′ : Stream.ϕ s s′ → tail s = tail s′ where for proving tail s = tail s′ we can use the coinduction hypothesis that ϕ (tail s) (tail s′) implies tail s = tail s′.

Anton Setzer (Swansea) Pattern and Copattern matching 24/ 46

slide-25
SLIDE 25

Bisimilarity and Coinduction

Indexed Coinduction

◮ Instead of defining ϕ as a predicate Stream → Stream → Set we can

assume A : Set s, t : A → Stream and define ϕ s′ t′ = (a : A) × (s′ = s a) × (t′ = t a)

◮ Coinduction of ϕ becomes then the principle of indexed coinduction

(see next slide)

Anton Setzer (Swansea) Pattern and Copattern matching 25/ 46

slide-26
SLIDE 26

Bisimilarity and Coinduction

Indexed Coinduction

◮ Assume

A : Set s0, s1 : A → Stream

◮ We can prove

∀a : A.s0 a = s1 a by showing ∀a : A.head (s a) = head (t a) ∀a : A.tail (s a) = tail (t a) where for proving tail (s a) = tail (t a) we can use that tail (s a) = s a′ and tail (t a) = t a′ and therefore by coinduction-hypothesis s a′ = t a′.

Anton Setzer (Swansea) Pattern and Copattern matching 26/ 46

slide-27
SLIDE 27

Bisimilarity and Coinduction

Example Proof by Coinduction

◮ Remember

inc : N → Stream head(inc n) = n tail (inc n) = inc (n + 1) inc′ : N → Stream head(inc′(n)) = n tail (inc′(n)) = inc′′(n + 1) inc′′ : N → Stream head(inc′′(n)) = n tail (inc′′(n)) = inc′(n + 1)

Anton Setzer (Swansea) Pattern and Copattern matching 27/ 46

slide-28
SLIDE 28

Bisimilarity and Coinduction

Example Proof by Coinduction

◮ We show

∀n ∈ N.inc′ n = inc n ∧ inc′′ n = inc n

◮ Formally we would use in the above

A = N + N s (inl n)= inc′ n s (inr n)= inc′′ n t (inl n)= inc n t (inr n)= inc n and show ∀a : A.s a = t a

Anton Setzer (Swansea) Pattern and Copattern matching 28/ 46

slide-29
SLIDE 29

Bisimilarity and Coinduction

Example Proof by Coinduction

◮ Proof of

∀n ∈ N.inc′ n = inc n ∧ inc′′ n = inc n

◮ Assume n : N.

head (inc′ n) = n = head (inc n) head (inc′′ n) = n = head (inc n) tail (inc′ n) = inc′′ (n + 1) co−IH = inc (n + 1) = tail (inc n) tail (inc′′ n) = inc′ (n + 1) co−IH = inc (n + 1) = tail (inc n)

Anton Setzer (Swansea) Pattern and Copattern matching 29/ 46

slide-30
SLIDE 30

Proofs by Coinduction of Bisimilarity in Transition Systems

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 30/ 46

slide-31
SLIDE 31

Proofs by Coinduction of Bisimilarity in Transition Systems

Bisimilarity

◮ Consider the following (unlabelled) transition system:

x x x p q r

◮ Bisimilarity is the final coalgebra

p ∼ q → (∀p′.p − → p′ → ∃q′.q − → q′ ∧ p′ ∼ q′) ∧ · · · symmetric case · · · }

Anton Setzer (Swansea) Pattern and Copattern matching 31/ 46

slide-32
SLIDE 32

Proofs by Coinduction of Bisimilarity in Transition Systems

Proof using the Definition of ∼

x x x p q r

◮ We show p ∼ q ∧ p ∼ r by coinduction: ◮ Coinduction step for p ∼ q:

◮ Assume p −

→ p′. Then p′ = p. We have q − → r and by co-IH p ∼ r.

◮ Assume q −

→ q′. Then q′ = r. We have p − → p and by co-IH p ∼ r.

◮ Coinduction step for p ∼ r:

◮ Assume p −

→ p′. Then p′ = p. We have r − → q and by co-IH p ∼ q.

◮ Assume r −

→ r ′. Then r ′ = q. We have p − → p and by co-IH p ∼ q.

Anton Setzer (Swansea) Pattern and Copattern matching 32/ 46

slide-33
SLIDE 33

Proofs by Coinduction of Bisimilarity in Transition Systems

Traditional Argument of Proving Bisimiliarity

◮ The standard argument for showing p ∼ q ∧ p ∼ r is as follows:

Define a relation ϕ on states by ϕ(p′, q′) ⇔ p′ = p ∧ (q′ = q ∨ q′ = r) Show ϕ is a simulation: ∀p, p′, q.ϕ(p, q) ∧ p − → p′ ⇒ ∃q′.q − → q′ ∧ ϕ(p′, q′) ∀p, q, q′.ϕ(p, q) ∧ q − → q′ ⇒ ∃p′.p − → p′ ∧ ϕ(p′, q′)

Anton Setzer (Swansea) Pattern and Copattern matching 33/ 46

slide-34
SLIDE 34

Proofs by Coinduction of Bisimilarity in Transition Systems

Comparison with Proofs by Induction

◮ We can compare both proofs to proofs by induction on natural

  • number. Consider a proof of

∀n, m, k.n + (m + k) = (n + m) + k

◮ The traditional proof would corresponds to defining a relation

R(k) ⇔ ∀n, m.n + (m + k) = (n + m) + k and showing R(0) ∧ ∀n.R(n) → R(S(n))

◮ Although this argument and the standard inductive proof using the

induction hypothesis are equivalent, the standard inductive proof is more convenient and easier to follow.

◮ We hope that proofs by coinduction will similarly be easier if we do it

by referring to the coinduction hypothesis.

Anton Setzer (Swansea) Pattern and Copattern matching 34/ 46

slide-35
SLIDE 35

Mixed Patterns and Copatterns

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 35/ 46

slide-36
SLIDE 36

Mixed Patterns and Copatterns

Nested Pattern Matching

◮ Course of Value primitive recursion allows deep pattern matching.

E.g. we can define the Fibonaccie numbers fib : N → N fib 0 = 1 fib (S 0) = 1 fib (S (S n)) = fib n + fib (S n)

◮ We can now even mix pattern and copattern matching.

Anton Setzer (Swansea) Pattern and Copattern matching 36/ 46

slide-37
SLIDE 37

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

◮ We can define now functions by patterns and copatterns. ◮ Example define stream:

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1,

Anton Setzer (Swansea) Pattern and Copattern matching 37/ 46

slide-38
SLIDE 38

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f = ?

Anton Setzer (Swansea) Pattern and Copattern matching 38/ 46

slide-39
SLIDE 39

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f = ? Copattern matching on f : N → Stream: f : N → Stream f n = ?

Anton Setzer (Swansea) Pattern and Copattern matching 38/ 46

slide-40
SLIDE 40

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Copattern matching on f n : Stream: f : N → Stream head (f n) = ? tail (f n) = ?

Anton Setzer (Swansea) Pattern and Copattern matching 38/ 46

slide-41
SLIDE 41

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Solve first case, copattern match on second case: f : N → Stream head (f n) = n head (tail (f n)) = ? tail (tail (f n)) = ?

Anton Setzer (Swansea) Pattern and Copattern matching 38/ 46

slide-42
SLIDE 42

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Solve second line, pattern match on n f : N → Stream head (f n) = n head (tail (f n)) = n tail (tail (f 0)) = ? tail (tail (f (S n))) = ?

Anton Setzer (Swansea) Pattern and Copattern matching 38/ 46

slide-43
SLIDE 43

Mixed Patterns and Copatterns

Example Mixed Pattern/Copattern Matching

f n = n, n, n−1, n−1, . . . 0, 0, N, N, N −1, N −1, . . . 0, 0, N, N, N −1, N −1, f : N → Stream f n = ? Solve remaining cases f : N → Stream head (f n) = n head (tail (f n)) = n tail (tail (f 0)) = f N tail (tail (f (S n))) = f n

Anton Setzer (Swansea) Pattern and Copattern matching 38/ 46

slide-44
SLIDE 44

Mixed Patterns and Copatterns

Results of paper in POPL (2013)

◮ Development of a recursive simply typed calculus (no termination

check).

◮ Allows to derive schemata for pattern/copattern matching. ◮ Proof that subject reduction holds.

t : A, t − → t′ implies t′ : A

◮ Subject reduction fails when using codata types in combination with

the equality type (e.g. in Coq and early versions of Agda).

Anton Setzer (Swansea) Pattern and Copattern matching 39/ 46

slide-45
SLIDE 45

Unnesting of Pattern/Copattern Matching

Iteration, Recursion, Induction Coiteration, Corecursion Bisimilarity and Coinduction Proofs by Coinduction of Bisimilarity in Transition Systems Mixed Patterns and Copatterns Unnesting of Pattern/Copattern Matching

Anton Setzer (Swansea) Pattern and Copattern matching 40/ 46

slide-46
SLIDE 46

Unnesting of Pattern/Copattern Matching

Consider Example from above

f : N → Stream head (f n) = n head (tail (f n)) = n tail (tail (f 0)) = f N tail (tail (f (S n))) = f n We show how this example can be reduced to unnested (co)pattern matching. In a second step (not shown today) one can reduce it to primitive (co)recursion operators.

Anton Setzer (Swansea) Pattern and Copattern matching 41/ 46

slide-47
SLIDE 47

Unnesting of Pattern/Copattern Matching

Unnesting of Nested (Co)Pattern Matching

We follow the steps in the pattern matching: We start with f : N → Stream head (f n) = n tail (f n) = ?

Anton Setzer (Swansea) Pattern and Copattern matching 42/ 46

slide-48
SLIDE 48

Copattern matching on tail (f n): f : N → Stream head (f n) = n head (tail (f n) = n tail (tail (f n) = ? corresponds to f : N → Stream head (f n) = n tail (f n) = g n g : N → Stream (head (tail (f n)) =) head (g n) = n (tail (tail (f n)) =) tail (g n) = ?

slide-49
SLIDE 49

Pattern matching on tail (tail (f n)): f : N → Stream head (f n) = n head (tail (f n) = n tail (tail (f 0) = f N tail (tail (f (S n)) = f n corresponds to f : N → Stream head (f n) = n tail (f n) = g n g : N → Stream (head (tail (f n)) =) head (g n) = n (tail (tail (f n)) =) tail (g n) = k n k : N → Stream (tail (tail (f 0)) =) k = f N (tail (tail (f (S n))) =) k (S n) = f n

slide-50
SLIDE 50

Unnesting of Pattern/Copattern Matching

Conclusion

◮ Principle of induction is well established and makes proofs much

easier.

◮ In theoretical computer science coinductive principles occur

frequently.

◮ Main reason: interactive programs running continuously in various

frameworks (imperative, object-oriented, process-calculi)

◮ Coalgebras as being defined by their eliminators rather than infinite

applications of constructors makes clear when recursive calls are allowed.

◮ Proofs by coinduction in the above situation can be carried out

similarly as proofs by induction.

◮ Main difficulty: when are we allowed to apply co-IH?

◮ In the corecursion step we have a proof obligation, and can use the

co-IH to prove it.

Anton Setzer (Swansea) Pattern and Copattern matching 45/ 46

slide-51
SLIDE 51

Unnesting of Pattern/Copattern Matching

Conclusion

◮ Copattern matching as the dual of pattern matching.

◮ Pattern matching is an elimination principle for inductive types (initial

algebras).

◮ Copattern matching is an introduction principle for coinductive types

(final coalgebras).

◮ Mixed pattern and copattern matching can be reduced to simple

pattern and copattern matching.

Anton Setzer (Swansea) Pattern and Copattern matching 46/ 46