# Building a system for symbolic differentiation

Size: px
Start display at page:

Transcription

1 Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e) Is e a variable? (same-variable? v 1 v 2 ) Are v 1 and v 2 the same variable? (sum? e) Is e a sum? (product? e) Is e a product? (addend e) Addend of the sum e. (augend e) Augend of the sum e. (multiplier e) Multiplier of the product e. (multiplicand e) Multiplicand of the product e. (make-constant c) Construct the constant c. (make-sum a 1 a 2 ) Construct the sum of a 1 and a 2. (make-product m 1 m 2 ) Construct product of m 1 and m 2. Differentiation procedure: (define (deriv exp var) (cond ((constant? exp) (make-constant 0)) ((variable? exp) (if (same-variable? exp var) (make-constant 1) (make-constant 0))) ((sum? exp) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) ((product? exp) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))))) d Recall x = 1, d y = 0, d (U + V ) = d U + d V, d (UV ) = U d V + V d U, and even dx dx dx dx dx dx dx dx d hi dx ho = ho d d hi hi dx hoho dx ho. 1

2 Simple representations: (define (constant? x) (number? x)) (define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2))) (define (make-constant x) x) (define (make-sum a1 a2) (list + a1 a2)) (define (make-product m1 m2) (list * m1 m2)) (define (sum? x) (if (not (atom? x)) (eq? (car x) +) nil)) (define (addend s) (cadr s)) (define (augend s) (caddr s)) (define (product? x) (if (not (atom? x)) (eq? (car x) *) nil)) (define (multiplier p) (cadr p)) (define (multiplicand p) (caddr p)) Simplification: (define (make-sum a1 a2) (cond ((and (number? a1) (number? a2)) (+ a1 a2)) ((number? a1) (if (= a1 0) a2 (list + a1 a2))) ((number? a2) (if (= a2 0) a1 (list + a1 a2))) (else (list + a1 a2)))) (define (make-product m1 m2) (cond ((and (number? m1) (number? m2)) (* m1 m2)) ((number? m1) (cond ((= m1 0) (make-constant 0)) ((= m1 1) m2) (else (list * m1 m2)))) ((number? m2) (cond ((= m2 0) (make-constant 0)) ((= m2 1) m1) (else (list * m1 m2)))) (else (list * m1 m2)))) Extensions to the system 2

3 Expanding to handle arbitrary numbers of arguments: (define (augend s) (if (null? (cdddr s)) (caddr s) (append (+) (cddr s)))) (define (multiplicand p) (if (null? (cdddr p)) (caddr p) (append (*) (cddr p)))) Expanding to handle exponentiation: To the deriv procedure, add the following clause, and the subsequent procedures: ((exponential? exp) (make-product (make-product (exponent exp) (make-exponential (base exp) (- (exponent exp) 1))) (deriv (base exp) var))) (define (exponential? exp) (if (not (atom? exp)) (eq? (car exp) **))) (define (base exp) (cadr exp)) (define (exponent exp) (caddr exp)) (define (make-exponential b e) (cond ((= e 0) (make-constant 1)) ((= e 1) b) (else (list ** b e)))) Pattern Matching and Simplification The code on the following pages summarizes the pattern matcher that we will build in this lecture. The basic idea behind this simplification system is to build a set of rules, consisting of a pattern and an evaluation skeleton. Given an expression, we try to match the pattern to the expression, keeping track of the assignment of pattern variables to segments of the expression in a dictionary. If we can perform such a pattern match, then we use the skeleton part of the rule to indicate the procedures that should be evaluated to perform the simplification. 3

