Just-in-Time Static Type Checking for Dynamic Languages
Brianna M. Ren

Jeffrey S. Foster

arXiv:1604.03641v1 [] 13 Apr 2016

University of Maryland, College Park
{bren, jfoster}@cs.umd.edu

Abstract

and convoluted; and prior mixed static/dynamic systems are
either cumbersome or make certain limiting assumptions.
(Section 6 discusses prior work in detail.)
In this paper, we introduce Hummingbird,1 a type checking system for Ruby that solves this problem using a new
approach we call just-in-time static type checking. In Hummingbird, user-provided type annotations actually execute at
run-time, adding types to an environment that is maintained
during execution. As metaprogramming code creates new
methods, it, too, executes type annotations to assign types to
dynamically created methods. Then whenever a method m is
called, Hummingbird statically type checks m’s body in the
current dynamic type environment. More precisely, Hummingbird checks that m calls methods at their types as annotated, and that m itself matches its annotated type. Moreover, Hummingbird caches the type check so that it need not
recheck m at the next call unless the dynamic type environment has changed in a way that affects m.
Just-in-time static type checking provides a highly effective tradeoff between purely dynamic and purely static
type checking. On the one hand, metaprogramming code is
very challenging to analyze statically, but in our experience
it is easy to create type annotations at run time for generated code. On the other hand, by statically analyzing whole
method bodies, we catch type errors earlier than a purely
dynamic system, and we can soundly reason about all possible execution paths within type checked methods. (Section 2
shows how several examples of metaprogramming are handled by Hummingbird.)
To ensure our approach to type checking is correct, we
formalize Hummingbird using a core, Ruby-like language in
which method creation and method type annotation can occur at arbitrary points during execution. We provide a flowsensitive type checking system and a dynamic semantics that
invokes the type system at method entry, caching the resulting typing proof. Portions of the cache may be invalidated
as new methods are defined or type annotations are changed.
We prove soundness for our type system. (Section 3 presents
the formalism.)
Our implementation of Hummingbird piggybacks on two
prior systems we developed. We use the Ruby Intermediate

Dynamic languages such as Ruby, Python, and JavaScript
have many compelling benefits, but the lack of static types
means subtle errors can remain latent in code for a long time.
While many researchers have developed various systems to
bring some of the benefits of static types to dynamic languages, prior approaches have trouble dealing with metaprogramming, which generates code as the program executes. In
this paper, we propose Hummingbird, a new system that uses
a novel technique, just-in-time static type checking, to type
check Ruby code even in the presence of metaprogramming.
In Hummingbird, method type signatures are gathered dynamically at run-time, as those methods are created. When
a method is called, Hummingbird statically type checks the
method body against current type signatures. Thus, Hummingbird provides thorough static checks on a per-method
basis, while also allowing arbitrarily complex metaprogramming. For performance, Hummingbird memoizes the static
type checking pass, invalidating cached checks only if necessary. We formalize Hummingbird using a core, Ruby-like
language and prove it sound. To evaluate Hummingbird, we
applied it to six apps, including three that use Ruby on Rails,
a powerful framework that relies heavily on metaprogramming. We found that all apps typecheck successfully using
Hummingbird, and that Hummingbird’s performance overhead is reasonable. We applied Hummingbird to earlier versions of one Rails app and found several type errors that had
been introduced and then fixed. Lastly, we demonstrate using Hummingbird in Rails development mode to typecheck
an app as live updates are applied to it.
Categories and Subject Descriptors F.3.2 [Semantics of
Programming Languages]: Program analysis
Keywords type checking; dynamic languages; Ruby

1. Introduction
Many researchers have explored ways to bring the benefits
of static typing to dynamic languages [1–4, 12, 14, 18, 20–
22, 25, 27, 36, 37]. However, many of these prior systems do
not work well in the presence of metaprogramming, in which
code the program relies on is generated as the program
executes. The challenge is that purely static systems cannot
analyze metaprogramming code, which is often complicated

1A

hummingbird can dynamically flap its wings while statically hovering
in place.

1

2016/4/14

Language [8, 13] to parse input Ruby files and translate them
to simplified control-flow graphs. We use RDL [26, 33], a
Ruby contract system, to intercept method calls and to represent and store method type signatures at run time. Hummingbird supports an extensive set of typing features, including union types, intersection types, code blocks (anonymous
functions), generics, modules, and type casts, among others.
(Section 4 describes our implementation.)
We evaluated Hummingbird by applying it to six Ruby
apps. Three use Ruby on Rails (just “Rails” from now
on), a popular, sophisticated web app framework that uses
metaprogramming heavily both to make Rails code more
compact and expressive and to support “convention over
configuration.” We should emphasize that Rails’s use of
metaprogramming makes static analysis of it very challenging [17]. Two additional apps use other styles of metaprogramming, and the last app does not use metaprogramming,
as a baseline.
We found that all of our subject apps type check successfully using Hummingbird, and that dynamically generated
types are essential for the apps that use metaprogramming.
We also found that Hummingbird’s performance overhead
ranges from 19% to 469%, which is much better than prior
approaches [18, 27], and that caching is essential to achieving this performance. For one Rails app, we ran type checking on many prior versions, and we found a total of six type
errors that had been introduced and then later fixed. We also
ran the app in Rails development mode, which reloads files
as they are edited, to demonstrate how Hummingbird type
check caching behaves in the presence of modified methods.
(Section 5 reports on our results.)
In summary, we believe Hummingbird is an important
step forward in our ability to bring the benefits of static
typing to dynamic languages while still supporting flexible
and powerful metaprogramming features.

1
2
3
4
5
6
7

class Talk < ActiveRecord::Base
belongs to :owner, :class name ⇒ ”User”
....
type :owner?, ”(User) → %bool”
def owner?(user)
return owner == user
end end

8
9
10
11
12
13
14
15
16
17
18
19

module ActiveRecord:: Associations :: ClassMethods
pre (: belongs to ) do |∗ args |
hmi = args[0]
options = args [1]
hm = hmi.to s
cn = options [: class name] if options
hmu = cn ? cn : hm. singularize . camelize
type hm. singularize , ”() → #{hmu}”
type ”#{hm.singularize}=”, ”(#{hmu}) →#{hmu}”
true
end end

Figure 1. Ruby on Rails Metaprogramming.
a keyword, but in fact it is simply a method call. The call
passes the symbol (an interned string) :owner as the first
argument, and the second argument is a hash that maps
symbol :class name to string ”User”.
Now consider the owner? method, defined on line 5. Just
before the method, we introduce a type annotation indicating
the method takes a User and returns a boolean. Given such
an annotation, Hummingbird’s goal is to check whether the
method body has the indicated type.2 This should be quite
simple in this case, as the body of owner? just calls noargument method owner and checks whether the result is
equal to user.
However, if we examine the remaining code of Talk (not
shown), we discover that owner is not defined anywhere in
the class! Instead, this method is created at run-time by belongs to. More specifically, when belongs to is called, it defines several convenience methods that perform appropriate
SQL queries for the relationship [23], in this case to get the
User instance associated with the Talk’s owner. Thus, as we
can see, it is critical for Hummingbird to handle such dynamically created methods even to type check simple Rails
code.
Our solution is to instrument belongs to so that, just
as it creates a method dynamically, it also creates method
type signatures dynamically. The code on lines 9–19 of Figure 1 accomplishes this. Hummingbird is built on RDL,
a Ruby contract system for specifying pre- and postconditions [26, 33]. The precondition is specified via a code
block—an anonymous function (i.e., a lambda) delimited by
do. . .end—passed to pre. Here the code block trivially re-

2. Overview
We begin our presentation by showing some uses of metaprogramming in Ruby and the corresponding Hummingbird
type checking process. The examples below are from the
experiments in Section 5.
Rails Associations. The top of Figure 1 shows an excerpt
from the Talks Rails app. This code defines a class Talk that
is a model in Rails, meaning an instance of Talk represents a
row in the talks database table. The change in case and pluralization here is not an accident—Rails favors “convention
over configuration,” meaning many relationships that would
otherwise be specified via configuration are instead implicitly expressed by using similar or the same name for things.
In this app, every talk is owned by a user, which in
implementation terms means a Talk instance has a foreign
key owner id indicating the owner, which is an instance of
class User (not shown). The existence of that relationship
is defined on line 2. Here it may look like belongs to is

2 In practice

type takes another argument to tell Hummingbird to type check
the body, in contrast to library and framework methods whose types are
trusted. We elide this detail for simplicity.

2

2016/4/14

1
2
3
4
5
6
7
8

