which types have a unique inhabitant? focusing on pure program equivalence

110
Which types have a unique inhabitant? Focusing on pure program equivalence Gabriel Scherer, under the supervision of Didier R´ emy Gallium – INRIA March 30, 2016 1

Upload: others

Post on 11-Sep-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Which types have a unique inhabitant? Focusing on pure program equivalence

Which types have a unique inhabitant?Focusing on pure program equivalence

Gabriel Scherer,under the supervision of Didier Remy

Gallium – INRIA

March 30, 2016

1

Page 2: Which types have a unique inhabitant? Focusing on pure program equivalence

1 Background

2 Overview

3 Focusing and saturation

2

Page 3: Which types have a unique inhabitant? Focusing on pure program equivalence

1 Background

2 Overview

3 Focusing and saturation

3

Page 4: Which types have a unique inhabitant? Focusing on pure program equivalence

Programming

Program execution

ML Robbo

⇐=

Program description

Tomasz Kokoszka

Like writing cooking recipe for a very very literal cook.

4

Page 5: Which types have a unique inhabitant? Focusing on pure program equivalence

Code inference

Some program fragments are boring to write.

Dear computer, please guess what I mean here.It should be clear from the context.

Tamisez la farine dans un saladier. Ajoutez le sel, le sucre enpoudre et le sucre vanille puis les œufs et melangez bien. Versezensuite le lait petit a petit en melangeant constamment poureviter les grumeaux. Terminez en ajoutant le Cognac.

http://www.papillesetpupilles.fr/2011/01/crepes-faciles.html

puis les œufs...

Implicit transformation, coercion: egg . (white× yolk)

melangez bien...

Implicit parameter.

5

Page 6: Which types have a unique inhabitant? Focusing on pure program equivalence

Code inference

Some program fragments are boring to write.

Dear computer, please guess what I mean here.It should be clear from the context.

Tamisez la farine dans un saladier. Ajoutez le sel, le sucre enpoudre et le sucre vanille puis les œufs et melangez bien. Versezensuite le lait petit a petit en melangeant constamment poureviter les grumeaux. Terminez en ajoutant le Cognac.

http://www.papillesetpupilles.fr/2011/01/crepes-faciles.html

puis les œufs...

Implicit transformation, coercion: egg . (white× yolk)

melangez bien...

Implicit parameter.

5

Page 7: Which types have a unique inhabitant? Focusing on pure program equivalence

Code inference

Some program fragments are boring to write.

Dear computer, please guess what I mean here.It should be clear from the context.

Tamisez la farine dans un saladier. Ajoutez le sel, le sucre enpoudre et le sucre vanille puis les œufs et melangez bien. Versezensuite le lait petit a petit en melangeant constamment poureviter les grumeaux. Terminez en ajoutant le Cognac.

http://www.papillesetpupilles.fr/2011/01/crepes-faciles.html

puis les œufs...

Implicit transformation, coercion: egg . (white× yolk)

melangez bien...

Implicit parameter.

5

Page 8: Which types have a unique inhabitant? Focusing on pure program equivalence

Code inference

Some program fragments are boring to write.

Dear computer, please guess what I mean here.It should be clear from the context.

Tamisez la farine dans un saladier. Ajoutez le sel, le sucre enpoudre et le sucre vanille puis les œufs et melangez bien. Versezensuite le lait petit a petit en melangeant constamment poureviter les grumeaux. Terminez en ajoutant le Cognac.

http://www.papillesetpupilles.fr/2011/01/crepes-faciles.html

puis les œufs sans leur coquille

Implicit transformation, coercion: egg . (white× yolk)

melangez bien...

Implicit parameter.

5

Page 9: Which types have a unique inhabitant? Focusing on pure program equivalence

Code inference

Some program fragments are boring to write.

Dear computer, please guess what I mean here.It should be clear from the context.

Tamisez la farine dans un saladier. Ajoutez le sel, le sucre enpoudre et le sucre vanille puis les œufs et melangez bien. Versezensuite le lait petit a petit en melangeant constamment poureviter les grumeaux. Terminez en ajoutant le Cognac.

