Types and perl language

49
Types and Perl Language hiratara FreakOut Inc. Sep. 20, 2013 / YAPC::Asia Tokyo hiratara Types and Perl Language

description

Talk about TypedPerl which is a type checker of perl.

Transcript of Types and perl language

Page 1: Types and perl language

Types and PerlLanguage

hirataraFreakOut Inc.

Sep. 20, 2013 / YAPC::Asia Tokyo

hiratara Types and Perl Language

Page 2: Types and perl language

What are “types”?

A thing which determines if aterm belong to some class.

1

my $x = "ABC"; $x + 3

sub id { @_ } id(10)

my $f = 0; $f->("X")

hiratara Types and Perl Language

Page 3: Types and perl language

What are “types”?

Typeable:

1

sub id { @_ } id(10)

Not typeable:

my $x = "ABC"; $x + 3

my $f = 0; $f->("X")

hiratara Types and Perl Language

Page 4: Types and perl language

What are “types”?

It’s important whether a term istypeable or not.It isn’t important what type a termhas.

hiratara Types and Perl Language

Page 5: Types and perl language

What is “typeable”?

Is this perl code typeable?

sub add1 {my $n = $_[0];return $n + 1;

}

hiratara Types and Perl Language

Page 6: Types and perl language

What is “typeable”?

Or, is this C code typeable?

int add1 (int n) {return n + 1;

}

hiratara Types and Perl Language

Page 7: Types and perl language

Considered as the same

sub add1 {my $n = $_[0];return $n + 1;

}

int add1 (int n) {return n + 1;

}

hiratara Types and Perl Language

Page 8: Types and perl language

Considered as the same

Perl and C versions of add1 willbe evaluated in the same way. Sothe Perl code are as safe as theC version.“The same way” means ...

hiratara Types and Perl Language

Page 9: Types and perl language

λ calculus

A model of computationJust a string of symbolsDon’t think what it means

hiratara Types and Perl Language

Page 10: Types and perl language

λ calculus

sub add1 {my $n = $_[0];return $n + 1;

}

λn.n + 1

hiratara Types and Perl Language

Page 11: Types and perl language

λ terms

That’s all.

abstraction λn. ...application fx

hiratara Types and Perl Language

Page 12: Types and perl language

β reduction

(λv.s)tβ−→ [t 7→ v]s

Example

(λxy.yx)z(λx.x)β−→ (λy.yz)(λx.x)β−→ (λx.x)z

β−→ z

hiratara Types and Perl Language

Page 13: Types and perl language

Typed λ calculusEvaluetion is the same with λcalculusGive lambda terms typingrulesTyped terms will beevaluated correctlyThe type wont be changed inevaluation

hiratara Types and Perl Language

Page 14: Types and perl language

Typing rules

x : T ∈ ΓΓ ` x : T

Γ, x : T1 ` t2 : T2

Γ ` λx : T1.t2 : T1 → T2

Γ ` t1 : T11 → T12 Γ ` t2 : T11

Γ ` t1t2 : T12

hiratara Types and Perl Language

Page 15: Types and perl language

example of Typing

x : Int ∈ x : Int, y : Int

x : Int, y : Int ` x : Int

y : Int ∈ x : Int, y : Int

x : Int, y : Int ` y : Int

x : Int, y : Int ` x+ y : Int

y : Int ` λx.x+ y : Int→ Int

` λxy.x+ y : Int→ Int→ Int

hiratara Types and Perl Language

Page 16: Types and perl language

How to infer types

....` (λx.x)1 : ?

break a problem into 2 partsbuilding equations, andsolving it.

hiratara Types and Perl Language

Page 17: Types and perl language

Building equations

x : T ∈ ΓΓ ` x : T |{}

Γ, x : T1 ` t2 : T2|CΓ ` λx : T1.t2 : T1 → T2|C

Γ ` t1 : X|C1 Γ ` t2 : T11|C2

Γ ` t1t2 : T12|C1 ∪ C2 ∪ {X = T11 → T12}

hiratara Types and Perl Language

Page 18: Types and perl language

Building equations

We can build equationsrecursively.

x : T1 ∈ x : T1

x : T1 ` x : T1|{}` λx : T1.x : T1 → T1|{} ` 1 : Int|{}` (λx : T1.x)1 : T2|{T1 → T1 = Int→ T2}

hiratara Types and Perl Language

Page 19: Types and perl language

Solving equations

Compare constructors

{X → Y = (Int→ Y )→ Y,

X = Y → Y }

hiratara Types and Perl Language

Page 20: Types and perl language

Solving equations

X was Int→ Y , then, remove Xby substitution

{X = Int→ Y , Y = Y,

X = Y → Y }

hiratara Types and Perl Language

Page 21: Types and perl language

Solving equations

Remove an equation satisfied

X = Int→ Y

{Y = Y , Int→ Y = Y → Y }

hiratara Types and Perl Language

Page 22: Types and perl language

Solving equations

X = Int→ Y

{Int→ Y = Y → Y }

hiratara Types and Perl Language

Page 23: Types and perl language

Solving equations

X = Int→ Y

{Int = Y, Y = Y }

hiratara Types and Perl Language

Page 24: Types and perl language

Solving equations

X = Int→ Int, Y = Int

{Int = Int}

hiratara Types and Perl Language

Page 25: Types and perl language

Solving equations

All equetions are satisfied :)

X = Int→ Int, Y = Int

{}

hiratara Types and Perl Language

Page 26: Types and perl language

Infer perl typesInfer the type of

sub { $_[0] + 1 }

The answer is

sub { $_[0] : Int + 1 }: Int -> Int