module Rolify :: Dynamic
def define dynamic method(role name, resource )
class eval do
define method(” is #{role name}?”.to sym) do
has role ?(”#{role name}”)
end if !method defined?(”is #{role name}?”.to sym)
...
end end

9
10
11
12
13

16
17
18
19
20

2
3
4
5
6
7
8
9

pre :define dynamic method do |role name, resource |
type ” is #{role name}?”, ”() → %bool”
true
end end

14
15

1

10
11
12
13
14

class User; include Rolify :: Dynamic end
user = User. first
user .define dynamic method(”professor”, ...)
user .define dynamic method(”student”, ...)
user . is professor ?
user . is student ?

Transaction = Struct.new(:type, :account name, :amount)
class ApplicationRunner
def process transactions
@transactions .each do | t |
name = t.account name
...
end ... end
field type : @transactions , ”Array<Transaction>”
end

15
16
17
18
19
20
21

class Struct
def self .add types(∗types)
members.zip(types).each {|name, t|
self . class eval do
type name, ”() → #{t}”
type ”#{name}=”, ”(t) →#{t}”
end
}
end
end
Transaction .add types(”String”, ”String”, ”String”)

Figure 2. Methods Dynamically Created by User Code.
Figure 3. Type Signatures for Struct.
turns true so the precondition is always satisfied (last line)
and, as a side effect, creates method type annotations for belongs to.
In more detail, hmi is set to the first argument to belongs to, and options is either nil or the hash argument, if
present. (Here hm is shorthand for “has many,” i.e., since the
Talk belongs to a User, the User has many Talks.) Then hmu
is set to either the class name argument, if present, or hmi
after singularizing and camel-casing it. Then type is called
twice, once to give a type to a getter method created by belongs to, and once for a setter method (whose name ends
with =). Notation #{e} inside a string evaluates the expression e and inserts the result in the string. In this particular
case, these two calls to type evaluate to

code matching the given type signature. Figure 2 shows
an example, extracted from Rolify, in which user code dynamically generates a method. The first part of the figure defines a module (aka mixin) with a two-argument
method define dynamic method. The method body calls define method to create a method named using the first argument, as long as that method does not exist (note the postfix
if on line 6). Similarly to earlier, line 10 adds a precondition to define dynamic method that provides an appropriate
method type. (We do not check for a previous type definition
since adding the same type again is harmless.)
The code starting at line 15 uses the module. This particular code is not from our experiment but is merely for expository purposes. Here we (re)open class User and mix in the
module. Then we create a user; call define dynamic method
twice; and then call the generated methods is professor? and
is student?.
In this case, since the generated methods have type annotations and are in user code, Hummingbird type checks
their bodies when they are called, just like any other userdefined method with a type. For example, consider the call
to is professor?, which is given type () → %bool. At the
call, Hummingbird type checks the code block at line 4 and
determines that it has no arguments and that its body returns
a boolean, i.e., it type checks.

type ”owner”, ”() → User”
type ”owner=”, ”(User) → User”

Now consider executing this code. When Talk is loaded,
belongs to will be invoked, adding those type signatures to
the class. Then when owner? is called, Hummingbird will
perform type checking using currently available type information, and so it will be able to successfully type check the
body. Moreover, notice this approach is very flexible. Rails
does not require that belongs to be used at the beginning
of a class or even during this particular class definition. (In
Ruby, it is possible to “re-open” a class later on and add more
methods to it.) But no matter where the call occurs, it must
be before owner? is called so that owner is defined. Thus in
this case, Hummingbird’s typing strategy matches well with
Ruby’s semantics.

User-provided Type Signatures. In the examples so far,
the types for dynamically created methods could be determined automatically. However, consider Figure 3, which
shows an excerpt from CCT that uses Struct from the Ruby
core library. Line 1 creates a new class, instances of which
are defined to have getters type, account name, and amount,

Type Checking Dynamically Created Methods. In the previous example, we trusted Rails to dynamically generate
3

2016/4/14

values
expressions

v
e

premths
val typs
mth typs

b
τ
τm

::=
::=
|
|
::=
::=
::=

x ∈ var ids

dyn env
dyn cls tab
contexts

E
DT
C

stack
S
type env
Γ, ∆
type tab
TT
cache
X
typ chk deriv DM
subtyp deriv D≤

m ∈ mth ids

:
:
::=
|
::=
:
:
::=
::=
::=

inheritance from our formalism for simplicity, but they are
handled by our implementation.
Expressions e include values, variables x, the special variable self, assignments x = e, and sequencing e; e. Objects
are created with A.new. Conditional if e1 then e2 else e3
evaluates to e2 unless e1 evaluates to nil, in which case it
evaluates to e3 . Method invocation e1 .m(e2 ) is standard, invoking the m method based on the run-time type of e1 .
Expression def A.m = λx.e, defines method m of class
A as taking argument x and returning e. (We refer to λx.e
as a premethod.) This form allows methods to be defined
anywhere during execution, thus it combines the features
of Ruby’s def and define method. As in Ruby, if A.m is
already defined, def overwrites the previous definition. The
def expression itself evaluates to nil.
Finally, expression type A.m : τ → τ ′ asserts that
method m of class A has domain type τ and range type τ ′ .
Types may be either classes A or nil, the type of expression nil. The type expression overwrites the previous type of
A.m, if any. Like Hummingbird, there is no ordering dependency between def and type—the only requirement is that
a method’s type must be declared by the time the method is
called. The type expression itself evaluates to nil.

nil | [A]
v | x | self | x = e | e; e | A.new
if e then e else e | e.m(e)
def A.m = b | type A.m : τm
λx.e
A | nil
τ →τ

A ∈ cls ids

var ids → vals
cls ids → mth ids → premths
✷ | x = C | C.m(e) | v.m(C)
C; e | if C then e else e
· | (E , C) :: S
var ids → val typs
cls ids → mth ids → mth typs
cls ids → mth ids → DM × D≤
TT ⊢ hΓ, ei ⇒ hΓ′ , τ i
τ1 ≤ τ2

Figure 4. Source Language and Auxiliary Definitions.

Type Checking. Figure 5 gives the static type checking
rules. As in Hummingbird, static type checking is performed
at run time at method entry—thus these rules will be invoked
as a subroutine by the dynamic semantics (below). The bottom part of Figure 4 defines the sets and maps used in this
figure and in the dynamic semantics.
In these rules, TT is a type table mapping class and
method ids A.m to their corresponding types, as declared
by type, and Γ is a type environment mapping local variables to their types. These rules prove judgments of the form
TT ⊢ hΓ, ei ⇒ hΓ′ , τ i, meaning with type table TT , in
type environment Γ, expression e has type τ , and after evaluating e, the new type environment is Γ′ . Using an “output”
type environment Γ′ allows us to build a flow-sensitive type
system, in which variables’ types can change at assignments.
Note there is no output TT because the type table does not
change during static type checking—it only changes as the
program is executed by the dynamic semantics.
The type rules are largely standard. (TNil) and (TObject)
give nil and instances the obvious types. (TSelf) and (TVar)
give self and local variables their types according to the type
environment. Since none of these four expressions updates
the state, the output type environment is the same as the input
environment.
(TSeq) types sequencing, threading the type environment
from the output of e1 to the input of e2 . (TAssn) types an
assignment, updating the output type environment to bind
the assigned variable x to the type of the right-hand side.
(TNew) types object creation in the obvious way. (TDef)
trivially type checks method definitions. Notice we do not
type check the method body; that will happen at run time

and setters type=, account name=, and amount=. The
process transactions method iterates through instance field
@transactions (whose type is provided on line 8), and calls
the account name method of each one.
From line 1 we know the account name method exists,
but we do not know its type. Indeed, a “struct field” can hold
any type by default. Thus, to fully type check the body of
process transactions, we need more information from the
programmer to specify the type of account name.
The bottom part of Figure 3 defines a new method,
add types, that the programmer can call to indicate desired
struct field types. The types are given in the same order
as the constructor arguments, and the body of add types
uses zip to pair up the constructor arguments (retrieved via
members) and the types, and then iterates through the pairs,
creating the appropriate type signatures for the getters and
setters. The last line of the figure uses add types to create
type signatures for this example, allowing us to type check
process transactions when it is called.
In this particular case, we could have individually specified type signatures for the methods of Transaction. However, because Hummingbird lets programmers write arbitrary Ruby programs to generate types, we were able to develop this much more elegant solution.

3. Formalism
We formalize Hummingbird using the core, Ruby-like language shown at the top of Figure 4. Values v include nil,
which can be treated as if it has any type, and [A], which
is an instance of class A. Note that we omit both fields and
4

2016/4/14

TT ⊢ hΓ, ei ⇒ hΓ′ , τ i
(TNil)

(TObject)

(TSelf)

(TVar)

TT ⊢ hΓ, nili ⇒ hΓ, nili

TT ⊢ hΓ, [A]i ⇒ hΓ, Ai

TT ⊢ hΓ, selfi ⇒ hΓ, Γ(self)i

TT ⊢ hΓ, xi ⇒ hΓ, Γ(x)i

(TSeq)
TT ⊢ hΓ, e1 i ⇒ hΓ1 , τ1 i
TT ⊢ hΓ1 , e2 i ⇒ hΓ2 , τ2 i
TT ⊢ hΓ, e1 ; e2 i ⇒ hΓ2 , τ2 i

(TAssn)
TT ⊢ hΓ, ei ⇒ hΓ′ , τ i

(TDef)

TT ⊢ hΓ, x = ei ⇒ hΓ′ [x 7→ τ ], τ i

TT ⊢ hΓ, def A.m = λx.ei ⇒ hΓ, nili
(TType)

(TNew)
TT ⊢ hΓ, A.newi ⇒ hΓ, Ai

TT ⊢ hΓ, type A.m : τm i ⇒ hΓ, nili

(TApp)
TT ⊢ hΓ, e0 i ⇒ hΓ0 , Ai
TT ⊢ hΓ0 , e1 i ⇒ hΓ1 , τ i
TT (A.m) = τ1 → τ2
τ ≤ τ1

(TIf)
TT ⊢ hΓ, e0 i ⇒ hΓ′ , τ i
TT ⊢ hΓ′ , e1 i ⇒ hΓ1 , τ1 i
′
TT ⊢ hΓ , e2 i ⇒ hΓ2 , τ2 i

TT ⊢ hΓ, e0 .m(e1 )i ⇒ hΓ1 , τ2 i

TT ⊢ hΓ, if e0 then e1 else e2 i ⇒ hΓ1 ⊔ Γ2 , τ1 ⊔ τ2 i

Figure 5. Type Checking System.
when the method is actually called. (TType) type checks a
type expression, which has no effect during type checking.
Such expressions are only evaluated at run-time, when they
update the type table (see below).
One consequence of (TType) is that our type system forbids typing a method and then immediately calling it in the
same method body. For example, the following method body
would fail to type check:
1
2
3
4

def A.m = λx.
def B.m = ...;
type B.m : ...;
B.new.m

environment of the conditional is the least upper bound of
the output environments of the branches, defined as (Γ1 ⊔
Γ2 )(x) = Γ1 (x) ⊔ Γ2 (x) if x ∈ dom(Γ1 ) ∧ x ∈ dom(Γ2 )
and (Γ1 ⊔ Γ2 )(x) is undefined otherwise.
Dynamic Semantics. Figure 6 gives a small-step dynamic
semantics for our language. The semantics operates on dynamic configurations of the form hX , TT , DT , E , e, S i.
The first two components are the key novelties to support
run-time static type checking. X is a cache mapping A.m to
the type checking proofs for its method body (more details
below). TT is the type table, which is updated at run time
by calls to type. The last four components are standard. DT
is a dynamic class table mapping A.m to its premethod. E
is the dynamic environment mapping local variables to values. e is the expression being reduced. Lastly, S is a stack
of pairs (E , C), where E is the dynamic environment and C
is the evaluation context (defined in the usual way) at a call
site. The semantics pushes onto the stack at calls and pops
off the stack at returns.
The first seven rules in the semantics are standard. (ESelf)
and (EVar) evaluate self and variables by looking them up in
the environment. (EAssn) binds a variable to a value in the
environment. Notice that, like Ruby, variables can be written
without first declaring them, but it is an error to try to read
a variable that has not been written. (ENew) creates a new
instance. Note that since objects do not have fields, we do not
need a separate heap. (ESeq) discards the left-hand side of a
sequence if it has been fully evaluated. (EIfTrue) reduces to
the true branch if the guard is non-nil, and (EIfFalse) reduces
to the false branch otherwise.
The next four rules are the heart of just-in-time static type
checking. Our goal is to statically type check methods once
at the first call, and then avoid rechecking them unless something has changed. To formalize this notion, we define the

# define B.m
# give B.m a type
# type error ! B.m not in type table

Here we type check A.m’s body at the first call to it, so the
type expression has not been run—and hence has not bound
a type to B.m—yet. Thus it is a type error to invoke B.m in
the method body.
While we could potentially solve this problem with a
more complex type system, in our experience (Section 5) we
have not needed such a feature.
Next, (TApp) types method invocation e0 .m(e1 ), where
we look up the method’s type in TT based on the compiletime type of e0 . (Note that since there is no inheritance, we
need not search the inheritance hierarchy to find the type of
A.m.) Here subtyping is defined as nil ≤ A and A ≤ A for
all A. Thus, as is standard in languages with nil, the type
system may accept a program that invokes a non-existent
method of nil even though this is a run-time error. However,
notice that if e0 evaluates to a non-nil value, then (TApp)
guarantees e0 has method m.
Finally, (TIf) types conditionals. Like Ruby, the guard
e0 may have any type. The type of the conditional is the
least upper bound of the types of the two branches, defined
as A ⊔ A = A and nil ⊔ τ = τ ⊔ nil = τ . The output
5

2016/4/14

hX , TT , DT , E , e, S i → hX ′ , TT ′ , DT ′ , E ′ , e′ , S ′ i
hX , TT , DT , E , E (self), S i
hX , TT , DT , E , E (x), S i
hX , TT , DT , E [x 7→ v], v, S i
hX , TT , DT , E , [A], S i
hX , TT , DT , E , e2 , S i
hX , TT , DT , E , e1 , S i
if v 6= nil
hX , TT , DT , E , e2 , S i
hX \A.m, TT , DT [A.m 7→ λx.e], E , nil, S i
h(X \A.m)[TT ′ ], TT ′ , DT , E , nil, S i
TT ′ = TT [A.m 7→ τm ] and A.m 6∈ TApp(S)
(EAppMiss)
hX , TT , DT , E , C[v1 .m(v2 )], S i → hX ′ , TT , DT , [self 7→ v1 , x 7→ v2 ], e, (E , C) :: S i
if A.m 6∈ dom(X ) and v1 = [A] and DT (A.m) = λx.e and TT (A.m) = τ1 → τ2 and type of(v2 ) ≤ τ1 and
DM = (TT ⊢ h[x 7→ τ1 , self 7→ A], ei ⇒ hΓ′ , τ i) holds and D≤ = (τ ≤ τ2 ) holds and
X ′ = X [A.m 7→ (DM , D≤ )]
(EAppHit)
hX , TT , DT , E , C[v1 .m(v2 )], S i → hX , TT , DT , [self 7→ v1 , x 7→ v2 ], e, (E , C) :: S i
if A.m ∈ dom(X ) and v1 = [A] and DT (A.m) = λx.e and TT (A.m) = τ1 → τ2 and type of(v2 ) ≤ τ1
(ERet)
hX , TT , DT , E ′ , v, (E , C) :: S i → hX , TT , DT , E , C[v], S i
(ESelf)
(EVar)
(EAssn)
(ENew)
(ESeq)
(EIfTrue)
(EIfFalse)
(EDef)
(EType)

hX , TT , DT , E , self, S i
hX , TT , DT , E , x, S i
hX , TT , DT , E , x = v, S i
hX , TT , DT , E , A.new, S i
hX , TT , DT , E , (v; e2 ), S i
hX , TT , DT , E , if v then e1 else e2 , S i
hX , TT , DT , E , if nil then e1 else e2 , S i
hX , TT , DT , E , def A.m = λx.e, S i
hX , TT , DT , E , type A.m : τm , S i

→
→
→
→
→
→
→
→
→

(EContext)
hX , TT , DT , E , e, S i → hX ′ , TT ′ , DT ′ , E ′ , e′ , S ′ i

∄v1 , v2 , e′ . e = (v1 .m(v2 )) ∨ e = v1 ∨ e = C[e′ ]

hX , TT , DT , E , C[e], S i → hX ′ , TT ′ , DT ′ , E ′ , C[e′ ], S ′ i

Figure 6. Dynamic Semantics.
cache X as a map from A.m to a pair of typing derivations
(DM , D≤ ). Here DM is a type checking derivation from Figure 5 for the body of A.m, and D≤ is a subtyping judgment
showing that the type of e is a subtype of the declared return type. We need D≤ because our type system is syntaxdirected and hence does not include a standalone subsumption rule.
(EDef) reduces to nil, updating the dynamic class table
to bind A.m to the given premethod along the way. Recall
that we allow a method to be redefined with def. Hence we
need to invalidate anything in the cache relating to A.m so
that A.m will be checked the next time it is called. More
precisely:

derivations that refer to A.m. Hence, cached type derivations
that use TT can safely use TT ′ . Formally, we define:
Definition 2 (Cache upgrading). We write X [TT ′ ] to indicate a new cache that is the same as X , except the type table
in every derivation is replaced by TT ′ .
Thus, in (EType), the output cache is upgraded to the new
type table after invalidation.
The next two rules use the type cache. Both rules evaluate
a method call in a context, written C[v1 .m(v2 )]; we will
discuss the other rule for contexts shortly. In both rules, the
receiver v1 is a run-time object [A]. (EAppMiss) applies
when A.m is not in the cache. In this case, we look up
the type of A.m in TT , yielding some type τ1 → τ2 . We
type check the method body e in an environment in which
formal variable x is bound to τ1 and self is bound to A,
yielding a derivation DM . We check that the resulting type
τ of e is a subtype of the declared type e2 , with subtyping
derivation D≤ . Finally, we check that the run-time type of
v2 —defined as type of(nil) = nil and type of([A]) = A—
is a subtype of τ1 . If all this holds, then it is type-safe to call
the method. Hence we update the cache with the method’s
typing derivations and start evaluating the method body,
pushing the context C and the environment E on the stack.
(EAppHit) is similar but far simpler. This rule applies
when A.m is in the cache. In this case we know its method
body has been successfully type checked, so we need only
check that the run-time type of v2 is a subtype of the declared domain type of v1 . If so, we allow the method call to
proceed.

Definition 1 (Cache invalidation). We write X \A.m to indicate a new cache that is the same as X , except A.m has
been invalidated, meaning:
1. Any entries with A.m as the key are removed.
2. Any entries with a DM that apply (TApp) with A.m are
removed.
Thus, in (EDef), the output cache is the same as the input
cache but with A.m invalidated.
(EType) also reduces to nil, updating the type table to
be TT ′ , which is the same as TT but with new type information for A.m. As with (EDef), we invalidate A.m in
the cache. However, there is a another subtlety. Recall that
cached typing derivations DM include the type table TT .
This is potentially problematic, because we are changing the
type table to TT ′ . However, cache invalidation removes any
6

2016/4/14

However a method is called, the return, handled by
(ERet), is the same. This rule applies when an expression
has been fully evaluated and is at the top level. In this case,
we pop the stack, replacing E ′ with E from the stack and
plugging the value v into the context C from the stack.
Finally, (EContext) takes a step in an subexpression inside a context C. This rule only applies if the subexpression
is not a method call (since that case is handled by (EApp*),
which must push the context on the stack) and not a fully
evaluated value (which is handled by (ERet), which must
pop the context from the stack). We also do not allow the
subexpression to itself be a context, since that could cause
(EApp*) and (ERet) to misbehave.

RDL and Type Checking. Like standard RDL, Hummingbird’s type annotation stores type information in a map and
wraps the associated method to intercept calls to it. We
should emphasize that RDL does not perform any static
checking on its own—rather, it solely enforces contracts
dynamically. In Hummingbird, when a wrapped method is
called, Hummingbird first checks to see if it has already been
type checked. If not, Hummingbird retrieves the method’s
CFG and type and then statically checks that the CFG
matches the given type.
Hummingbird uses RDL’s type language, which includes
nominal types, intersection types, union types, optional
and variable length arguments, block (higher-order method)
types, singleton types, structural types, a self type, generics,
and types for heterogenous arrays and hashes. Hummingbird supports all of these kinds of types except structural
types, self types, heterogeneous collections, and some variable length arguments. In addition, Hummingbird adds support for both instance field types (as seen in Figure 3) and
class field types.
There is one slight subtlety in handling union types: If in
a method call the receiver has a union type, Hummingbird
performs type checking once for each arm of the union and
the unions the possible return types. For example if in call
e.m(. . .) the receiver has type A ∪ B, then Hummingbird
checks the call assuming e has type of A.m, yielding a return
type τA ; checks the call assuming B.m, yielding return type
τB ; and then sets the call’s return type to τA ∪ τB .

Soundness. Our type system forbids invoking non-existent
methods of objects. However, there are three kinds of errors
the type system does not prevent: invoking a method on nil;
calling a method whose body does not type check at run
time; and calling a method that has a type signature but is
itself undefined. (We could prevent the latter error by adding
a side condition to (TApp) that requires the method to be
defined, but we opt not to to keep the formalism slightly
simpler.) To state a soundness theorem, we need to account
for these cases, which we do by extending the dynamic
semantics with rules that reduce to blame in these three
cases. After doing so, we can state soundness:
Theorem 1 (Soundness). If ∅ ⊢ h∅, ei ⇒ hΓ′ , τ i then either
e reduces to a value, e reduces to blame, or e diverges.
We show soundness using a standard progress and preservation approach. The key technical challenge is preservation,
in which we need to show that not only are expression types
preserved, but also the validity of the cache and types of contexts pushed on the stack. The proof can be found in Appendix A.

Hummingbird is implemented using a combination of Ruby
and OCaml. On the OCaml side, we use the Ruby Intermediate Language (RIL) [13] to parse input Ruby files and translate them to control-flow graphs (CFG) on which we perform type checking. On the Ruby side, we extend RDL [26],
a contract system for Ruby, to perform static type checking. We next discuss the major challenges of implementing
Hummingbird.

Eliminating Dynamic Checks. Recall the (EApp*) rules
dynamically check that a method’s actual arguments have
the expected types before calling a statically typed method.
This check ensures that an untrusted caller cannot violate
the assumptions of a method. However, observe that if the
immediate caller is itself statically checked, then we know
the arguments are type-safe. Thus, as a performance optimization, Hummingbird only dynamically checks arguments
of statically typed methods if the caller is itself not statically checked. As a further optimization, Hummingbird also
does not dynamically check calls from Ruby standard library methods or the Rails framework, which are assumed to
be type-safe. The one exception is that Hummingbird does
dynamically check types for the Rails params hash, since
those values come from the user’s browser and hence are
untrusted.

RIL. RIL is essentially the front-end of Diamondback
Ruby (DRuby) [8, 14]. Given an input Ruby program, RIL
produces a CFG that simplifies away many of the tedious
features of Ruby, e.g., multiple forms of conditionals. We
modified DRuby so it emits the RIL CFG as a JSON file and
then exits. When loading each application file at run-time,
we read the corresponding JSON file and store a mapping
from class and method names and positions (file and line
number) to the JSON CFG. At run-time we look up CFGs in
this map to perform static type checking.

Numeric Hierarchy. Ruby has a Numeric tower that provides several related types for numbers. For example, Fixnum
< Integer < Numeric and Bignum < Integer < Numeric.
Adding two Fixnums normally results in another Fixnum,
but adding two large Fixnums could result in a Bignum in
the case of numeric overflow. To keep the type checking system simple, Hummingbird omits the special overflow case
and does not take Bignum into consideration. (This could
be addressed by enriching the type system [32].) Numeric
overflow does not occur in our experiments.

4. Implementation

7

2016/4/14

Code Blocks. As mentioned earlier, Ruby code blocks are
anonymous functions delimited by do. . .end. Hummingbird
allows methods that take code block arguments to be annotated with the block’s type. For example:

Second, by default Hummingbird gives instances of
generic classes their “raw” type with no type parameters.
To add parameters, we use type casts, as in the following
code:

type :m, ”() { (T) → U } → nil ”

a = []
# a has type Array
a. rdl cast (”Array<Fixnum>”) # cast to Array<Fixnum>
a.push(0)
# ok
a.push(”str”)
# type error due to cast

indicates that m takes no regular arguments; one code block
argument where the block takes type T and returns type U;
and m itself returns type nil.
There are two cases involving code blocks that we need
to consider. First, suppose Hummingbird is statically type
checking a call m() do |x| body end, and m has the type
given just above. Then at the call, Hummingbird statically
checks that the code block argument matches the expected
type, i.e., assuming x has type T, then body must produce
a value of type U. Second, when statically type checking m
itself, Hummingbird should check that calls to the block are
type correct. Currently this second case is unimplemented as
it does not arise in our experiments.
Recall from above that Hummingbird sometimes needs
to dynamically check the arguments to a statically typed
method. While this test is easy to do for objects, it is hard
to do for code blocks, which would require higher-order
contracts [11]. Currently Hummingbird does not implement
this higher order check, and simply assumes code block
arguments are type safe. Also, Hummingbird currently assumes the self inside a code block is the same as in the
enclosing method body. This assumption holds in our experiments, but it can be violated using instance eval and instance exec [33]. In the future, we plan to address this limitation by allowing the programmer to annotate the self type
of code blocks.

Here without the type annotation the last line would succeed;
with the annotation it triggers a type error. Note that when
casting an array or hash to a generic type, rdl cast iterates
through the elements to ensure they have the given type.
Modules. Ruby supports mixins via modules, which are
collections of methods that can be added to a class. Recall
that Hummingbird caches which methods have been statically type checked. Because a module can be mixed in to
multiple different classes—and can actually have different
types in those different classes—we need to be careful that
module method type checks are cached via where they are
mixed in rather than via the module name.
For example, consider the following code, where the
method foo defined in module M calls bar, which may vary
depending on where M is mixed in:
1
2
3

module M def foo(x) bar(x) end end
class C; include M; def bar(x) x + 1 end end
class D; include M; def bar(x) x. to s end end

Here method foo returns Fixnum when mixed into C and
String when mixed into D. Thus, rather that track the type
checking status of M#foo, Hummingbird separately tracks
the statuses of C#foo and D#foo.

Type Casts. While Hummingbird’s type system is quite
powerful, it cannot type check every Ruby program, and thus
in some cases we need to insert type casts. Hummingbird
includes a method o.rdl cast(t) that casts o’s type to t. After
such a call, Hummingbird assumes that o has type t. At runtime, the call dynamically checks that o has the given type.
In our experience, type casts have two main uses. First,
sometimes program logic dictates that we can safely downcast an object. For example, consider the following method
from one of our experiments:

Cache Invalidation. Recall from Section 3 that Hummingbird needs to invalidate portions of the cache when certain
typing assumptions change. While Hummingbird currently
does not support cache invalidation in general, it does support one important case. In Rails development mode, Rails
automatically reloads modified files without restarting, thus
redefining the methods in those files but leaving other methods intact [24]. In Rails development mode, Hummingbird
intercepts the Rails reloading process and performs appropriate cache invalidation. More specifically, when a method
is called, if there is a difference between its new and old
method body (which we check using the RIL CFGs), we invalidate the method and any methods that depend on it. We
also maintain a list of methods defined in each class, and
when a class is reloaded we invalidate dependencies of any
method that has been removed. In the next section, we report
on an experiment running a Rails app under Hummingbird
as it is updated.
We plan to add more general support for cache invalidation in future work. There are two main cases to consider.
The first is when a method is redefined or is removed (which

def self . load cache
f = datafile path ([‘‘ cache’ ’ , ‘‘ countries ’ ’ ])
t = Marshal.load( File . binread(f ))
@@cache ||= t. rdl cast (‘‘ Hash<String, %any>’’)
end

Marshal.load returns the result of converting its serialized
data argument into a Ruby object of arbitray type. However,
in our example, the argument passed to Marshal.load is
always an application data file that will be converted to the
annotated Hash.
8

2016/4/14

never happens in our experiments except in Rails development mode). Ruby provides two methods, method added
and method removed, that can be used to register callbacks
when the corresponding actions occur, which could be used
for cache invalidation.
The second case of cache invalidation is method’s type
changes. However, in RDL and Hummingbird, multiple calls
to type for the same method are used to create intersection
types. For example, the core library Array#[] method is
given its type with the following code:
1
2
3

which is an industrial strength web app framework that is
widely deployed; the next two use various metaprogramming styles in different ways than Rails; and the last one
does not use metaprogramming, as a baseline.
Table 1 summarizes the results of applying Hummingbird
to these apps. On the left we list the app name, version number or date (if no version number is available), and lines of
code as measured with sloccount [40]. For the Rails apps,
we ran sloccount on all ruby files in the model, controller,
helper, and mailer directories. We do not include lines of
code for views, as we do not type check views. For Countries and CCT, we ran sloccount on all files in the lib directory. For Rolify, we only statically checked several methods,
comprising 84 lines of code, that use define method in an
interesting way.

type Array, :[], ’ (Fixnum or Float) → t ’
type Array, :[], ’ (Fixnum, Fixnum) →Array<t>’
type Array, :[], ’ (Range<Fixnum>) →Array<t>’

meaning if given a Fixnum or Float, method Array#[] returns the array contents type; and, if given a pair of Fixnums
or a Range<Fixnum>, it returns an array.
In this setting, we cannot easily distinguish adding a
new arm of an intersection type from replacing a method
type. Moreover, adding a new arm to an intersection type
should not invalidate the cache, since the other arms are
still in effect. Thus, full support of cache invalidation will
likely require an explicit mechanism for replacing earlier
type definitions.

Type Annotations. For all apps, we used common type annotations from RDL for the Ruby core and standard libraries.
For several apps, we also added type annotations for thirdparty libraries and for the Rails framework. We trusted the
annotations for all these libraries, i.e., we did not statically
type check the library methods’ bodies.
We also added code to dynamically generate types for
metaprogramming code. For Rails, we added code to dynamically generate types for model getters and setters based
on the database schema; for finder methods such as find by name and find all by password (the method name indicates which field is being searched); and for Rails associations such as belongs to.
In Figure 2, we showed code we added to Rolify to generate types for a method created by calling define dynamic method. Calling define dynamic method also dynamically creates another method, is #{role name} of(arg)?,
which we also provide types for in the pre block.
In CCT, we used the code in Figure 3 to generate types
for Struct getters and setters.
Finally, we wrote type annotations for the app’s own
methods that were included in the lines of code count in Table 1. We marked those methods to indicate Hummingbird
should statically type check their bodies. Developing these
annotations was fairly straightforward, especially since we
could quickly detect incorrect annotations by running Hummingbird.

5. Experiments
We evaluated Hummingbird by applying it to six Ruby apps:
• Talks3 is a Rails app, written by the second author, for

publicizing talk announcements. Talks has been in use in
the UMD CS department since February 2012.
• Boxroom4 is a Rails implementation of a simple file shar-

ing interface.
• Pubs is a Rails app, developed several years ago by the

second author, for managing lists of publications.
• Rolify5 is a role management library for Rails. For this

evaluation, we integrated Rolify with Talks on the User
resource.
• Credit Card Transactions (CCT)6 is a library that per-

forms simple credit card processing tasks.
• Countries7 is an app that provides useful data about each

country.

Type Checking Results. For each program, we performed
type checking while running unit tests that exercised all the
type-annotated app methods. For Talks and Pubs, we wrote
unit tests with the goal of covering all application methods.
For Boxroom, we used its unit tests on models but wrote our
own unit tests on controllers, since it did not have controller
tests. For Rolify, we wrote a small set of unit tests for the
dynamic method definition feature. For CCT and Countries,
we used the unit tests that came with those apps.
In all cases, the app methods type check correctly in
Hummingbird; there were no type errors. The middle group
of columns summarizes more detailed type checking data.

We selected these apps for variety rather than for being representative. We chose these apps because their source code
is publicly available (except Pubs); they work with the latest versions of Ruby and RDL; and they do not rely heavily
on other packages. Moreover, the first three apps use Rails,
3 https://github.com/jeffrey-s-foster/talks
4 http://boxroomapp.com
5 https://github.com/RolifyCommunity/rolify
6 https://github.com/daino3/credit_card_transactions
7 https://github.com/hexorx/countries

9

2016/4/14

App
Talks-1/4/2013
Boxroom-1.7.1
Pubs-1/12/2015
Rolify-4.0.0
CCT-3/23/2014
Countries-1.1.0

LoC
1,055
854
620
84
172
227

Static types
Chk’d App All
111
201 363
127
221 306
47
86 171
14
24
71
23
27
75
33
40 111

Dynamic types
Gen’d
Used
990
45
534
93
445
33
26
2
6
3
0
0

Casts
31
17
13
15
6
22

Phs
1
1
1
12
1
1

Orig
162
263
72.0
5.63
3.06
1.02

Running time (s)
No$ Hum Or. Ratio
1,590
256
1.6×
705
327
1.2×
4,470
217
3.0×
7.79
6.71
1.2×
78.2
17.4
5.7×
18.1
4.62
4.5×

Table 1. Type checking results.
which dynamically defines other methods and adds their
type annotations. The other phases come from the order
in which the type annotation files are required—unlike the
Rails apps, the Rolify type annotation files are loaded piecemeal as the application loads.

The “Static types” columns report data on static type
annotations. The count under “Chk’d” is the number of type
annotations for the app’s methods whose bodies we statically
type checked. The count under “App” is that number plus
the number of types for app-specific methods with (trusted)
static type annotations, e.g., some Rails helper functions
have types that we do not currently dynamically generate.
The count under “All” reports the total number of static
type annotations we used in type checking each app. This
includes the “App” count plus standard, core, and third-party
library type annotations for methods referred to in the app.
The “Dynamic types” columns report the number of types
that were dynamically generated (“Gen’d”) and the number of those that were actually used during type checking
(“Used”). These numbers differ because we tried to make the
dynamic type information general rather than app-specific,
e.g., we generate both the getter and setter for belongs to
even if only one is used by the app.
These results show that having types for methods generated by metaprogramming is critical for successfully typing these programs—every app except Countries requires at
least a few, and sometimes many, such types.
The “Casts” column reports the number of type casts we
needed to make these programs type check; essentially this
measures how often Hummingbird’s type system is overly
conservative. The results show we needed a non-trivial but
relatively small number of casts. All casts were for the reasons discussed in Section 4: downcasting and generics.
The “Phs” column in Table 1 shows the number of type
checking phases under Hummingbird. Here a phase is defined as a sequence of type annotation calls with no intervening static type checks, followed by a sequence of static
type checks with no intervening annotations. We can see that
almost all apps have only a single phase, where the type annotations are executed before any static type checks. Investigating further, we found this is due to the way we added
annotations. For example, we set up our Rails apps so the
first loaded application file in turn loads all type annotation files. In practice the type annotations would likely be
spread throughout the app’s files, thus increasing the number of phases.
Rolify is the only application with multiple phases. Most
of the phases come from calling define dynamic method,

Performance. The last four columns of Table 1 report
the overhead of using Hummingbird. The “Orig” column
shows the running time without Hummingbird. The next
two columns report the running time with Hummingbird,
with caching disabled (“No$”) and enabled (“Hum”). The
last column lists the ratio of the “Hum” and “Orig” column.
For Talks, Boxroom, and Pubs, we measured the running
time of a client script that uses curl to connect to the web
server and exercise a wide range of functionality. For CCT,
we measured the time for running its unit tests 100 times. For
Countries and Rolify, we measured the time for running the
unit tests once (since these take take much more time than
CCT’s tests). For all apps, we performed each measurement
three times and took the arithmetic mean.
These results show that for the Rails apps, where IO
is significant, Hummingbird slows down performance from
24% to 201% (with caching enabled). We think these are
reasonable results for an early prototype that we have not
spent much effort optimizing. Moreover, across all apps, the
ratios are significantly better than prior systems that mix
static and dynamic typing for Ruby [18, 27], which report
orders of magnitude slowdowns.
Investigating further, we found that the main Hummingbird overhead arises from intercepting method calls to statically type checked methods. (Note the interception happens
regardless of the cache state.) The higher slowdowns for
CCT and Countries occur because those applications spend
much of their time in code with intercepted calls, while the
other applications spend most of their time in framework
code, whose methods are not intercepted. We expect performance can be improved with further engineering effort.
We can also see from the results that caching is an important performance optimization: without caching, performance slows down 1.4× to 62×. We investigated pubs, the
app with the highest no-caching slowdown, and found that
while running the application with large array inputs, certain
application methods are called more than 13,000 times while
10

2016/4/14

iterating through the large arrays. This means that each of
these application methods are statically type checked more
than 13,000 times when caching is disabled.

Version
5/14/12
7/24/12
8/24/12-1
8/24/12-2
8/24/12-3
9/14/12
1/4/13

Type Errors in Talks. We downloaded many earlier versions of Talks from its github repository and ran Hummingbird on them using mostly the same type annotations as
for the latest version, changed as necessary due to program
changes. Cumulatively, we found six type errors that were
introduced and later removed as Talks evolved. Below the
number after the date indicates which checkin it was, with 1
for the first checkin of the day, 2 for the second, etc.

∆ Meth
N/A
1
8
1
1
4

Added
N/A
2
1
1
-

Deps
N/A
4
8
-

Chk’d
77
15 / 5
24 / 14
11 / 1
12 / 2
15 / 1
13 / 4

Table 2. Talks Update Results

• 1/8/12-4: This version misspells compute edit fields as

time of Talks in Table 1 ; update the code to the next version;
and repeat.
Table 2 shows the results of our experiment. The “∆
Meth” column lists the number of methods whose bodies or
types were changed compared to the previous version. Note
there are no removed methods in any of these versions. The
“Added” column lists the number of methods added; such
methods will be checked when they are called for the first
time but do not cause any cache invalidations. The “Deps”
column counts the number of dependent methods that call
one or more of the changed methods. These methods plus the
changed methods are those whose previous static type check
are invalidated by the update. The last column, “Chk’d,” reports how many methods are newly or re-type checked after
the update. Currently, Hummingbird always rechecks Rails
helper methods, due to a quirk in the Rails implementation—
the helper methods’ classes get a new name each time the
helper file is reloaded, causing Hummingbird to treat their
methods as new. Thus (except for the first line, since this issue does not arise on the first run), we list two numbers in
the column: the first with all rechecks, including the helper
methods, and the second excluding the helper methods.
These results show that in almost all cases, the second
number in “Chk’d” is equal to the sum of the three previous columns. There is one exception: in 8/24/12/-1, there 14
rechecked methods but 18 changed/added/dependent methods. We investigated and found that the 14 rechecks are composed of six changed methods that are rechecked once; two
changed methods that are rechecked twice because they have
dependencies whose updates are interleaved with calls to
those methods; one added method that is checked; and three
dependent methods that are rechecked. The remaining added
method is not called by the curl scripts, and the remaining
dependent methods are also changed methods (this is the
only version where there is overlap between the changed and
dependent columns).
Finally, as there are no type errors in this sequence of
updates, we confirmed that this streak of updates type checks
under Hummingbird.

copute edit fields. Hummingbird reported this error because the latter was an unbound local variable and was
also not a valid method.
• 1/7/12-5: Instead of calling @list.talks.upcoming.sort{|

a, b | ...}, this version calls @list.talks.upcoming{| a,
b | ...} (leaving off the sort). Hummingbird detects this
error because upcoming’s type indicates it does not take
a block. Interestingly, this error would not be detected
at run-time by Ruby, which simply ignores unused block
arguments.
• 1/26/12-3: This version calls user.subscribed talks(true),

but subscribed talks’s argument is a Symbol.
• 1/28/12: This version calls @job.handler.object, but

@job.handler returns a String, which does not have an
object method.
• 2/6/12-2: This version uses undefined variable old talk.

Thus, Hummingbird assumes old talk is a no-argument
method and attempts to look up its type, which does not
exist.
• 2/6/12-3: This version uses undefined variable new talk

We should emphasize that although we expected there
would be type errors in Talks, we did not know exactly what
they were or what versions they were in. While the second
author did write Talks, the errors were made a long time ago,
and the second author rediscovered them independently by
running Hummingbird.
Updates to Talks Finally, we performed an experiment in
which we launched one version of Talks in Rails development mode and then updated the code to the next six consecutive versions of the app. (We skipped versions in which
none of the Ruby application files changed) Notice that
cache invalidation is particular useful here, since in typical
usage only a small number of methods are changed by each
update.
In more detail, after launching the initial version of the
app, we repeated the following sequence six times: Reset
the database (so that we run all versions with the same initial
data); run a sequence of curl commands that access the same
Talks functionalities as the ones used to measure the running

6. Related Work
There are several threads of related work.
11

2016/4/14

Type Systems for Ruby. We have developed several prior
type systems for Ruby. Diamondback Ruby (DRuby) [14]
is the first comprehensive type system for Ruby that we are
aware of. Because Hummingbird checks types at run-time,
we opted to implement our own type checker rather than
reuse DRuby for type checking, which would have required
some awkward shuffling of the type table between Ruby and
OCaml. Another reason to reimplement type checking was
to keep the type system a little easier to understand—DRuby
performs type inference, which is quite complex for this type
language, in contrast to Hummingbird, which implements
much simpler type checking.
DRuby was effective but did not handle highly dynamic
language constructs well. PRuby [12] solves this problem
using profile-based type inference. To use PRuby, the developer runs the program once to record dynamic behavior, e.g.,
what methods are invoked via send, what strings are passed
to eval, etc. PRuby then applies DRuby to the original program text plus the profiled strings, e.g., any string that was
passed to eval is parsed and analyzed like any other code.
While PRuby can be effective, we think that Hummingbird’s
approach is ultimately more practical because Hummingbird
does not require a separate, potentially cumbersome, profiling phase. We note that Hummingbird does not currently
handle eval, because it was not used in our subject apps’
code, but it could be supported in a straightforward way.
We also developed DRails [17], which type checks Rails
apps by applying DRuby to translated Rails code. For example, if DRails sees a call to belongs to, it outputs Ruby
code that explicitly contains the methods generated from the
call, which DRuby can then analyze. While DRails was applied to a range of programs, its analysis is quite brittle.
Supporting each additional Rails feature in DRails requires
implementing, in OCaml, a source-to-source transformation
that mimics that feature. This is a huge effort and is hard
to sustain as Rails evolves. In contrast, Hummingbird types
are generated in Ruby, which is far easier. DRails is also
complex to use: The program is combined into one file, then
run to gather profile information, then transformed and type
checked. Using Hummingbird is far simpler. Finally, DRails
is Rails-specific, whereas Hummingbird applies readily to
other Ruby frameworks. Due to all these issues, we feel
Hummingbird is much more lightweight, agile, scalable, and
maintainable than DRails.
Finally, RubyDust [18] implements type inference for
Ruby at run time. RubyDust works by wrapping objects
to annotate them with type variables. More precisely, consider a method def m(x) ... end, and let α be the type variable for x. RubyDust’s wrapping is approximately equal to
adding x = Wrap.new(x, α) to the beginning of m. Uses of
the wrapped x generate type constraints on α and then delegate to the underlying object. The Ruby Type Checker [27]
(rtc) is similar but implements type checking rather than type
inference.

Hummingbird has several important advantages over
RubyDust and rtc. First, RubyDust and rtc can only report
errors on program paths they observe. In contrast, Hummingbird type checks all paths through methods it analyzes.
Second, wrapping every object with a type annotation is
extremely expensive. By doing static analysis, Hummingbird avoids this overhead. Finally, RubyDust and rtc have
no special support for metaprogramming. In RubyDust, dynamically created methods could have their types inferred
in a standard way, though RubyDust would likely not infer useful types for Rails-created methods. In rtc, dynamically created methods would lack types, so their use would
not be checked. (Note that it would certainly be possible
to add Hummingbird-style support for metaprogramminggenerated type annotations to either RubyDust or rtc.) In
sum, we think that Hummingbird strikes the right compromise between the purely static DRuby approach and the
purely dynamic RubyDust/rtc approach.
Type Systems for Other Dynamic Languages. Many researchers have proposed type systems for dynamic languages, including Python [3], JavaScript [2, 21, 36], Racket [32,
37, 38], and Lua [22], or developed new dynamic languages
or dialects with special type systems, such as Thorn [5],
TypeScript [4, 25], and Dart [7]. To our knowledge, these
type systems are focused on checking the core language and
can have difficulty in the face of metaprogramming.
One exception is RPython [1], which introduces a notion
of load time, during which highly dynamic features may
be used, and run time, when they may not be. In contrast,
Hummingbird does not need such a separation.
Lerner et al [20] propose a system for type checking
programs that use JQuery, a very sophisticated Javascript
framework. The proposed type system has special support
for JQuery’s abstractions, making it quite effective in that
domain. On the other hand, it does not easily apply to other
frameworks.
Feldthaus et al’s TSCHECK [10] is a tool to check the
correctness of TypeScript interfaces for JavaScript libraries.
TSCHECK discovers a library’s API by taking a snapshot
after executing the library’s top-level code. It then performs
checking using a separate static analysis. This is similar
to Hummingbird’s tracking of type information at run-time
and then performing static checking based on it. However,
Hummingbird allows type information to be generated at any
time and not just in top-level code.
Related Uses of Caching. Several researchers have proposed systems that use caching in a way related to Hummingbird. Koukoutos et al [19] reduce the overhead of
checking data structure contracts (e.g., “this is a binary
search tree”) at run time by modifying nodes to hold key verification properties. This essentially caches those properties.
However, because the properties are complex, the process of
caching them is not automated.
12

2016/4/14

Stulova et al [34] propose memoizing run-time assertion
checking to improve performance. This is similar to Hummingbird’s type check caching, but much more sophisticated
because the cached assertions arise from a rich logic.
Hermenegildo et al [16] proposed a method to incrementally update analysis results at run-time as code is added,
deleted, or changed. Their analysis algorithms are designed
for constraint logic programming languages, and are much
more complicated than Hummingbird’s type checking.

l/F# features and Hummingbird is that Ruby does not have a
separate compile time.

7. Conclusion
We presented Hummingbird, a novel tool that type checks
Ruby apps using an approach we call just-in-time static type
checking. Hummingbird works by tracking type information
dynamically, but then checking method bodies statically at
run time as each method is called. As long as any metaprogramming code is extended to generate types as it creates
methods, Hummingbird will, in a very natural way, be able
to check code that uses the generated methods. Furthermore,
Hummingbird can cache type checking so it need not be unnecessarily repeated at later calls to the same method.
We formalized Hummingbird using a core, Ruby-like language that allows methods and their types to be defined at
arbitrary (and arbitrarily separate) points during execution,
and we proved type soundness. We implemented Hummingbird on top of RIL, for parsing Ruby source code, and RDL,
for intercepting method calls and storing type information.
We applied Hummingbird to six Ruby apps, some of which
use Rails. We found that Hummingbird’s approach is effective, allowing it to successfully type check all the apps even
in the presence of metaprogramming. We ran Hummingbird
on earlier versions of one app and found several type errors. Furthermore, we ran Hummingbird while applying a
sequence of updates to a Rails app in development mode
to demonstrate cache invalidation under Hummingbird. Finally, we measured Hummingbird’s run-time overhead and
found it is reasonable.
In sum, we think that Hummingbird takes a strong step
forward in bringing static typing to dynamic languages.

Staged Analysis. MetaOCaml [35] is a multi-stage extension of OCaml in which code is compiled in one stage and
executed in a later stage. The MetaOCaml compiler performs static type checking on any such delayed code, which
is similar to Hummingbird’s just-in-time type checking. A
key difference between MetaOCaml and Hummingbird is
that Ruby programs do not have clearly delineated stages.
Chugh et al’s staged program analysis [6] performs static
analysis on as much code as is possible at compile time, and
then computes a set of remaining checks to be performed at
run time. Hummingbird uses a related idea in which no static
analysis is performed at compile time, but type checking is
always done when methods are called. Hummingbird is simpler because it need not compute which checks are necessary, as it always does the same kind of checking.
Other. Several researchers have explored other ways to
bring the benefits of static typing to dynamic languages.
Contracts [11] check assertions at function or method entry
and exit. In contrast, Hummingbird performs static analysis
of method bodies, which can find bugs on paths before
they are run. At the same time, contracts can encode richer
properties than types.
Gradual typing [31] lets developers add types gradually
as programs evolve; Vitousek et al recently implemented
gradual typing for Python [39]. Like types [41] bring some
of the flexibility of dynamic typing to statically typed languages. The goal of these systems is to allow mixing of
typed and untyped code. This is orthogonal to Hummingbird, which focuses on checking code with type annotations.
Richards et al [28, 29] have explored how highly dynamic
language features are used in JavaScript. They find such features, including eval, are used extensively in a wide variety
of ways, including supporting metaprogramming.
The GHC Haskell compiler lets developers defer type errors until run-time to suppress type errors on code that is
never actually executed [15]. Hummingbird provides related
behavior in that a method that is never called will never be
type checked by Hummingbird. Template Haskell [30] can
be used for compile-time metaprogramming. Since Haskell
programs contain types, template Haskell is often used to
generate type annotations, analogously to the type annotations generated using Hummingbird. Similarly, F# type
providers [9] allow users to create compile time types, properties and methods. A key difference between these Haskel-

Acknowledgments
Thanks to ThanhVu Nguyen and the anonymous reviewers
for their helpful comments. This research was supported in
part by NSF CCF-1319666 and Subcontract to Northeastern
University, NSF CCF-1518844.

References
[1] D. Ancona, M. Ancona, A. Cuni, and N. D. Matsakis.
RPython: A Step Towards Reconciling Dynamically and Statically Typed OO Languages. In Proceedings of the 2007 Symposium on Dynamic Languages, DLS ’07, pages 53–64, New
York, NY, USA, 2007. ACM. ISBN 978-1-59593-868-8. doi:
10.1145/1297081.1297091.
[2] C. Anderson, P. Giannini, and S. Drossopoulou. Towards Type
Inference for Javascript. In Proceedings of the 19th European
Conference on Object-Oriented Programming, ECOOP’05,
pages 428–452, Berlin, Heidelberg, 2005. Springer-Verlag.
ISBN 3-540-27992-X, 978-3-540-27992-1. doi: 10.1007/
11531142 19.
[3] J. Aycock. Aggressive Type Inference.
Python Conference, 2000.

13

In International

2016/4/14

ence on Automated Software Engineering (ASE), pages 590–
594, Auckland, New Zealand, November 2009. Short paper.

[4] G. Bierman, M. Abadi, and M. Torgersen. Understanding
typescript. In ECOOP 2014–Object-Oriented Programming,
pages 257–281. Springer, 2014.

[18] J. hoon (David) An, A. Chaudhuri, J. S. Foster, and M. Hicks.
Dynamic Inference of Static Types for Ruby. In ACM
SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL), pages 459–472, Austin, TX, USA,
January 2011. doi: 10.1145/1926385.1926437.

[5] B. Bloom, J. Field, N. Nystrom, J. Östlund, G. Richards,
R. Strniša, J. Vitek, and T. Wrigstad. Thorn: Robust, Concurrent, Extensible Scripting on the JVM. In Proceedings of
the 24th ACM SIGPLAN Conference on Object Oriented Programming Systems Languages and Applications, OOPSLA
’09, pages 117–136, New York, NY, USA, 2009. ACM. ISBN
978-1-60558-766-0. doi: 10.1145/1640089.1640098.

[19] E. Koukoutos and V. Kuncak. Checking Data Structure Properties Orders of Magnitude Faster. In Runtime Verification 5th International Conference, RV 2014, Toronto, ON, Canada,
September 22-25, 2014. Proceedings, pages 263–268, 2014.
doi: 10.1007/978-3-319-11164-3 22.

[6] R. Chugh, J. A. Meister, R. Jhala, and S. Lerner. Staged Information Flow for Javascript. In Proceedings of the 30th
ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI ’09, pages 50–62, New York,
NY, USA, 2009. ACM. ISBN 978-1-60558-392-1. doi:
10.1145/1542476.1542483.

[20] B. S. Lerner, L. Elberty, J. Li, and S. Krishnamurthi. Combining Form and Function: Static Types for JQuery Programs.
In Proceedings of the 27th European Conference on ObjectOriented Programming, ECOOP’13, pages 79–103, Berlin,
Heidelberg, 2013. Springer-Verlag. ISBN 978-3-642-390371. doi: 10.1007/978-3-642-39038-8 4.

[7] Dart. Dart, 2015. https://www.dartlang.org.
[8] DRuby.
Diamondback
Ruby,
http://www.cs.umd.edu/projects/PL/druby/.

2009.

[21] B. S. Lerner, J. G. Politz, A. Guha, and S. Krishnamurthi. TeJaS: Retrofitting Type Systems for JavaScript. In Proceedings
[9] F
#.
Type
Provider,
2016.
https://msdn.microsoft.com/en-us/library/hh156509.aspx. of the 9th Symposium on Dynamic Languages, DLS ’13, pages
1–16, New York, NY, USA, 2013. ACM. ISBN 978-1-4503[10] A. Feldthaus and A. Møller. Checking Correctness of Type2433-5. doi: 10.1145/2508168.2508170.
Script Interfaces for JavaScript Libraries. In Proceedings of
[22] A. M. Maidl, F. Mascarenhas, and R. Ierusalimschy. Typed
the 2014 ACM International Conference on Object Oriented
Lua: An Optional Type System for Lua. In Proceedings of the
Programming Systems Languages & Applications, OOPSLA
Workshop on Dynamic Languages and Applications, Dyla’14,
’14, pages 1–16, New York, NY, USA, 2014. ACM. ISBN
pages 3:1–3:10, New York, NY, USA, 2014. ACM. ISBN
978-1-4503-2585-1. doi: 10.1145/2660193.2660215.
978-1-4503-2916-3. doi: 10.1145/2617548.2617553.
[11] R. B. Findler and M. Felleisen. Contracts for Higher-order
[23]
RailsGuides.
Active Record Associations, 2015.
Functions. In Proceedings of the Seventh ACM SIGPLAN
http://guides.rubyonrails.org/association_basics.html.
International Conference on Functional Programming, ICFP
’02, pages 48–59, New York, NY, USA, 2002. ACM. ISBN
[24] RailsGuides. Autoloading and Reloading Constants, 2016.
1-58113-487-8. doi: 10.1145/581478.581484.
http://guides.rubyonrails.org/autoloading_and_reloading_con
[12] M. Furr, J. hoon (David) An, and J. S. Foster. Profile[25] A. Rastogi, N. Swamy, C. Fournet, G. Bierman, and P. Vekris.
Guided Static Typing for Dynamic Scripting Languages. In
Safe & Efficient Gradual Typing for TypeScript. In ProceedACM SIGPLAN International Conference on Object-Oriented
ings of the 42Nd Annual ACM SIGPLAN-SIGACT Symposium
Programming, Systems, Languages and Applications (OOPon Principles of Programming Languages, POPL ’15, pages
SLA), pages 283–300, Orlando, Floria, October 2009. doi:
167–180, New York, NY, USA, 2015. ACM. ISBN 978-110.1145/1639949.1640110. Best student paper award.
4503-3300-9. doi: 10.1145/2676726.2676971.
[13] M. Furr, J. hoon (David) An, J. S. Foster, and M. Hicks.
[26] RDL. RDL, 2015. https://github.com/plum-umd/rdl.
The Ruby Intermediate Langauge. In Dynamic Languages
[27] B. M. Ren, J. Toman, T. S. Strickland, and J. S. Foster.
Symposium (DLS), pages 89–98, Orlando, Florida, October
The Ruby Type Checker. In Object-Oriented Program Lan2009. doi: 10.1145/1837513.1640148.
guages and Systems (OOPS) Track at ACM Symposium on
[14] M. Furr, J. hoon (David) An, J. S. Foster, and M. Hicks.
Applied Computing, pages 1565–1572, Coimbra, Portugal,
Static Type Inference for Ruby. In Object-Oriented Program
March 2013. doi: 10.1145/2480362.2480655.
Languages and Systems (OOPS) Track at ACM Symposium
[28] G. Richards, S. Lebresne, B. Burg, and J. Vitek. An Analysis
on Applied Computing (SAC), pages 1859–1866, Honolulu,
of the Dynamic Behavior of JavaScript Programs. In ProceedHawaii, March 2009. doi: 10.1145/1529282.1529700.
ings of the 31st ACM SIGPLAN Conference on Programming
[15] GHCLanguageFeatures.
DeferLanguage Design and Implementation, PLDI ’10, pages 1–12,
ring
Type
Errors
to
Runtime,
2016.
New York, NY, USA, 2010. ACM. ISBN 978-1-4503-0019-3.
https://downloads.haskell.org/~ ghc/latest/docs/html/users_guide/defer-type-errors.html.
doi: 10.1145/1806596.1806598.
[16] M. Hermenegildo, G. Puebla, K. Marriott, and P. J. Stuckey.
[29] G. Richards, C. Hammer, B. Burg, and J. Vitek. The Eval That
Incremental Analysis of Constraint Logic Programs. ACM
Men Do: A Large-scale Study of the Use of Eval in Javascript
Trans. Program. Lang. Syst., 22(2):187–223, Mar. 2000. ISSN
Applications. In Proceedings of the 25th European Con0164-0925. doi: 10.1145/349214.349216.
ference on Object-oriented Programming, ECOOP’11, pages
[17] J. hoon (David) An, A. Chaudhuri, and J. S. Foster. Static Typing for Ruby on Rails. In IEEE/ACM International Confer-

52–78, Berlin, Heidelberg, 2011. Springer-Verlag. ISBN 9783-642-22654-0.

14

2016/4/14

We show soundness by first showing preservation and
progress. As is typical, the hardest part of the proof is preservation, which shows that an expression’s type is preserved
under a step in the dynamic semantics. To make the theorem
work, we also need to reason about preserving key properties about the typing environment, run-time stack, and cache.
Here is the statement of the theorem, which we explain in
detail next:

[30] T. Sheard and S. P. Jones. Template Meta-programming for
Haskell. SIGPLAN Not., 37(12):60–75, Dec. 2002. ISSN
0362-1340. doi: 10.1145/636517.636528.
[31] J. G. Siek and W. Taha. Gradual Typing for Functional Languages. In Scheme and Functional Programming Workshop,
2006.
[32] V. St-Amour, S. Tobin-Hochstadt, M. Flatt, and M. Felleisen.
Typing the Numeric Tower. In Proceedings of the 14th
International Conference on Practical Aspects of Declarative Languages, PADL’12, pages 289–303, Berlin, Heidelberg, 2012. Springer-Verlag. ISBN 978-3-642-27693-4. doi:
10.1007/978-3-642-27694-1 21.

Theorem 2 (Preservation). If
(1)
(2)
(3)
(4)
(5)
(6)

[33] T. S. Strickland, B. Ren, and J. S. Foster. Contracts for
Domain-Specific Languages in Ruby. In Dynamic Languages
Symposium (DLS), Portland, OR, October 2014. doi: 10.1145/
2661088.2661092.
[34] N. Stulova, J. F. Morales, and M. V. Hermenegildo. Practical
Run-time Checking via Unobtrusive Property Caching. CoRR,
abs/1507.05986, 2015.

Then there exist ∆, ∆′ , TS ′ , τ ′ such that
(a)
(b)
(c)
(d)
(e)
(f)

[35] W.
Taha.
MetaOcaml,
2016.
http://www.cs.rice.edu/~ taha/MetaOCaml.
[36] P. Thiemann. Towards a Type System for Analyzing Javascript
Programs. In Proceedings of the 14th European Conference on Programming Languages and Systems, ESOP’05,
pages 408–422, Berlin, Heidelberg, 2005. Springer-Verlag.
ISBN 3-540-25435-8, 978-3-540-25435-5. doi: 10.1007/
978-3-540-31987-0 28.

TT ′ ⊢ h∆, e′ i ⇒ h∆′ , τ ′ i
τ ′ ≤ TS ′
If S = S ′ then ∆′ ≤ Γ′
∆ ∼ E′
TT ′ ⊢ TS ′ ∼ S ′
X ′ ∼ (TT ′ , DT ′ )

Let’s step through the assumptions and conclusions of the
theorem. (1) and (2) are standard—they assume that e takes
a step and is well-typed, respectively. The corresponding
conclusion (a) states that e′ is also well-typed.
(4) assumes the type and dynamic environments are
consistent—meaning values in E have the corresponding
types in Γ—and conclusion (d) states that they are still consistent after reduction. Formally:

[37] S. Tobin-Hochstadt and M. Felleisen. The Design and Implementation of Typed Scheme. In Proceedings of the 35th
Annual ACM SIGPLAN-SIGACT Symposium on Principles of
Programming Languages, POPL ’08, pages 395–406, New
York, NY, USA, 2008. ACM. ISBN 978-1-59593-689-9. doi:
10.1145/1328438.1328486.
[38] S. Tobin-Hochstadt and M. Felleisen. Logical Types for Untyped Languages. In Proceedings of the 15th ACM SIGPLAN
International Conference on Functional Programming, ICFP
’10, pages 117–128, New York, NY, USA, 2010. ACM. ISBN
978-1-60558-794-3. doi: 10.1145/1863543.1863561.

Definition 3 (Environment consistency). Type environment
Γ is consistent with dynamic environment E , written Γ ∼ E ,
if dom(Γ) ⊆ dom(E ) and for all x ∈ dom(Γ) there exists τ
such that · ⊢ hΓ, E (x)i ⇒ hΓ, τ i and τ ≤ Γ(x).

[39] M. M. Vitousek, A. M. Kent, J. G. Siek, and J. Baker. Design
and Evaluation of Gradual Typing for Python. In Proceedings
of the 10th ACM Symposium on Dynamic Languages, DLS
’14, pages 45–56, New York, NY, USA, 2014. ACM. ISBN
978-1-4503-3211-8. doi: 10.1145/2661088.2661101.
[40] D.
A.
Wheeler.
SLOCCount,
http://www.dwheeler.com/sloccount.

hX , TT , DT , E , e, S i → hX ′ , TT ′ , DT ′ , E ′ , e′ , S ′ i
TT ⊢ hΓ, ei ⇒ hΓ′ , τ i
τ ≤ TS
Γ∼E
TT ⊢ TS ∼ S
X ∼ (TT , DT )

Notice this definition allows E to include some variables
that are not bound in Γ. This is necessary to handle (TIf),
which discards any variables from the type environment that
are bound in one arm of the conditional but not the other.
Next, (3) and (5) concern the type of e and the stack.
The goal of preservation is to show e’s type is preserved, but
consider (EApp*) and (ERet). These rules both push and pop
the stack and change the expression being evaluated—hence
e′ could potentially have an entirely different type than e.
Our solution is to introduce the notion of a type stack
TS to mirror the run-time stack. To understand how the
type stack works, suppose we want to apply preservation to
C[v1 .m(v2 )], i.e., we are about to call a method. The typing judgment is TT ⊢ hΓ, C[v1 .m(v2 )i ⇒ hΓ′ , τ ′ i. In the
dynamic semantics, the (EApp*) rules will push the current
environment E and the context C on the stack. Correspondingly, we will push the current typing judgment onto the type

2015.

[41] T. Wrigstad, F. Z. Nardelli, S. Lebresne, J. Östlund, and
J. Vitek. Integrating Typed and Untyped Code in a Scripting Language. In Proceedings of the 37th Annual ACM
SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL ’10, pages 377–388, New York, NY,
USA, 2010. ACM. ISBN 978-1-60558-479-9. doi: 10.1145/
1706299.1706343.

A. Complete Formalism
This section contains the full definitions and proofs for the
formalism.
15

2016/4/14

stack—at least the key pieces of it. More specifically, we
push an element of the form (Γ[τ ], hΓ′ , τ ′ i), where Γ and Γ′
are the initial and final environments of the current typing
judgment; C is the context; and τ is the type of expression
v1 .m(v2 ), i.e., the type that the method must return.
Given this mechanism, the key invariant to maintain is
that the type of the expression is compatible with what the
calling functions expects. We define:

frame is popped when a method returns. Hence in this case
the antecedent of (c) is false, and the conclusion is trivial.
Finally, we need to reason about the cache. As we
saw earlier, the key cache invariant to preserve is that all
the derivations stored in the cache hold and apply to the
premethod stored in DT and the type stored in TT . Formally:
Definition 7 (Cache consistency). We say that cache X is
consistent with type class table TT and dynamic class table
DT , written X ∼ (TT , DT ), if for all A.m ∈ dom(X )
where X (A.m) = (DM , D≤ ), with DM = (TT ⊢ h[x 7→
τ1 , self 7→ A], ei ⇒ hΓ′ , τ i) and D≤ = (τ ≤ τ2 ), it is
the case that DM and D≤ hold and DT (A.m) = λx.e and
TT (A.m) = τ1 → τ2 .

Definition 4 (Stack subtyping). τ0 ≤ (Γ[τ ], hΓ′ , τ ′ i) :: TS
if τ0 ≤ τ .
Then (3) assumes that the type of e is a subtype of the
type expected by the calling function. (At the top-level, we
initialize the type stack with a frame that expects whatever
the top-level type is.) (b) states that the type of e′ is also a
subtype of the type expected by its calling function. Thus, if
the stack does not change, this means that e′ and e have the
same type (up to subtyping). If the stack does change, then
we still maintain the invariant.
Of course, we need this invariant to hold no matter how
many pushes and pops happen. Thus, rather than only talk
about the top element of the type stack, we need to ensure
that all elements of the type stack are consistent with all
elements of the dynamic stack. Formally:

Thus, (6) assumes the cache is consistent, and (f) concludes the new cache is also consistent.
To show preservation, we also need a few lemmas:
Lemma 1. For all Γ1 and Γ2 , it is the case that Γ1 ≤
(Γ1 ⊔ Γ2 ).
Lemma 2 (Contextual Substitution). If
TT ⊢ hΓ, ei ⇒ hΓ, τ ′ i
..
.

Definition 5 (Stack consistency). Type stack element
(Γ[τ ], hΓ′ , τ ′ i) is consistent with dynamic stack element
(E, C), written TT ⊢ (Γ[τ ], hΓ′ , τ ′ i) ∼ (E, C), if Γ ∼ E
and TT ⊢ hΓ[✷ 7→ τ ], Ci ⇒ hΓ′ , τ ′ i. (Here we abuse
notation and treat ✷ as if it’s a variable.)
Type stack TS is consistent with dynamic stack S , written
TT ⊢ TS ∼ S , is defined inductively as

TT ⊢ hΓC , C[e]i ⇒ hΓ′C , τC i
then TT ⊢ hΓC [✷ 7→ τ ′ ], Ci ⇒ hΓ′C , τC i.
Lemma 3 (Substitution). If
1. TT ⊢ h∆[✷ 7→ τC ], Ci ⇒ h∆′ , τC′ i
2. TT ⊢ h·, vi ⇒ h·, τ i
3. τ ≤ τC

1. TT ⊢ · ∼ ·
2. TT ⊢ (Γ[τ ], hΓ′ , τ ′ i) :: TS ∼ (E, C) :: S if
(a) (Γ[τ ], hΓ′ , τ ′ i) ∼ (E, C)
(b) TS ∼ S
(c) τ ′ ≤ TS if TS 6= ·

Then TT ⊢ h∆, C[v]i ⇒ h∆′ , τC′′ i where τC′′ ≤ τC′ .
Finally, we can prove preservation:
Proof. (Preservation) By induction on hX , TT , DT , E , e, S i →
hX ′ , TT ′ , DT ′ , E ′ , e′ , S ′ i.

Thus, (5) assumes the type and dynamic stacks are consistent, and (e) concludes they remain consistent after taking
a step.
Next, (c) relates the output environment of e′ with the
output environment of e.There are two cases. If the stack did
not change (the antecedent of the conclusion is true), then
the output environment of e′ should be compatible with Γ′ .
Again because of (TIf), we need to allow the environment to
shrink:

• Case (EContext). Notice that we cannot have S ′ 6= S ,

since the only cases where that can happen is if (EApp)
or (ERet) apply, and they cannot be used as a hypothesis
of (EContext). Thus the left-hand side of the implication
(c) is true, and we have ∆′ ≤ Γ′ . Using this fact, the
remainder of the proof is routine.
• Case (ESelf). By assumption we have
(1) hX , TT , DT , E , self, S i → hX , TT , DT , E , E (self), S i
by (ESelf)
(2) TT ⊢ hΓ, selfi ⇒ hΓ, Γ(self)i by (TSelf)
(3) Γ(self) ≤ TS
(4) Γ ∼ E
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )

Definition 6 (Type environment subsumption). We write
Γ1 ≤ Γ2 if dom(Γ2 ) ⊆ dom(Γ1 ) and for all x ∈ dom(Γ2 ),
it is the case that Γ1 (x) ≤ Γ2 (x).
If the stack does change, then the output environment is
irrelevant: It either is captured in the type stack if this is a
push due to a method call. Or it is discarded as the stack
16

2016/4/14

Let ∆ = ∆′ = Γ, and let TS ′ = TS . By (2) and (4)
there exists τ ′ such that · ⊢ h∆, E (self)i ⇒ h∆, τ ′ i and
τ ′ ≤ ∆(self). Then (a) holds, since typing of E (self)
was by (TNil) or (TObject), which do not depend of the
type class table. Also, (b) holds since τ ′ ≤ ∆(self) =
Γ(self) ≤ T S by (3). Also, the right-hand side of the
implication (c) holds trivially. Finally, (d) holds by (4),
(e) holds by (5), and (f) holds by (6).
• Case (EVar). Similar to (ESelf) case.
• Case (EAssn). By assumption we have
(1) hX , TT , DT , E , x = v, S i → hX , TT , DT , E [x 7→
v], v, S i
(2)
TT ⊢ hΓ, vi ⇒ hΓ, τ i
TT ⊢ hΓ, x = vi ⇒ hΓ[x 7→ τ ], τ i

• Case (EIfFalse). Similar to (EIfTrue) case.
• Case (EDef). By assumption we have

(1) hX , TT , DT , E , def A.m = λx.e, S i →
hX \A.m, TT , DT [A.m 7→ λx.e], E , nil, S i by (EDef)
(2) TT ⊢ hΓ, def A.m = λx.ei ⇒ hΓ, nili by (TDef)
(3) nil ≤ TS
(4) Γ ∼ E
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )
Let ∆ = ∆′ = Γ, let TS ′ = TS , and let τ ′ = nil.
Then (a) holds trivially by (TNil). (c) holds trivially by
definition. (b), (d), and (e) hold trivially by (3), (4), and
(5).
For (f), pick some B.m′ ∈ dom(X \A.m). Observe that
B.m′ 6= A.m, by construction. By (6), we have
1. DM = (TT ⊢ h[y 7→ τy , self 7→ B], ei ⇒ hΓ′y , τy′ i)
2. D≤ = (τy′ ≤ τ2 )
3. DM and D≤ hold
4. DT (B.m′ ) = λy.e′
5. TT (B.m′ ) = τy → τ2
We need to show the above with the same type class table
and with dynamic class table DT [A.m 7→ λx.e]. But
then 1, 2, 3, and 5 are trivial, and since B.m′ 6= A.m we
have (DT [A.m 7→ λx.e])(B.m′ ) = DT (B.m′ ), thus 4
is trivial.
• Case (EType). This case is very similar to (EDef), except
the reduction in the semantics is different. By assumption, we have
(1) hX , TT , DT , E , type A.m : τm , S i →
h(X \A.m)[TT ′ ], TT ′ , DT , E , nil, S i where TT ′ =
TT [A.m 7→ τm ], by (EType)
(2) TT ⊢ hΓ, def A.m = λx.ei ⇒ hΓ, nili by (TDef)
(3) nil ≤ TS
(4) Γ ∼ E
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )
(a)–(d) hold by the same reasoning above. To see (e),
observe that side condition A.m 6∈ TApp(S) means that
in the typing judgments internal to (5), (TApp) is never
applied with A.m. Hence those same judgments hold
under TT ′ , which only differs from TT in its binding
or A.m.
Let X ′ = (X \A.m)[TT ′ ]. For (f), again pick some
B.m′ ∈ dom(X ′ ). Observe that B.m′ 6= A.m, by construction. By (6), we have
1. DM = (TT ⊢ h[y 7→ τy , self 7→ B], ei ⇒ hΓ′y , τy′ i)
2. D≤ = (τy′ ≤ τ2 )
3. DM and D≤ hold
4. DT (B.m′ ) = λy.e′
5. TT (B.m′ ) = τy → τ2
We need to show the above, but in X ′ and with type
class table TT ′ and the same dynamic class table. By