4 An example of the sort of rules we will wish to specify is the following set for algebraic simplification. (define algebra-rules ( ( ((? op) (?c e1) (?c e2)) (: (op e1 e2)) ) ( ((? op) (? e1) (?c e2)) ((: op) (: e2) (: e1)) ) ( (+ 0 (? e)) (: e) ) ( (* 1 (? e)) (: e) ) ( (* 0 (? e)) 0 ) ( (* (?c e1) (* (?c e2) (? e3))) (* (: (* e1 e2)) (: e3)) ) ( (* (? e1) (* (?c e2) (? e3))) (* (: e2) (* (: e1) (: e3))) ) ( (* (* (? e1) (? e2)) (? e3)) (* (: e1) (* (: e2) (: e3))) ) ( (+ (?c e1) (+ (?c e2) (? e3))) (+ (: (+ e1 e2)) (: e3)) ) ( (+ (? e1) (+ (?c e2) (? e3))) (+ (: e2) (+ (: e1) (: e3))) ) ( (+ (+ (? e1) (? e2)) (? e3)) (+ (: e1) (+ (: e2) (: e3))) ) ( (+ (* (?c c) (? a)) (* (?c d) (? a))) (* (: (+ c d)) (: a)) ) ( (* (? c) (+ (? d) (? e))) (+ (* (: c) (: d)) (* (: c) (: e))) ) )) A simple matcher is given below, built on the appropriate data abstractions. This will be used to match the left side of rules against data expressions. (define (match pattern expression dictionary) (cond ((eq? dictionary failed) failed) ((atom? pattern) (if (atom? expression) (if (eq? pattern expression) dictionary failed) failed)) ((arbitrary-constant? pattern) (if (constant? expression) (extend-dictionary pattern expression dictionary) failed)) ((arbitrary-variable? pattern) (if (variable? expression) (extend-dictionary pattern expression dictionary) failed)) ((arbitrary-expression? pattern) (extend-dictionary pattern expression dictionary)) ((atom? expression) failed) (else (match (cdr pattern) (cdr expression) (match (car pattern) (car expression) dictionary))))) 4

5 Once we have a consistent match, we instantiate the skeleton part of the rule, evaluating when appropriate. (define (instantiate skeleton dictionary) (cond ((atom? skeleton) skeleton) ((skeleton-evaluation? skeleton) (evaluate (evaluation-expression skeleton) dictionary)) (else (cons (instantiate (car skeleton) dictionary) (instantiate (cdr skeleton) dictionary))))) The control of the rule base matcher consists of a series of tightly interwoven recursive loops. The basic idea is that given an expression, we apply the simplification rules in order until we either succeed or run out of rules. Moreover, if the expression to be simplified is compound, this control pattern is applied first to the elementary components of the expression, and then to the resulting simplified whole expression. The procedure simplifier on the next page returns a procedure, based on a set of rules, that can be applied to any expression. (define (simplifier the-rules) (define (simplify-exp exp) (try-rules (if (compound? exp) (simplify-parts exp) exp))) (define (simplify-parts exp) (if (null? exp) () (cons (simplify-exp (car exp)) (simplify-parts (cdr exp))))) (define (try-rules exp) (define (scan rules) (if (null? rules) exp (let ((dictionary (match (pattern (car rules)) exp (make-empty-dictionary)))) (if (eq? dictionary failed) (scan (cdr rules)) (simplify-exp (instantiate (skeleton (car rules)) dictionary)))))) (scan the-rules)) simplify-exp) 5

