Meta-Circular Evaluation CMSC 11500 Introduction to Computer Programming November 20, 2002.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
ISBN Chapter 15 Functional Programming Languages.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Assignment: Changing Data CMSC Introduction to Computer Programming November 1, 2002.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Mutual Recursion: Web pages CMSC Introduction to Computer Programming November 25, 2002.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
Data Abstraction: Sets Binary Search Trees CMSC Introduction to Computer Programming October 30, 2002.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Scope: What’s in a Name? CMSC Introduction to Computer Programming October 16, 2002.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Data Abstraction: Sets
Functional Programming Languages
Functional Programming
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Introduction to Scheme
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Programming Language Concepts
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
Env. Model Implementation
Original material by Eric Grimson
Using local variable without initialization is an error.
Is everyone signed up on piazza?
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
You replace it with its simplest name
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Changing Data: (Continued)
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Meta-Circular Evaluation CMSC Introduction to Computer Programming November 20, 2002

Roadmap ● Recap: Streams ● Meta-circular Evaluation – Using Scheme to evaluate Scheme ● Building representations of scheme expressions – Starting point: Numeric expressions ● Data definitions – Numeric? ● Evaluating expressions ● Toward function application – Substitution

Recap: Streams ● Handling infinite data – Mechanism: (delay x) => promise; (force (delay x)) x ● Data definition: – (cons x (delay stream-of-numbers)) ● Template: – (define (fn-for-stream stream) –..(car stream)... (fn-for-stream (force (cdr stream))) ● Posints, filter-stream, map-stream, add-streams,...

Evaluators ● DrScheme: – 2 key actions ● Check structure of scheme expressions ● Evaluate scheme expressions ● Develop code to evaluate scheme expressions – In Scheme!! – “Meta-circular evaluation” ● Build evaluator for language in same language

Scheme Subset ● Simple arithmetic expressions – e.g. 3, (+ 1 4), (* 7 8), (+ (* 1 2) (+ 3 4)) ● With variables: – e.g. x, (+ 1 x), (* 3 y), (+ (+ 1 2) (*7 x)) ● Primitives: numbers, symbols, variables ● Arithmetic expressions: addition, multiplication – (define-struct add (left right)) – (define-struct mul (left right))

Simple Expressions& Representations ● 4 ● x ● (+ 1 2) ● (* 1 x) ● (+ (* 1 2) (+3 4)) ● 4 ● 'x ● (make-add 1 2) ● (make-mul 1 'x) ● (make-add – (make-mul 1 2) – (make-add 3 4))

Data Definition: Scheme Expressions ● A scheme-expression (s-exp) is – number – Symbol – (make-add left right) – (make-mul left right) ● Where left, right are s-exp

S-Exp Template (define (fn-for-s-exp sexp) (cond ((number? sexp)...) ((symbol? sexp)...) ((add? sexp)... (fn-for-sexp (add-left sexp)) (fn-for-sexp (add-right sexp))... ((mul? sexp)... (fn-for-sexp (mul-left sexp)) (fn-for-sexp (mul-right sexp))...

Numeric? ● Don't know how to evaluate “defines” – So restrict to pure numeric expressions ● No variables ● Contract: numeric?: sexp -> boolean ● Purpose: Determine if a pure numeric expression

Numeric? (define (numeric? sexp) (cond ((number? sexp) #t) ((symbol? sexp) #f) ((add? sexp) (and (numeric? (add-left sexp)) (numeric? (add-right sexp)))) ((mul? sexp) (and (numeric? (mul-left sexp)) (numeric? (mul-right sexp))))))

Evaluate-Expression ● Contract: – Evaluate-expression: sexp -> number ● Purpose: – Compute value of a numeric expression

Evaluate-Expression (define (evaluate-expression sexp) (cond ((number? sexp) sexp) ((symbol? sexp) (error “cannot eval var”)) ((add? sexp) (+ (evaluate-expression (add-left sexp)) (evaluate-expression (add-right sexp)))) ((mul? sexp) (* (evaluate-expression (mul-left sexp)) (evaluate-expression (mul-right sexp))))))

Toward Function Application ● (define (f x) (* x x)) ● Apply: (f 1) => 1 – In application, bind formal parameter to invoking val ● E.g. Bind x to 1 – Substitute 1 for all instances of x in scope of function – e.g. (* 1 1) ● To build evaluator for functions, – Need substitution

Subst ● Contract: – Subst: symbol number sexp -> sexp ● Purpose: – Replace all instances of symbol with number in sexp

Subst (define (subst var val sexp) (cond ((number? sexp) sexp) ((symbol? sexp) (if (symbol=? var sexp) val sexp)) ((add? sexp) (make-add (subst var val (add-left sexp)) (subst var val (add-right sexp)))) ((mul? sexp) (make-mul (subst var val (mul-left sexp)) (subst var val (mul-right sexp))))))