Depending on equations A proof-relevant framework for unification - - PowerPoint PPT Presentation

depending on equations
SMART_READER_LITE
LIVE PREVIEW

Depending on equations A proof-relevant framework for unification - - PowerPoint PPT Presentation

Depending on equations A proof-relevant framework for unification in dependent type theory Jesper Cockx DistriNet KU Leuven 3 September 2017 Unification for dependent types Unification is used for many purposes: logic programming, type


slide-1
SLIDE 1

Depending on equations

A proof-relevant framework for unification in dependent type theory Jesper Cockx

DistriNet – KU Leuven

3 September 2017

slide-2
SLIDE 2

Unification for dependent types

Unification is used for many purposes: logic programming, type inference, term rewriting, automated theorem proving, natural language processing, . . . This talk: checking definitions by dependent pattern matching

1 / 52

slide-3
SLIDE 3

Disclaimer

My work is on dependently typed languages, I know little about unification.

2 / 52

slide-4
SLIDE 4

Disclaimer

My work is on dependently typed languages, I know little about unification. This talk is about first-order unification: (suc x = suc y) = ⇒ (x = y)

x:=y

= = ⇒ OK

2 / 52

slide-5
SLIDE 5

Disclaimer

My work is on dependently typed languages, I know little about unification. This talk is about first-order unification: (suc x = suc y) = ⇒ (x = y)

x:=y

= = ⇒ OK (suc x = zero) = ⇒ ⊥

2 / 52

slide-6
SLIDE 6

Disclaimer

My work is on dependently typed languages, I know little about unification. This talk is about first-order unification: (suc x = suc y) = ⇒ (x = y)

x:=y

= = ⇒ OK (suc x = zero) = ⇒ ⊥ . . . but there will be types everywhere!

2 / 52

slide-7
SLIDE 7

Dependent types: the ‘big five’

During this presentation, we’ll spot:

  • Dependent functions: (x : A) → B x

3 / 52

slide-8
SLIDE 8

Dependent types: the ‘big five’

During this presentation, we’ll spot:

  • Dependent functions: (x : A) → B x
  • Indexed datatypes: Vec A n, . . .

3 / 52

slide-9
SLIDE 9

Dependent types: the ‘big five’

During this presentation, we’ll spot:

  • Dependent functions: (x : A) → B x
  • Indexed datatypes: Vec A n, . . .
  • Identity types: x ≡A y

3 / 52

slide-10
SLIDE 10

Dependent types: the ‘big five’

During this presentation, we’ll spot:

  • Dependent functions: (x : A) → B x
  • Indexed datatypes: Vec A n, . . .
  • Identity types: x ≡A y
  • Universes: Typei

3 / 52

slide-11
SLIDE 11

Dependent types: the ‘big five’

During this presentation, we’ll spot:

  • Dependent functions: (x : A) → B x
  • Indexed datatypes: Vec A n, . . .
  • Identity types: x ≡A y
  • Universes: Typei
  • Univalence: (A ≡ B) ≃ (A ≃ B)

3 / 52

slide-12
SLIDE 12

Dependent types: the ‘big five’

During this presentation, we’ll spot:

  • Dependent functions: (x : A) → B x
  • Indexed datatypes: Vec A n, . . .
  • Identity types: x ≡A y
  • Universes: Typei
  • Univalence: (A ≡ B) ≃ (A ≃ B)

and see how they interact with unification!

3 / 52

slide-13
SLIDE 13

Depending on equations

Checking dependently typed programs Unification in dependent type theory Unification of dependently typed terms

slide-14
SLIDE 14

Depending on equations

Checking dependently typed programs Unification in dependent type theory Unification of dependently typed terms

slide-15
SLIDE 15

Why use dependent types?

With dependent types, you can . . .

4 / 52

slide-16
SLIDE 16

Why use dependent types?

With dependent types, you can . . . . . . guarantee that a program matches its specification

4 / 52

slide-17
SLIDE 17

Why use dependent types?

With dependent types, you can . . . . . . guarantee that a program matches its specification . . . use the same language for writing programs and proofs

4 / 52

slide-18
SLIDE 18

Why use dependent types?