6 Having built our simplification system, we now must build the data abstractions on which it rests. The dictionary is represented as a list of pairs, each pair consisting of the variable name and its associated value from the expression. Note that assq is a primitive Scheme procedure which has the behaviour of scanning a list of pairs, testing a key against the first element of each pair. If it finds a pair whose first element is equal to the key, then that pair is returned. (define (make-empty-dictionary) ()) (define (extend-dictionary pat dat dictionary) (let ((vname (variable-name pat))) (let ((v (assq vname dictionary))) (cond ((null? v) (cons (list vname dat) dictionary)) ((equal? (cadr v) dat) dictionary) (else failed))))) (define (lookup var dictionary) (let ((v (assq var dictionary))) (if (null? v) var (cadr v)))) Predicates for types of expressions are straightforward. (define (compound? exp) (pair? exp)) (define (constant? exp) (number? exp)) (define (variable? exp) (atom? exp)) A rule is represented by a list of two parts, a pattern and a skeleton. (define (pattern rule) (car rule)) (define (skeleton rule) (cadr rule)) Within each pattern, explicit objects are represented by themselves, while generic objects are represented by a list, the first element of which identifies the type of generic object, and the second of which identifies the variable name of that object within the pattern. We distinguish three types of generic objects. (define (arbitrary-constant? pat) (if (pair? pat) (eq? (car pat)?c) false)) (define (arbitrary-expression? pat) (if (pair? pat) (eq? (car pat)?) false)) (define (arbitrary-variable? pat) (if (pair? pat) (eq? (car pat)?v) false)) (define (variable-name pat) (cadr pat)) On the skeleton side of the rule, we have two procedures (define (skeleton-evaluation? pat) (if (pair? pat) (eq? (car pat) :) false)) (define (evaluation-expression evaluation) (cadr evaluation)) The following procedure is included for completeness, but you need not worry about understanding exactly what it does until later in the term. For the purposes of this lecture, you can treat it as an abstraction that evaluates a given form, relative to the assignment of values to variables indicated in the dictionary. 6

7 (define (evaluate form dictionary) (if (atom? form) (lookup form dictionary) (apply (eval (lookup (car form) dictionary) user-initial-environment) (map (lambda (v) (lookup v dictionary)) (cdr form))))) Using the rules we called algebra-rules earlier we can define an algebraic simplification function as: (define algsimp (simplifier algebra-rules)) Exactly the same system can be used to build other symbolic simplifiers. The following rules create a simple symbolic differentiator. (define deriv-rules (( (dd (?c c) (? v)) 0 ) ( (dd (?v v) (? v)) 1 ) ( (dd (?v u) (? v)) 0 ) ( (dd (+ (? x1) (? x2)) (? v)) (+ (dd (: x1) (: v)) (dd (: x2) (: v))) ) ( (dd (* (? x1) (? x2)) (? v)) (+ (* (: x1) (dd (: x2) (: v))) (* (dd (: x1) (: v)) (: x2))) ) ( (dd (** (? x) (?c n)) (? v)) (* (* (: n) (** (: x) (: (- n 1)))) (dd (: x) (: v))) ) )) (define dsimp (simplifier deriv-rules)) In closing, it is worth considering that important aspects of the kind of evaluation characterizing Scheme itself can be captured by such rules. This should not be entirely surprising, since these tables of rules define certain fixed kinds of substitutions, and it is the substitution model that is our current model of Scheme. For example: (define scheme-rules (( (square (?c n)) (: (* n n)) ) ( (fact 0) 1 ) ( (fact (?c n)) (* (: n) (fact (: (- n 1)))) ) ( (fib 0) 0 ) ( (fib 1) 1 ) ( (fib (?c n)) (+ (fib (: (- n 1))) (fib (: (- n 2)))) ) ( ((? op) (?c e1) (?c e2)) (: (op e1 e2)) ) )) (define scheme-evaluator (simplifier scheme-rules)) A good exercise (though by no means trivial) is to work out the substitution model itself via such a set of rules. 7

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

### Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1

### An Explicit-Continuation Metacircular Evaluator

Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

### A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

### 6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

### Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

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

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

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

.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

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

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

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

1 File: regsim.scm Register machine simulator from section 5.2 of STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS This file can be loaded into Scheme as a whole. Then you can define and simulate machines

### 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

6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

### CS61A Midterm 2 Review (v1.1)

Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

### 6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

### Documentation for LISP in BASIC

Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

### Project 2: Scheme Interpreter

Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

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

