SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Downloaden Sie, um offline zu lesen
Types and Perl
Language
hiratara
FreakOut Inc.
Sep. 20, 2013 / YAPC::Asia Tokyo
hiratara Types and Perl Language
What are “types”?
A thing which determines if a
term belong to some class.
1
my $x = "ABC"; $x + 3
sub id { @_ } id(10)
my $f = 0; $f->("X")
hiratara 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
What are “types”?
It’s important whether a term is
typeable or not.
It isn’t important what type a term
has.
hiratara 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
What is “typeable”?
Or, is this C code typeable?
int add1 (int n) {
return n + 1;
}
hiratara 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
Considered as the same
Perl and C versions of add1 will
be evaluated in the same way. So
the Perl code are as safe as the
C version.
“The same way” means ...
hiratara Types and Perl Language
λ calculus
A model of computation
Just a string of symbols
Don’t think what it means
hiratara Types and Perl Language
λ calculus
sub add1 {
my $n = $_[0];
return $n + 1;
}
λn.n + 1
hiratara Types and Perl Language
λ terms
That’s all.
abstraction λn. ...
application fx
hiratara Types and Perl Language
β reduction
(λv.s)t
β
−→ [t → v]s
Example
(λxy.yx)z(λx.x)
β
−→ (λy.yz)(λx.x)
β
−→ (λx.x)z
β
−→ z
hiratara Types and Perl Language
Typed λ calculus
Evaluetion is the same with λ
calculus
Give lambda terms typing
rules
Typed terms will be
evaluated correctly
The type wont be changed in
evaluation
hiratara 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
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
How to infer types
....
(λx.x)1: ?
break a problem into 2 parts
building equations, and
solving it.
hiratara 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
Building equations
We can build equations
recursively.
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
Solving equations
Compare constructors
{X → Y = (Int → Y ) → Y,
X = Y → Y }
hiratara Types and Perl Language
Solving equations
X was Int → Y , then, remove X
by substitution
{X = Int → Y , Y = Y,
X = Y → Y }
hiratara Types and Perl Language
Solving equations
Remove an equation satisfied
X = Int → Y
{Y = Y , Int → Y = Y → Y }
hiratara Types and Perl Language
Solving equations
X = Int → Y
{Int → Y = Y → Y }
hiratara Types and Perl Language
Solving equations
X = Int → Y
{Int = Y, Y = Y }
hiratara Types and Perl Language
Solving equations
X = Int → Int, Y = Int
{Int = Int}
hiratara Types and Perl Language
Solving equations
All equetions are satisfied :)
X = Int → Int, Y = Int
{}
hiratara Types and Perl Language
Infer perl types
Infer the type of
sub { $_[0] + 1 }
The answer is
sub { $_[0] : Int + 1 }
: Int -> Int
hiratara Types and Perl Language
Various types
Polymorphic Type
Record Type
Subtype
Recursive Type
hiratara Types and Perl Language
Polymorphic Type
Simple typing doesn’t work well
with following terms.
my $id = sub { $_[0] };
$id->("YAPC");
$id->(2013);
Neither $id : Str -> Str nor
$id : Int -> Int.
hiratara Types and Perl Language
Universal quantifier
x: T ∈ T, x: T
T, 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
Let-bound polymorphism
Change 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
Let-bound polymorphism
More practically, ∀ appears only
in the type environment.
Γ, X1, . . . , Xn t1 : T1 Γ, $x: ∀X1 . . . Xn.T1 t2 : T2
Γ my $x =t1; t2 : T2
hiratara Types and Perl Language
Record type
Record type is the type for
structures.
{age => 26, name => "Perl"}
: {age: Int, name: String}
hiratara 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
Subtyping
covariant 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
Infer subtyping
Subtyping relations are not
equivalence relation but ordering
relation.
It’s non-deterministic to solve
inequality expressions.
hiratara Types and Perl Language
row variables
α ⊕ {l1 : T1, . . . , ln : Tn}
hiratara Types and Perl Language
row variables
if 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
variable arguments
We consider variable arguments
as a record type.
my $plus10 = sub { $_[0] + 10 };
$plus10->(5);
$plus10->(0, "Dummy");
hiratara 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
Object
Object consists of 2 record types
The record type of fields, and
The record type of methods
hiratara 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
Object
An instance of Language will
have this subroutine as methods
&Language::hello:
∀αβγ.α ⊕ {0: (β ⊕ {name: Str}, γ)}
→ Unit
hiratara Types and Perl Language
Object
$perl:
∀αβγ. (
{name: Str, age: Int},
{hello:
α ⊕ {0: (β ⊕ {name: Str}, γ)}
→ Unit}
)
hiratara Types and Perl Language
Invoke methods
$perl->hello() means
Reffer the type of the hello
field
Pass $perl as the first
argument
hiratara Types and Perl Language
Invoke methods
We must solve the following
equation to infer this type.
γ = {hello: α ⊕ {0: (β ⊕ {name: Str}, γ)} → Unit}
hiratara Types and Perl Language
Recursive type
The fixed point of types. We
introduce the operator µ .
µX.T = [µX.T → X]T
hiratara Types and Perl Language
Recursive types
$perl:
(
{name: Str, age: Int},
µγ.{hello: {0: ({name: Str, age: Int}, γ)}
→ Unit}
}
)
hiratara Types and Perl Language
infer recursive types
I wonder if this algorithm is
correct, but it works for me.
X = µX.T if X = T and
X ∈ FV (T)
[µX.T1 → X]T1 = T2 if
µX.T1 = T2 and T1 = X
hiratara Types and Perl Language
future work
Subtype
Variant type
Meaningful errors
Support Hashes and Arrays
Use external Parser
hiratara Types and Perl Language

