Elaborating dependent (co)pattern matching Andreas Abel Jesper - - PowerPoint PPT Presentation

elaborating dependent co pattern matching
SMART_READER_LITE
LIVE PREVIEW

Elaborating dependent (co)pattern matching Andreas Abel Jesper - - PowerPoint PPT Presentation

Elaborating dependent (co)pattern matching Andreas Abel Jesper Cockx Chalmers & Gothenburg University 23 March 2018 Type systems & proof assistants: science or faith? surface core language language 1 / 38 Type systems &


slide-1
SLIDE 1

Elaborating dependent (co)pattern matching

Jesper Cockx Andreas Abel

Chalmers & Gothenburg University

23 March 2018

slide-2
SLIDE 2

Type systems & proof assistants: science or faith?

surface language core language

1 / 38

slide-3
SLIDE 3

Type systems & proof assistants: science or faith?

surface language

elaboration

= = = = = = ⇒ core language

1 / 38

slide-4
SLIDE 4

Type systems & proof assistants: science or faith?

science ↓ surface language

elaboration

= = = = = = ⇒ core language

1 / 38

slide-5
SLIDE 5

Type systems & proof assistants: science or faith?

faith ↓ science ↓ surface language

elaboration

= = = = = = ⇒ core language

1 / 38

slide-6
SLIDE 6

Type systems & proof assistants: science or faith?

faith ↓ science ↓ surface language

elaboration

= = = = = = ⇒ core language Goal: turn piece of faith into science.

1 / 38

slide-7
SLIDE 7
  • Presenting. . .

A core language with inductive data types, coinductive record types, an identity type, and typed case trees.

2 / 38

slide-8
SLIDE 8
  • Presenting. . .

A core language with inductive data types, coinductive record types, an identity type, and typed case trees. An elaboration algorithm from copattern matching to a well-typed case tree.

2 / 38

slide-9
SLIDE 9
  • Presenting. . .

A core language with inductive data types, coinductive record types, an identity type, and typed case trees. An elaboration algorithm from copattern matching to a well-typed case tree. A proof that elaboration preserves the first-match semantics of the clauses.

2 / 38

slide-10
SLIDE 10

Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

slide-11
SLIDE 11

Example: maximum

max : N → N → N max zero y = y max x zero = x max (suc x) (suc y) = suc (max x y)

3 / 38

slide-12
SLIDE 12

Example: maximum

max : N → N → N max zero y = y max x zero = x max (suc x) (suc y) = suc (max x y) First-match semantics: We don’t have max x zero = x, but only max (suc x) zero = suc x.

3 / 38

slide-13
SLIDE 13

Example: conatural numbers

record N∞ : Set where iszero : B pred : iszero ≡B false → N∞

4 / 38

slide-14
SLIDE 14

Example: conatural numbers

record N∞ : Set where iszero : B pred : iszero ≡B false → N∞ zero : N∞ zero .iszero = true zero .pred ∅ suc : N∞ → N∞ suc n .iszero = false suc n .pred = n inf : N∞ inf .iszero = false inf .pred = inf

4 / 38

slide-15
SLIDE 15

Example: C Streams

record S : Set where head : N tail : (m : N) → head ≡N suc m → S

5 / 38

slide-16
SLIDE 16

Example: C Streams

record S : Set where head : N tail : (m : N) → head ≡N suc m → S timer : N → S timer n .head = n timer zero .tail m ∅ timer (suc m) .tail m refl = timer m

5 / 38

slide-17
SLIDE 17

Example based on #2896

data D : N → Set where c : (n : N) → D n foo : (m : N) → D (suc m) → N foo m (c (suc n)) = m + n

6 / 38

slide-18
SLIDE 18

Example based on #2896

data D : N → Set where c : (n : N) → D n foo : (m : N) → D (suc m) → N foo m (c (suc n)) = m + n What does this even mean???

6 / 38

slide-19
SLIDE 19

Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

slide-20
SLIDE 20

Term syntax (surface and core)

A, B, u, v ::= (x : A) → B | Setℓ | D ¯ u | R ¯ u | u ≡A v | x ¯ e | f ¯ e | c ¯ u | refl e ::= u | .π ∆ ::= ϵ | (x : A)∆

7 / 38

slide-21
SLIDE 21

Surface language