by (TAssn) and either (TNil) or (TObject)
(3) τ ≤ TS
(4) Γ ∼ E
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )
Let ∆ = ∆′ = Γ[x 7→ τ ], let TS ′ = TS , and let τ ′ = τ .
Notice that in (2), the hypothesis can only be proven by
either (TNil) or (TObject), both of which are insensitive
to the type environment. Thus, by the hypothesis of (2),
we also have TT ⊢ h∆, vi ⇒ h∆, τ i, which is (a).
Also, (b), (e), and (f) hold trivially by (3), (5), and (6).
Also, the right-hand side of the implication (c) holds
trivially. Finally, from (4) and the hypothesis of (2) we
have ∆ = Γ[x 7→ τ ] ∼ E[x 7→ v], which is (d).
• Case (ENew). Trivial.
• Case (ESeq). Trivial.
• Case (EIfTrue). By assumption we have
(1) hX , TT , DT , E , if v then e1 else e2 , S i →
hX , TT , DT , E , e1 , S i where v 6= nil
(2)
TT ⊢ hΓ, vi ⇒ hΓ, τ i
TT ⊢ hΓ, e1 i ⇒ hΓ1 , τ1 i
TT ⊢ hΓ, e2 i ⇒ hΓ2 , τ2 i
TT ⊢ hΓ, if v then e1 else e2 i ⇒ hΓ1 ⊔ Γ2 , τ1 ⊔ τ2 i
by (TIf) and (TObject), since v 6= nil
(3) τ1 ⊔ τ2 ≤ TS
(4) Γ ∼ E
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )
Let ∆ = Γ, let ∆′ = Γ1 , let TS ′ = TS , and let
τ ′ = τ1 . From the second hypothesis of (2) we trivially
have (a). Moreover, τ ′ = τ1 ≤ τ1 ⊔ τ2 , so by (3) we
have τ ′ ≤ TS , which is (b). By (4), (5), and (6) we
trivially have (d), (e), and (f). Finally, by Lemma 1 we
have ∆′ = Γ1 ≤ (Γ1 ⊔ Γ2 ), which is the right-hand side
of the implication (c).
17

