Turtles All The Way Down

Similar documents
Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional Programming. Pure Functional Programming

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CSc 520 Principles of Programming Languages

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Parsing Scheme (+ (* 2 3) 1) * 1

CS61A Midterm 2 Review (v1.1)

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Discussion 12 The MCE (solutions)

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Project 2: Scheme Interpreter

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

CS 314 Principles of Programming Languages. Lecture 16

Lexical vs. Dynamic Scope

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

(scheme-1) has lambda but NOT define

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

CS 314 Principles of Programming Languages

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

An Introduction to Scheme

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

TAIL RECURSION, SCOPE, AND PROJECT 4 11

FP Foundations, Scheme

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

A Brief Introduction to Scheme (II)

Documentation for LISP in BASIC

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Principles of Programming Languages

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

An Explicit-Continuation Metacircular Evaluator

CS 314 Principles of Programming Languages

Comp 311: Sample Midterm Examination

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

COP4020 Programming Assignment 1 - Spring 2011

Sample Final Exam Questions

Scheme: Strings Scheme: I/O

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

Administrivia. Simple data types

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CSE413 Midterm. Question Max Points Total 100

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Programming Languages

6.001: Structure and Interpretation of Computer Programs

Functional Programming

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

CS 275 Name Final Exam Solutions December 16, 2016

Intro. Scheme Basics. scm> 5 5. scm>

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

Lecture 16: Object Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Principles of Programming Languages 2017W, Functional Programming

April 2 to April 4, 2018

C311 Lab #3 Representation Independence: Representation Independent Interpreters

6.001, Spring Semester, 1998, Final Exam Your Name: 2 Question 1 (12 points): Each of the parts below should be treated as independent. For each part,

Evaluating Scheme Expressions

Scheme Basics > (butfirst '(help!)) ()

CS 360 Programming Languages Interpreters

Functional Programming. Pure Functional Languages

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Lecture 3: Expressions

CMSC 330: Organization of Programming Languages

User-defined Functions. Conditional Expressions in Scheme

Functional Programming. Pure Functional Languages

Announcements. Today s Menu

Functional Programming

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Organization of Programming Languages CS3200/5200N. Lecture 11

Introduction to Scheme

LECTURE 16. Functional Programming

CSCC24 Functional Programming Scheme Part 2

Vectors in Scheme. Data Structures in Scheme

CS61A Notes Week 13: Interpreters

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

CS 314 Principles of Programming Languages

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

SCHEME AND CALCULATOR 5b

Functional Languages. Hwansoo Han

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

SOFTWARE ARCHITECTURE 6. LISP

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

RbCl: A Reective Object-Oriented Concurrent Language. without a Run-time Kernel. Yuuji Ichisugi, Satoshi Matsuoka, Akinori Yonezawa

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Data abstraction, revisited

An Explicit Continuation Evaluator for Scheme

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

CSE 341: Programming Languages

Variables and Bindings

CSE341, Spring 2013, Final Examination June 13, 2013

Transcription:

Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of four elephants, who stand on the back of a giant turtle. Russell replied, Madame, what does the turtle stand on? The woman replied, That's easy. It's turtles all the way down.

Semantics of Programming Languages How do we define the meaning of computer programs? Denotational Construct mathematical objects which formally define the meaning of programs. Operational Translate the programs into a language with well defined semantics.

Semantics of Programming Languages How do we define the meaning of computer programs? Denotational Operational Construct mathematical objects which formally define the meaning of programs. Translate the programs into a language with well defined semantics. Metacircular Write an interpreter for programs written in the same language as the programs themselves.

assoc (define assoc (lambda (key alist) (if (null? alist) #f (if (eq? (car (car alist)) key) (car alist) (assoc key (cdr alist)))))) > (let ((x 2) (y 3)) y) 3 >(assoc 'y '((x 2) (y 3))) (y 3) frame

Environments and Lexical Scope (let ((x 4) (y 3) (let ((y 2) (z 3)) (let ((z 7)) (+ x y z)))) (((z 7)) ((y 2) (z 3)) ((x 4) (y 3))) frame 3 frame 2 environment frame 1

lookup (define lookup (lambda (var env) ((lambda (val) used instead of let (if val (car (cdr val)) (lookup var (cdr env)))) (assoc var (car env))))) > (let* ((x 2) (y 3)) (let ((x 4)) y)) 3 > (lookup 'y '(((x 4)) ((x 2)(y 3)))) 3 frame 2 frame 1 environment

Function Objects a.k.a. ''Closures'' (define make-closure (lambda (args body env) (cons (quote closure) (cons args (cons body (cons env (quote ()))))))) > (let ((y 3)) (lambda (x) (+ x y))) (closure (x) (+ x y) (((y 3)) global-env)) type args body frame 1 frame 2 environment closure

A Recursive Function (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1)))))) CLOSURE arguments (n) body (if (= n 0) 1 (* n (fact (- n 1)))) environment (((fact ) )) ouroboros