Weitere ähnliche Inhalte

Was ist angesagt?

Scala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To KnowScala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To KnowLightbend
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#Doncho Minkov
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015Dhaval Dalal
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesteach4uin
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented ParadigmHüseyin Ergin
 
JAVA Data Types - Part 1
JAVA Data Types - Part 1JAVA Data Types - Part 1
JAVA Data Types - Part 1Ashok Asn
 
Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2PRN USM
 
Fundamental Unicode in Perl
Fundamental Unicode in PerlFundamental Unicode in Perl
Fundamental Unicode in PerlNova Patch
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3C# Learning Classes
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpMichael Girouard
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 

Was ist angesagt? (20)

Scala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To KnowScala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To Know
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
7-Java Language Basics Part1
7-Java Language Basics Part17-Java Language Basics Part1
7-Java Language Basics Part1
 
Sparql
SparqlSparql
Sparql
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
 
What is OOP?
What is OOP?What is OOP?
What is OOP?
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
inheritance in C++
inheritance in C++inheritance in C++
inheritance in C++
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented Paradigm
 
JAVA Data Types - Part 1
JAVA Data Types - Part 1JAVA Data Types - Part 1
JAVA Data Types - Part 1
 
Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2Inheritance & Polymorphism - 2
Inheritance & Polymorphism - 2
 
Fundamental Unicode in Perl
Fundamental Unicode in PerlFundamental Unicode in Perl
Fundamental Unicode in Perl
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
 
ExAlg Overview
ExAlg OverviewExAlg Overview
ExAlg Overview
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Php Oop
Php OopPhp Oop
Php Oop
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 

Andere mochten auch

循環参照のはなし
循環参照のはなし循環参照のはなし
循環参照のはなしMasahiro Honma
 
すべてが@__kanになる
すべてが@__kanになるすべてが@__kanになる
すべてが@__kanになるMasahiro Honma
 
モナモナ言うモナド入門
モナモナ言うモナド入門モナモナ言うモナド入門
モナモナ言うモナド入門Masahiro Honma
 