2016/4/14

′
′
construction, X ′ (B.m′ ) = (DM
, D≤ ) where DM
=
(TT ′ ⊢ h[y 7→ τy , self 7→ B], ei ⇒ hΓ′y , τy′ i), which is
′
1 and 2. Notice by construction that DM and DM
cannot
refer to A.m, thus we have 3. Finally, 4 holds trivially,
and 5 holds since B.m′ 6= A.m by construction.
• Case (EAppMiss). The inductive cases are similar to
(EContext). In the non-inductive case, by assumption we
have
(1) hX , TT , DT , E , C[[A].m(v2 )], S i →
hX [A.m 7→ (DM , D≤ )], TT , DT , [self 7→ [A], x 7→
v2 ], e, (E , C) :: S i where
(1a) DT (A.m) = λx.e
(1b) TT (A.m) = τ1 → τ2
(1c) type of(v2 ) ≤ τ1
(1d) DM = (TT ⊢ h[x 7→ τ1 , self 7→ A], ei ⇒
hΓ′ , τ2′ i) holds
(1e) D≤ = (τ2′ ≤ τ2 ) holds
(1f) A.m 6∈ dom(X )
(2)

3 holds by (1d) and (1e), 4 holds by (1a), and 5 holds by
(1b).
• Case (EAppHit). This case follows mostly the same
reasoning as above. The inductive cases are similar to
(EContext). In the non-inductive case, by assumption we
have
(1) hX , TT , DT , E , C[[A].m(v2 )], S i →
hX , TT , DT , [self 7→ [A], x 7→ v2 ], e, (E , C) :: S i
where
(1a) DT (A.m) = λx.e
(1b) TT (A.m) = τ1 → τ2
(1c) type of(v2 ) ≤ τ1
(1d) A.m ∈ dom(X )
(2)
TT ⊢ hΓ, [A]i ⇒ hΓ, Ai
TT ⊢ hΓ, v2 i ⇒ hΓ, τ i
TT (A.m) = τ1 → τ2
τ ≤ τ1
TT ⊢ hΓ, [A].m(v2 )i ⇒ hΓ, τ2 i
..
.

