A sound unification algorithm based on telescope equivalences - - PowerPoint PPT Presentation

a sound unification algorithm based on telescope
SMART_READER_LITE
LIVE PREVIEW

A sound unification algorithm based on telescope equivalences - - PowerPoint PPT Presentation

A sound unification algorithm based on telescope equivalences Jesper Cockx DistriNet KU Leuven 20 April 2016 Pattern matching is awesome Agda uses unification to: check which constructors are possible specialize the result type data Vec


slide-1
SLIDE 1

A sound unification algorithm based on telescope equivalences

Jesper Cockx

DistriNet – KU Leuven

20 April 2016

slide-2
SLIDE 2

Pattern matching is awesome

Agda uses unification to: check which constructors are possible specialize the result type data Vec (A : Set) : N → Set where [] : Vec A 0 cons : (n : N) → A → Vec A n → Vec A (1 + n) f : Vec A 1 → T f (cons .0 x xs) = . . .

1 / 29

slide-3
SLIDE 3

Pattern matching is awesome

Agda uses unification to: check which constructors are possible specialize the result type data Vec (A : Set) : N → Set where [] : Vec A 0 cons : (n : N) → A → Vec A n → Vec A (1 + n) f : Vec A 1 → T f (cons .0 x xs) = . . .

1 / 29

slide-4
SLIDE 4

Pattern matching is awesome

Agda uses unification to: check which constructors are possible specialize the result type data Vec (A : Set) : N → Set where [] : Vec A 0 cons : (n : N) → A → Vec A n → Vec A (1 + n) f : Vec A 1 → T f (cons .0 x xs) = . . .

1 / 29

slide-5
SLIDE 5

Details of unification are important

Agda has pattern matching as a primitive, so results of unification determine Agda’s notion of equality Example: deleting reflexive equations implies K

2 / 29

slide-6
SLIDE 6

Details of unification are important

Agda has pattern matching as a primitive, so results of unification determine Agda’s notion of equality Example: deleting reflexive equations implies K

2 / 29

slide-7
SLIDE 7

Time for a quiz