Stateモナドの解説 前編
Stateモナドの解説 前編Stateモナドの解説 前編
Stateモナドの解説 前編Masahiro Honma
 
Stateモナドの解説 中編
Stateモナドの解説 中編Stateモナドの解説 中編
Stateモナドの解説 中編Masahiro Honma
 
モデルから知るGit
モデルから知るGitモデルから知るGit
モデルから知るGitMasahiro Honma
 
カレーとHokkaidopm
カレーとHokkaidopmカレーとHokkaidopm
カレーとHokkaidopmMasahiro Honma
 
モナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gzモナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gzMasahiro Honma
 
Hachioji.pm in Machida の LT
Hachioji.pm in Machida の LTHachioji.pm in Machida の LT
Hachioji.pm in Machida の LTMasahiro Honma
 
ウヰスキーとPSGI
ウヰスキーとPSGIウヰスキーとPSGI
ウヰスキーとPSGIMasahiro Honma
 
Monads in python
Monads in pythonMonads in python
Monads in pythoneldariof
 
Stateモナドの解説 後編
Stateモナドの解説 後編Stateモナドの解説 後編
Stateモナドの解説 後編Masahiro Honma
 
レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)Masahiro Honma
 

Andere mochten auch (20)

循環参照のはなし
循環参照のはなし循環参照のはなし
循環参照のはなし
 
すべてが@__kanになる
すべてが@__kanになるすべてが@__kanになる
すべてが@__kanになる
 
モナモナ言うモナド入門
モナモナ言うモナド入門モナモナ言うモナド入門
モナモナ言うモナド入門
 
Stateモナドの解説 前編
Stateモナドの解説 前編Stateモナドの解説 前編
Stateモナドの解説 前編
 
Stateモナドの解説 中編
Stateモナドの解説 中編Stateモナドの解説 中編
Stateモナドの解説 中編
 
Git入門
Git入門Git入門
Git入門
 
モデルから知るGit
モデルから知るGitモデルから知るGit
モデルから知るGit
 
20120526 hachioji.pm
20120526 hachioji.pm20120526 hachioji.pm
20120526 hachioji.pm
 
カレーとHokkaidopm
カレーとHokkaidopmカレーとHokkaidopm
カレーとHokkaidopm
 
Perl saved a lady.
Perl saved a lady.Perl saved a lady.
Perl saved a lady.
 
Math::Category
Math::CategoryMath::Category
Math::Category
 
モナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gzモナモナ言うモナド入門.tar.gz
モナモナ言うモナド入門.tar.gz
 
定理3
定理3定理3
定理3
 
Hachioji.pm in Machida の LT
Hachioji.pm in Machida の LTHachioji.pm in Machida の LT
Hachioji.pm in Machida の LT
 
ウヰスキーとPSGI
ウヰスキーとPSGIウヰスキーとPSGI
ウヰスキーとPSGI
 
Monads in python
Monads in pythonMonads in python
Monads in python
 
Stateモナドの解説 後編
Stateモナドの解説 後編Stateモナドの解説 後編
Stateモナドの解説 後編
 
レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)レンズ (ぶつかり稽古の没プレゼン)
レンズ (ぶつかり稽古の没プレゼン)
 
TraitとMoose::Role
TraitとMoose::RoleTraitとMoose::Role
TraitとMoose::Role
 
Arrows in perl
Arrows in perlArrows in perl
Arrows in perl
 

Ähnlich wie Types and perl language

Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name ResolutionEelco Visser
 
Transpilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing HydraTranspilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing HydraJoshua Shinavier
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Bryan O'Sullivan
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 
Functional programming in f sharp
Functional programming in f sharpFunctional programming in f sharp
Functional programming in f sharpchribben
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2Nihar Ranjan Paital
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in rDr Nisha Arora
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdfMdAshik35
 