TT ⊢ hΓ, [A]i ⇒ hΓ, Ai
TT ⊢ hΓ, v2 i ⇒ hΓ, τ i
TT (A.m) = τ1 → τ2
τ ≤ τ1
TT ⊢ hΓ, [A].m(v2 )i ⇒ hΓ, τ2 i
..
.

TT ⊢ hΓC , C[[A].m(v2 )]i ⇒ hΓ′C , τC i
by (TApp) and (TObject) and possible (TNil).
(3) τC ≤ TS
(4) ΓC ∼ EC
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )
By (6), we have X (A.m) = (DM , D≤ ) where DM =
(TT ⊢ h[x 7→ τ1 , self 7→ A], ei ⇒ hΓ′ , τ2′ i) holds and
D≤ = (τ2′ ≤ τ2 ) holds. Notice that we use properties
4 and 5 of the cache in combination with (1a) and (1b)
to know the assigned types in the cache, and the method
body at run-time, match in the cached derivation.
Let ∆ = [x 7→ τ1 , self 7→ A], let ∆′ = Γ′ , let TS ′ =
(ΓC [τ2 ], hΓ′C , τC i) :: TS , and let τ ′ = τ2′ . Then (a) holds
immediately by DM . (b) holds by D≤ and construction of
TS ′ .
The reasoning for (c)–(e) are the same as the (EAppMiss)
case. Finally, (f) holds trivially by (6), since the cache did
not change.
• Case (ERet). We have
(1) hX , TT , DT , E ′ , v, (E , C) :: S i →
hX , TT , DT , E , C[v], S i
(2) TT ⊢ hΓ′ , vi ⇒ hΓ′ , τ i by either (TObject) or
(TNil).
(3) τ ≤ τC
(4) Γ′ ∼ E ′
(5) TT ⊢ (ΓC [τC ], hΓ′C , τC′ i) :: TS ∼ (E , C) :: S
(6) X ∼ (TT , DT )
Let ∆ = ΓC , let ∆′ = Γ′C , and let TS ′ = TS . By (5),
we have TT ⊢ h∆[✷ 7→ τC ], Ci ⇒ h∆′ , τC′ i. Putting
that together with (2) and (3) via the substitution lemma,
we have TT ⊢ h∆, C[v]i ⇒ h∆′ , τC′′ i where τC′′ ≤ τC′ .
Let τ ′ = τC′′ , and we have (a). By (3) we have τC′ ≤ TS ,