decl ::= data D ∆ : Setℓ where c ∆ | record self : R ∆ : Setℓ where π : A | definition f : A where cls cls ::= ¯ q ֒ → u | ¯ q ֒ → impossible q ::= p | .π p ::= x | c ¯ p | refl | ⌊u⌋ | ∅

8 / 38

slide-22
SLIDE 22

Core language: typing rules

⊢ Γ Γ ⊢ Setℓ : Setℓ+1 Γ ⊢ A : Setℓ Γ(x : A) ⊢ B : Setℓ′ Γ ⊢ (x : A) → B : Setmax(ℓ,ℓ′) D : Setℓ ∈ Σ Γ ⊢ D : Setℓ R : Setℓ ∈ Σ Γ ⊢ R : Setℓ Γ ⊢ A : Setℓ Γ ⊢ u : A Γ ⊢ v : A Γ ⊢ u ≡A v : Setℓ x : A ∈ Γ Γ | x : A ⊢ ¯ e : C Γ ⊢ x ¯ e : C f : A ∈ Σ Γ | f : A ⊢ ¯ e : C Γ ⊢ f ¯ e : C c ∆c : D ∈ Σ Γ ⊢ ¯ v : ∆c Γ ⊢ c ¯ v : D Γ ⊢ A Γ ⊢ u : A Γ ⊢ refl : u ≡A u Γ ⊢ v : A Γ | u v : B[v / x] ⊢ ¯ e : C Γ | u : (x : A) → B ⊢ v ¯ e : C self : R ⊢ .π : A ∈ Σ Γ | u .π : A[u / self ] ⊢ ¯ e : C Γ | u : R ⊢ .π ¯ e : C Γ ⊢ u : A Γ ⊢ A = B Γ ⊢ u : B Γ ⊢ A = A′ Γ | u : A′ ⊢ ¯ e : C Γ | u : A ⊢ ¯ e : C 9 / 38

slide-23
SLIDE 23

Core language: case trees

Q ::= u | λx. Q | record{π1 → Q1; . . . ; πn → Qn} | casex{c1 ˆ ∆1 → Q1; . . . ; cn ˆ ∆n → Qn} | casex{refl →τ Q}

10 / 38

slide-24
SLIDE 24

Case tree typing

Γ | f ¯ q : A ⊢ Q

“The case tree Q gives a well-typed implementation of f applied to copatterns ¯ q”

11 / 38

slide-25
SLIDE 25

Case tree typing: v

Γ ⊢ v : C Γ | f ¯ q : C ⊢ v Side effect: Σ := Σ, (Γ ⊢ f ¯ q ֒ → v : C)

12 / 38

slide-26
SLIDE 26

Case tree typing: λx. Q

Γ(x : A) | f ¯ q x : B ⊢ Q Γ | f ¯ q : (x : A) → B ⊢ λx. Q

13 / 38

slide-27
SLIDE 27

Case tree typing: record{. . .}

record self : R : Setℓ where πi : Ai ∈ Σ (Γ | f ¯ q .πi : Ai[f ⌈¯ q⌉ / self ] ⊢ Qi)i=1...n Γ | f ¯ q : R ⊢ record{π1 → Q1; . . . ; πn → Qn}

14 / 38

slide-28
SLIDE 28

Case tree typing: casex{. . .}

D : Setℓ where ci ∆i ∈ Σ

  

ρi = [ci ˆ ∆i / x] Γ1∆i(Γ2ρi) | f ¯ qρi : Cρi ⊢ Qi

  

i=1...n

Γ1(x : D)Γ2 | f ¯ q : C ⊢ casex{c1 ˆ ∆1 → Q1; . . . ; cn ˆ ∆n → Qn}

15 / 38

slide-29
SLIDE 29

Case tree typing: casex{refl →τ Q}

Γ1 ⊢ u =? v : B ⇒ yes(Γ′

1, ρ, τ)

Γ′

1(Γ2ρ) | f ¯

qρ : Cρ ⊢ Q Γ1(x : u ≡B v)Γ2 | f ¯ q : C ⊢ casex{refl →τ Q}

   Γ′

1 ⊢ uρ = vρ : Aρ

Γ′

1 ⊢ τ; ρ = 1 : Γ′ 1

  

16 / 38

slide-30
SLIDE 30

Case tree typing: casex{}

Γ1 ⊢ u =? v : B ⇒ no Γ1(x : u ≡B v)Γ2 | f ¯ q : C ⊢ casex{}