With dependent types, you can . . . . . . guarantee that a program matches its specification . . . use the same language for writing programs and proofs . . . develop programs and proofs interactively

4 / 52

slide-19
SLIDE 19

Dependent types

Per Martin-L¨

  • f

A dependent type is a family of types, depending

  • n a term of a base type.

5 / 52

slide-20
SLIDE 20

Dependent types

Per Martin-L¨

  • f

A dependent type is a family of types, depending

  • n a term of a base type.

e.g. Vec A n is the type of vectors of length n.

5 / 52

slide-21
SLIDE 21

The Agda language

Agda is a purely functional language

6 / 52

slide-22
SLIDE 22

The Agda language

Agda is a purely functional language . . . with a strong, static type system

6 / 52

slide-23
SLIDE 23

The Agda language

Agda is a purely functional language . . . with a strong, static type system . . . for writing programs and proofs

6 / 52

slide-24
SLIDE 24

The Agda language

Agda is a purely functional language . . . with a strong, static type system . . . for writing programs and proofs . . . with datatypes and pattern matching

6 / 52

slide-25
SLIDE 25

The Agda language

Agda is a purely functional language . . . with a strong, static type system . . . for writing programs and proofs . . . with datatypes and pattern matching . . . with first-class dependent types

6 / 52

slide-26
SLIDE 26

The Agda language

Agda is a purely functional language . . . with a strong, static type system . . . for writing programs and proofs . . . with datatypes and pattern matching . . . with first-class dependent types . . . with support for interactive development

6 / 52

slide-27
SLIDE 27

The Agda language

Agda is a purely functional language . . . with a strong, static type system . . . for writing programs and proofs . . . with datatypes and pattern matching . . . with first-class dependent types . . . with support for interactive development All examples are (mostly) valid Agda code!

6 / 52

slide-28
SLIDE 28

Using dependent types

With dependent types, we can give more precise types to our programs: replicate : (n : N) → A → Vec A n

7 / 52

slide-29
SLIDE 29

Using dependent types

With dependent types, we can give more precise types to our programs: replicate : (n : N) → A → Vec A n ⇒ replicate 10 ‘a’ : Vec Char 10

7 / 52

slide-30
SLIDE 30

Using dependent types

With dependent types, we can give more precise types to our programs: replicate : (n : N) → A → Vec A n tail : (n : N) → Vec A (suc n) → Vec A n

7 / 52

slide-31
SLIDE 31

Using dependent types

With dependent types, we can give more precise types to our programs: replicate : (n : N) → A → Vec A n tail : (n : N) → Vec A (suc n) → Vec A n append : (m n : N) → Vec A m → Vec A n → Vec A (m + n)

7 / 52

slide-32
SLIDE 32

Simple pattern matching

data N : Type where zero : N suc : N → N

8 / 52

slide-33
SLIDE 33

Simple pattern matching

data N : Type where zero : N suc : N → N minimum : N → N → N minimum x y = { }

8 / 52

slide-34
SLIDE 34

Simple pattern matching

data N : Type where zero : N suc : N → N minimum : N → N → N minimum zero y = { } minimum (suc x) y = { }

8 / 52

slide-35
SLIDE 35

Simple pattern matching

data N : Type where zero : N suc : N → N minimum : N → N → N minimum zero y = zero minimum (suc x) y = { }

8 / 52

slide-36
SLIDE 36

Simple pattern matching

data N : Type where zero : N suc : N → N minimum : N → N → N minimum zero y = zero minimum (suc x) zero = { } minimum (suc x) (suc y) = { }

8 / 52

slide-37
SLIDE 37

Simple pattern matching

data N : Type where zero : N suc : N → N minimum : N → N → N minimum zero y = zero minimum (suc x) zero = zero minimum (suc x) (suc y) = { }

8 / 52

slide-38
SLIDE 38

Simple pattern matching

data N : Type where zero : N suc : N → N minimum : N → N → N minimum zero y = zero minimum (suc x) zero = zero minimum (suc x) (suc y) = suc (minimum x y)

8 / 52

slide-39
SLIDE 39

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n)

9 / 52

slide-40
SLIDE 40

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail k xs = { }

9 / 52

