back 2 basics with proto jonathan bachrach mit ai lab

39
Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Upload: randell-weaver

Post on 12-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Back 2 Basics with Proto

Jonathan Bachrach

MIT AI Lab

Page 2: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto

• Goals

• Examples

• Relation

• Definition

• State

• Future

Page 3: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto Hello World

(format out “hello world”)

Page 4: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto Goals

• Simple• Productive• Powerful• Extensible• Dynamic• Efficient• Real-time

• Teaching and research vehicle

• Electronic music is domain to keep it honest

Page 5: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Simplicity

• 10K lines 10 page manual

• Hard limit – pressure makes pearls

Page 6: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Best of All Worlds

• Want scripting and delivery language rolled into one

• Tools work better

• No artificial boundaries and cliffs

• Never been done effectively

• Electronic music forces realism

Page 7: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto Ancestors

• Language Design is Difficult– Leverage proven ideas– Make progress in selective directions

• Ancestors– Scheme– Cecil– Dylan

Page 8: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto <=> Scheme

• Concise naming• Procedural macros• Objects all the way

• Long-winded naming• Rewrite rule only• Only records

Page 9: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto <=> Cecil

• Prefix syntax• Scheme inspired

special forms

• Infix syntax• Smalltalk inspired

special forms

Page 10: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto <=> Dylan

• Prefix syntax• Prototype-based• Procedural macros• Rationalized collection

protocol / hierarchy• Always open• Predicate types

• Infix syntax• Class-based• Rewrite-rule only …• Conflated collection

protocol / hierarchy• Sealing• Fixed set of types

Page 11: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Object Orientation

• Assume you know OO basics

• Motivations:– Abstraction– Reuse– Extensibility

Page 12: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Prototype-based OO

• Simplified object model

• No classes

• Cloning basic operation for instance and prototype creation

• Prototypes are special objects that serve as classes

• Inheritance follows cloned-from relation

Page 13: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: OO & MM

(dv <point> (isa <any>))

(slot <point> (point-x <int>) 0)

(slot <point> (point-y <int>) 0)

(dv p1 (isa <point>))

