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
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
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
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
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
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
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