slide-41
SLIDE 41

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail k nil = { } -- suc k = zero tail k (cons n x xs) = { } -- suc k = suc n

9 / 52

slide-42
SLIDE 42

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail k nil = { } -- impossible tail k (cons n x xs) = { } -- suc k = suc n

9 / 52

slide-43
SLIDE 43

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail k (cons n x xs) = { } -- suc k = suc n

9 / 52

slide-44
SLIDE 44

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail k (cons n x xs) = { } -- k = n

9 / 52

slide-45
SLIDE 45

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail .n (cons n x xs) = { }

9 / 52

slide-46
SLIDE 46

Dependent pattern matching

data Vec (A : Type) : N → Type where nil : Vec A zero cons : (n : N) → A → Vec A n → Vec A (suc n) tail : (k : N) → Vec A (suc k) → Vec A k tail .n (cons n x xs) = xs

9 / 52

slide-47
SLIDE 47

Specialization by unification

Agda uses unification to:

  • eliminate impossible cases
  • specialize the result type

10 / 52

slide-48
SLIDE 48

Specialization by unification

Agda uses unification to:

  • eliminate impossible cases
  • specialize the result type

The output of unification can change Agda’s notion of equality!

10 / 52

slide-49
SLIDE 49

Specialization by unification

Agda uses unification to:

  • eliminate impossible cases
  • specialize the result type

The output of unification can change Agda’s notion of equality! Main question: How to make sure the output of unification is correct?

10 / 52

slide-50
SLIDE 50

Depending on equations

Checking dependently typed programs Unification in dependent type theory Unification of dependently typed terms

slide-51
SLIDE 51

Q: What is the fastest way to start a fight between type theorists?

11 / 52

slide-52
SLIDE 52

Q: What is the fastest way to start a fight between type theorists? A: Mention the topic of equality.

11 / 52

slide-53
SLIDE 53

The identity type x ≡A y

. . . a dependent type depending on x, y : A.

12 / 52

slide-54
SLIDE 54

The identity type x ≡A y

. . . a dependent type depending on x, y : A. . . . type theory’s built-in notion of equality.

12 / 52

slide-55
SLIDE 55

The identity type x ≡A y

. . . a dependent type depending on x, y : A. . . . type theory’s built-in notion of equality. . . . the type of proofs that x = y.

12 / 52

slide-56
SLIDE 56

Operations on the identity type

refl : x ≡A x

13 / 52

slide-57
SLIDE 57

Operations on the identity type

refl : x ≡A x sym : x ≡A y → y ≡A x

13 / 52

slide-58
SLIDE 58

Operations on the identity type

refl : x ≡A x sym : x ≡A y → y ≡A x trans : x ≡A y → y ≡A z → x ≡A z

13 / 52

slide-59
SLIDE 59

Operations on the identity type

refl : x ≡A x sym : x ≡A y → y ≡A x trans : x ≡A y → y ≡A z → x ≡A z cong f : x ≡A y → f x ≡B f y

13 / 52

slide-60
SLIDE 60

Operations on the identity type

refl : x ≡A x sym : x ≡A y → y ≡A x trans : x ≡A y → y ≡A z → x ≡A z cong f : x ≡A y → f x ≡B f y subst P : x ≡A y → P x → P y

13 / 52

slide-61
SLIDE 61

Unification problems as telescopes

A unification problem consists of

  • 1. Flexible variables x1 : A1, x2 : A2, . . .
  • 2. Equations u1 = v1 : B1, . . .

14 / 52

slide-62
SLIDE 62

Unification problems as telescopes

A unification problem consists of

  • 1. Flexible variables x1 : A1, x2 : A2, . . .
  • 2. Equations u1 = v1 : B1, . . .

This can be represented as a telescope: (x1 : A1)(x2 : A2) . . . (e1 : u1 ≡B1 v1)(e2 : u2 ≡B2 v2) . . . e.g. (k : N)(n : N)(e : suc k ≡N suc n)

14 / 52

slide-63
SLIDE 63

Unification problems as telescopes

A unification problem consists of

  • 1. Flexible variables Γ
  • 2. Equations u1 = v1 : B1, . . .