TT ⊢ hΓC , C[[A].m(v2 )]i ⇒ hΓ′C , τC i
by (TApp) and (TObject) and possible (TNil).
(3) τC ≤ TS
(4) ΓC ∼ EC
(5) TT ⊢ TS ∼ S
(6) X ∼ (TT , DT )
Let ∆ = [x 7→ τ1 , self 7→ A], let ∆′ = Γ′ , let TS ′ =
(ΓC [τ2 ], hΓ′C , τC i) :: TS , and let τ ′ = τ2′ . Then (a) holds
immediately by (1d). (b) holds by (1e) and construction
of TS ′ . In this case the stack changes, so the left-hand
side of the implication (c) is false, hence (c) holds trivially. (d) holds because by (TObject) we have [A] has
type A, and by the second hypothesis of (2), which is
either (TObject) or (TNil), we have v2 has type τ , and by
the last hypothesis of (2) we have τ ≤ τ1 .
Next we show (e). By (4) we have ΓC ∼ E, and by
(2) and the Contextual Substitution Lemma we have
TT ⊢ hΓC [✷ 7→ τ2 ], Ci ⇒ hΓ′C , τC i. Thus we
have TT ⊢ (ΓC [τ2 ], hΓ′C , τC i) ∼ (E, C). Further,
by (3) we have τC ≤ TS . Finally, by (5) we have
TT ⊢ TS ∼ S . Putting this all together, we have TT ⊢
(ΓC [τ2 ], hΓ′C , τC i) ∼ (E, C) :: TS ∼ (E, C) :: S ,
which is (e).
Finally, to show (f), pick some element in the domain
of X ′ = X [A.m 7→ (DM , D≤ )]. If we pick some
B.m′ 6= A.m then all the necessary properties hold by
(6). If we pick A.m, then 1 and 2 hold by construction,
18

