Eight languages Eight weeks How I designed and implemented a teaching OO language in 4 days.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Control Flow Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Garbage Collection. Terminology Heap – a finite pool of data cells, can be organized in many ways Roots - Pointers from the program into the Heap. – We.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Basic Semantics Associating meaning with language entities.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
Environments, Stores, and Interpreters. Overview As we study languages we will build small languages that illustrate language features We will use two.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
Design issues for Object-Oriented Languages
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Haskell Chapter 7.
Programming Languages and Compilers (CS 421)
Names and Attributes Names are a key programming language feature
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Implementing Recursion
Env. Model Implementation
5.4.1: Implementation Method Invocation
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Mini Language Interpreter Programming Languages (CS 550)
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
Procedures App B: SLLGEN 1.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
OO programming with Language E8
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
Environments, Stores, and Interpreters
Assignments and Procs w/Params
CSE 341 Lecture 11 b closures; scoping rules
Scope, Function Calls and Storage Management
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
Recursive Procedures and Scopes
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
Presentation transcript:

Eight languages Eight weeks How I designed and implemented a teaching OO language in 4 days.

Eight languages, Eight weeks prog := exp exp := var | int | '(' ':=' var exp ')' | '(' 'while' exp exp ')' | '(' 'if' exp exp exp ')' | '(' 'write' exp ')' | '(' 'block' { exp } ')' | '(' '+' exp exp ')' | '(' '-' exp exp ')' | '(' '*' exp exp ')' | '(' '/' exp exp ')' | '(' '<=' exp exp ')' (for i i (block (:= i (+ i 2)) 10) (block (write i) (:= i (+ i 3)))) 1.Exp language 2.While language 3.Functional language 4.Multiple parameter passing 5.Exceptions 6.Typed language 7.Module language 8.Object Oriented 9.Garbage collection

Friday Andrew Black and I sit down an discuss “What do students who are studying languages need to understand about OO languages?”

Some thoughts Objects Encapsulation Inheritance Dynamic Method Binding Classes Name binding Exceptions Typing

We outlined the semantics of an interpreter -- Environments data Env = Env { self:: Object -- The dynamic part, vars::[(Name,Binding)] -- The static part } data Binding = Val Object | Ref Addr | Action Code data Code = Prim ([Object] -> State -> IO(Object,State)) | Closure Name (Maybe Object) Env [Name] Exp -- Semantic objects data Value = DoneV | BoolV Bool | IntV Int | StringV String | ExceptV Name deriving (Eq,Ord) data Object = Obj{ value::(Maybe Value), fields::[(Name,Binding)], super:: (Maybe Object)} | Thunk Name (State -> IO(Object,State)) -- The interpretor eval :: Env -> Exp -> State -> IO (Object, State)

Sketch of abstract Syntax data Exp = Var SourcePos Name | Bool SourcePos Bool -- Literal objects | Int SourcePos Int | String SourcePos String | While Exp Exp -- Control structures | Write String Exp | Block [Exp] | If Exp Exp Exp -- (if x y z) --> (x.ifTrueFalse y z) | Asgn Exp Exp -- Only to Var not Def. | Apply Exp [Exp] -- (Apply x y z) ---> (Request x "apply" y z) | Lam [Name] Exp -- (\ ( a b c) e) this object has one method: apply | Local [Decl] Exp | ObjLit (Maybe Exp) [Decl] (Maybe Exp) | Request Exp Name [Exp] -- (exp.field e1 e2 e3) set a handler for field. | Return String Exp -- (return f x) return from the enclosing method f | Self SourcePos | Done SourcePos | EmptyObj SourcePos | Super SourcePos | Op String [Exp] -- ( ) --> (((4.+ 5).+ 6).+ 7)

Binding Methods introduce parameters which are statically scoped. Should there be other ways of binding objects to names? Inspiration. Let blocks and objects are different manifestations of binding.

Semantic Similarities Both introduce mutually recursive set of declarations. But – Let introduces a new local (static) scope which extends only over the the mutually recursive bindings and the body of the let. The body is executed. The value of the let is the value of the body. – Object encapsulates a new scope, it introduces no bindings at all. Each binding is reachable from the others via projection from “self”, the object itself. We run the body only to initialize the scope, and the value returned is the object itself.

Syntatctic Similarities data Exp m= … | Local [Decl] Exp | ObjLit (Maybe Exp) [Decl] (Maybe Exp) data Decl = MethodDecl Name [Name] Exp -- procedure | DefDecl Name Exp -- Immutable | VarDecl Name (Maybe Exp) -- Mutable

Names Let bound names and object fields live in two completely different name spaces. They have two different roles. – Let bound names, name objects – Object fields select fields. We use different key words in different contexts Let (names) Object (fields) fun method val def ref var

Examples (fun plus2 (x) (+ x 2)) (val tim 99) (ref tom) (val test (object (var x) (def color “red”) (method inc () (:= (self.x) (self.x.(+ 1)))) init (:= (self.x) 99)))

Invocation Method request – (x. length) – (x.+ 4) – (x.(+ 4). even) Function invocation – f x 3) – (f.apply x 3) Operator invocation – ( ) – ((3.+ 4).+ 5)

Let binding run (Local ds body) state = do { (bindings,state1) <- fixIO (evalRec (Just (self env)) (ext env) –- extend static scope ds ([],state)) ; (bindings2,state2) <- initialize state1 bindings ; eval (ext env bindings2) body state2}

Object creation run (ObjLit parent ms init) state = do { … -- inheritance stuff ; let extend allBs = -- extend dynamic scope env{self=(Obj val (allBs ++ inheritedms) super)} ; (allBs,st1) <- fixIO (evalRec Nothing extend ms ([],st0)) ; let selfObj = Obj val (reverse allBs ++ inheritedms) super ; … -- initializtion stuff ; return(selfObj,…)}

Encapsulation (fun pair ( x y) (object (def fst x) (def snd y) (method asString ( ) (++ "(" (self.fst.asString) "," (self.snd.asString) ")")))) An object is described by what requests it responds to (fst,snd)

Inheritance (val bool (object (def true (object inherits True (method and (x) x) (method or (x) self) (method not () (bool.false)) (method ifTrueFalse (x y) (x.apply)) )) (def false (object inherits False (method and (x) self) (method or (x) x) (method not () (bool.true)) (method ifTrueFalse (x y) (y.apply)) ))

Dynamic method binding (val i3 (object inherits done (def x 5) (def y 3) (method asString () (++ (super.asString) " + " (self.x.asString) " + " (self.y.asString))))) enter Exp> i3 done[x 5[+, -, *, /, asString, =, ], y 3[+, -, *, /, asString, =, ], asString, asString ] Which asString method does it answer to?

Name Binding vs record field declaration (fun temp (x y) (local (val hidden x) (object (def age y) (method foo (w) (+ hidden (self.age) w))))) (val tt 4 7))

Exceptions (fun elem (x xs) (local (ref ptr xs) (block (while (ptr.null)) (if (ptr.head.= x) (return elem 99) (:= ptr (ptr.tail)))) 0)))