This can be represented as a telescope: Γ (e1 : u1 ≡B1 v1)(e2 : u2 ≡B2 v2) . . . e.g. (k : N)(n : N)(e : suc k ≡N suc n)

14 / 52

slide-64
SLIDE 64

Unification problems as telescopes

A unification problem consists of

  • 1. Flexible variables Γ
  • 2. Equations ¯

u = ¯ v : ∆ This can be represented as a telescope: Γ(¯ e : ¯ u ≡∆ ¯ v) e.g. (k : N)(n : N)(e : suc k ≡N suc n)

14 / 52

slide-65
SLIDE 65

Unifiers as telescope maps

A unifier of ¯ u and ¯ v is a substitution σ : Γ′ → Γ such that ¯ uσ = ¯ vσ.

15 / 52

slide-66
SLIDE 66

Unifiers as telescope maps

A unifier of ¯ u and ¯ v is a substitution σ : Γ′ → Γ such that ¯ uσ = ¯ vσ. This can be represented as a telescope map: f : Γ′ → Γ(¯ e : ¯ u ≡∆ ¯ v) e.g. f : () → (n : N)(e : n ≡N zero) f () = zero; refl

15 / 52

slide-67
SLIDE 67

Evidence of unification

A map f : () → (n : N)(e : n ≡N zero) gives us two things:

16 / 52

slide-68
SLIDE 68

Evidence of unification

A map f : () → (n : N)(e : n ≡N zero) gives us two things:

  • 1. A value for n such that n ≡N zero

16 / 52

slide-69
SLIDE 69

Evidence of unification

A map f : () → (n : N)(e : n ≡N zero) gives us two things:

  • 1. A value for n such that n ≡N zero
  • 2. Explicit evidence e of n ≡N zero

16 / 52

slide-70
SLIDE 70

Evidence of unification

A map f : () → (n : N)(e : n ≡N zero) gives us two things:

  • 1. A value for n such that n ≡N zero
  • 2. Explicit evidence e of n ≡N zero

= ⇒ Unification is guaranteed to respect ≡!

16 / 52

slide-71
SLIDE 71

Three valid unifiers

f1 : (k : N) → (k n : N)(e : k ≡N n) f1 k = k; k; refl f2 : () → (k n : N)(e : k ≡N n) f2 () = zero; zero; refl f3 : (k n : N) → (k n : N)(e : k ≡N n) f3 k n = k; k; refl

17 / 52

slide-72
SLIDE 72

Most general unifiers

A most general unifier of ¯ u and ¯ v is a unifier σ such that for any σ′ with ¯ uσ′ = ¯ vσ′, there is a ν such that σ′ = σ ◦ ν.

18 / 52

slide-73
SLIDE 73

Most general unifiers

A most general unifier of ¯ u and ¯ v is a unifier σ such that for any σ′ with ¯ uσ′ = ¯ vσ′, there is a ν such that σ′ = σ ◦ ν. This is quite difficult to translate to type theory directly. . .

18 / 52

slide-74
SLIDE 74

Most general unifiers

A most general unifier of ¯ u and ¯ v is a unifier σ such that for any σ′ with ¯ uσ′ = ¯ vσ′, there is a ν such that σ′ = σ ◦ ν. This is quite difficult to translate to type theory directly. . . Intuition: if f : Γ′ → Γ(¯ e : ¯ u ≡∆ ¯ v) is MGU, we can go back from Γ(¯ e : ¯ u ≡∆ ¯ v) to Γ′ without losing any information.

18 / 52

slide-75
SLIDE 75

Equivalences

A function f : A → B is an equivalence if it has both a left and a right inverse: isLinv : (x : A) → g1 (f x) ≡A x isRinv : (y : B) → f (g2 y) ≡B y In this case, we write f : A ≃ B.

19 / 52

slide-76
SLIDE 76

Most general unifiers are equivalences!

f : Γ(¯ e : ¯ u ≡∆ ¯ v) ≃ Γ′

20 / 52

slide-77
SLIDE 77

Example of unification

(k n : N)(e : suc k ≡N suc n)

21 / 52

slide-78
SLIDE 78

Example of unification

(k n : N)(e : suc k ≡N suc n) ≃ (k n : N)(e : k ≡N n)