Deferred operations Continuations 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (define the-cons (cons 1 #f)) (set-cdr! the-cons the-cons) (define (run-in-circles l) (+

### Discussion 12 The MCE (solutions)

Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of

### CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

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

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

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

Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas

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

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

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

Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

### Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Simplifying Logical Formulas

Simplifying Logical Formulas Assume we have a SCHEME logical expression involving the operators and, or, not, and the truth constants #t and (). Example: (and a (not b) (or b c) (not (and c (not d))) #t

### SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

### Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

### ;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

### CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

### Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

### Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that

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

6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

### 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

1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 1998, Final Exam Solutions Your

### CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp http://www.cs.washington.edu/341/ Lists (list expr2... exprn) '(value1 value2...

### Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process

### CS450 - Structure of Higher Level Languages

Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.

### SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### 6.001 Notes: Section 15.1

6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

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

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

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

6.001 SICP Variations on a Scheme Scheme Evaluator A Grand Tour Techniques for language design: Interpretation: eval/appl Semantics vs. snta Sntactic transformations Building up a language... 3. 1. eval/appl

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

### Lecture Notes on Lisp A Brief Introduction

Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham

### CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

### From Syntactic Sugar to the Syntactic Meth Lab:

From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

### FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti Comments in Lisp ; comments

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

Page 1/11 (require (lib "trace")) Allow tracing to be turned on and off (define tracing #f) Define a string to hold the error messages created during syntax checking (define error msg ""); Used for fancy

### Lisp Basic Example Test Questions

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

### University of Massachusetts Lowell

University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

### 4.2 Variations on a Scheme -- Lazy Evaluation

[Go to first, previous, next page; contents; index] 4.2 Variations on a Scheme -- Lazy Evaluation Now that we have an evaluator expressed as a Lisp program, we can experiment with alternative choices in

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

6.034 Artificial Intelligence February 9, 2007 Recitation # 1 1 Lists and Trees (a) You are given two lists: (define x (list 1 2 3)) (define y (list 4 5 6)) What would the following evaluate to: (append

### CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

### Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

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

CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the

### C311 Lab #3 Representation Independence: Representation Independent Interpreters

C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction

### Type Inference and Type Habitation (5/10/2004)

1 Type Inference and Type Habitation (5/10/2004) Daniel P. Friedman, William E. Byrd, David W. Mack Computer Science Department, Indiana University Bloomington, IN 47405, USA Oleg Kiselyov Fleet Numerical

### CSc 520. Principles of Programming Languages 7: Scheme List Processing

CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

### 15 Unification and Embedded Languages in Lisp

15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed

### Functional Programming - 2. Higher Order Functions

Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

### cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number

### SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Lecture #24: Programming Languages and Programs

Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

Constructing Lists CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona The most important

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

### ALISP interpreter in Awk

ALISP interpreter in Awk Roger Rohrbach 1592 Union St., #94 San Francisco, CA 94123 January 3, 1989 ABSTRACT This note describes a simple interpreter for the LISP programming language, written in awk.

### Sample Final Exam Questions

91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will

### Lexical vs. Dynamic Scope

Intro Lexical vs. Dynamic Scope where do I point to? Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure points to (its defining environment). This is called

### A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

### (scheme-1) has lambda but NOT define

CS61A Lecture 12 2011-0-11 Colleen Lewis (calc) review (scheme-1) has lambda but NOT define Remember calc-apply? STk> (calc-apply '+ '(1 2 )) 6 STk> (calc-apply '* '(2 4 )) 24 STk> (calc-apply '/ '(10

### Imperative, OO and Functional Languages A C program is

Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,

### Typed Scheme: Scheme with Static Types

Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit

### Project 5 - The Meta-Circular Evaluator

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 2005 Project 5 - The Meta-Circular

### Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### 6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

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

Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

### Programming Languages

Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

### COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

### CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number

### FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti L I S P - Introduction L I S P

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

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

### SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized

MITOCW Lecture 4A PROFESSOR: Well, yesterday we learned a bit about symbolic manipulation, and we wrote a rather stylized program to implement a pile of calculus rule from the calculus book. Here on the

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### Proving Properties of Recursive Functions and Data Structures. CS 270 Math Foundations of CS Jeremy Johnson

Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson 1 Objective To implement and verify recursive functions for processing recursive data structures.

### Lists in Lisp and Scheme

Lists in Lisp and Scheme a Lists in Lisp and Scheme Lists are Lisp s fundamental data structures, but there are others Arrays, characters, strings, etc. Common Lisp has moved on from being merely a LISt

Administrivia Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp) Massachusetts Institute of Technology Project 0 was due today Reminder: