Specifying JavaScript in ML · Slide 9 Meeting in the middle precise, accurate feasible informal...

31
Specifying JavaScript in ML Dave Herman Cormac Flanagan

Transcript of Specifying JavaScript in ML · Slide 9 Meeting in the middle precise, accurate feasible informal...

Page 1: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

Specifying JavaScript in ML

Dave Herman

Cormac Flanagan

Page 2: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 2

JavaScript: the “j” in Ajax

Page 3: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 3

JavaScript is not Java-Lite.

JavaScript is almost nothing like Java!

� Multi-paradigm

� Functional! Lexically-scoped!

� Prototypes, no classes (until now…)

� Dynamically typed (until now…)

Page 4: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 4

Outline

I. How we got here

II. Specifying JavaScript with ML

III. The evolution of JavaScript

IV. Status report

Page 5: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 5

I. Testimonial: coming to ML

Page 6: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 6

Standardized JavaScript

� Many implementations exist (multiple browsers, JVM, Flash, …)

� Highly competitive market

� First standardized at Ecma Int’l in ’97

Page 7: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 7

Existing JavaScript specifications

Grammar production:

ConditionalExpression →

LogicalORExpression ???? AssignmentExpression :::: AssignmentExpression

Evaluation:

1. Evaluate LogicalORExpression.

2. Call GetValue(Result(1)).

3. Call ToBoolean(Result(2)).

4. If Result(3) is false, go to step 8.

5. Evaluate the first AssignmentExpression.

6. Call GetValue(Result(5)).

7. Return Result(6).

8. Evaluate the second AssignmentExpression.

9. Call GetValue(Result(8)).

10. Return Result(9).

Any bugs in there?Who knows?

Page 8: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 8

Existing JavaScript specifications

From: Brendan Eich

Date: 12/2/2005

To: Dave Herman

Subject: Specifying JavaScript semantics

...In my view, we TG1’ers desperately need a machine-

checkable specification... Others in TG1 may agree in

principle, but are not wild about taking too much time to

develop a checkable spec. So we are looking for help...

Page 9: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 9

Meeting in the middle

precise,accurate

feasible

informal prose,pseudocode

formal methods,mechanized frameworks

definitionalinterpreter

Page 10: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 10

One interpreter, several artifacts

� Read—literate source

� Run, test—executable

� Prove theorems—source code (maybe)

Page 11: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 11

An executable spec in SML/NJ

� First-level sanity checks

� Tests: JS2 standard library,JS2 → Flash VM compiler

� Biggest benefit: Mozilla/Adobe regression test suites for JavaScript 1.x

Page 12: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 12

II. Specifying JavaScript with SML

Page 13: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 13

Interpreter style

� No gratuitous optimization

� No gratuitous mutation (duh!)

� Modularize language feature encodings (“keep the monster in a box”)

� Big-step, impure, direct styleeval : EXPR * ENV -> VAL

Page 14: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 14

Exploiting ML: algebraic datatypes

datatype EXPR =UnaryExpr of (UNOP * EXPR)

| BinaryExpr of (BINOP * EXPR * EXPR)| TernaryExpr of (EXPR * EXPR * EXPR)| LiteralExpr of LITERAL| CallExpr of { func: EXPR,

actuals: EXPR list }| ...

and STMT =ExprStmt of EXPR

| ForStmt of FOR_STMT| WhileStmt of WHILE_STMT| IfStmt of { cnd: EXPR,

thn: STMT,els: STMT }

| ...

Page 15: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 15

Exploiting ML: refs

datatype VAL =Object of OBJ

| Null| Undef

and OBJ =Obj of { ident: int,

tag: VAL_TAG,proto: VAL,props: PROP_BINDINGS }

and VAL_TAG =ObjectTag of FIELD_TYPE list

| ArrayTag of TYPE_EXPR list| FunctionTag of FUNC_TYPE| ClassTag of NAME