Should the following code be accepted? {-# OPTIONS --without-K #-} . . . -- imports f : (Bool , true) ≡ (Bool , false) → ⊥ f ()

3 / 29

slide-8
SLIDE 8

Time for a quiz

Should the following code be accepted? {-# OPTIONS --without-K #-} . . . -- imports f : (Bool , true) ≡ (Bool , false) → ⊥ f () Answer: depends on the type of the equation!

4 / 29

slide-9
SLIDE 9

Postponing equations causes problems

If we postpone an equation, following equations can be heterogeneous Naively continuing unification is bad Equality of second projections Injectivity of type constructors . . . It’s hard to distinguish good and bad situations!

5 / 29

slide-10
SLIDE 10

Postponing equations causes problems

If we postpone an equation, following equations can be heterogeneous Naively continuing unification is bad Equality of second projections Injectivity of type constructors . . . It’s hard to distinguish good and bad situations!

5 / 29

slide-11
SLIDE 11

Postponing equations causes problems

If we postpone an equation, following equations can be heterogeneous Naively continuing unification is bad Equality of second projections Injectivity of type constructors . . . It’s hard to distinguish good and bad situations!

5 / 29

slide-12
SLIDE 12

We need a general way to think about unification

It’s not sufficient to “make things equal” Core idea: Unification rules are equivalences between telescopes of equations This is the basis of the new unification algorithm in Agda 2.5.1

6 / 29

slide-13
SLIDE 13

We need a general way to think about unification

It’s not sufficient to “make things equal” Core idea: Unification rules are equivalences between telescopes of equations This is the basis of the new unification algorithm in Agda 2.5.1

6 / 29

slide-14
SLIDE 14

We need a general way to think about unification

It’s not sufficient to “make things equal” Core idea: Unification rules are equivalences between telescopes of equations This is the basis of the new unification algorithm in Agda 2.5.1

6 / 29

slide-15
SLIDE 15

A sound unification algorithm based on telescope equivalences

1 Unifiers as equivalences 2 Unification rules 3 Higher-dimensional unification

slide-16
SLIDE 16

A sound unification algorithm based on telescope equivalences

1 Unifiers as equivalences 2 Unification rules 3 Higher-dimensional unification

slide-17
SLIDE 17

What do we want from unification?

It has to be possible to translate pattern matching to eliminators The core tool we need is specialization by unification Build a function m : Γ → ¯ u ≡∆ ¯ v → T from a function m′ : Γ′ → Tσ where σ : Γ′ → Γ is computed by unification

7 / 29

slide-18
SLIDE 18

What do we want from unification?

It has to be possible to translate pattern matching to eliminators The core tool we need is specialization by unification Build a function m : Γ → ¯ u ≡∆ ¯ v → T from a function m′ : Γ′ → Tσ where σ : Γ′ → Γ is computed by unification

7 / 29

slide-19
SLIDE 19

Intermezzo: telescopic equality

Type of an equation may depend

  • n solution of previous equations

Heterogeneous equality doesn’t keep enough information: Safe to consider equation homogeneous? Does equation depend on other equation? How do equations depend on each other?

8 / 29

slide-20
SLIDE 20

Intermezzo: telescopic equality

Type of an equation may depend

  • n solution of previous equations

Heterogeneous equality doesn’t keep enough information: Safe to consider equation homogeneous? Does equation depend on other equation? How do equations depend on each other?

8 / 29

slide-21
SLIDE 21

Intermezzo: telescopic equality

Solution: use “path over” construction to keep track of dependencies For example: (e1 : m ≡N n)(e2 : u ≡e1

Vec A v)

Cubical (abuse of) notation: (e1 : m ≡N n)(e2 : u ≡Vec A e1 v)

9 / 29

slide-22
SLIDE 22

Intermezzo: telescopic equality

Solution: use “path over” construction to keep track of dependencies For example: (e1 : m ≡N n)(e2 : u ≡e1

Vec A v)

Cubical (abuse of) notation: (e1 : m ≡N n)(e2 : u ≡Vec A e1 v)

9 / 29

slide-23
SLIDE 23

Specialization by unification

The goal is to construct m : Γ → ¯ u ≡∆ ¯ v → T Input: Telescope Γ of flexible variables Telescope ¯ u ≡∆ ¯ v of equations Output: New telescope Γ′ Substitution σ : Γ′ → Γ Evidence of unification ¯ e : Γ′ → ¯ uσ ≡∆σ ¯ vσ

10 / 29

slide-24
SLIDE 24

Specialization by unification

The goal is to construct m : Γ → ¯ u ≡∆ ¯ v → T Input: Telescope Γ of flexible variables Telescope ¯ u ≡∆ ¯ v of equations Output: New telescope Γ′ Substitution σ : Γ′ → Γ Evidence of unification ¯ e : Γ′ → ¯ uσ ≡∆σ ¯ vσ

10 / 29

slide-25
SLIDE 25

Specialization by unification

The goal is to construct m : Γ → ¯ u ≡∆ ¯ v → T Input: Telescope Γ of flexible variables Telescope ¯ u ≡∆ ¯ v of equations Output: New telescope Γ′ Substitution σ : Γ′ → Γ Evidence of unification ¯ e : Γ′ → ¯ uσ ≡∆σ ¯ vσ

10 / 29

slide-26
SLIDE 26

Specialization by unification

The goal is to construct m : Γ → ¯ u ≡∆ ¯ v → T Input: Telescope Γ of flexible variables Telescope ¯ u ≡∆ ¯ v of equations Output: New telescope Γ′ Telescope mapping f : Γ′ → Γ(¯ u ≡∆ ¯ v)

11 / 29

slide-27
SLIDE 27

Two more requirements

Let f : Γ′ → Γ(¯ u ≡∆ ¯ v) be a unifier f should be most general ⇒ f needs a right inverse g1 Γ′ should be minimal ⇒ f needs a left inverse g2

12 / 29

slide-28
SLIDE 28

Two more requirements

Let f : Γ′ → Γ(¯ u ≡∆ ¯ v) be a unifier f should be most general ⇒ f needs a right inverse g1 Γ′ should be minimal ⇒ f needs a left inverse g2

12 / 29

slide-29
SLIDE 29

Two more requirements

Let f : Γ′ → Γ(¯ u ≡∆ ¯ v) be a unifier f should be most general ⇒ f needs a right inverse g1 Γ′ should be minimal ⇒ f needs a left inverse g2

12 / 29

slide-30
SLIDE 30

Most general unifiers as equivalences

A most general unifier of ¯ u and ¯ v is an equivalence f : Γ(¯ u ≡∆ ¯ v) ≃ Γ′ for some Γ′ Specialization by unification: m : Γ → ¯ u ≡∆ ¯ v → T m ¯ x ¯ e = subst (λ¯ x ¯

  • e. T) (isLinv f ¯

x ¯ e) (m′ (f ¯ x ¯ e))

13 / 29

slide-31
SLIDE 31

Most general unifiers as equivalences

A most general unifier of ¯ u and ¯ v is an equivalence f : Γ(¯ u ≡∆ ¯ v) ≃ Γ′ for some Γ′ Specialization by unification: m : Γ → ¯ u ≡∆ ¯ v → T m ¯ x ¯ e = subst (λ¯ x ¯

  • e. T) (isLinv f ¯

x ¯ e) (m′ (f ¯ x ¯ e))

13 / 29

slide-32
SLIDE 32

Disunifiers

A disunifier of ¯ u and ¯ v is an equivalence f : Γ(¯ u ≡∆ ¯ v) ≃ ⊥ Specialization by unification: m : Γ → ¯ u ≡∆ ¯ v → T m ¯ x ¯ e = elim⊥ T (f ¯ x ¯ e)

14 / 29

slide-33
SLIDE 33

Disunifiers

A disunifier of ¯ u and ¯ v is an equivalence f : Γ(¯ u ≡∆ ¯ v) ≃ ⊥ Specialization by unification: m : Γ → ¯ u ≡∆ ¯ v → T m ¯ x ¯ e = elim⊥ T (f ¯ x ¯ e)

14 / 29

slide-34
SLIDE 34

A sound unification algorithm based on telescope equivalences

1 Unifiers as equivalences 2 Unification rules 3 Higher-dimensional unification

slide-35
SLIDE 35

Basic unification rules

MGU is constructed by chaining together equivalences given by unification rules (k l : N)(e : suc k ≡N suc l) ≃ (k l : N)(e : k ≡N l) ≃ (k : N) f −1 : (k : N) → (k l : N)(e : suc k ≡N suc l) f −1 k = k; k; refl

15 / 29

slide-36
SLIDE 36

Basic unification rules

MGU is constructed by chaining together equivalences given by unification rules (k l : N)(e : suc k ≡N suc l) ≃ (k l : N)(e : k ≡N l) ≃ (k : N) f −1 : (k : N) → (k l : N)(e : suc k ≡N suc l) f −1 k = k; k; refl

15 / 29

slide-37
SLIDE 37

Basic unification rules

MGU is constructed by chaining together equivalences given by unification rules (k l : N)(e : suc k ≡N suc l) ≃ (k l : N)(e : k ≡N l) ≃ (k : N) f −1 : (k : N) → (k l : N)(e : suc k ≡N suc l) f −1 k = k; k; refl

15 / 29

slide-38
SLIDE 38

Basic unification rules

MGU is constructed by chaining together equivalences given by unification rules (k l : N)(e : suc k ≡N suc l) ≃ (k l : N)(e : k ≡N l) ≃ (k : N) f −1 : (k : N) → (k l : N)(e : suc k ≡N suc l) f −1 k = k; k; refl

15 / 29

slide-39
SLIDE 39

Basic unification rules

MGU is constructed by chaining together equivalences given by unification rules (k l : N)(e : suc k ≡N suc l) ≃ (k l : N)(e : k ≡N l) ≃ (k : N) f −1 : (k : N) → (k l : N)(e : suc k ≡N suc l) f −1 k = k; k; refl

15 / 29

slide-40
SLIDE 40

Basic unification rules

Solution: (x : A)(e : x ≡A t) ≃ () Deletion: (f x ≡N f x) ≃ () Injectivity: (suc x ≡N suc y) ≃ (x ≡N y) Conflict: (inj1 x ≡A⊎B inj2 y) ≃ ⊥ Cycle: (n ≡N suc n) ≃ ⊥ + auxiliary rules for weakening and reordering

16 / 29

slide-41
SLIDE 41

Rules for η-equality of records

η-expansion of a flexible variable: (p : N × N)(e : fst p ≡N zero) ≃ (x : N)(y : N)(e : x ≡N zero) ≃ (y : N) η-expansion of an equation: (e : x, y ≡N×N f z) ≃ (e1 : x ≡N fst (f z)) (e2 : y ≡N snd (f z))

17 / 29

slide-42
SLIDE 42

Rules for η-equality of records

η-expansion of a flexible variable: (p : N × N)(e : fst p ≡N zero) ≃ (x : N)(y : N)(e : x ≡N zero) ≃ (y : N) η-expansion of an equation: (e : x, y ≡N×N f z) ≃ (e1 : x ≡N fst (f z)) (e2 : y ≡N snd (f z))

17 / 29

slide-43
SLIDE 43

Rules for indexed data types

Idea: rules solve equations between indices together with equations between constructors Example: (e1 : suc m ≡N suc n) (e2 : cons m x xs ≡Vec A e1 cons n y ys) ≃ (e1 : m ≡N n)(e2 : x ≡A y) (e3 : xs ≡Vec A e1 ys)

18 / 29

slide-44
SLIDE 44

Rules for indexed data types

Idea: rules solve equations between indices together with equations between constructors Example: (e1 : suc m ≡N suc n) (e2 : cons m x xs ≡Vec A e1 cons n y ys) ≃ (e1 : m ≡N n)(e2 : x ≡A y) (e3 : xs ≡Vec A e1 ys)

18 / 29

slide-45
SLIDE 45

Rules for indexed data types

This can give a real boost to power: data Im (f : A → B) : B → Set where image : (x : A) → Im f (f x) (x y : A)(e1 : f x ≡B f y) (e2 : image x ≡Im f e1 image y) ≃ (x y : A)(e : x ≡A y) ≃ (x : A)

19 / 29

slide-46
SLIDE 46

From this point, there be dragons

Any questions so far?

20 / 29

slide-47
SLIDE 47

A sound unification algorithm based on telescope equivalences

1 Unifiers as equivalences 2 Unification rules 3 Higher-dimensional unification

slide-48
SLIDE 48

Indexed rules are too restrictive

Rules for indexed datatypes require indices to be fully general This is too restrictive: (e1 : cons n x xs ≡Vec A (suc n) cons n y ys) ̸≃ (e1 : x ≡A y)(e2 : xs ≡Vec A n ys)

21 / 29

slide-49
SLIDE 49

Indexed rules are too restrictive

Rules for indexed datatypes require indices to be fully general This is too restrictive: (e1 : cons n x xs ≡Vec A (suc n) cons n y ys) ̸≃ (e1 : x ≡A y)(e2 : xs ≡Vec A n ys)

21 / 29

slide-50
SLIDE 50

Generalized rules for indexed data

The following rules can be generalized to arbitrary indices: Conflict Cycle Injectivity: only if index types satisfy K!

22 / 29

slide-51
SLIDE 51

Reverse unification rules

Idea: we can generalize the indices by applying unification rules in reverse

23 / 29

slide-52
SLIDE 52

Reverse unification rules: example

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

24 / 29

slide-53
SLIDE 53

Reverse unification rules: example

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

24 / 29

slide-54
SLIDE 54

Reverse unification rules: example

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

24 / 29

slide-55
SLIDE 55

Reverse unification rules: example

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

24 / 29

slide-56
SLIDE 56

Reverse unification rules: example

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

24 / 29

slide-57
SLIDE 57

Reverse unification rules: problems

Applicability is limited: indices need to be linear patterns Hard to implement Not clear how to apply injectivity for indexed data in reverse

25 / 29

slide-58
SLIDE 58

Going beyond the first level

Realization: same problem as for case splitting,

  • nly for equations instead of variables

We can solve it in the same way as well: by specialization by unification

26 / 29

slide-59
SLIDE 59

Going beyond the first level

Realization: same problem as for case splitting,

  • nly for equations instead of variables

We can solve it in the same way as well: by specialization by unification

26 / 29

slide-60
SLIDE 60

Going beyond the first level

Realization: same problem as for case splitting,

  • nly for equations instead of variables

We can solve it in the same way as well: by specialization by unification

26 / 29

slide-61
SLIDE 61

Higher-dimensional unification: example

(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) (f : e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : cong suc e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : e1 ≡n≡Nn refl) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys)

27 / 29

slide-62
SLIDE 62

Higher-dimensional unification: example

(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) (f : e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : cong suc e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : e1 ≡n≡Nn refl) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys)

