Class 30: Language Construction cs1120 Fall 2009 David Evans.

Slides:



Advertisements
Similar presentations
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
Advertisements

David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 23: Inheritance.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
The Object Oriented Programming Languages (OOPL). Done by: Tayeb El Alaoui Supervised by: Dr Driss Kettani.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Yixin Sun Class 17: Concurrency and OOP Fall 2010 UVa David Evans cs2220: Engineering Software.
“Doctoral Level Independent Study” Fall Semester 2006 Assess An Existing PhD Thesis Peng Michael Shao Yingdan Huang.
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
Class 24: Programming with Objects University of Virginia cs1120 David Evans.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping and Inheritance.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Class 37: Computability in Theory and Practice cs1120 Fall 2011 David Evans 21 November 2011 cs1120 Fall 2011 David Evans 21 November 2011.
SE-1010 Dr. Mark L. Hornick 1 Introduction to Object-Oriented Programming (OOP) Part 1.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 27: Viruses and Object-Oriented Programming.
Simula Programming Language Lenar Uri T. Barcelona.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 3: Rules of Evaluation.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 20: Objects I invented the term Object-
Types in programming languages1 What are types, and why do we need them?
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 26: Proving Uncomputability.
Alan Kay: LCC 2700: Intro to Computational Media Spring 2005.
Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Procedural Abstraction Object-Oriented Code.
Salman Marvasti Sharif University of Technology Winter 2015.
1 cs205: engineering software university of virginia fall 2006 GUI Design and Implementation Schedule design meetings this week.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 23: Programming with Objects.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Class 29: Charme School cs1120 Fall 2009 University of Virginia David Evans.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 22: Objects I invented the term Object-
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism © 2011 | PN AZRINA.
Class 28: Interpreters cs1120 Fall 2009 David Evans.
Lecture 3: Rules of Evaluation CS150: Computer Science
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 22: Objectifying Objects.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
Cs205: engineering software university of virginia fall 2006 David Evans Object-Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Behavioral Subtyping.
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
David Evans Class 20: Objects I invented the term Object-Oriented, and I can tell you I did not have C++ in mind. — Alan.
LCC 2700: Intro to Computational Media
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Class 22: Inheritance CS150: Computer Science University of Virginia
The role of abstractions
Class 19: Think Globally, Mutate Locally CS150: Computer Science
Lecture 21: Inheritance CS200: Computer Science University of Virginia
Lecture 28: Types of Types
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
6.001 SICP Variations on a Scheme
David Evans Lecture 32: Truthiness “It would appear that we have reached the limits of what it is possible to achieve.
Presentation transcript:

Class 30: Language Construction cs1120 Fall 2009 David Evans

Menu Completing the Charme Interpreter History of Object-Oriented Programming Checking Grades Plans for Exam 2 Checking Grades Plans for Exam 2

Software Voting Univac predicts big win for Eisenhower (1952) “We do have people complain and say they don’t get it, I completely understand what they’re saying, but it's not something I can control.” – Sheri Iachetta, Charlottesville general registrar

Recap To build an evaluator we need to: Figure out how to represent data in programs Last class: program text, Environment Today: Procedures, primitives Implement the evaluation rules For each evaluation rule, define a procedure that follows the behavior of that rule. Friday: application Last class: definitions, names Today: special forms, primitives def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression...) def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression...)

How should we represent procedures? (lambda (x) (+ x x))

Representing Procedures Environment pointer x (+ x x) Input parameters (in mouth) Procedure Body

class Procedure: def __init__(self, params, body, env): self._params = params self._body = body self._env = env def getParams(self): return self._params def getBody(self): return self._body def getEnvironment(self): return self._env def __str__(self): return ' ' % (str(self._params), str(self._body)) Procedure Class def mapply(proc, operands): if (isPrimitiveProcedure(proc)):... elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) return meval(proc.getBody(), newenv)

Evaluating Lambda Expressions def isSpecialForm(expr, keyword): return isinstance(expr, list) and len(expr) > 0 and expr[0] == keyword def isLambda(expr): return isSpecialForm(expr, 'lambda') def evalLambda(expr,env): assert isLambda(expr) if len(expr) != 3: evalError ("Bad lambda expression: %s" % str(expr)) return Procedure(expr[1], expr[2], env) parse("(lambda (a) (+ a 1))")[0] ['lambda', ['a'], ['+', 'a', '1']]

Recap def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression...) def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression...)

Making Primitives Evaluation Rule 1: Primitives A primitive evaluates to its pre-defined value. To implement a primitive, we need to give it its pre-defined value!

Primitives def isPrimitive(expr): return (isNumber(expr) or isPrimitiveProcedure(expr)) def isNumber(expr): return isinstance(expr, str) and expr.isdigit() def isPrimitiveProcedure(expr): return callable(expr) def evalPrimitive(expr): if isNumber(expr): return int(expr) else: return expr

Making Primitive Procedures def primitivePlus (operands): if (len(operands) == 0): return 0 else: return operands[0] + primitivePlus (operands[1:]) def primitiveEquals (operands): checkOperands (operands, 2, '=') return operands[0] == operands[1] def mapply(proc, operands): if (isPrimitiveProcedure(proc)): return proc(operands) elif isinstance(proc, Procedure):... def mapply(proc, operands): if (isPrimitiveProcedure(proc)): return proc(operands) elif isinstance(proc, Procedure):... To apply a primitive procedure, “just do it”.