17 / 38

slide-31
SLIDE 31

Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

slide-32
SLIDE 32

From clauses to a case tree

The clauses guide us in the construction of a well-typed case tree:

18 / 38

slide-33
SLIDE 33

From clauses to a case tree

The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses.

18 / 38

slide-34
SLIDE 34

From clauses to a case tree

The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. Γ | f ¯ q : A ⊢ P Q

18 / 38

slide-35
SLIDE 35

From clauses to a case tree

The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. Γ | f ¯ q : A ⊢ P Q entails Γ | f ¯ q : A ⊢ Q

18 / 38

slide-36
SLIDE 36

From clauses to a case tree

The clauses guide us in the construction of a well-typed case tree: as we construct the case tree, we deconstruct the clauses. Γ | f ¯ q : A ⊢ P Q entails Γ | f ¯ q : A ⊢ Q P =

{

[wik /? pik] ¯ qi ֒ → rhsi

}

i=1...n

18 / 38

slide-37
SLIDE 37

max : N → N → N zero j ֒ → j i zero ֒ → i (suc k) (suc l) ֒ → suc (max k l)

19 / 38

slide-38
SLIDE 38

(m : N) | max m : N → N [m /? zero] j ֒ → j [m /? i] zero ֒ → i [m /? suc k] (suc l) ֒ → suc (max k l)

19 / 38

slide-39
SLIDE 39

max zero : N → N [zero /? zero] j ֒ → j [zero /? i] zero ֒ → i [zero /? suc k] (suc l) ֒ → suc (max k l) (p : N) | max (suc p) : N → N [suc p /? zero] j ֒ → j [suc p /? i] zero ֒ → i [suc p /? suc k] (suc l) ֒ → suc (max k l)

19 / 38

slide-40
SLIDE 40

max zero : N → N j ֒ → j [zero /? i] zero ֒ → i (p : N) | max (suc p) : N → N [suc p /? i] zero ֒ → i [p /? k] (suc l) ֒ → suc (max k l)

19 / 38

slide-41
SLIDE 41

(n : N) | max zero n : N [n /? j] ֒ → j [zero /? i, n /? zero] ֒ → i (p : N)(n : N) | max (suc p) n : N [suc p /? i, n /? zero] ֒ → i [p /? k, n /? suc l] ֒ → suc (max k l)

19 / 38

slide-42
SLIDE 42

(n : N) | max zero n ֒ → n : N (p : N)(n : N) | max (suc p) n : N [suc p /? i, n /? zero] ֒ → i [p /? k, n /? suc l] ֒ → suc (max k l)

19 / 38

slide-43
SLIDE 43

(n : N) | max zero n ֒ → n : N (p : N) | max (suc p) zero : N [suc p /? i] ֒ → i (p : N)(q : N) | max (suc p) (suc q) : N [p /? k, q /? l] ֒ → suc (max k l)

19 / 38

slide-44
SLIDE 44

(n : N) | max zero n ֒ → n : N (p : N) | max (suc p) zero ֒ → suc p : N (p : N)(q : N) | max (suc p) (suc q) : N [p /? k, q /? l] ֒ → suc (max k l)

19 / 38

slide-45
SLIDE 45

(n : N) | max zero n ֒ → n : N (p : N) | max (suc p) zero ֒ → suc p : N (p : N)(q : N) | max (suc p) (suc q) ֒ → suc (max p q) : N

19 / 38

slide-46
SLIDE 46

Case tree for max

λm. casem

                          

zero → λn. n suc p → λn. casen

            

zero → suc p suc q → suc (max p q)

                                       

20 / 38

slide-47
SLIDE 47

zero : N∞ .iszero ֒ → true .pred ∅ ֒ → impossible

21 / 38

slide-48
SLIDE 48

zero .iszero : B ֒ → true zero .pred : zero .iszero ≡B false → N∞ ∅ ֒ → impossible

21 / 38

slide-49
SLIDE 49

zero .iszero ֒ → true : B zero .pred : zero .iszero ≡B false → N∞ ∅ ֒ → impossible

21 / 38

slide-50
SLIDE 50

zero .iszero ֒ → true : B x : zero .iszero ≡B false | zero .pred x : N∞ [x /? ∅] ֒ → impossible

21 / 38

slide-51
SLIDE 51