21 / 52

slide-79
SLIDE 79

Example of unification

(k n : N)(e : suc k ≡N suc n) ≃ (k n : N)(e : k ≡N n) ≃ (k : N)

21 / 52

slide-80
SLIDE 80

Example of unification

(k n : N)(e : suc k ≡N suc n) ≃ (k n : N)(e : k ≡N n) ≃ (k : N) f : (k : N) → (k n : N)(e : suc k ≡N suc n) f k = k; k; refl

21 / 52

slide-81
SLIDE 81

The solution rule

solution : (x : A)(e : x ≡A t) ≃ ()

22 / 52

slide-82
SLIDE 82

The deletion rule

deletion : (e : t ≡A t) ≃ ()

23 / 52

slide-83
SLIDE 83

The injectivity rule

injectivitysuc : (e : suc x ≡N suc y) ≃ (e′ : x ≡N y)

24 / 52

slide-84
SLIDE 84

Negative unification rules

A negative unification rule applies to impossible equations, e.g. suc x = zero.

25 / 52

slide-85
SLIDE 85

Negative unification rules

A negative unification rule applies to impossible equations, e.g. suc x = zero. This can be represented by an equivalence: (e : suc x ≡N zero) ≃ ⊥ where ⊥ is the empty type.

25 / 52

slide-86
SLIDE 86

The conflict rule

conflictsuc,zero : (e : suc x ≡N zero) ≃ ⊥

26 / 52

slide-87
SLIDE 87

The cycle rule

cyclen,suc n : (e : n ≡N suc n) ≃ ⊥

27 / 52

slide-88
SLIDE 88

Unifiers as equivalences

By requiring unifiers to be equivalences:

  • we exclude bad unification rules
  • we can safely introduce new rules

28 / 52

slide-89
SLIDE 89

Unifiers as equivalences

By requiring unifiers to be equivalences:

  • we exclude bad unification rules
  • we can safely introduce new rules

Next, we’ll explore how this idea can help us. Any questions so far?

28 / 52

slide-90
SLIDE 90

Depending on equations

Checking dependently typed programs Unification in dependent type theory Unification of dependently typed terms

slide-91
SLIDE 91

Time for the interesting bits!

  • Equations between types
  • Heterogeneous equations
  • Equations on indexed datatypes
  • Equations between equations

29 / 52

slide-92
SLIDE 92

Equations between types

Types are first-class terms of type Type: Bool : Type, N : Type, N → N : Type, . . .

30 / 52

slide-93
SLIDE 93

Equations between types

Types are first-class terms of type Type: Bool : Type, N : Type, N → N : Type, . . . We can form equations between types, e.g. Bool ≡Type Bool.

30 / 52

slide-94
SLIDE 94

Equations between types

Types are first-class terms of type Type: Bool : Type, N : Type, N → N : Type, . . . We can form equations between types, e.g. Bool ≡Type Bool. Q: Can we apply the deletion rule?

30 / 52

slide-95
SLIDE 95

Equations between types

Types are first-class terms of type Type: Bool : Type, N : Type, N → N : Type, . . . We can form equations between types, e.g. Bool ≡Type Bool. Q: Can we apply the deletion rule? A: Depends on which type theory we use!

30 / 52

slide-96
SLIDE 96

The univalence axiom (2009)

Vladimir Voevodsky

31 / 52

slide-97
SLIDE 97

The univalence axiom (2009)

Vladimir Voevodsky “Isomorphic types can be identified.”

31 / 52

slide-98
SLIDE 98

The univalence axiom (2009)

Vladimir Voevodsky “Isomorphic types can be identified.” (A ≡ B) ≃ (A ≃ B)

31 / 52

slide-99
SLIDE 99

The univalence axiom (2009)

Bool is equal to Bool in two ways: true false Bool

32 / 52

slide-100
SLIDE 100

The univalence axiom (2009)

Bool is equal to Bool in two ways: true false Bool true false Bool

32 / 52

slide-101
SLIDE 101

The univalence axiom (2009)

Bool is equal to Bool in two ways: true false Bool true false Bool

32 / 52

slide-102
SLIDE 102

The univalence axiom (2009)