2016/4/14

and since τC′′ ≤ τC′ we therefore have (b) In this case the
stack changes, so the left-hand side of the implication (c)
is false, hence (c) holds trivially. (d) holds by (5), as does
(e). Finally, (f) holds by (6)

Now there are two cases. If A.m ∈ dom(X ) we can
immediately apply (EAppHit). Otherwise, if A.m 6∈
dom(X ), then we must have
DM = (TT ⊢ h[x 7→ τ1 , self 7→ A], ei ⇒ hΓ′ , τ i)
holds and D≤ = (τ ≤ τ2 ) holds because (EAppNTyp)
did not apply. But then combining this with our previous
assumptions we can apply (EAppMiss).
• Else e = C[e′ ]. Holds by induction and (EContext).

The progress theorem is much simpler:
Theorem 3 (Progress). If
(1)
(2)
(3)
(4)
(5)

TT ⊢ hΓ, ei ⇒ hΓ′ , τ i
τ ≤ TS
Γ∼E
TT ⊢ TS ∼ S
X ∼ (TT , DT )

Finally, we can put these together to prove soundness.
Theorem 4 (Soundness). If ∅ ⊢ h∅, ei ⇒ hΓ′ , τ i then either
e reduces to a value, e reduces to blame, or e does not
terminate.