http://www.papillesetpupilles.fr/2011/01/crepes-faciles.html

puis les œufs sans leur coquille

Implicit transformation, coercion: egg . (white× yolk)

melangez bien...

Implicit parameter.

5

Page 10: Which types have a unique inhabitant? Focusing on pure program equivalence

Code inference

Some program fragments are boring to write.

Dear computer, please guess what I mean here.It should be clear from the context.

Tamisez la farine dans un saladier. Ajoutez le sel, le sucre enpoudre et le sucre vanille puis les œufs et melangez bien. Versezensuite le lait petit a petit en melangeant constamment poureviter les grumeaux. Terminez en ajoutant le Cognac.

http://www.papillesetpupilles.fr/2011/01/crepes-faciles.html

puis les œufs sans leur coquille

Implicit transformation, coercion: egg . (white× yolk)

melangez bien avec une cuillere

Implicit parameter.

5

Page 11: Which types have a unique inhabitant? Focusing on pure program equivalence

Unicity

“Guess from the context”

What if there are several possible choices?

Refuse to guess? Ask the programmer?(Heuristics studied in the literature.)

When is there only one choice?When are all choices equivalent?

6

Page 12: Which types have a unique inhabitant? Focusing on pure program equivalence

Type systems

Not all sentences are grammatically correct.Not all grammatical sentences are meaningful:

verser le saladier dans le beurre

There are rules to follow to create well-formed, meaningful programs.

In this talk: simply-typed lambda-calculus.Very simple, restrictive rules.Realistic languages use richer systems.

“In this context, are all guesses equivalent?”=⇒“Given these type constraints, are all guesses equivalent?”

7

Page 13: Which types have a unique inhabitant? Focusing on pure program equivalence

A,B,C ,D ::= (simple) types| X ,Y ,Z atomic types: white, yolk, sugar...| A× B pairs: white× yolk

| A + B sums, choices: oil + butter

| A→ B functions: white→ foam

| . . .

8

Page 14: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

? : meringue

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 15: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