Bool is equal to Bool in two ways: true false Bool true false Bool

32 / 52

slide-103
SLIDE 103

Limiting the deletion rule

The deletion rule does not always hold: there might be multiple proofs of x ≡A x. E.g. Bool ≡Type Bool has two elements.

33 / 52

slide-104
SLIDE 104

Limiting the deletion rule

The deletion rule does not always hold: there might be multiple proofs of x ≡A x. E.g. Bool ≡Type Bool has two elements. We cannot use deletion in this case!

33 / 52

slide-105
SLIDE 105

Heterogeneous equations

Σn:NVec A n is the type of pairs (n, xs) where n : N and xs : Vec A n.

34 / 52

slide-106
SLIDE 106

Heterogeneous equations

Σn:NVec A n is the type of pairs (n, xs) where n : N and xs : Vec A n. (e : (0, nil) ≡Σn:NVec A n (1, cons 0 x xs)) ≃ (e1 : 0 ≡N 1)(e2 : nil ≡Vec A ??? cons 0 x xs)

34 / 52

slide-107
SLIDE 107

Heterogeneous equations

Σn:NVec A n is the type of pairs (n, xs) where n : N and xs : Vec A n. (e : (0, nil) ≡Σn:NVec A n (1, cons 0 x xs)) ≃ (e1 : 0 ≡N 1)(e2 : nil ≡Vec A ??? cons 0 x xs) What is the type of e2?

34 / 52

slide-108
SLIDE 108

Heterogeneous equations

Solution: use equation variables as placeholders for their solutions: (e : (0, nil) ≡Σn:NVec A n (1, cons 0 x xs)) ≃ (e1 : 0 ≡N 1)(e2 : nil ≡Vec A e1 cons 0 x xs)

35 / 52

slide-109
SLIDE 109

Heterogeneous equations

Solution: use equation variables as placeholders for their solutions: (e : (0, nil) ≡Σn:NVec A n (1, cons 0 x xs)) ≃ (e1 : 0 ≡N 1)(e2 : nil ≡Vec A e1 cons 0 x xs) This is called a telescopic equality.

35 / 52

slide-110
SLIDE 110

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeA (Bool, false))

36 / 52

slide-111
SLIDE 111

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeA (Bool, false)) ≃ (e1 : Bool ≡Type Bool)(e2 : true ≡e1 false)

36 / 52

slide-112
SLIDE 112

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeA (Bool, false)) ≃ (e1 : Bool ≡Type Bool)(e2 : true ≡e1 false) ≃ ⊥

36 / 52

slide-113
SLIDE 113

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeA (Bool, false)) ≃ (e1 : Bool ≡Type Bool)(e2 : true ≡e1 false) ≃ ⊥ The conflict rule does not apply!

36 / 52

slide-114
SLIDE 114

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeBool (Bool, false))

37 / 52

slide-115
SLIDE 115

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeBool (Bool, false)) ≃ (e1 : Bool ≡Type Bool)(e2 : true ≡Bool false)

37 / 52

slide-116
SLIDE 116

Be careful with heterogeneous equations!

(e : (Bool, true) ≡ΣA:TypeBool (Bool, false)) ≃ (e1 : Bool ≡Type Bool)(e2 : true ≡Bool false) ≃ ⊥ Whether a unification rule can be applied depends on the type of the equation!

37 / 52

slide-117
SLIDE 117

Injectivity for indexed data

Do standard unification rules apply to constructors of indexed datatypes? (e : cons n x xs ≡Vec A (suc n) cons n y ys) ≃ ???

38 / 52

slide-118
SLIDE 118

Injectivity for indexed data

Idea: simplify equations between indices together with equation between constructors: (e1 : suc k ≡N suc n) (e2 : cons k x xs ≡Vec A e1 cons n y ys)

39 / 52

slide-119
SLIDE 119

Injectivity for indexed data

Idea: simplify equations between indices together with equation between constructors: (e1 : suc k ≡N suc n) (e2 : cons k x xs ≡Vec A e1 cons n y ys) ≃ (e′

1 : k ≡N n)(e′ 2 : x ≡A y)

(e′

3 : xs ≡Vec A e1 ys)

