regular expressions of types · lambda calculus example rule: introducing (making) a pair (or a...

74
2018-09-13 • SMALL FP CONF • HELSINKI Regular expressions of types Oleg Grenrus

Upload: others

Post on 18-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

2018-09-13 • SMALL FP CONF • HELSINKI

Regular expressions oftypes

Oleg Grenrus

Page 2: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Me

Oleg Grenrus, @phadej

I work atI co-organise the HaskHel-meetup

I’m the “release manager” of servant

Page 3: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Me

Oleg Grenrus, @phadejI work at

I co-organise the HaskHel-meetup

I’m the “release manager” of servant

Page 4: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Me

Oleg Grenrus, @phadejI work atI co-organise the HaskHel-meetup

I’m the “release manager” of servant

Page 5: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Me

Oleg Grenrus, @phadejI work atI co-organise the HaskHel-meetup

I’m the “release manager” of servant

Page 6: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

find - search for files

find [-H|-L] path... [operand_expression...]

is it just stringly typed?is it

(s/cat : link (s/? (s/alt :H :H :L :L)):path (s/∗ string?):expr operand -expression?)

Page 7: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

find - search for files

find [-H|-L] path... [operand_expression...]

is it just stringly typed?

is it

(s/cat : link (s/? (s/alt :H :H :L :L)):path (s/∗ string?):expr operand -expression?)

Page 8: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

find - search for files

find [-H|-L] path... [operand_expression...]

is it just stringly typed?is it

(s/cat : link (s/? (s/alt :H :H :L :L)):path (s/∗ string?):expr operand -expression?)

Page 9: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Using find

Shell:find -L src -type f

Clojure?

(�nd :L "src" : type :�le)

Haskell!

L �nd_ #L,"src",#type,#�le M

Page 10: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Using find

Shell:find -L src -type f

Clojure?

(�nd :L "src" : type :�le)

Haskell!

L �nd_ #L,"src",#type,#�le M

Page 11: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Using find

Shell:find -L src -type f

Clojure?

(�nd :L "src" : type :�le)

Haskell!

L �nd_ #L,"src",#type,#�le M

Page 12: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Types

Page 13: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : BPair

Γ

` pair x y : A× B

Let’s walk through the notation.

Page 14: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

PremisesRulename

Conclusion

We can conclude proposition below the line, if we have proof ofpropositions on top of the line

Page 15: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ

` pair x y : A× B

Γ is a context, “what is in the scope”, or “what we know”.For example x : Int, y : Bool - there are x and y in scope.

Page 16: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ `

pair x y : A× B

`, turnstile, “entails”. Γ ` . . . is pronounced “In the context Γ . . . ”

Page 17: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ `

pair x y

:

A× B

: colon, “has type”

Page 18: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ ` pair x y :

A× B

On the left hand side are expressions, I color them in red.

Page 19: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ `

pair x y

: A× B

On the right hand side are types, I color them blue

Page 20: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ ` pair x y : A× B

On the left hand side are expressions, I color them in red. On the righthand side are types, I color them blue

Page 21: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

Pair

Γ

` pair x y : A× B

So. . .

Page 22: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ `

pair x y : A× B

In (any) context Γ ,

pair x y has type A× B; given that in the samecontext Γ , x has type A and y has type B. �

Page 23: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ ` pair x y

: A× B

In (any) context Γ , pair x y

has type A× B; given that in the samecontext Γ , x has type A and y has type B. �

Page 24: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ ` pair x y :

A× B

In (any) context Γ , pair x y has type

A× B; given that in the samecontext Γ , x has type A and y has type B. �

Page 25: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : B

PairΓ ` pair x y : A× B

In (any) context Γ , pair x y has type A× B;

given that in the samecontext Γ , x has type A and y has type B. �

Page 26: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ `

x :A Γ ` y : B

PairΓ ` pair x y : A× B

In (any) context Γ , pair x y has type A× B; given that in the samecontext Γ ,

x has type A and y has type B. �

Page 27: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A

Γ ` y : B

PairΓ ` pair x y : A× B

In (any) context Γ , pair x y has type A× B; given that in the samecontext Γ , x has type A

and y has type B. �

Page 28: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ

` y : B

PairΓ ` pair x y : A× B

In (any) context Γ , pair x y has type A× B; given that in the samecontext Γ , x has type A and

y has type B. �

Page 29: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : BPair

Γ ` pair x y : A× B

In (any) context Γ , pair x y has type A× B; given that in the samecontext Γ , x has type A and y has type B.

Page 30: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus

Example rule: introducing (making) a pair (or a product)

Γ ` x :A Γ ` y : BPair

Γ ` pair x y : A× B

In (any) context Γ , pair x y has type A× B; given that in the samecontext Γ , x has type A and y has type B. �

Page 31: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lambda calculus: more rules

VarΓ, x :A ` x :A

Γ ` f :A→ B Γ ` x :AApp

Γ ` f x : BΓ, x :A ` e : B

LamΓ ` λx: A→ e :A→ B

Γ ` x :A Γ ` y : BPair

Γ ` pair x y :A× BΓ ` p :A× B

FstΓ ` fst p :A

Γ ` p :A× BSnd

Γ ` snd p : BΓ ` x :A

LeftΓ ` inl x :A+ B

Γ ` y : BRight

Γ ` inr y :A+ B

Γ ` · · ·Either

Γ ` · · ·

Page 32: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Decidability of typed calculi

For the various type systems, we can ask three typical questions.

Page 33: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Decidability of typed calculi

1. Type-checking

?

Γ ` x : α

Page 34: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Decidability of typed calculi

2. Typability, or type-inference

?

Γ ` x : ?

Page 35: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Decidability of typed calculi

3. Inhabitation, or proof search

?

Γ ` ? : α

Page 36: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

Really nice feature of the system above is that everything is decidableIn particular: inhabitation.

Γ = f :A× B→ C, x :A,y : B

Page 37: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

Var

Γ ` :A

Var

Γ ` : BPair

Γ `App

Γ ` : CLam2

f :A× B→ C ` :A→ B→ CLam

` ? : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 38: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

Var

Γ ` :A

Var

Γ ` : BPair

Γ `App

Γ ` : CLam2

f :A× B→ C ` ? :A→ B→ CLam

` λf→ ? : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 39: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

Var

Γ ` :A

Var

Γ ` : BPair

Γ `App

Γ ` ? : CLam2

f :A× B→ C ` λxy→ ? :A→ B→ CLam

` λf x y→ ? : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 40: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

Var

Γ ` :A

Var

Γ ` : BPair

Γ ` ? :A× BApp

Γ ` f ? : CLam2

f :A× B→ C ` λxy→ f ? :A→ B→ CLam

` λf x y→ f ? : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 41: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

Var

Γ ` ? :A

Var

Γ ` ? : BPair

Γ ` pair ? ? :A× BApp

Γ ` f (pair ? ?) : CLam2

f :A× B→ C ` λxy→ f (pair ? ?) :A→ B→ CLam

` λf x y→ f (pair ? ?) : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 42: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

VarΓ ` x :A

Var

Γ ` ? : BPair

Γ ` pair x ? :A× BApp

Γ ` f (pair x ?) : CLam2

f :A× B→ C ` λxy→ f (pair x ?) :A→ B→ CLam

` λf x y→ f (pair x ?) : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 43: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Inhabitation, an example

VarΓ ` f :A× B→ C

VarΓ ` x :A

VarΓ ` y : B

PairΓ ` pair xy :A× B

AppΓ ` f (pair xy) : C

Lam2

f :A× B→ C ` λxy→ f (pair xy) :A→ B→ CLam

` λf x y→ f (pair xy) : (A× B→ C) → (A→ B→ C)

Γ = f :A× B→ C, x :A,y : B

Page 44: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Regular expressions

Page 45: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Stand back. . .

Not the Perl or POSIX regular expressions , but “real” regularexpressions:

regular expression r ::= ε empty string| a “character”, a ∈ Σ| r1 � r2 concatenation| r1 ∨ r2 alternation| r? Kleene closure

Page 46: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Stand back. . .

Not the Perl or POSIX regular expressions , but “real” regularexpressions:

regular expression r ::= ε empty string| a “character”, a ∈ Σ| r1 � r2 concatenation| r1 ∨ r2 alternation| r? Kleene closure

Page 47: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Stand back. . .

Not the Perl or POSIX regular expressions , but “real” regularexpressions:

regular expression r ::= ε empty string| a “character”, a ∈ Σ| r1 � r2 concatenation| r1 ∨ r2 alternation| r? Kleene closure

Page 48: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

MatchingWe formalise the what-matches -intuition using ∆ r relation, which isread as “regular expression rmatches string ∆”.

For example we can ask,

[x, y, z] (x∨ y)? � z

?

∆1

r1 ∆2 r2

Concat

∆1 ++ ∆2

r1 � r2

Page 49: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Matching

We formalise the what-matches -intuition using ∆ r relation, which isread as “regular expression rmatches string ∆”.

∆1

r1 ∆2 r2

Concat

∆1 ++ ∆2

r1 � r2

Page 50: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Matching

We formalise the what-matches -intuition using ∆ r relation, which isread as “regular expression rmatches string ∆”.

∆1

r1

∆2 r2

Concat

∆1 ++ ∆2

r1 � r2

Page 51: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Matching

We formalise the what-matches -intuition using ∆ r relation, which isread as “regular expression rmatches string ∆”.

∆1 r1

∆2 r2

Concat

∆1 ++ ∆2

r1 � r2

Page 52: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Matching

We formalise the what-matches -intuition using ∆ r relation, which isread as “regular expression rmatches string ∆”.

∆1 r1 ∆2 r2 Concat

∆1 ++ ∆2

r1 � r2

Page 53: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Matching

We formalise the what-matches -intuition using ∆ r relation, which isread as “regular expression rmatches string ∆”.

∆1 r1 ∆2 r2 Concat∆1 ++ ∆2 r1 � r2

Page 54: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

And there are more rules

a ∈ ΣAtom

[a] aEps

[] ε

∆ r1 Left∆ r1 ∨ r2

∆ r2 Right∆ r1 ∨ r2

Nil[] r?

∆1 r ∆2 r?

Cons∆1 ++ ∆2 r

?

Page 55: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

A match

Atom[x] x

Left[x] x∨ y

Atom[y] y

Right[y] x∨ y

Nil[] (x∨ y)?

Cons[y] (x∨ y)?

Cons[x, y] (x∨ y)?

Atom[z] z

Concat[x, y, z] (x∨ y)? � z

Page 56: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

REList

∆1 r1 ∆2 r2 Concat∆1 ++ ∆2 r1 � r2

Γ ` x : Int Γ ` y : BoolΓ ` pair x y : Int× Bool

Γ ` x : REList r1 Γ ` y : REList r2Γ ` pair x y : REList (r1 � r2)

You only need REList ! No pair, sums, ordinary lists . . .

Page 57: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

REList

∆1 r1 ∆2 r2 Concat∆1 ++ ∆2 r1 � r2

Γ ` x : Int Γ ` y : BoolΓ ` pair x y : Int× Bool

Γ ` x : REList r1 Γ ` y : REList r2Γ ` pair x y : REList (r1 � r2)

You only need REList ! No pair, sums, ordinary lists . . .

Page 58: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

REList

∆1 r1 ∆2 r2 Concat∆1 ++ ∆2 r1 � r2

Γ ` x : Int Γ ` y : BoolΓ ` pair x y : Int× Bool

Γ ` x : REList r1 Γ ` y : REList r2Γ ` pair x y : REList (r1 � r2)

You only need REList ! No pair, sums, ordinary lists . . .

Page 59: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

What’s the point?

pair (left 1) (cons ’a’ (cons ’b’ nil)) : REList ((Int∨ Bool) � Char?)

Nicer expression syntax!

J1,’a’,’b’K : REList ((Int∨ Bool) � Char?)

Page 60: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

What’s the point?

pair (left 1) (cons ’a’ (cons ’b’ nil)) : REList ((Int∨ Bool) � Char?)

Nicer expression syntax!

J1,’a’,’b’K : REList ((Int∨ Bool) � Char?)

Page 61: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Does it work?

Yes! ∆ r is decidable

And I have made a type-checker GHC plugin making these decisions!

Page 62: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Does it work?

Yes!

∆ r is decidable

And I have made a type-checker GHC plugin making these decisions!

Page 63: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Does it work?

Yes! ∆ r is decidable

And I have made a type-checker GHC plugin making these decisions!

Page 64: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Does it work?

Yes! ∆ r is decidable

And I have made a type-checker GHC plugin making these decisions!

Page 65: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

find - again

find [-H|-L] path... [operand_expression...]

(-H∨ -L∨ ε) � String? � operand -expression

(s/cat : link (s/? (s/alt :H :H :L :L)):path (s/∗ string?):expr operand -expression?)

Page 66: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lets put regular expressions into types!

(-H∨ -L∨ ε) � String? � operand -expression

type FIND= (E∨ Flag "H"∨ Flag "L")� S String� OPERAND_EXPRESSION

�nd_ :: REList FIND→ IO ()

Page 67: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lets put regular expressions into types!

(-H∨ -L∨ ε) � String? � operand -expression

type FIND= (E∨ Flag "H"∨ Flag "L")� S String� OPERAND_EXPRESSION

�nd_ :: REList FIND→ IO ()

Page 68: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Lets put regular expressions into types!

(-H∨ -L∨ ε) � String? � operand -expression

type FIND= (E∨ Flag "H"∨ Flag "L")� S String� OPERAND_EXPRESSION

�nd_ :: REList FIND→ IO ()

Page 69: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

DEMO

Page 70: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Conclusion

Page 71: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

This all is cool

Which one is “better”?

�nd_ :: REList FIND→ IO ()-- or

�nd_ ::Maybe FollowLinks→ [Path ]→ Expr→ IO ()

Missing bit is implementing the rule

∆ r1 r1 ≤ r2 Sub∆ r2

so we could use relist syntax but have Haskell types e.g.J1,’a’,’b’K : (Either Int Bool, [Char ])

Page 72: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

This all is coolWhich one is “better”?

�nd_ :: REList FIND→ IO ()-- or

�nd_ ::Maybe FollowLinks→ [Path ]→ Expr→ IO ()

Missing bit is implementing the rule

∆ r1 r1 ≤ r2 Sub∆ r2

so we could use relist syntax but have Haskell types e.g.J1,’a’,’b’K : (Either Int Bool, [Char ])

Page 73: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

This all is coolWhich one is “better”?

�nd_ :: REList FIND→ IO ()-- or

�nd_ ::Maybe FollowLinks→ [Path ]→ Expr→ IO ()

Missing bit is implementing the rule

∆ r1 r1 ≤ r2 Sub∆ r2

so we could use relist syntax but have Haskell types e.g.J1,’a’,’b’K : (Either Int Bool, [Char ])

Page 74: Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a product) ‘x:A ‘y:B Pair ‘pair xy: A B On the left hand side are expressions,

Oleg [email protected]/gists/

github.com/phadej/kleene-type

2018-09-13 • SMALL FP CONF • HELSINKI

Thank you!Questions?