Representing_Text_and_Language_v1.8.pptx
Representing_Text_and_Language_v1.8.pptxRepresenting_Text_and_Language_v1.8.pptx
Representing_Text_and_Language_v1.8.pptxSandeepTiwari353341
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingz9819898203
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 

Ähnlich wie Types and perl language (20)

Declare Your Language: Name Resolution
Declare Your Language: Name ResolutionDeclare Your Language: Name Resolution
Declare Your Language: Name Resolution
 
Transpilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing HydraTranspilers Gone Wild: Introducing Hydra
Transpilers Gone Wild: Introducing Hydra
 
Ch6.ppt
Ch6.pptCh6.ppt
Ch6.ppt
 
Ch5a
Ch5aCh5a
Ch5a
 
Ch6
Ch6Ch6
Ch6
 
C# programming
C# programming C# programming
C# programming
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
Testing for share
Testing for share Testing for share
Testing for share
 
Functional programming in f sharp
Functional programming in f sharpFunctional programming in f sharp
Functional programming in f sharp
 
Ch3
Ch3Ch3
Ch3
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
2 data types and operators in r
2 data types and operators in r2 data types and operators in r
2 data types and operators in r
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
 
Representing_Text_and_Language_v1.8.pptx
Representing_Text_and_Language_v1.8.pptxRepresenting_Text_and_Language_v1.8.pptx
Representing_Text_and_Language_v1.8.pptx
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 

Kürzlich hochgeladen

Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Kürzlich hochgeladen (20)

Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