27 / 29

slide-63
SLIDE 63

Higher-dimensional unification: example

(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) (f : e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : cong suc e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : e1 ≡n≡Nn refl) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys)

27 / 29

slide-64
SLIDE 64

Higher-dimensional unification: example

(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) (f : e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : cong suc e1 ≡suc n≡Nsuc n refl) ≃ (e1 : n ≡N n)(e2 : x ≡A y)(e3 : xs ≡Vec A e1 ys) (f : e1 ≡n≡Nn refl) ≃ (e2 : x ≡A y)(e3 : xs ≡Vec A n ys)

27 / 29

slide-65
SLIDE 65

Representing higher-order problems using first-order syntax

An n-dimensional unification problem consists of a telescope Γ of flexible variables equation telescopes ∆1, . . . , ∆n such that ⊢ Γ∆1 . . . ∆n left- and right-hand sides ¯ u1, ¯ v1, . . . ¯ un, ¯ vn such that Γ∆1 . . . ∆i−1 ⊢ ¯ ui, ¯ v1 : ∆i

28 / 29

slide-66
SLIDE 66

Discussion

Higher-dimensional unification seems easier to implement than reverse rules But maybe it goes too far? Alternative: use reflection to implement a case splitting tactic based on unification

29 / 29

slide-67
SLIDE 67

Discussion

Higher-dimensional unification seems easier to implement than reverse rules But maybe it goes too far? Alternative: use reflection to implement a case splitting tactic based on unification

29 / 29