39 / 52

slide-120
SLIDE 120

Injectivity for indexed data

Idea: simplify equations between indices together with equation between constructors: (e1 : suc k ≡N suc n) (e2 : cons k x xs ≡Vec A e1 cons n y ys) ≃ (e′

1 : k ≡N n)(e′ 2 : x ≡A y)

(e′

3 : xs ≡Vec A e1 ys)

Length of the Vec must be fully general: must be an equation variable.

39 / 52

slide-121
SLIDE 121

The image datatype

The type Im f y consists of elements image x such that f x = y: data Im (f : A → B) : B → Type where image : (x : A) → Im f (f x)

40 / 52

slide-122
SLIDE 122

Solving unsolvable equations

(x1 x2 : A)(e1 : f x1 ≡B f x2) (e2 : image x1 ≡Im f e1 image x2)

41 / 52

slide-123
SLIDE 123

Solving unsolvable equations

(x1 x2 : A)(e1 : f x1 ≡B f x2) (e2 : image x1 ≡Im f e1 image x2) ≃ (x1 x2 : A)(e : x1 ≡A x2)

41 / 52

slide-124
SLIDE 124

Solving unsolvable equations

(x1 x2 : A)(e1 : f x1 ≡B f x2) (e2 : image x1 ≡Im f e1 image x2) ≃ (x1 x2 : A)(e : x1 ≡A x2) ≃ (x1 : A)

41 / 52

slide-125
SLIDE 125

What if the indices are not fully general?

(e : cons n x xs ≡Vec A (suc n) cons n y ys)

42 / 52

slide-126
SLIDE 126

What if the indices are not fully general?

(e : cons n x xs ≡Vec A (suc n) cons n y ys) ≃ (e1 : suc n ≡N suc n) (e2 : cons n x xs ≡Vec A e1 cons n y ys) (p : e1 ≡suc n≡Nsuc n refl)

42 / 52

slide-127
SLIDE 127

What if the indices are not fully general?

(e : cons n x xs ≡Vec A (suc n) cons n y ys) ≃ (e1 : suc n ≡N suc n) (e2 : cons n x xs ≡Vec A e1 cons n y ys) (p : e1 ≡suc n≡Nsuc n refl) ≃ (e′

1 : n ≡N n)(e′ 2 : x ≡A y)(e′ 3 : xs ≡Vec A e′

1 ys)

(p : cong suc e′

1 ≡suc n≡Nsuc n refl)

42 / 52

slide-128
SLIDE 128

What if the indices are not fully general?

(e : cons n x xs ≡Vec A (suc n) cons n y ys) ≃ (e1 : suc n ≡N suc n) (e2 : cons n x xs ≡Vec A e1 cons n y ys) (p : e1 ≡suc n≡Nsuc n refl) ≃ (e′

1 : n ≡N n)(e′ 2 : x ≡A y)(e′ 3 : xs ≡Vec A e′

1 ys)

(p : cong suc e′

1 ≡suc n≡Nsuc n refl)

42 / 52

slide-129
SLIDE 129

Higher-dimensional equations

(e′

1 : n ≡N n)(e′ 2 : x ≡A y)(e′ 3 : xs ≡Vec A e′

1 ys)

(p : cong suc e′

1 ≡suc n≡Nsuc n refl)

We call an equation between equality proofs (e.g. p) a higher-dimensional equation.

43 / 52

slide-130
SLIDE 130

How to solve higher-dimensional equations?

Existing unification rules do not apply. . .

44 / 52

slide-131
SLIDE 131

How to solve higher-dimensional equations?

Existing unification rules do not apply. . . We solve the problem in three steps:

  • 1. lower the dimension of equations
  • 2. solve lower-dimensional equations
  • 3. lift unifier to higher dimension

44 / 52

slide-132
SLIDE 132

Step 1: lower the dimension of equations

