Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf ·...

67
Of Scripts and Programs Tall tales, Urban Legends and Future Prospects Jan Vitek based on joint work with Bard Bloom John Field IBM Nate Nystrom U. Texas Arlington Francesco Zappa Nardelli INRIA Tobias Wrigstad Stockholm University Brian Burg Nicholas Kidd Sylvain Lebresne Johan Östlund Gregor Richards Purdue S3lab, Purdue University

Transcript of Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf ·...

Page 1: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Of Scripts and ProgramsTall tales, Urban Legends and Future Prospects

Jan Vitek

based on joint work withBard Bloom John Field IBMNate Nystrom U. Texas Arlington Francesco Zappa Nardelli INRIA Tobias Wrigstad Stockholm UniversityBrian Burg Nicholas Kidd Sylvain Lebresne Johan Östlund Gregor Richards Purdue

S3lab, Purdue University

Page 2: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Tall Tales

I want it tall and strong

…and cheap ...and

fast

No worries mate. I’ve got just the right language for the job

Page 3: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

At the beginning...

• Our work motivated by Tobias’ involvement with Pluto (aka in Swedish Premiepensionmyndigheten)

• We have been trying to get code for three years, but it’s too sensitive to release...

• So, apply salt to the following slides, second hand material only, based ondiscussions with Tobias and slidesby Lemonnier and Lundborg,errors are mine, all mine.

Page 4: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

...there was a script

• A modest Perl program hacked together to perform a simple data migration step while the grown ups (BIC = Big IT Company) built the real system

• Unfortunately, the real system was late, over budget, and unusable

• So, BIC got fired and the script became the real system

Page 5: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

...that grew up to be a program that...

• … manages the retirement savings of 5.5 million users

• … for a value of 23 billion Euros

• … with a team of 30 developers over 7 years

Page 6: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Pluto

320 000 lines of Perl

68 000 lines of SQL

27 000 lines of shell

26 000 lines of HTML

230 database tables

750 G bytes of data

24/7 availability

0 bugs allowed

Page 7: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration
Page 8: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

The Road to Glory

• A number of factors contributed to the success of Pluto

• High productivity of scripting languages Perl won over Java in all internal evaluations

• Disciplined use of the languageMany features disallowed by standards. Only C-like code. No floating points. No threads. No OO.

• Fail fast, Abort, UndoBatch daily runs, undo all changes if an error is detected.

• ContractsHome-brewed contract notation for Perl, runtime checked

Page 9: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Contracts for Perl

contract(‘do_sell_current_holdings’) -> in(&is_person, &is_date) -> out(&is_state) -> enable;

