Class 29: Charme School cs1120 Fall 2009 University of Virginia David Evans.

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Class 39: Universality cs1120 Fall 2009 David Evans University of Virginia.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 31: Types of Types.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Introduction to Python
Chapter 2 Writing Simple Programs
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Class 30: Language Construction cs1120 Fall 2009 David Evans.
Python for Informatics: Exploring Information
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
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.
Functions Chapter 4 Python for Informatics: Exploring Information
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 30: Laziness.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Variables, Expressions, and Statements
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 29: Typed Scheme MC Escher, Liberation.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Python Let’s get started!.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Class 28: Interpreters cs1120 Fall 2009 David Evans.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 6: Ordered Data Abstractions
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
Interpreting Exceptions UW CSE 160 Spring 2015 download examples from the calendar 1.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 27: Types of Types “It would appear.
Chapter 2 Writing Simple Programs
(Thunking about Thunks)
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
CSc 120 Introduction to Computer Programing II Adapted from slides by
Lecture 17: Environments CS200: Computer Science
CSc 120 Introduction to Computer Programing II Adapted from slides by
Class 27: Universal Turing Machines CS150: Computer Science
Class 30: Models of Computation CS200: Computer Science
Lecture 37: A Universal Computer
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Presented By S.Yamuna AP/IT
Original material by Eric Grimson
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
Data types Numeric types Sequence types float int bool list str
Lecture 28: Types of Types
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Lecture 24: Metalinguistics CS200: Computer Science
Lecture 27: In Praise of Idleness CS200: Computer Science
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Lecture 2 Python Programming & Data Types
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Class 31: Universal Turing Machines CS200: Computer Science
6.001 SICP Interpretation Parts of an interpreter
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 3: Rules of Evaluation CS200: Computer Science
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
David Evans Lecture 32: Truthiness “It would appear that we have reached the limits of what it is possible to achieve.
Lecture 25: The Metacircular Evaluator Eval Apply
Python Reserved Words Poster
Control 9 / 25 / 19.
Lecture 6 - Recursion.
Presentation transcript:

Class 29: Charme School cs1120 Fall 2009 University of Virginia David Evans

PS6: Charlottansville is an interesting and scary place! People who get drunk and do silly things: FirstYear, Greek, FratStar, PartyingStudent, FratBoy Things that attack students: Zombie, Aliens, Corpses, Grue, Person with swine flu who infects other students, Cops who give parking tickets, Swiper who steals clothes, Tablers who hand out flyers, BrotherMicah who proselytizes Squirrels: DevilSquirrel, Squirrel, Squirrel FirstYear, Visitor, NewStudent, HighSchooler Hullabahoo GusBurger StreakingGnome Academicish things: Nutritionist; Coach, AssistantCoach; TAs who can be “bribed” with Treats; SlackerStudent who avoids library and makes dumb CS jokes; KingPython, LordEvans “MINION SCHEMERS! WE MUST CONQUER THE LANDS WITH OUR INTERPRETERS! DO NOT LOSE YOUR STRENGTH AND PERSERVERANCE!”

Building an Evaluator To build an evaluator we need to: – Figure out how to represent data in programs What is a procedure, frame, environment, etc. – Implement the evaluation rules For each evaluation rule, define a procedure that follows the behavior of that rule.

How should we represent programs? “(+ 1 2)” Its a string of characters ['+', '1', '2'] Represent as a Python object: lists give structure: We provide a definition of parse(s). It takes a string as input, and outputs a list of Python objects showing the structure of the input program text. You should understand it but won’t need to change it in PS7.

Examples >>> parse("(+ 1 2)")[0] ['+', '1', '2'] >>> parse("(define x 3)")[0] ['define', 'x', '3'] >>> parse("(define x (+ 1 2))")[0] ['define', 'x', ['+', '1', '2']] >>> parse("(define bigger (lambda (a b) (if (> a b) a b))))")[0] Parse error: Unmatched close paren: (define bigger (lambda (a b) (if (> a b) a b)))) Traceback (most recent call last): File " ", line 1, in parse("(define bigger (lambda (a b) (if (> a b) a b))))")[0] TypeError: 'NoneType' object is unsubscriptable >>> parse("(define bigger (lambda (a b) (if (> a b) a b)))")[0] ['define', 'bigger', ['lambda', ['a', 'b'], ['if', ['>', 'a', 'b'], 'a’ 'b']]]

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 type: ' + str(expr)) Core of the evaluator: meval

Representing Environments global environment count : 3 x: 12 name: “Alice”

Environments class Environment: def __init__(self, parent): self._parent = parent self._frame = {} def addVariable(self, name, value): self._frame[name] = value def lookupVariable(self, name): if self._frame.has_key(name): return self._frame[name] elif (self._parent): return self._parent.lookupVariable(name) else: evalError('Undefined name: %s' % (name))

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 type: ' + str(expr))

Definitions def isDefinition(expr): return isSpecialForm(expr, 'define') def evalDefinition(expr, env): assert isDefinition(expr) if len(expr) != 3: evalError ('Bad definition: %s' % str(expr)) name = expr[1] if isinstance(name, str): value = meval(expr[2], env) env.addVariable(name, value) else: evalError ('Bad definition: %s' % str(expr))

Names def isName(expr): return isinstance(expr, str) def evalName(expr, env): assert isName(expr) return env.lookupVariable(expr) class Environment:... def lookupVariable(self, name): if self._frame.has_key(name): return self._frame[name] elif (self._parent): return self._parent.lookupVariable(name) else: evalError('Undefined name: %s' % (name)) class Environment:... def lookupVariable(self, name): if self._frame.has_key(name): return self._frame[name] elif (self._parent): return self._parent.lookupVariable(name) else: evalError('Undefined name: %s' % (name))

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 type: ' + str(expr))

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”.

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 type: ' + str(expr)) What’s left: special forms (evalIf) setting up the global environment What’s left: special forms (evalIf) setting up the global environment

Charge Start working on PS7 (after you’ve finished reading Chapter 11) 16 Front abc8a … dwa2x eab8d … jsw8a jta9nk … mz2hos9e … wch9a