hiratara Types and Perl Language

Page 27: Types and perl language

Various types

Polymorphic TypeRecord TypeSubtypeRecursive Type

hiratara Types and Perl Language

Page 28: Types and perl language

Polymorphic TypeSimple typing doesn’t work wellwith following terms.

my $id = sub { $_[0] };$id->("YAPC");$id->(2013);

Neither $id : Str -> Str nor$id : Int -> Int.

hiratara Types and Perl Language

Page 29: Types and perl language

Universal quantifier

x : T ∈ T, x : TT, x : T ` x : T

T ` λx : T.x : T → T` λT.λx : T.x : ∀T.T → T

` (λT.λx : T.x)Int : Int→ Int ` 1 : Int

` (λT.λx : T.x)Int 1 : Int

hiratara Types and Perl Language

Page 30: Types and perl language

Let-bound polymorphismChange the let rule from

Γ ` t1 : T1 Γ,$x : T1 ` t2 : T2

Γ ` my $x =t1; t2 : T2

to

Γ ` t1 : T1 Γ ` [$x→ t1]t2 : T2

Γ ` my $x =t1; t2 : T2

hiratara Types and Perl Language

Page 31: Types and perl language

Let-bound polymorphism

More practically, ∀ appears onlyin the type environment.

Γ, X1, . . . , Xn ` t1 : T1 Γ,$x : ∀X1 . . . Xn.T1 ` t2 : T2Γ ` my $x =t1; t2 : T2

hiratara Types and Perl Language

Page 32: Types and perl language

Record type

Record type is the type forstructures.

{age => 26, name => "Perl"}

: {age : Int,name : String}

hiratara Types and Perl Language

Page 33: Types and perl language

Subtyping

{l1 : T1, . . . , lk : Tk, . . . , ln : Tn} <:

{l1 : T1, . . . , lk : Tk}

Si <: Ti{l1 : S1, . . . , ln : Sn}<: {l1 : T1, . . . , ln : Tn}

hiratara Types and Perl Language

Page 34: Types and perl language

Subtypingcovariant and contravariant

T1 <: S1 S2 <: T2

S1 → S2 <: T1 → T2

For example,

sub { { x => $_[0]->{x} + $_[0]->{x},

y => $_[0]->{x} } }

<:

sub { { x => $_[0]->{x} + $_[0]->{y} } }

hiratara Types and Perl Language

Page 35: Types and perl language

Infer subtyping

Subtyping relations are notequivalence relation but orderingrelation.It’s non-deterministic to solveinequality expressions.

hiratara Types and Perl Language

Page 36: Types and perl language

row variables

α⊕ {l1 : T1, . . . , ln : Tn}

hiratara Types and Perl Language

Page 37: Types and perl language

row variablesif we have

β ⊕ {l3 : Bool} = α⊕ {l1 : Int, l2 : Str}α⊕ {l1 : Int, l2 : Str} = {l1 : Int, l2 : Str, l3 : Bool}

then

α = {l3 : Bool}β = {l1 : Int, l2 : Str}

hiratara Types and Perl Language

Page 38: Types and perl language

variable arguments

We consider variable argumentsas a record type.

my $plus10 = sub { $_[0] + 10 };$plus10->(5);$plus10->(0, "Dummy");

hiratara Types and Perl Language

Page 39: Types and perl language

variable arguments

The type of 0, "Dummy" is{0 : Int, 1 : Str}, and

$plus10 : ∀α.α⊕ {0 : Int} → Int

So α is {1 : Str}

hiratara Types and Perl Language

Page 40: Types and perl language

Object

Object consists of 2 record typesThe record type of fields, andThe record type of methods

hiratara Types and Perl Language

Page 41: Types and perl language

Object

package Language;sub hello {

my $msg = "I’m " .$_[0]->{name} .", Hello.";

print($msg)}

package main;my $perl = bless {name => "Perl", age => "26"},

"Language";$perl->hello();

hiratara Types and Perl Language

Page 42: Types and perl language

Object

An instance of Language willhave this subroutine as methods

&Language::hello :

∀αβγ.α⊕ {0 : (β ⊕ {name : Str}, γ)}→ Unit

hiratara Types and Perl Language

Page 43: Types and perl language

Object

$perl :

∀αβγ. ({name : Str,age : Int},{hello :

α⊕ {0 : (β ⊕ {name : Str}, γ)}→ Unit})

hiratara Types and Perl Language

Page 44: Types and perl language

Invoke methods

$perl->hello() means

Reffer the type of the hellofieldPass $perl as the firstargument

hiratara Types and Perl Language

Page 45: Types and perl language

Invoke methods

We must solve the followingequation to infer this type.

γ = {hello : α⊕ {0: (β ⊕ {name : Str}, γ)} → Unit}

hiratara Types and Perl Language

Page 46: Types and perl language

Recursive type

The fixed point of types. Weintroduce the operator µ .

µX.T = [µX.T 7→ X ]T

hiratara Types and Perl Language

Page 47: Types and perl language

Recursive types

$perl :

(

{name : Str,age : Int},µγ.{hello : {0: ({name : Str,age : Int}, γ)}→ Unit}}

)

hiratara Types and Perl Language

Page 48: Types and perl language

infer recursive types

I wonder if this algorithm iscorrect, but it works for me.

X = µX.T if X = T andX ∈ FV (T )

[µX.T1 7→ X ]T1 = T2 ifµX.T1 = T2 and T1 6= X

hiratara Types and Perl Language

Page 49: Types and perl language

future work

SubtypeVariant typeMeaningful errorsSupport Hashes and ArraysUse external Parser

hiratara Types and Perl Language