then one of the following holds
1. e is a value, or
2. There exist X ′ , TT ′ , DT ′ , E ′ , e′ , S ′ such that
hX , TT , DT , E , e, S i → hX ′ , TT ′ , DT ′ , E ′ , e′ , S ′ i, or
3. hX , TT , DT , E , e, S i → blame

Proof. Let X = ∅, let TT = ∅, let Γ = ∅, let E = ∅, let
DT = ∅, let S = (∅, ✷) :: ·, and let TS = (∅[τ ], h∅, τ i).
Then by assumption we have TT ⊢ hΓ, ei ⇒ hΓ′ , τ i. By
construction we have τ ≤ TS and Γ ∼ E and TT ⊢
TS ∼ S and X ∼ (TT , DT ). Thus, these choices of
X , TT , Γ, DT , S , and TS satisfy the preconditions of
progress and preservation. Thus soundness holds by standard
arguments.

Proof. By induction on e.
• Case e = nil or e = [A]. These are values, so the theorem

holds trivially.
• Case self. By assumption (1) we have

TT ⊢ hΓ, selfi ⇒ hΓ, Γ(self)i

•
•

•
•

Thus, self ∈ dom(Γ). But then by (3), self ∈ dom(E ).
Thus (ESelf) can be applied.
(Note that assuming we start executing the program in a
standard environment, self will in fact always be bound
in all type and dynamic environments, unlike variables.)
Case x. Similar to self.
Case x = v, A.new, v; e, def A.m = (λx.e), type A.m :
τm . These cases are trivial, as there is one semantics rule
for each of these forms, and it will always be able to take
a step.
Case if v then e2 else e3 . This case is trivial, since either
(EIfTrue) or (EIfFalse) will apply.
Case v0 .m(v1 ). By assumption (1) we have
TT ⊢ hΓ, v0 i ⇒ hΓ0 , Ai
TT ⊢ hΓ0 , v1 i ⇒ hΓ1 , τ i
TT (A.m) = τ1 → τ2
τ ≤ τ1
TT ⊢ hΓ, v0 .m(v1 )i ⇒ hΓ1 , τ2 i
There are a few cases. If (EAppNil), (EAppNExist), or
(EAppNTyp) apply, then the theorem holds trivially. Otherwise, we must have v1 = [A] and DT (A.m) = λx.e.
More importantly, by (1) we have type of(v1 ) ≤ τ1 ,
since τ = type of(v1 ) by (1), i.e., v1 has the expected argument type. Also by (1) we have TT (A.m) = τ1 → τ2 .
19

2016/4/14