zero .iszero ֒ → true : B

21 / 38

slide-52
SLIDE 52

Case tree for zero

record

    

iszero → true pred → λx. casex{}

    

22 / 38

slide-53
SLIDE 53

Case tree for timer

λn. record

                                  

head → n tail → λm, p. casen

                  

zero → casep{} suc n′ → casep

    

refl →1 timer m

                                                          

23 / 38

slide-54
SLIDE 54

data D : N → Set where c : (n : N) → D n foo : (m : N) → D (suc m) → N m (c (suc n)) ֒ → m + n

24 / 38

slide-55
SLIDE 55

data D : N → Set where c : (n : N) → D n (m : N)(x : D (suc m)) | foo m x : N [m /? m, x /? c (suc n)] ֒ → m + n

24 / 38

slide-56
SLIDE 56

data D : N → Set where c : (n : N) → D n (m : N) | foo m (c (suc m)) : N [m /? m, c (suc m) /? c (suc n)] ֒ → m + n

24 / 38

slide-57
SLIDE 57

data D : N → Set where c : (n : N) → D n (m : N) | foo m (c (suc m)) : N [m /? m, m /? n] ֒ → m + n

24 / 38

slide-58
SLIDE 58

data D : N → Set where c : (n : N) → D n (m : N) | foo m (c (suc m)) ֒ → m + m : N

24 / 38

slide-59
SLIDE 59

Case tree for foo

λm, x. casex

    

c n p → casep

{

refl →1m (m + m)

}     

25 / 38

slide-60
SLIDE 60

Constructing a case tree: nothing to split

¯ q1 = ϵ Γ ⊢ E1 ⇒ solved(σ) rhs1 = v Γ ⊢ vσ : C Γ | f ¯ q : C ⊢ P vσ Side effect: Σ = Σ, Γ ⊢ f ¯ q ֒ → vσ : C

26 / 38

slide-61
SLIDE 61

Constructing a case tree: introduce new variable

¯ q1 = p ¯ q′

1

C ↘ (x : A) → B Γ(x : A) | f ¯ q x : B ⊢ P (x : A) Q Γ | f ¯ q : C ⊢ P λx. Q

27 / 38

slide-62
SLIDE 62

Constructing a case tree: split on result

¯ q1 = .πi ¯ q′

1

C ↘ R record self : R : Setℓ where πi : Ai ∈ Σ (Γ | f ¯ q .πi : Ai[f ⌈¯ q⌉ / self ] ⊢ P .πi Qi)i=1...n Γ | f ¯ q : C ⊢ P record{π1 → Q1; . . . ; πn → Qn}

28 / 38

slide-63
SLIDE 63

Constructing a case tree: absurd split on result

¯ q1 = ∅ m = 1 C ↘ R ¯ v record : R : Setℓ where ϵ ∈ Σ rhs1 = impossible Γ | f ¯ q : C ⊢ P record{}

29 / 38

slide-64
SLIDE 64

Constructing a case tree: split on variable

(x /? cj ¯ p : A) ∈ E1 A ↘ D Γ = Γ1(x : A)Γ2 data D : Setℓ where ci ∆i ∈ Σ

 