(dm + ((p1 <point>) (p2 <point>) => <point>)

(isa <point>

(set point-x (+ (point-x p1) (point-x p2)))

(set point-y (+ (point-y p1) (point-y p2))))

Page 14: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Language Design:User Goals -- The “ilities”

• Learnability

• Understandability

• Writability

• Modifiability

• Runnability

• Interoperability

Page 15: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Learnability

• Simple

• Small

• Regular

• Gentle learning curve

• Perlis: “Symmetry is a complexity reducing concept…; seek it everywhere.”

Page 16: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Learnability

• Simple and Small: – 16 special forms: if, seq, set, fun, let, loc, lab,

fin, dv, dm, dg, isa, slot, ds, ct, quote

– 7 macros: try, rep, mif, and, or, select, case

• Gentle Learning Curve: – Graceful transition from functional to object-oriented

programming

– Perlis: “Purely applicative languages are poorly applicable.”

Page 17: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Special Forms

IF (IF ,test ,then ,else)SEQ (SEQ ,@forms)SET (SET ,name ,form) | (SET (,name ,@args) ,form)LET (LET ((,var ,init) …) ,@body)FUN (FUN ,sig ,@body)LOC (LOC ((,name ,sig ,@body) …) .@body)LAB (LAB ,name ,@body)FIN (FIN ,protected-form ,@cleanup-forms)DV (DV ,var ,form)DM (DM ,name ,sig ,@body)DG (DG ,name ,sig)ISA (ISA (,@parents) ,@slot-inits)SLOT (SLOT ,owner ,var ,init)

sig (,@vars) | (,@vars => ,var)var ,name | (,name ,type)slot-init (SET ,name ,value)

Page 18: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Understandability

• Natural notation

• Simple to predict behavior

• Modular

• Models application domain

• Concise

Page 19: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Understandability

• Describable by a small interpreter– Size of interpreter is a measure of complexity

of language

• Regular syntax– Debatable whether prefix is natural, but it’s

simple, regular and easy to implement

Page 20: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Writability

• Expressive features and abstraction mechanisms

• Concise notation

• Domain-specific features and support

• No error-prone features

• Internal correctness checks (e.g., typechecking) to avoid errors

Page 21: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Error Proneness

• No out of language errors– At worst all errors will be be caught in

language at runtime– At best potential errors such as “no applicable

methods” will be caught statically earlier and in batch

• Unbiased dispatching and inheritance– Example: Method selection not based on

lexicographical order as in CLOS

Page 22: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Design Principle Two:Planned Serendipity

• Serendipity:– M-W: the faculty or phenomenon of finding

valuable or agreeable things not sought for

• Orthogonality– Collection of few independent powerful

features combinable without restriction

• Consistency

Page 23: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Serendipity

• Objects all the way down• Slots accessed only through calls to

generic’s• Simple orthogonal special forms• Expression oriented• Example:

– Exception handling can be built out of a few special forms: lab, fin, loc, …

Page 24: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Modifiability

• Minimal redundancy

• Hooks for extensibility included automatically

• Users equal partner in language design

• No features that make it hard to change code later

Page 25: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Extensible Syntax

• Syntactic Abstraction

• Procedural macros

• WSYWIG– Pattern matching– Code generation

• Example:(ds (unless ,test ,@body)

`(if (not ,test) (seq ,@body)))

Page 26: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Multimethods

• Can add methods outside original class definition:– (dm jb-print ((x <node>)) …)– (dm jb-print ((x <str>)) …)

Page 27: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Generic Accessors

• All slot access goes through generic function calls

• Can easily redefine these generic’s without affecting client code

Page 28: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Runnability

• Features for programmers to control efficiency

• Analyzable by compilers and other tools

Page 29: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Optional Types

• All bindings and parameters can take optional types

• Rapid prototype without types

• Add types for documentation and efficiency

• Example:(dm format (s msg (args …)) …)

(dm format ((s <stream>)(msg <str>) (args …)) …)

Page 30: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Proto: Pay as You Go

• Don’t charge for features not used• Pay more for features used in more complicated

ways• Examples:

– Dispatch• Just function call if method unambiguous from argument types• Otherwise require dynamic method lookup

– Proto’s bind-exit called “lab”• Local exits are set + goto• Non local exits must create a frame and stack alloc an exit

closure

Page 31: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

The Rub

• Support for evolutionary programming creates a serious challenge for implementers

• Straightforward implementations would exact a tremendous performance penalty

Page 32: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Implementation Strategy

• Simple dynamic compilation

• Maintains both– optimization and – interactivity

Page 33: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Initial Loose Compilation

• Very quick compilation

• Generate minimal dependencies – only names and macros

Page 34: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Dynamic Whole Program Compilation

• Assume complete information

• Perform aggressive type flow analysis– Chooses, clones and inlines methods

• Compilation can be triggered manually, through dependencies, or through feedback

Page 35: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Dependency Tracking

• Assumptions are tracked

• Changed assumptions trigger recompilation

• Based on Fun-O-Dylan approach– Dependencies logged on bindings– Record dependent and compilation stage

Page 36: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Simple Code Generator

• Focus is on high-level optimizations

• Potentially gen-code direct from AST with approximated peep-hole optimizations

Page 37: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Save Image

• Save executable copy of image to disk– Maintains optimizations and dependencies– Uses dump/undump approach of emacs

• Avoid hassles of– File formats– Databases– etc

Page 38: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Status

• Fully bootstrapped

• Module system on line by week’s end

• Native code-gen in progress

• Dependency tracking by summer’s end

• Flow-typist by summer’s end

Page 39: Back 2 Basics with Proto Jonathan Bachrach MIT AI Lab

Research Directions

• Language Design

• Dynamic parameterized types

• Dynamic Interfaces• Series• Macros

• Language Implementation

• Dynamic compilation• Analysis/optimizations• Visualization• Real-time