Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a...
Transcript of Regular expressions of types · Lambda calculus Example rule: introducing (making) a pair (or a...
![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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/1.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/2.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/3.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/4.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/5.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/6.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/7.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/8.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/9.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/10.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/11.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/12.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/13.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/14.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/15.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/16.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/17.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/18.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/19.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/20.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/21.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/22.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/23.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/24.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/25.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/26.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/27.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/28.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/29.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/30.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/31.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/32.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/33.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/34.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/35.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/36.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/37.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/38.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/39.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/40.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/41.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/42.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/43.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/44.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/45.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/46.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/47.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/48.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/49.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/50.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/51.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/52.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/53.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/54.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/55.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/56.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/57.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/58.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/59.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/60.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/61.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/62.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/63.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/64.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/65.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/66.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/67.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/68.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/69.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/70.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/71.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/72.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/73.jpg)
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,](https://reader036.fdocuments.us/reader036/viewer/2022090605/605ad1ca8390d946b912f53d/html5/thumbnails/74.jpg)
Oleg [email protected]/gists/
github.com/phadej/kleene-type
2018-09-13 • SMALL FP CONF • HELSINKI
Thank you!Questions?