Types and perl language

  • 1. Types and Perl Language hiratara FreakOut Inc. Sep. 20, 2013 / YAPC::Asia Tokyo hiratara Types and Perl Language
  • 2. What are “types”? A thing which determines if a term belong to some class. 1 my $x = "ABC"; $x + 3 sub id { @_ } id(10) my $f = 0; $f->("X") hiratara Types and Perl Language
  • 3. 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
  • 4. What are “types”? It’s important whether a term is typeable or not. It isn’t important what type a term has. hiratara Types and Perl Language
  • 5. What is “typeable”? Is this perl code typeable? sub add1 { my $n = $_[0]; return $n + 1; } hiratara Types and Perl Language
  • 6. What is “typeable”? Or, is this C code typeable? int add1 (int n) { return n + 1; } hiratara Types and Perl Language
  • 7. Considered as the same sub add1 { my $n = $_[0]; return $n + 1; } int add1 (int n) { return n + 1; } hiratara Types and Perl Language
  • 8. Considered as the same Perl and C versions of add1 will be evaluated in the same way. So the Perl code are as safe as the C version. “The same way” means ... hiratara Types and Perl Language
  • 9. λ calculus A model of computation Just a string of symbols Don’t think what it means hiratara Types and Perl Language
  • 10. λ calculus sub add1 { my $n = $_[0]; return $n + 1; } λn.n + 1 hiratara Types and Perl Language
  • 11. λ terms That’s all. abstraction λn. ... application fx hiratara Types and Perl Language
  • 12. β reduction (λv.s)t β −→ [t → v]s Example (λxy.yx)z(λx.x) β −→ (λy.yz)(λx.x) β −→ (λx.x)z β −→ z hiratara Types and Perl Language
  • 13. Typed λ calculus Evaluetion is the same with λ calculus Give lambda terms typing rules Typed terms will be evaluated correctly The type wont be changed in evaluation hiratara Types and Perl Language
  • 14. 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
  • 15. 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
  • 16. How to infer types .... (λx.x)1: ? break a problem into 2 parts building equations, and solving it. hiratara Types and Perl Language
  • 17. 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
  • 18. Building equations We can build equations recursively. 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
  • 19. Solving equations Compare constructors {X → Y = (Int → Y ) → Y, X = Y → Y } hiratara Types and Perl Language
  • 20. Solving equations X was Int → Y , then, remove X by substitution {X = Int → Y , Y = Y, X = Y → Y } hiratara Types and Perl Language
  • 21. Solving equations Remove an equation satisfied X = Int → Y {Y = Y , Int → Y = Y → Y } hiratara Types and Perl Language
  • 22. Solving equations X = Int → Y {Int → Y = Y → Y } hiratara Types and Perl Language
  • 23. Solving equations X = Int → Y {Int = Y, Y = Y } hiratara Types and Perl Language
  • 24. Solving equations X = Int → Int, Y = Int {Int = Int} hiratara Types and Perl Language
  • 25. Solving equations All equetions are satisfied :) X = Int → Int, Y = Int {} hiratara Types and Perl Language
  • 26. Infer perl types Infer the type of sub { $_[0] + 1 } The answer is sub { $_[0] : Int + 1 } : Int -> Int hiratara Types and Perl Language
  • 27. Various types Polymorphic Type Record Type Subtype Recursive Type hiratara Types and Perl Language
  • 28. Polymorphic Type Simple typing doesn’t work well with following terms. my $id = sub { $_[0] }; $id->("YAPC"); $id->(2013); Neither $id : Str -> Str nor $id : Int -> Int. hiratara Types and Perl Language
  • 29. Universal quantifier x: T ∈ T, x: T T, 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
  • 30. Let-bound polymorphism Change 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
  • 31. Let-bound polymorphism More practically, ∀ appears only in the type environment. Γ, X1, . . . , Xn t1 : T1 Γ, $x: ∀X1 . . . Xn.T1 t2 : T2 Γ my $x =t1; t2 : T2 hiratara Types and Perl Language
  • 32. Record type Record type is the type for structures. {age => 26, name => "Perl"} : {age: Int, name: String} hiratara Types and Perl Language
  • 33. 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
  • 34. Subtyping covariant 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
  • 35. Infer subtyping Subtyping relations are not equivalence relation but ordering relation. It’s non-deterministic to solve inequality expressions. hiratara Types and Perl Language
  • 36. row variables α ⊕ {l1 : T1, . . . , ln : Tn} hiratara Types and Perl Language
  • 37. row variables if 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
  • 38. variable arguments We consider variable arguments as a record type. my $plus10 = sub { $_[0] + 10 }; $plus10->(5); $plus10->(0, "Dummy"); hiratara Types and Perl Language
  • 39. 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
  • 40. Object Object consists of 2 record types The record type of fields, and The record type of methods hiratara Types and Perl Language
  • 41. 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
  • 42. Object An instance of Language will have this subroutine as methods &Language::hello: ∀αβγ.α ⊕ {0: (β ⊕ {name: Str}, γ)} → Unit hiratara Types and Perl Language
  • 43. Object $perl: ∀αβγ. ( {name: Str, age: Int}, {hello: α ⊕ {0: (β ⊕ {name: Str}, γ)} → Unit} ) hiratara Types and Perl Language
  • 44. Invoke methods $perl->hello() means Reffer the type of the hello field Pass $perl as the first argument hiratara Types and Perl Language
  • 45. Invoke methods We must solve the following equation to infer this type. γ = {hello: α ⊕ {0: (β ⊕ {name: Str}, γ)} → Unit} hiratara Types and Perl Language
  • 46. Recursive type The fixed point of types. We introduce the operator µ . µX.T = [µX.T → X]T hiratara Types and Perl Language
  • 47. Recursive types $perl: ( {name: Str, age: Int}, µγ.{hello: {0: ({name: Str, age: Int}, γ)} → Unit} } ) hiratara Types and Perl Language
  • 48. infer recursive types I wonder if this algorithm is correct, but it works for me. X = µX.T if X = T and X ∈ FV (T) [µX.T1 → X]T1 = T2 if µX.T1 = T2 and T1 = X hiratara Types and Perl Language
  • 49. future work Subtype Variant type Meaningful errors Support Hashes and Arrays Use external Parser hiratara Types and Perl Language