So, what do we do with the primitives? def initializeGlobalEnvironment(): global globalEnvironment globalEnvironment = Environment(None) globalEnvironment.addVariable('true', True) globalEnvironment.addVariable('false', False) globalEnvironment.addVariable('+', primitivePlus) globalEnvironment.addVariable('-', primitiveMinus) globalEnvironment.addVariable('*', primitiveTimes) globalEnvironment.addVariable('=', primitiveEquals) globalEnvironment.addVariable('zero?', primitiveZero) globalEnvironment.addVariable('>', primitiveGreater) globalEnvironment.addVariable('<', primitiveLessThan)

Evaluation Rule 5: If IfExpression ::= (if Expression Predicate Expression Consequent Expression Alternate ) To evaluate an if expression: (a)Evaluate Expression Predicate. (b)If it evaluates to a false value, the value of the if expression is the value of Expression Alternate ; otherwise, the value of the if expression is the value of Expression Consequent. 14

Making Special Forms def isIf(expr): return isSpecialForm(expr, 'if') def evalIf(expr,env): assert isIf(expr) if len(expr) != 4: evalError ('Bad if expression: %s' % str(expr)) if meval(expr[1], env) != False: return meval(expr[2],env) else: return meval(expr[3],env)

All Done! def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression...) def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isIf(expr): return evalIf(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: error ('Unknown expression...) This is a universal programming language – we can define every procedure in Charme.

History of Object-Oriented Programming Object-oriented programming is an exceptionally bad idea which could only have originated in California. Edsger Dijkstra

Sketchpad Ivan Sutherland’s 1963 PhD thesis (supervised by Claude Shannon) Interactive drawing program Light pen

Components in Sketchpad

Objects in Sketchpad In the process of making the Sketchpad system operate, a few very general functions were developed which make no reference at all to the specific types of entities on which they operate. These general functions give the Sketchpad system the ability to operate on a wide range of problems. The motivation for making the functions as general as possible came from the desire to get as much result as possible from the programming effort involved. For example, the general function for expanding instances makes it possible for Sketchpad to handle any fixed geometry subpicture. The rewards that come from implementing general functions are so great that the author has become reluctant to write any programs for specific jobs. Each of the general functions implemented in the Sketchpad system abstracts, in some sense, some common property of pictures independent of the specific subject matter of the pictures themselves. Ivan Sutherland, Sketchpad: a Man-Machine Graphical Communication System, 1963

Simula Considered the first “object-oriented” programming language Language designed for simulation by Kristen Nygaard and Ole-Johan Dahl (Norway, 1962) Had special syntax for defining classes that packages state and procedures together

Counter in Simula class counter; integer count; begin procedure reset(); count := 0; end; procedure next(); count := count + 1; end; integer procedure current(); current := count; end; end Does this have everything we need for “object-oriented programming”?

Object-Oriented Programming Object-Oriented Programming is a state of mind where you program by thinking about objects It is difficult to reach that state of mind if your language doesn’t have mechanisms for packaging state and procedures (Python has class, Scheme has lambda expressions) Other things can help: dynamic dispatch, inheritance, automatic memory management, mixins, good donuts, etc.

XEROX Palo Alto Research Center (PARC) 1970s: Bitmapped display Graphical User Interface – Steve Jobs paid $1M to visit and PARC, and returned to make Apple Lisa/Mac Ethernet First personal computer (Alto) PostScript Printers Object-Oriented Programming

Dynabook, 1972 (Just a model) “Don’t worry about what anybody else is going to do… The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws!” — Alan Kay, 1971

Dynabook 1972 Tablet computer intended as tool for learning Alan Kay wanted children to program it also Hallway argument, Kay claims you could define “the most powerful language in the world in a page of code” Proof: Smalltalk Scheme is as powerful, but takes two pages Before the end of the course, we will see an equally powerful language that fits in ¼ page Proof: Smalltalk Scheme is as powerful, but takes two pages Before the end of the course, we will see an equally powerful language that fits in ¼ page

BYTE Magazine, August 1981

Smalltalk Everything is an object Objects communicate by sending and receiving messages Objects have their own state (which may contain other objects) How do you do 3 + 4? send the object 3 the message “+ 4”

Counter in Smalltalk class name counter instance variable names count new count <- 0 next count <- count + 1 current ^ count

So, who really was the first object-oriented programmer? Object-oriented programming is an exceptionally bad idea which could only have originated in California. Edsger Dijkstra

Ada, Countess of Lovelace, around 1843 By the word operation, we mean any process which alters the mutual relation of two or more things, be this relation of what kind it may. This is the most general definition, and would include all subjects in the universe. Again, it might act upon other things besides number, were objects found whose mutual fundamental relations could be expressed by those of the abstract science of operations... Supposing, for instance, that the fundamental relations of pitched sounds in the science of harmony and of musical composition were susceptible of such expression and adaptations, the engine might compose elaborate and scientific pieces of music of any degree of complexity or extent.

Charge Friday: changing the language rules PS7 due Monday Remember to vote (early and often!) in the Exam 2 poll before 5pm Thursday.