withtype PROP = { ty: TYPE_EXPR,attrs: ATTRS,state: VAL }

and PROP_BINDINGS = (NAME * PROP) list ref

Page 16: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 16

Exploiting ML: exceptions

� Non-local jumps in JS: return, throw, tail calls

� Tail calls via modified trampoline:

exception TailCallException of (unit -> VAL)

� Non-tail function call protocol:

[[expr]]handle TailCallException thunk => thunk()

Page 17: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 17

Exploiting (S)ML(/NJ): callcc

� JavaScript generators (coroutines)

� yield instead of return: suspend current procedure activation as an object

function nats() {

var i = 0;

while (true) { yield i; i++; }

}

var gen = nats();

print(gen.next()); // 0

print(gen.next()); // 1

print(gen.next()); // 2

Page 18: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 18

Exploiting (S)ML(/NJ): callcc

� yield captures a (delimited) continuation

� Alternatives: threads, delimited continuations, CPS

� Benefit of callcc: localizing the encoding

Page 19: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 19

Exploiting ML: modules

datatype OBJ = Obj of { ... props: PROP_BINDINGS ... }

withtype PROP = { ty: TYPE_EXPR,

attrs: ATTRS,

state: VAL }

and PROP_BINDINGS = (NAME * PROP) list ref

Page 20: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 20

Exploiting ML: modules

datatype OBJ = Obj of { ... props: PROP_BINDINGS ... }

withtype PROP = { ty: TYPE_EXPR,

attrs: ATTRS,

state: VAL }

and PROP_BINDINGS = (PROP NameMap.map) ref

structure NameMap = BinaryMapFn (NameKey);

(* structure NameMap = SplayMapFn (NameKey); *)

(* structure NameMap = ListMapFn (NameKey); *)

Page 21: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 21

Limitations of code

� Underspecification: sort must be n log(n)

� Overspecification: type(x) reveals concrete implementation of interface

� Conclusion: prose is still necessary

Page 22: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 22

Frustrations with SML

� Non-nested withtype declarations

� Deciphering type inference errors

� No recursive modules

Page 23: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 23

III. The evolution of JavaScriptTyped functional programming for the web

Page 24: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 24

JavaScript 1.x/ECMAScript Edition 3

� Dynamically typed

� Mostly well-behaved scoping

� Prototypes inspired by Self

� Objects: (string × value) table

Page 25: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 25

JavaScript 2.0/ECMAScript Edition 4

� “Opt-in” static typing

� Improved lexical scoping

� Prototypes and classes

� Objects: ((key × string) × value) table

� Generous syntactic sugar

� Advanced control constructs (generators)

Page 26: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 26

Gradual typing

� Conservative extension of dynamically typed JavaScript 1.x

� Optional type annotations

� Optional static type checker

� Careful interface between typed and untyped code

Page 27: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 27

Type system

� Nominal types (Java, C#)

� Structural typestype thunk = function():int;

function foo(f:thunk):[int] { ... }

var obj : { m:function(thunk):[int] } = { m:foo };

� Type “dynamic”:var image : * = read(filename);

switch type (image) { ... }

� Parameterized types

Page 28: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 28

IV. Status report

Page 29: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 29

Status report

� Feature freeze, coding and writing

� ~25 KLOC mostly pure SML, ~12 KLOC JS2 standard libraries

� SML/NJ, proof-of-concept ports to SML.NET and MLton

� Release: 2008 (tentatively)

Page 30: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 30

Steal this source code

� Read, run, test �

� Prove theorems?

� Industry-scale case study for research

� Experimental language extensions

� Development tools, IDE’s

� Inference, migration tools

� …what else? Have at it:www.ecmascript.org/download.php

Page 31: Specifying JavaScript in ML ·  Slide 9 Meeting in the middle precise, accurate feasible informal prose, pseudocode formal methods, mechanized frameworks definitional

http://www.ecmascript.org Slide 31

Thank you!

http://www.ecmascript.org