? : meringue

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 16: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(

? : foam , ? : sugar)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 17: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(

? : foam , ? : sugar)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 18: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(

? : foam , πsugar kitchen)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 19: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(

? : foam , πsugar kitchen)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 20: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(whisk (? : white ), πsugar kitchen

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 21: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(whisk (? : white ), πsugar kitchen

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 22: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(whisk (πwhite (split (? : egg ))), πsugar kitchen

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 23: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(whisk (πwhite (split (? : egg ))), πsugar kitchen

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 24: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(whisk (πwhite (split (πegg fridge))), πsugar kitchen

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 25: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

oven(whisk (πwhite (split (πegg fridge))), πsugar kitchen

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 26: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

? : steak

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 27: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

? : steak

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 28: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(? : meat , ? : oil + butter

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 29: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(? : meat , ? : oil + butter

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 30: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge, ? : oil + butter

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 31: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge, ? : oil + butter

)

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 32: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge,

{? : oil? : butter

})

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 33: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge,

{? : oil? : butter

})

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 34: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge,

{πoil kitchen

? : butter

})

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 35: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge,

{πoil kitchen

? : butter

})

Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 36: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge,

{πoil kitchen

πbutter fridge

})Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 37: Which types have a unique inhabitant? Focusing on pure program equivalence

kitchen : salt× sugar× oil

fridge : meat× egg× butter

split : egg→ white× yolk× shell

whisk : white→ foam

oven : foam× sugar→ meringue

pan : meat× (oil + butter)→ steak

`

pan

(πmeat fridge,

{πoil kitchen

πbutter fridge

})Two different choices!

Additional difficulties: equivalent programs, infinite search.

9

Page 38: Which types have a unique inhabitant? Focusing on pure program equivalence

Proofs-programs correspondence (Curry-Howard)

Another domain has to respect strict rules of well-formedness:mathematical proofs.Types connect programming languages to logic (study of formal proofs).

Type system LogicType PropositionProgram Formal proof

A× B A and BA + B A or BA→ B A implies B

Program search Proof searchInhabitation Provability

Unicity N/A

10

Page 39: Which types have a unique inhabitant? Focusing on pure program equivalence

Proofs-programs correspondence (Curry-Howard)

Another domain has to respect strict rules of well-formedness:mathematical proofs.Types connect programming languages to logic (study of formal proofs).

Type system LogicType PropositionProgram Formal proof

A× B A and BA + B A or BA→ B A implies B

Program search Proof search

Inhabitation Provability

Unicity N/A

10

Page 40: Which types have a unique inhabitant? Focusing on pure program equivalence

Proofs-programs correspondence (Curry-Howard)

Another domain has to respect strict rules of well-formedness:mathematical proofs.Types connect programming languages to logic (study of formal proofs).

Type system LogicType PropositionProgram Formal proof

A× B A and BA + B A or BA→ B A implies B

Program search Proof searchInhabitation Provability

Unicity N/A

10

Page 41: Which types have a unique inhabitant? Focusing on pure program equivalence

Proofs-programs correspondence (Curry-Howard)

Another domain has to respect strict rules of well-formedness:mathematical proofs.Types connect programming languages to logic (study of formal proofs).

Type system LogicType PropositionProgram Formal proof

A× B A and BA + B A or BA→ B A implies B

Program search Proof searchInhabitation Provability

Unicity N/A

10

Page 42: Which types have a unique inhabitant? Focusing on pure program equivalence

Canonical proofs

Proof theory has studied the question of “canonical” proof representations.

Can we adopt these representations for programs?Do they lose computational information?

Do logical techniques respect program equivalence?

11

Page 43: Which types have a unique inhabitant? Focusing on pure program equivalence

Contribution

A functional programming problem:Which types have a unique inhabitant?

A method from logic: focusing.New bridge between the communities.(Noam, Teyjus, Psyche, L. More elementary, new audience.)

A result: Unique inhabitation is decidable for the simply-typedlambda-calculus with atoms, sums and empty types.An effective algorithm. For any (Γ ` ? : A) returns 0, 1 or 2 programs.

12

Page 44: Which types have a unique inhabitant? Focusing on pure program equivalence

1 Background

2 Overview

3 Focusing and saturation

13

Page 45: Which types have a unique inhabitant? Focusing on pure program equivalence

Examples

Abstract library functions. Glue code.

fst : α× β → α ? : α→ α→ α

curry : (α× β → γ)→ α→ β → γ

error Adef= exn + A

Monad error

{return : α→ error αbind : error α→ (α→ error β)→ error β

Functor error (unique)

Applicative error (not unique)

ap : error (α→ β)→ error α→ error β

14

Page 46: Which types have a unique inhabitant? Focusing on pure program equivalence

Examples

Abstract library functions. Glue code.

fst : α× β → α ? : α→ α→ α

curry : (α× β → γ)→ α→ β → γ

error Adef= exn + A

Monad error

{return : α→ error αbind : error α→ (α→ error β)→ error β

Functor error (unique)

Applicative error (not unique)

ap : error (α→ β)→ error α→ error β

14

Page 47: Which types have a unique inhabitant? Focusing on pure program equivalence

Examples

Abstract library functions. Glue code.

fst : α× β → α ? : α→ α→ α

curry : (α× β → γ)→ α→ β → γ

error Adef= exn + A

Monad error

{return : α→ error αbind : error α→ (α→ error β)→ error β

Functor error (unique)

Applicative error (not unique)

ap : error (α→ β)→ error α→ error β

14

Page 48: Which types have a unique inhabitant? Focusing on pure program equivalence

Examples

Abstract library functions. Glue code.

fst : α× β → α ? : α→ α→ α

curry : (α× β → γ)→ α→ β → γ

error Adef= exn + A

Monad error

{return : α→ error αbind : error α→ (α→ error β)→ error β

Functor error (unique)

Applicative error (not unique)

ap : error (α→ β)→ error α→ error β

14

Page 49: Which types have a unique inhabitant? Focusing on pure program equivalence

Examples

Abstract library functions. Glue code.

fst : α× β → α ? : α→ α→ α

curry : (α× β → γ)→ α→ β → γ

error Adef= exn + A

Monad error

{return : α→ error αbind : error α→ (α→ error β)→ error β

Functor error (unique)

Applicative error (not unique)

ap : error (α→ β)→ error α→ error β

14

Page 50: Which types have a unique inhabitant? Focusing on pure program equivalence

General idea

Enumerate all possible terms in (Γ ` ? : A):

t1, t2, . . . , tn

Stop if we find two distinct terms.

We must remove duplicates.

t1, (λx . x) t1, (λx . x) ((λx . x) t1), . . .

Infinitely many duplicates =⇒ non-termination.

Enumerate terms without duplicate: canonical representations.

15

Page 51: Which types have a unique inhabitant? Focusing on pure program equivalence

β-normal formsEasy in the negative fragment (→,×, 1). Values and neutrals.

v ::= values| λx . v| (v1, v2)| n

n ::= neutrals| x| πi n| n v

Problem: no clear way to add positives (+, 0).

v ,w ::= values| λx . v| (v1, v2)| n

? | σi v

n ::= neutrals| x| πi n| n v

? | match n with | σ1 x1 → v1 | σ2 x2 → v2? | match n with | σ1 x1 → n1 | σ2 x2 → n2

match n with

∣∣∣∣ σ1 x1 → σ1 v1σ2 x2 → σ2 v2

16

Page 52: Which types have a unique inhabitant? Focusing on pure program equivalence

β-normal formsEasy in the negative fragment (→,×, 1). Values and neutrals.

v ::= values| λx . v| (v1, v2)| n

n ::= neutrals| x| πi n| n v

Problem: no clear way to add positives (+, 0).

v ,w ::= values| λx . v| (v1, v2)| n

? | σi v

n ::= neutrals| x| πi n| n v

? | match n with | σ1 x1 → v1 | σ2 x2 → v2? | match n with | σ1 x1 → n1 | σ2 x2 → n2

match n with

∣∣∣∣ σ1 x1 → σ1 v1σ2 x2 → σ2 v2

16

Page 53: Which types have a unique inhabitant? Focusing on pure program equivalence

β-normal formsEasy in the negative fragment (→,×, 1). Values and neutrals.

v ::= values| λx . v| (v1, v2)| n

n ::= neutrals| x| πi n| n v

Problem: no clear way to add positives (+, 0).

v ,w ::= values| λx . v| (v1, v2)| n

? | σi v

n ::= neutrals| x| πi n| n v

? | match n with | σ1 x1 → v1 | σ2 x2 → v2? | match n with | σ1 x1 → n1 | σ2 x2 → n2(

match n with

∣∣∣∣ σ1 x1 → λy . v1σ2 x2 → λy . v2

)w

16

Page 54: Which types have a unique inhabitant? Focusing on pure program equivalence

β-normal formsEasy in the negative fragment (→,×, 1). Values and neutrals.

v ::= values| λx . v| (v1, v2)| n

n ::= neutrals| x| πi n| n v

Problem: no clear way to add positives (+, 0).

v ,w ::= values| λx . v| (v1, v2)| n

? | σi v

n ::= neutrals| x| πi n| n v

? | match n with | σ1 x1 → v1 | σ2 x2 → v2? | match n with | σ1 x1 → n1 | σ2 x2 → n2

match n with

∣∣∣∣ σ1 x1 → σ1 v1σ2 x2 → σ2 v2

16

Page 55: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x : X + Y ` Y + X?

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 56: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x : X + Y ` Y

x : X + Y ` Y + Xσ1 ?

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 57: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → ?

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 58: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → ?

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 59: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → x2

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 60: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → x2

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 61: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → x2

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 62: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → x2

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 63: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing (overview)

x1 : X ` Y x2 : Y ` Y

x : X + Y ` Y

x : X + Y ` Y + Xσ1

(match x with

∣∣∣∣∣ σ1 x1 → ?

σ2 x2 → x2

)

If you apply rules in the wrong order, you can get stuck.

invertible rule: cannot get stuck. match x with . . .non-invertible rule: risky choice. σi . . .

Focusing imposes restrictions based on invertibility.

17

Page 64: Which types have a unique inhabitant? Focusing on pure program equivalence

Focused normal forms (Overview)

v ::= values| λx . v| (v1, v2)| n

n ::= neutrals| x| πi n| n v

Remark that values are invertible, neutrals non-invertible.

(The details are a bit different.)

18

Page 65: Which types have a unique inhabitant? Focusing on pure program equivalence

Focused normal forms (Overview)

v ::= values| λx . v| (v1, v2)| n| match x with | σ1 x → v1 | σ2 x → v2

n ::= neutrals| x| πi n| n v

Remark that values are invertible, neutrals non-invertible.

(The details are a bit different.)

18

Page 66: Which types have a unique inhabitant? Focusing on pure program equivalence

Focused normal forms (Overview)

v ::= values| λx . v| (v1, v2)| n| match x with | σ1 x → v1 | σ2 x → v2

n ::= neutrals| x| πi n| n v| σi n

Remark that values are invertible, neutrals non-invertible.

(The details are a bit different.)

18

Page 67: Which types have a unique inhabitant? Focusing on pure program equivalence

Focused normal forms (Overview)

v ::= values| λx . v| (v1, v2)| n| match x with | σ1 x → v1 | σ2 x → v2

n ::= neutrals| x| πi n| n v| σi n

Remark that values are invertible, neutrals non-invertible.

(The details are a bit different.)

18

Page 68: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing is not enough

For

x : X + Y ` ? : Y + X

there is a unique focused normal form (good!).

But not for

f : 1→ (X + Y ) ` ? : Y + X

Applying a function is non-invertible, no imposed ordering.

Can be applied:

before or after making choices

zero, one, many times

19

Page 69: Which types have a unique inhabitant? Focusing on pure program equivalence

Saturation

Focusing forces splitting of values of sum type.Computations of sum type may be done at any point.Duplicates (in a pure setting).

Saturation: deduce all positives before making any choice.Run computations of sum type as early as possible.

Stronger than focusing: no more duplicates.

20

Page 70: Which types have a unique inhabitant? Focusing on pure program equivalence

Saturation

Focusing forces splitting of values of sum type.Computations of sum type may be done at any point.Duplicates (in a pure setting).

Saturation: deduce all positives before making any choice.Run computations of sum type as early as possible.

Stronger than focusing: no more duplicates.

20

Page 71: Which types have a unique inhabitant? Focusing on pure program equivalence

Saturation

Focusing forces splitting of values of sum type.Computations of sum type may be done at any point.Duplicates (in a pure setting).

Saturation: deduce all positives before making any choice.Run computations of sum type as early as possible.

Stronger than focusing: no more duplicates.

20

Page 72: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 73: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 74: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in ? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 75: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in ? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 76: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in match z with

∣∣∣∣∣ σ1 xX → ? : Y + Xσ2 y

Y → ? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 77: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in match z with

∣∣∣∣∣ σ1 xX → ? : Y + Xσ2 y

Y → ? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 78: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in match z with

∣∣∣∣∣ σ1 xX → ? : Y + Xσ2 y

Y → ? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 79: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in match z with

∣∣∣∣∣ σ1 xX → σ2 xσ2 y

Y → ? : Y + X

Final result: zero, one or two (distinct) terms.

21

Page 80: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in match z with

∣∣∣∣∣ σ1 xX → σ2 xσ2 y

Y → σ1 y

Final result: zero, one or two (distinct) terms.

21

Page 81: Which types have a unique inhabitant? Focusing on pure program equivalence

Demo time

Implementation available:https://gitlab.com/gasche/unique-inhabitant

f : (1→ X + Y )

`

let zX+Y = f () in match z with

∣∣∣∣∣ σ1 xX → σ2 xσ2 y

Y → σ1 y

Final result: zero, one or two (distinct) terms.

21

Page 82: Which types have a unique inhabitant? Focusing on pure program equivalence

Two-or-more approximationDoes saturation terminate in general?

f : N→ Y + Y , . . .

Even when they are infinitely many programs, proof search terminates.

Subformula property: normal forms use finitely many types/formulas.

proofs environments: sets of formulas (A,B, . . . )

typing environments: variable→type mappings (x : A, y : B, . . . )

x1 : X , x2 : X , x3 : X , . . .

Result: to detect unicity, keep at most two variables of each type.

Consequence: Saturation can stop after two proofs of each positive:

0, 1,many

22

Page 83: Which types have a unique inhabitant? Focusing on pure program equivalence

Two-or-more approximationDoes saturation terminate in general?

f : N→ Y + Y , . . .

Even when they are infinitely many programs, proof search terminates.

Subformula property: normal forms use finitely many types/formulas.

proofs environments: sets of formulas (A,B, . . . )

typing environments: variable→type mappings (x : A, y : B, . . . )

x1 : X , x2 : X , x3 : X , . . .

Result: to detect unicity, keep at most two variables of each type.

Consequence: Saturation can stop after two proofs of each positive:

0, 1,many

22

Page 84: Which types have a unique inhabitant? Focusing on pure program equivalence

Two-or-more approximationDoes saturation terminate in general?

f : N→ Y + Y , . . .

Even when they are infinitely many programs, proof search terminates.

Subformula property: normal forms use finitely many types/formulas.

proofs environments: sets of formulas (A,B, . . . )

typing environments: variable→type mappings (x : A, y : B, . . . )

x1 : X , x2 : X , x3 : X , . . .

Result: to detect unicity, keep at most two variables of each type.

Consequence: Saturation can stop after two proofs of each positive:

0, 1,many

22

Page 85: Which types have a unique inhabitant? Focusing on pure program equivalence

Two-or-more approximationDoes saturation terminate in general?

f : N→ Y + Y , . . .

Even when they are infinitely many programs, proof search terminates.

Subformula property: normal forms use finitely many types/formulas.

proofs environments: sets of formulas (A,B, . . . )

typing environments: variable→type mappings (x : A, y : B, . . . )

x1 : X , x2 : X , x3 : X , . . .

Result: to detect unicity, keep at most two variables of each type.

Consequence: Saturation can stop after two proofs of each positive:

0, 1,many

22

Page 86: Which types have a unique inhabitant? Focusing on pure program equivalence

Two-or-more approximationDoes saturation terminate in general?

f : N→ Y + Y , . . .

Even when they are infinitely many programs, proof search terminates.

Subformula property: normal forms use finitely many types/formulas.

proofs environments: sets of formulas (A,B, . . . )

typing environments: variable→type mappings (x : A, y : B, . . . )

x1 : X , x2 : X , x3 : X , . . .

Result: to detect unicity, keep at most two variables of each type.

Consequence: Saturation can stop after two proofs of each positive:

0, 1,many

22

Page 87: Which types have a unique inhabitant? Focusing on pure program equivalence

1 Background

2 Overview

3 Focusing and saturation

23

Page 88: Which types have a unique inhabitant? Focusing on pure program equivalence

Sequent calculus (logic)

Γ ` A Γ,B ` C

Γ,A→ B ` C–

Γ,A ` B

Γ ` A→ B

Γ,Ai ` C

Γ,A1 × A2 ` C–

Γ ` A1 Γ ` A2

Γ ` A1 × A2

Γ ` 1

Γ,A1 ` C Γ,A2 ` C

Γ,A1 + A2 ` C

Γ ` Ai

Γ ` A1 + A2+

Γ, 0 ` A

Negatives: (→,×, 1) Positives: (+, 0)

24

Page 89: Which types have a unique inhabitant? Focusing on pure program equivalence

Sequent calculus (logic)

Γ ` A Γ,B ` C

Γ,A→ B ` C–

Γ,A ` B

Γ ` A→ B

Γ,Ai ` C

Γ,A1 × A2 ` C–

Γ ` A1 Γ ` A2

Γ ` A1 × A2

Γ ` 1

Γ,A1 ` C Γ,A2 ` C

Γ,A1 + A2 ` C

Γ ` Ai

Γ ` A1 + A2+

Γ, 0 ` A

Negatives: (→,×, 1) Positives: (+, 0)

24

Page 90: Which types have a unique inhabitant? Focusing on pure program equivalence

Sequent calculus (logic)

Γ ` A Γ,B ` C

Γ,A→ B ` C–

Γ,A ` B

Γ ` A→ B

Γ,Ai ` C

Γ,A1 × A2 ` C–

Γ ` A1 Γ ` A2

Γ ` A1 × A2

Γ ` 1

Γ,A1 ` C Γ,A2 ` C

Γ,A1 + A2 ` C

Γ ` Ai

Γ ` A1 + A2+

Γ, 0 ` A

Negatives: (→,×, 1) Positives: (+, 0)

24

Page 91: Which types have a unique inhabitant? Focusing on pure program equivalence

Sequent calculus (logic)

Γ ` A Γ,B ` C

Γ,A→ B ` C–

Γ,A ` B

Γ ` A→ B

Γ,Ai ` C

Γ,A1 × A2 ` C–

Γ ` A1 Γ ` A2

Γ ` A1 × A2

Γ ` 1

Γ,A1 ` C Γ,A2 ` C

Γ,A1 + A2 ` C

Γ ` Ai

Γ ` A1 + A2+

Γ, 0 ` A

Negatives: (→,×, 1) Positives: (+, 0)

24

Page 92: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing

Invertible phase: apply invertible rules as early as possible.

Γ `inv A

When all invertible rules are done, we must choose a focus.

Γ `foc A

(Γ negative, A positive)

Focus on a formula:

on the right: Γ `foc.r [A]

or on the left: Γ, [A] `foc.l BThen keep applying non-invertible rules on the formula under focus.

25

Page 93: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing

Invertible phase: apply invertible rules as early as possible.

Γ `inv A

When all invertible rules are done, we must choose a focus.

Γ `foc A

(Γ negative, A positive)

Focus on a formula:

on the right: Γ `foc.r [A]

or on the left: Γ, [A] `foc.l BThen keep applying non-invertible rules on the formula under focus.

25

Page 94: Which types have a unique inhabitant? Focusing on pure program equivalence

Focusing

Invertible phase: apply invertible rules as early as possible.

Γ `inv A

When all invertible rules are done, we must choose a focus.

Γ `foc A

(Γ negative, A positive)

Focus on a formula:

on the right: Γ `foc.r [A]

or on the left: Γ, [A] `foc.l BThen keep applying non-invertible rules on the formula under focus.

25

Page 95: Which types have a unique inhabitant? Focusing on pure program equivalence

Focused λ-calculus

t, u, r ::= invertible| λx . t| (t, u)

|match x with∣∣∣∣ σ1 x → u1σ2 x → u2

| ()| absurd(x)| (f : Apa)

f , g ::= choice of focus| (n : X )| let (x : Ap) = n in t| (p : Ap)

n,m ::= negative neutral| πi n| n p| (x : An)

p, q ::= positive neutral| σi p| (x : X )| (t : Ana)

26

Page 96: Which types have a unique inhabitant? Focusing on pure program equivalence

Saturation

Γ,∆ `foc AΓ; ∆ `inv A

Old and New.

Left focusing: Γna ` let (x : Ap) = n in t

Saturation: Γ; ∆ ` let (x i )i∈I = (ni )

i∈I in t

where (ni )i∈I def

= { Γ,∆ ` n ⇓ Ap | n uses ∆ }2

All typed terms: Γ ` t : A

Focused terms: Γat `foc f : Pat

Saturated terms: Γat; Γat′ `sat g : Pat

27

Page 97: Which types have a unique inhabitant? Focusing on pure program equivalence

Saturation

Γ,∆ `foc AΓ; ∆ `inv A

Old and New.

Left focusing: Γna ` let (x : Ap) = n in t

Saturation: Γ; ∆ ` let (x i )i∈I = (ni )

i∈I in t

where (ni )i∈I def

= { Γ,∆ ` n ⇓ Ap | n uses ∆ }2

All typed terms: Γ ` t : A

Focused terms: Γat `foc f : Pat

Saturated terms: Γat; Γat′ `sat g : Pat

27

Page 98: Which types have a unique inhabitant? Focusing on pure program equivalence

Saturation

Γ,∆ `foc AΓ; ∆ `inv A

Old and New.

Left focusing: Γna ` let (x : Ap) = n in t

Saturation: Γ; ∆ ` let (x i )i∈I = (ni )

i∈I in t

where (ni )i∈I def

= { Γ,∆ ` n ⇓ Ap | n uses ∆ }2

All typed terms: Γ ` t : A

Focused terms: Γat `foc f : Pat

Saturated terms: Γat; Γat′ `sat g : Pat

27

Page 99: Which types have a unique inhabitant? Focusing on pure program equivalence

Canonicity

CompletenessFocusing:(known result)

Γat `foc t f : Pat with t ≈βη f

Saturation: Γat; Γat′ `sat f g : Pat with f ≈βη g

Theorem (Canonicity)

Γat `foc f 1 ≈βη f 2 : Pat

∅; Γat `sat f 1 g1 : Pat

∅; Γat `sat f 2 g2 : Pat

=⇒ g1 = g2

Result: searching among saturated terms decides unicity

Corollary: equivalence with sums and empty type is decidable

28

Page 100: Which types have a unique inhabitant? Focusing on pure program equivalence

Canonicity

CompletenessFocusing:(known result)

Γat `foc t f : Pat with t ≈βη f

Saturation: Γat; Γat′ `sat f g : Pat with f ≈βη g

Theorem (Canonicity)

Γat `foc f 1 ≈βη f 2 : Pat

∅; Γat `sat f 1 g1 : Pat

∅; Γat `sat f 2 g2 : Pat

=⇒ g1 = g2

Result: searching among saturated terms decides unicity

Corollary: equivalence with sums and empty type is decidable

28

Page 101: Which types have a unique inhabitant? Focusing on pure program equivalence

Canonicity

CompletenessFocusing:(known result)

Γat `foc t f : Pat with t ≈βη f

Saturation: Γat; Γat′ `sat f g : Pat with f ≈βη g

Theorem (Canonicity)

Γat `foc f 1 ≈βη f 2 : Pat

∅; Γat `sat f 1 g1 : Pat

∅; Γat `sat f 2 g2 : Pat

=⇒ g1 = g2

Result: searching among saturated terms decides unicity

Corollary: equivalence with sums and empty type is decidable

28

Page 102: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇒ g1 ≈βη g2 ⇒ g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 103: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇐ g1 ≈βη g2 g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 104: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇐= g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 105: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇐= g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 106: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇐= g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 107: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇐= g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 108: Which types have a unique inhabitant? Focusing on pure program equivalence

Future work

Semantic proof of equivalence with sums and empty type.

g1 = g2 ⇐= g1 ≈ctx g2

System L.

Implement the feature in programming languages (ML).Design choices.

Richer type systems (polymorphism, dependency...)Undecidable; hopefully inspire semi-decision procedure.Which equivalence to consider?The richer the system, the more unique types.

Unicity modulo equations?

29

Page 109: Which types have a unique inhabitant? Focusing on pure program equivalence

Conclusion

Program inference is not yet fully understood;we propose a principled approach.

Focusing and saturationinspire powerful program representations.

Purity and types are not just restrictions

they can make programming easier.

30

Page 110: Which types have a unique inhabitant? Focusing on pure program equivalence

Conclusion

Program inference is not yet fully understood;we propose a principled approach.

Focusing and saturationinspire powerful program representations.

Purity and types are not just restrictionsthey can make programming easier.

30