ρi = [ci ˆ ∆i / x] Pρi ⇒ Pi (Γ1∆i(Γ2ρi) | f ¯ qρi : Cρi ⊢ Pi Qi

  i=1...n

Γ | f ¯ q : C ⊢ P casex{c1 ˆ ∆1 → Q1; . . . ; cn ˆ ∆n → Qn}

30 / 38

slide-65
SLIDE 65

Constructing a case tree: split on equation

(x /? refl : A) ∈ E1 A ↘ u ≡B v Γ = Γ1(x : A)Γ2 Γ1 ⊢ u =? v : B ⇒ yes(Γ′

1, ρ, τ)

Σ ⊢ Pρ ⇒ P′ Γ′

1(Γ2ρ) | f ¯

qρ : Cρ ⊢ P′ Q Γ | f ¯ q : C ⊢ P casex{refl →τ ′ Q}

31 / 38

slide-66
SLIDE 66

Constructing a case tree: split on empty type

(x /? ∅ : A) ∈ E1 Γ ⊢ ∅ : A rhs1 = impossible Γ | f ¯ q : C ⊢ P casex{}

32 / 38

slide-67
SLIDE 67

Dependent copattern matching Surface and core languages From clauses to a case tree Preservation of first-match semantics

slide-68
SLIDE 68

Evaluation of case trees

(λx. Q)σ u ¯ e − → Q(σ ⊎ [u / x]) ¯ e

33 / 38

slide-69
SLIDE 69

Evaluation of case trees

(λx. Q)σ u ¯ e − → Q(σ ⊎ [u / x]) ¯ e (record{π1 → Q1; . . . ; πn → Qn})σ .πi ¯ e − → Qiσ ¯ e

33 / 38

slide-70
SLIDE 70

Evaluation of case trees

(λx. Q)σ u ¯ e − → Q(σ ⊎ [u / x]) ¯ e (record{π1 → Q1; . . . ; πn → Qn})σ .πi ¯ e − → Qiσ ¯ e (casex{c1 ˆ ∆1 → Q1; . . . ; cn ˆ ∆n → Qn})σ ¯ e − → Qi(σ\x ⊎ [¯ u / ˆ ∆i]) ¯ e (if xσ ↘ c ¯ u)

33 / 38

slide-71
SLIDE 71

Evaluation of case trees

(λx. Q)σ u ¯ e − → Q(σ ⊎ [u / x]) ¯ e (record{π1 → Q1; . . . ; πn → Qn})σ .πi ¯ e − → Qiσ ¯ e (casex{c1 ˆ ∆1 → Q1; . . . ; cn ˆ ∆n → Qn})σ ¯ e − → Qi(σ\x ⊎ [¯ u / ˆ ∆i]) ¯ e (if xσ ↘ c ¯ u) (casex{refl →τ Q})σ ¯ e − → Q(τ; σ) ¯ e (if xσ ↘ refl)

33 / 38

slide-72
SLIDE 72

Matching algorithm

[v / x] ↘ [v / x] [v / ⌊u⌋] ↘ [] v ↘ refl [v / refl] ↘ [] v ↘ c ¯ u [¯ u / ¯ p] ↘ σ⊥ [v / c ¯ p] ↘ σ⊥ v ↘ c2 ¯ u c1 ̸= c2 [v / c1 ¯ p] ↘ ⊥ [.π / .π] ↘ [] π1 ̸= π2 [.π2 / .π1] ↘ ⊥ [ϵ / ϵ] ↘ [] [e / q] ↘ σ⊥ [¯ e / ¯ q] ↘ τ⊥ [e ¯ e / q ¯ q] ↘ σ⊥ ⊎ τ⊥

34 / 38

slide-73
SLIDE 73

First-match semantics

If f is given by {¯ qi ֒ → rhsi | i = 1 . . . n} and

e / ¯ qj] ↘ ⊥ for j = 1 . . . i − 1

e / ¯ qi] ↘ σ then f ¯ e − → uiσ.

35 / 38

slide-74
SLIDE 74

Preservation of first-match semantics

Let f be given by P = {¯ qi ֒ → rhsi}i=1...n. If

  • ϵ | f : A ⊢ P Q
  • Γ | f : A ⊢ ¯

e : B

  • f ¯

e − → u (according to first-match) then also Q ¯ e − → u.

36 / 38

slide-75
SLIDE 75

The shortcut rule for matching

Can we allow the following rule? [e / q] ↘ ⊥ [e ¯ e / q ¯ q] ↘ ⊥

37 / 38

slide-76
SLIDE 76

The shortcut rule for matching

Can we allow the following rule? [e / q] ↘ ⊥ [e ¯ e / q ¯ q] ↘ ⊥ No! Otherwise this function has no case tree: f : (A : Set) → A → B → (A ≡Set B) → B f ⌊B⌋ true true refl = true f = false

37 / 38

slide-77
SLIDE 77

Conclusion

To use a typechecker or proof assistant, we need to trust not only the core language but also the elaboration algorithm.

38 / 38

slide-78
SLIDE 78

Conclusion

To use a typechecker or proof assistant, we need to trust not only the core language but also the elaboration algorithm. It pays off to formalize elaboration: I found a bug in Agda by writing the proof.

38 / 38

slide-79
SLIDE 79

Conclusion

To use a typechecker or proof assistant, we need to trust not only the core language but also the elaboration algorithm. It pays off to formalize elaboration: I found a bug in Agda by writing the proof. Let’s work together on a formally verified typechecker for dependent types!

38 / 38