Mutually Recursive Functions (define odd? (lambda (n) (if (= n 0) #f (even? (- n 1))))) (define even? (lambda (n) (if (= n 0) #t (odd? (- n 1))))) CLOSURE CLOSURE arguments (n) arguments (n) body (if (= n 0) #t (odd? (- n 1))) body (if (= n 0) #f (even? (- n 1))) environment (((even? ) (odd? environment (((even? ) (odd? ) )) ) ))

Functional Programs Can Only Make Trees

Box and Pointer Notation > (cons 'adam '()) (adam) car adam > (define eden (cons 'adam 'eve)) (adam. eve) eden car adam cdr eve

set-car! > eden (adam. eve) eden car cdr eve adam > (set-car! eden 'apple)) (apple. eve) eden car apple cdr eve

Procedures With Side Effects > (display "Hello World") Hello World > > 7 > > 9 (define x 7) x (set! x 9) x > (define ls '(1 2 3 4)) > ls (1 2 3 4) > (set-cdr! ls 0) > ls (1. 0)

Sequencing Effects (define bonjour (lambda (name) (if (equal? name "Napoleon") (begin (display "Vive L'Empereur!") (newline)) (begin (display "Bonjour ") (display name) (display ".") (newline)))) > (bonjour "Napoleon") Vive L'Empereur! >

Environment in Box and Pointer Notation env (((foo 7) (bar 8)) ((foo 5) (x 3))) frame 2 frame 1

Adding a Definition to the Front Frame (define add-defn! (lambda (var val env) (set-car! env (cons (cons var (cons val (quote ()))) (car env))) (quote ok))) > (add-defn! 'fred 5 '(((foo 7) (bar 8)) ((foo 5) (x 3)))) ok

Mutual Recursion Between Eval and Apply Expressions are evaluated by applying functions to values... eval apply...functions are applied to values by evaluating expressions in environments.

Primitive Functions (define global-env (cons (cons (cons (quote car) (cons car (quote ()))) (cons (cons (quote cdr) (cons cdr (quote ()))) (cons (cons (quote cons) (cons cons (quote ()))) (cons (cons (quote set-car!) (cons set-car! (quote ()))) (cons (cons (quote null?) (cons null? (quote ()))) (cons (cons (quote eq?) (cons eq? (quote ()))) (cons (cons (quote apply) (cons apply (quote ()))) (cons (cons (quote symbol?) (cons symbol? (quote ()))) (cons (cons (quote pair?) (cons pair? (quote ()))) (quote ())))))))))) (quote ()))) minimum necessary to define eval

make-frame (define make-frame (lambda (vars vals) (if (null? vars) (quote ()) (cons (cons (car vars) (cons (car vals) (quote ()))) (make-frame (cdr vars) (cdr vals)))))) > (make-frame '(x y) '(1 2)) ((x 1) (y 2))

Applying a Function to Values Reduced to Evaluating an Expression in an Environment vals (apply-function foo '(5)) (closure (x) (+ x y) (((y 3)) global-env)) expression (+ x y) (x) (eval (closure-body foo) (cons (make-frame (closure-args foo) vals) (closure-env foo))) (5) (((y 3)) global-env) (((x 5)) ((y 3)) global-env) environment

eval-list and sequence (define eval-list map eval (lambda (ls env) (if (null? ls) (quote ()) (cons (eval (car ls) env) (eval-list (cdr ls) env))))) (define sequence map eval for effect and return last (lambda (ls env) (if (null? (cdr ls)) (eval (car ls) env) (begin (eval (car ls) env) (sequence (cdr ls) env)))))

Two More Helper Functions (define self-evaluating? (lambda (sexpr) (if (pair? sexpr) (if (eq? (car sexpr) (quote closure)) #t #f) (if (symbol? sexpr) #f #t)))) (define apply-function (lambda (proc vals) (if (pair? proc) closure-body eval closure-args (sequence (car (cdr (cdr proc))) (cons (make-frame (car (cdr proc)) vals) (car (cdr (cdr (cdr proc)))))) (apply proc vals)))) closure-env primitive function

eval special-forms (define eval (lambda (sexpr env) (if (self-evaluating? sexpr) self-evaluating sexpr (if (symbol? sexpr) symbol (lookup sexpr env) ((lambda (first) (if (eq? first (quote quote)) quote (car (cdr sexpr)) (if (eq? first (quote define)) (add-defn! (car (cdr sexpr)) define (eval (car (cdr (cdr sexpr))) env) env) (if (eq? first (quote if)) (if (eval (car (cdr sexpr)) env) if (eval (car (cdr (cdr sexpr))) env) (eval (car (cdr (cdr (cdr sexpr)))) env)) (if (eq? first (quote lambda)) (make-closure (car (cdr sexpr)) lambda (cdr (cdr sexpr)) env) (if (eq? first (quote begin)) begin (sequence (cdr sexpr) env) (apply-function (eval (car sexpr) env) (eval-list (cdr sexpr) env)))))))) (car sexpr))))))

(Socrates)