sub do_sell_current_holdings { my ($person, $date) … if ($operation eq “BUD_”) { … … return $state;}

Page 10: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Lessons Learned

• The Pluto developers complained about

• Syntax (it’s ugly)

• Typing (it’s weak)

• Speed (it’s slow)

• Support for concurrency and parallelism is lacking

• Lack of encapsulation and modularity

Page 11: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

The Questions are thus...

• Can we write dynamic scripts and robust programs in the same language?

• Can we go from scripts to programs and from programs to scripts freely?

• Can we do this without losing either the flexibility of scripting or the benefits that come with static guarantees?

• Can I have my cake and eat it too?

Page 12: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Related Work

• Lundborg, Lemonnier. PPM or how a system written in Perl can juggle with billions. Freenix 2006

• Lemonnier. Testing Large Software With Perl. Nordic Perl Workshop 2007

• Stephenson. Perl Runs Sweden's Pension System. O’Reilly On Lamp, 2005

Thanks: Tobias Wrigstad

Page 13: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Urban Legends

Page 14: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Understanding the dynamics of dynamic languages

• How dynamic should we, must we, be?Many anecdotal stories about need and use of dynamic features, but few case studiesAre dynamic features used to make up for missing static features?Or are the programmers just “programmers”?

• Can we add a static type system to an existing dynamic language? without having to rewrite all legacy programs and libraries

Page 15: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Methodology

• We selected a very dynamic language, JavaScript, a cross between Scheme and Self without their elegance but with a large user base

• We instrumented a popular browser (Safari) and collected traces from the 100 most popular websites (Alexa) plus many other traces

• We ran an offline analysis of the traces to gather data

• We analyzed the source code to get static metrics

Page 16: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

JavaScript is a Programming Language

• A familiar syntax

function List(v,n) {this.value=v; this.next=n;}

List.prototype.map = function(f){ return new List( f(this.value), this.next ? this.next.map(f) : null); }

var ls = new List(1, new List(2, new List(3, null)));

var nl = ls.map( function(x){return x*2;} );

Page 17: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

JavaScript is a Programming Language

• A familiar syntax

function List(v,n) {this.value=v; this.next=n;}

List.prototype.map = function(f){ return new List( f(this.value), this.next ? this.next.map(f) : null); }

var ls = new List(1, new List(2, new List(3, null)));

var nl = ls.map( function(x){return x*2;} );

Constructorfield addition

Page 18: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

JavaScript is a Programming Language

• A familiar syntax

function List(v,n) {this.value=v; this.next=n;}

List.prototype.map = function(f){ return new List( f(this.value), this.next ? this.next.map(f) : null); }

var ls = new List(1, new List(2, new List(3, null)));

var nl = ls.map( function(x){return x*2;} );

adding a shared method

higher order function

Page 19: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

JavaScript is a Programming Language

• A familiar syntax

function List(v,n) {this.value=v; this.next=n;}

List.prototype.map = function(f){ return new List( f(this.value), this.next ? this.next.map(f) : null); }

var ls = new List(1, new List(2, new List(3, null)));

var nl = ls.map( function(x){return x*2;} );

object construction

Page 20: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

JavaScript is a Programming Language

• A familiar syntax

function List(v,n) {this.value=v; this.next=n;}

List.prototype.map = function(f){ return new List( f(this.value), this.next ? this.next.map(f) : null); }

var ls = new List(1, new List(2, new List(3, null)));

var nl = ls.map( function(x){return x*2;} );

Closure

Page 21: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Challenges to static typing

• Lack of type declarations

• Eval and dynamic loading

• Addition/deletion of fields/methods

• Changes in the prototype hierarchy

Page 22: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Corpus

• 100 JavaScript programs were recorded

Focus on the following:

280slides, Bing, Blogger, CNET, Digg, ESPN, Facebook, Flicker, GMaps, Gmail, Google, ImageShack, LivelyKernel, Other, Purdue, Twitter, Wikipedia, WordPress, YouTube, eBay, AppleMe

• The total size of the traces is 6.7 GB

Page 23: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Corpus

• Size of source in bytes

• Size of average trace in bytes

280s

lides

Bing

Blog

ger

CNET

Digg

ESPN

Fboo

kFl

ickr

GM

aps

Gm

ail

Goo

gle

ImgS

hack

Live

lyO

ther

Purd

ueTw

itter

Wiki

pYo

uTub

eeB

aym

e.co

m

0

1000

2000

3000

4000

5000

6000

7000

280s

lides

Bing

Blog

ger

CNET

Digg

ESPN

Fboo

kFl

ickr

GM

aps

Gm

ail

Goo

gle

ImgS

hack

Live

lyO

ther

Purd

ueTw

itter

Wiki

pYo

uTub

eeB

aym

e.co

m

0

1000

2000

3000

4000

5000

6000

7000

1 MB

500 KB

5 MB

Page 24: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Instruction Mix

stores_directstores_hashmapstores_idxreads_directreads_hashmapreads_idxdeletes_directdeletes_hashmapdeletes_idxdefinescreatescallsthrowscatchs

stores_directstores_hashmapstores_idxreads_directreads_hashmapreads_idxdeletes_directdeletes_hashmapdeletes_idxdefinescreatescallsthrowscatchs

Image ShackWordPress

Average

Write

Read

Call

Page 25: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Function Size280slides

Bing

Blogger

CNET

Digg

ESPN

Fbook

Flickr

GMaps

Gmail

Google

ImgShack

Lively

Other

Purdue

Twitter

Wikip

YouTube

eBay

me.com

050

100

150

200

• Number of instruction executed within each function

Average: less than 30 operations per function.

Page 26: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Live Data

• Objects allocated in the traces broken down in major categories

anonymous

domarrays

dates

regexps functionsinstanceserrorsprototypes

280 YTubeBing

WorPCnet

Digg

eBay

Wikp

Fbok

FLikr

Gmai

Gmap

Googl

ImShk Lively

Me

PU

TwitEspn

Other

Blog

Page 27: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Prototype chain lengths

280slides

Bing

Blogger

CNET

Digg

ESPN

Fbook

Flickr

GMaps

Gmail

Google

ImgShack

Lively

Other

Purdue

Twitter

Wikip

WordP

YouTube

eBay

me.com

02

46

810

12

x._proto_ = y

Max

Avg

• Prototype chains allow sharing behavior in a more flexible way than inheritance

• Prototype chain length similar to inheritance depth metrics

• While, average close to 1, maximum depends on coding style

Page 28: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Oh Eval, my Eval

• Eval can perform arbitrary damage to data structures

• What if most evals were deserialization of JSON data?

280slides

Bing

Blogger

CNET

Digg

ESPN

Fbook

Flickr

GMaps

Gmail

Google

ImgShack

Lively

Purdue

Twitter

WordP

YouTube

eBay

me.com

01000

2000

3000

4000

5000

6000

7000

eval(“x.f = 3”)

JSON

Random code

Page 29: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

f(1) # too fewf(1,2,3) # too many

f = function(x,y) {…}

Variadicity

• Functions need not be called with the “right” number of arguments

• Missing args have value UNDEFINED, additional args accessed by position

280slides

Bing

Blogger

CNET

Digg

ESPN

Fbook

Flickr

GMaps

Gmail

Google

ImgShack

Lively

Other

Purdue

Twitter

Wikip

WordP

YouTube

eBay

me.com

0.0

0.1

0.2

0.3

0.4

0.5

60% of functions are used

variadically

Page 30: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Dynamic dispatch

• Dynamic binding is a hallmark of object-oriented languages

• How dynamic is our corpus?

1 10 100 1000 10000

1100

10000

#of different functions called from a call site

>100K call sites monomorphic

1 call site dispatches to

100K functions

Page 31: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Dynamic dispatch

• What if there were many identical functions?

• Programming style (or lack thereof) matters

1 10 100 1000 10000

1100

10000

#of functions sharing the same body

function List(h,t){ this.head=h; this.tail=t; this.c=function(l){…}}

~100K bodies are unique

1 body shared 50K functions

Page 32: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Dynamic dispatch

• These are not so different from Java or C#…

1 5 10 50 100 500 1000

1100

10000

#of different function body called from a call site

~100K call sites monomorphic

1 call site dispatches <2K

functions

Page 33: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Constructor dynamism

• Constructors are “just” functions that side-effect this.

• Accordingly a constructor can return different “types”, i.e. objects with different properties

1 2 5 10 20 50 100 200

110

100

1000

10000

function Person(n,M){ this.name=n; this.sex=M; if(M){ this.likes= “guns” }} #of different “types” returned by a constructor

>2K constructors monomorphic

1 constructor returns

~300 “types”

Page 34: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

• JavaScript allows runtime addition and deletion of fields and methods in objects (including prototypes)

• Ignoring constructors, the average number of additions per object is considerable

• Deletion are less frequent but can’t be ignored

Addition/Deletion

x._proto_.f = F

delete x.y 280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

adds

deletes

Page 35: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

• Programming style (or lack thereof) matters.

• What about objects constructed by extending an empty object?

• Heuristic: construction ends at first read

Addition/Deletion

280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

x = {}

x.head=”Mickey”x.map=function(x){…}

… = x.f …

Page 36: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Addition/Deletion

280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

280

Bing

Blog

CNET

Digg

ESPN

Fbook

Flikr

GMail

Gmap

Googl

IShak

Livly

Other PU Twit

Wikip

WordP

eBay

YTube

me.com all

0.0

0.5

1.0

1.5

2.0

2.5

• Hash tables & arrays are objects, and vice versa

• What if most of add/deletes were hash/index operation?

• Heuristic: syntax of access operations

… x[name] …

… x[3]…

Page 37: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Conclusions, maybe

• JavaScript programs are indeed dynamic

• All features are used, but not all the time

• Some of the abuse is sloppy programming

• Hope of imposing types on legacy code?

Page 38: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Related Work

• Anderson, Giannini, Drossopoulou. Towards type inference for JavaScript. ECOOP 2005

• Mikkonen, Taivalsaari. Using JavaScript as a Real Programming Language. TR SUN 2007

• Holkner, Harland. Evaluating the dynamic behavior of Python applications. ACSC 2009

• Chugh, Meister, Jhala, Lerner. Staged information flow for JavaScript. PLDI 2009

• Furr, An, Foster, Hicks. Static type inference for Ruby. SAC 2009

• Guha, Krishnamurthi, Jim. Using static analysis for Ajax intrusion detection. WWW 2009

• Jensen, Møller, Thiemann. Type analysis for JavaScript. SAS 2009

Current team: Sylvain Lebresne, Gregor Richards, Brian BurgAlumni: Johan Ostlund, Tobias WrigstadSponsor: ONR, NSF

Page 39: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

?F

utu

re P

ros

pe

cts

Page 40: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Thorn

• The story so far:

• We are looking into how to help scripts grow up to be programs

• Scripts are really dynamic

• Existing languages are ill-suited to non-invasive evolution

Page 41: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Methodology

• Design a new language, benefit from

• the ability to correct unlucky language design decision

• the freedom from legacy code and user base

• Thorn is an experiment in language design

• Thorn lets scripts grow up by

• addition of encapsulation/modularization (classes, modules, components)

• addition of concurrency (components)

• addition of types (like types)

Page 42: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Thorn is a Programming Language

• A familiar syntax

class List(hd,tl) {

def lmap(f) = List( f.apply(head), if(hd==null) tl.lmap(f); else null;

}

ls = List(1, List(2, List(3, null)));

Page 43: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Growing up modular

Page 44: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Modularity and encapsulation mechanisms

• A script

• A simple script that does some text manipulation

• No encapsulation, no modularity, but does the job

words = "story.txt".file.contents.split("\\W+");

wc = %group(word=w.toLower){ n=%count; | for w<-words };

sorted = %sort["%3d %s".format(n,word)

%> n %< word | for {:word, n:} <- wc];

println( sorted.joined("\n") );

Page 45: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Modularity and encapsulation mechanisms

• A class

• Classes provide basic encapsulation and modularity

• Limited support for access control

class Counter(name) {

words = name.file.contents.split("\\W+");

def count() = %group(word=w.toLower) { n=%count; | for w <- words };

def sort() = %sort["%3d %s".format(n,w) %> n %< w | for {:w,n:} <- count()];

}

Page 46: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Modularity and encapsulation mechanisms

• A module

• Better support for encapsulation

• Control over linking

module COUNT {

import own file.*; import util.Vector;

class Counter(name) { … }}

Page 47: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Modularity and encapsulation mechanisms

• A component

• Stronger encapsulation

component Count {

sync count(name) ...

}

Page 48: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Growing up concurrent

Page 49: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Concurrency features

• The unit of concurrency is the component

• Components are

• single-threaded

• fully isolated

• have a mailbox

More details in Bard’s OOPSLA talk

Page 50: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Growing up typed

Page 51: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Static and dynamic type checking

Static type checking is great:• catches bugs earlier;• enables faster execution.

Dynamic type checking is great:• anything goes, until it doesn't;• a program can be run even when crucial pieces are missing

Can they co-exist in the same design?

Page 52: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Problem

class Foo{ def bar(x:Int) = x+1; }

a:Foo = Foo();

a.bar(X); ,# assume no static type information available on X

Idea: let the run-time check that X is compatible with type Int.

When should this check be performed? How long does it take?

Page 53: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Run-time wrappers

class Ordered { def compare(o:Ordered):Int; }

fun sort (x:[Ordered]):[Ordered] = …

a:[Ordered] = sort(X);

• Checking that X is an array of Ordered is linear time

• Arrays are mutable, so checking at invocation of sort is not enough.

Idea: add a wrapper around X that checks that it can respond to methods invoked on it

Compiled code:

a:[Ordered] = sort(#[Ordered]#X)

Page 54: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Cannot optimize code

class Foo{ def bar(x:Int) = x+1; }

a:Foo = Foo();

a.bar(X);

With static types, body of Foo.bar compiled with aggressive optimizations

Any decent compiler would unbox the Int

This is not possible if it is a wrapped object #Int#X

Page 55: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Our design

Page 56: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Our design principles

Permissive:

accept as many programs as possible

Modular:

be as modular as possible

Reward good behavior:

reward programmer with performance or clear correctness guarantees

Page 57: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Introduce a novel type construct that mediates between static and dynamic.

Our design

static

fast

catch errors

like type

catch some errors

still flexibleflexible

dynamic

• For each class name C, add type like C

• Compiler checks that operations on like C variables are well-typed if the referred object had type C

• Does not restrict binding of like C variables, checks at run-time that invoked method exists

Page 58: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

An example

class Point(var x:Int, var y:Int) { def getX():Int = x; def getY():Int = y; def move(p ) { x := p.getX(); y := p.getY(); } }

Requirements:

1. Fields x and y declared Int

2. move accepts any object with getX and getY methods

Page 59: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

class Point(var x:Int, var y:Int) { def getX():Int = x; def getY():Int = y; def move(p:like Point) { x := p.getX(); y := p.getY(); } }

like Point

Page 60: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

class Point(var x:Int, var y:Int) { def getX():Int = x; def getY():Int = y; def move(p:like Point) { x := p.getX(); y := p.getY(); } }

class Coordinate(x:Int,y:Int) { def getX():Int = x; def getY():Int = y; }

p = Point(0,0);c = Coordinate(5,6);p.move(c);

1. Flexibility

move runs fine if c has getX/getY

Page 61: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

class Point(var x:Int, var y:Int) { def getX():Int = x; def getY():Int = y; def move(p:like Point) { x := p.getX(); y := p.getY(); p.hog; } }

2. Checks

move is type-checked under assumption that the argument is a Point

Compile-time Error

Page 62: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

3. Return values have the expected type

class Cell(var contents) { def get() = contents; def set(c) { contents := c }}

class IntCell { def get():Int; def set(c:Int); }

q:Cell = Cell(41);p:like IntCell = Cell(42);

q.get() + 1;

p.get() + 1;

q.get():dyn

p.get():Intoptimizations possible

Page 63: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Like types

• A unilateral promise as to how a value will be treated locally

allow most of the regular static checking machinery

allows the flexibility of structural subtyping

• Concrete types can stay concrete

allow reusing names as semantics tags

• Interact nicely with generics

Page 64: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Rewards

Page 65: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Lies, big and small...

.0.0

0.5

1.0

1.5

2.0

2.5

3.0

spectral-norm1000 1500 1000 1500 11 12

mandelbrot fannkuch

Typed ThornDynamic Thorn

Python 2.5.1Ruby 1.8.6

runn

ing

spee

d re

lativ

e to

Pyt

hon

2.5.

1

4.87 4.84

* Obtained with an older version of Thorn* Benefits due to unboxing of numeric values* Benchmarks are meaningless* Still slower than Java

Page 66: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Related Work

• Findler, Felleisen. Contracts for higher-order functions. 2002

• Bracha. The Strongtalk Type System for Smalltalk. 2003

• Gray, Findler, Flatt. Fine-grained interoperability through mirrors and contracts. 2005

• Siek, Taha. Gradual typing for functional languages. 2006

• Tobin-Hochstadt, Felleisen. Interlanguage migration: From scripts to programs. 2006

• ECMA. Proposed ECMAScript 4th Edition – Language Overview. 2008

• Siek, Garcia, Taha. Exploring the design space of higher-order casts. 2009

• Wadler, Findler. Well-typed programs can’t be blamed. 2009

Current team: Bard Bloom, John Field, Johan Ostlund, Gregor Richards, Brian Burg, Nick KiddAlumni: Tobias Wrigstad, Nate Nystrom, Rok StrinsaSponsor: IBM, ONR, NSF

Page 67: Of Scripts and Programs Tall tales, Urban Legends and Future …janvitek.org/talks/dls09.pdf · 2020-05-06 · • A modest Perl program hacked together to perform a simple data migration

Conclusion, actually

• Transforming a one-off script into a program requires language and virtual machine support

• Language design may have to make certain compromises in order to achieve performance

• Rewarding programmer effort is essential for adoption

• More information:

THORN tutorial @ OOPSLA

Talk in the main track

Online-demohttp://thorn-lang.org