We replace all equation variables by regular variables: instead of (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (p : cong suc e1 ≡suc n≡Nsuc n refl) let’s first consider (k : N)(u : A)(us : Vec A k) (e : suc k ≡N suc n)

45 / 52

slide-133
SLIDE 133

Step 2: solve lower-dimensional equations

This gives us an equivalence f of type (k : N)(u : A)(us : Vec A k) (e : suc k ≡N suc n) ≃ (u : A)(us : Vec A n)

46 / 52

slide-134
SLIDE 134

Step 3: lift unifier to higher dimension

We lift f to an equivalence f ↑ of type (e1 : n ≡N n)(e2 : x ≡A y) (e3 : xs ≡Vec A e1 ys) (p : cong suc e1 ≡suc n≡Nsuc n refl) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys)

47 / 52

slide-135
SLIDE 135

Final result of steps 1-3

(e : cons n x xs ≡Vec A (suc n) cons n y ys) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys)

48 / 52

slide-136
SLIDE 136

Final result of steps 1-3

(e : cons n x xs ≡Vec A (suc n) cons n y ys) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys) This is the forcing rule for cons.

48 / 52

slide-137
SLIDE 137

Lifting equivalences: (mostly) general case

  • Theorem. If we have an equivalence f of type

(x : A)(e : b1 x ≡B x b2 x) ≃ C we can construct f ↑ of type (e : u ≡A v)(p : cong b1 e ≡r≡B es cong b2 e) ≃ (e′ : f u r ≡C f v s)

49 / 52

slide-138
SLIDE 138

Implementation in Agda

This is all used by Agda to check definitions by dependent pattern matching.

  • More general than before
  • Fixed many bugs
  • Implementation matches theory

You can try it for yourself: wiki.portal.chalmers.se/agda

50 / 52

slide-139
SLIDE 139

Conclusion

Unification rules should return evidence

  • f their correctness.

51 / 52

slide-140
SLIDE 140

Conclusion

Unification rules should return evidence

  • f their correctness.

A most general unifier can be represented internally as an equivalence.

51 / 52

slide-141
SLIDE 141

Conclusion

Unification rules should return evidence

  • f their correctness.

A most general unifier can be represented internally as an equivalence. Unification cannot ignore the types!

51 / 52

slide-142
SLIDE 142

Questions?

If you want to know more, you can:

  • Try out Agda:

wiki.portal.chalmers.se/agda

  • Look at the source:

github.com/agda/agda

  • Read my thesis:

Dependent pattern matching and proof-relevant unification (2017)

52 / 52

slide-143
SLIDE 143

Two applications of unification

Filling in implicit arguments Checking definitions by pattern matching

52 / 52

slide-144
SLIDE 144

Two applications of unification

Filling in implicit arguments

  • Higher order

Checking definitions by pattern matching

  • First order

52 / 52

slide-145
SLIDE 145

Two applications of unification

Filling in implicit arguments

  • Higher order
  • ‘Syntactic’

Checking definitions by pattern matching

  • First order
  • ‘Semantic’

52 / 52

slide-146
SLIDE 146

Two applications of unification

Filling in implicit arguments

  • Higher order
  • ‘Syntactic’
  • MGU optional

Checking definitions by pattern matching

  • First order
  • ‘Semantic’
  • MGU required

52 / 52

slide-147
SLIDE 147

Two applications of unification

Filling in implicit arguments

  • Higher order
  • ‘Syntactic’
  • MGU optional

Checking definitions by pattern matching

  • First order
  • ‘Semantic’
  • MGU required

Focus of this talk

52 / 52

slide-148
SLIDE 148

Two notions of equality

Definitional equality x = y : A

  • Weaker

Propositional equality e : x ≡A y

  • Stronger

52 / 52

slide-149
SLIDE 149

Two notions of equality

Definitional equality x = y : A

  • Weaker
  • Decidable

Propositional equality e : x ≡A y

  • Stronger
  • Undecidable

52 / 52

slide-150
SLIDE 150

Two notions of equality

Definitional equality x = y : A

  • Weaker
  • Decidable
  • Meta-theoretic

Propositional equality e : x ≡A y

  • Stronger
  • Undecidable
  • Internal to theory

52 / 52

slide-151
SLIDE 151

Two notions of equality

Definitional equality x = y : A

  • Weaker
  • Decidable
  • Meta-theoretic
  • Implicit

Propositional equality e : x ≡A y

  • Stronger
  • Undecidable
  • Internal to theory
  • Explicit

52 / 52