Prof. Fateman CS 164 Lecture 161 Properly Tail Recursive Interpreter. Some timings of compilation vs interpretation Lecture 16.

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Recursion CS 367 – Introduction to Data Structures.
1 Compiler Construction Intermediate Code Generation.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS AM MWF 10 Evans.
Prof. Fateman CS 164 Lecture 261 Functional Tiger, Calling variations Lecture 26.
Prof. Fateman CS 164 Lecture 151 Language definition by interpreter, translator, continued Lecture 15.
Prof. Fateman CS164 Lecture 241 Other Control Flow ideas: Throw, Catch, Continuations and Call/CC Lecture 24.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Prof. Fateman CS 164 Lecture 141 Language definition by interpreter Lecture 14.
Introduction CSC 358/458 3/27/2006. Outline Introductions Course goals Course organization History of Lisp Lisp syntax Examples Lisp IDE.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
For loops in programming Assumes you have seen assignment statements and print statements.
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
1 CS 177 Week 12 Recitation Slides Running Time and Performance.
Prof. Fateman CS 164 Lecture 281 Virtual Machine Structure Lecture 28.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Introduction to Lisp
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
Prof. Fateman CS 164 Lecture 181 Language definition by interpreter Lecture 18.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
Syntax-Directed Translation
Mini Language Interpreter Programming Languages (CS 550)
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
The Metacircular Evaluator
CSE401 Introduction to Compiler Construction
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
Statement-Level Control Structures
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Introduction to Intermediate Code, virtual machine implementation
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
Yan Shi CS/SE 2630 Lecture Notes
Functional MiniJava, Calling variations
Common Lisp II.
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
CISC101 Reminders Assignment 3 due today.
Programming Languages
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
Presentation transcript:

Prof. Fateman CS 164 Lecture 161 Properly Tail Recursive Interpreter. Some timings of compilation vs interpretation Lecture 16

Prof. Fateman CS 164 Lecture 162 Why do we want to eliminate recursion? Uses up stack Required by Scheme language specification (not required in CL but generally done if optimizing) Makes compiling more obvious Applicable to Java, too (defun (mydo f j lim) (cond ((> j lim) ‘done) (t (funcall f j)(mydo f (+ j 1) lim)))) ;; or equivalent in Scheme

Prof. Fateman CS 164 Lecture 163 Tail recursion elimination ;; A PROPERLY TAIL-RECURSIVE INTERPRETER (approx for mj) (defun mj-statement (x &optional env) ;; (block nil ;; block establishes a tagbody with possible labels. :TOP ;; this is a label for a goto statement (yes, Lisp has “go” within block!) (return (cond ((symbolp x) (get-var x env)) ((atom x) x);; integers and strings are atoms. (t (case (first x) (Block ;;remove Block to get at args (pop x) ;; interpret all but the last arg (loop while (rest x) do (mj-statement (pop x) env)) ;; finally, rename the last expression as x < The KEY (setf x (first x)) (go :TOP)) (Assign ( mj-set-var (idname (elt x 1))(elt x 2) env)) ;;;;..more

Prof. Fateman CS 164 Lecture 164 Tail recursion elimination ;; more of the above program (defun mj-statement (x &optional env) ;;;………snipped out (IfExp (setf x (if (equal ‘true (mj-exp-eval (elt x 1) env)) (elt x 2) (elt x 3))) (go :TOP)) ;; more snipped out, not changed..

Prof. Fateman CS 164 Lecture 165 Tail recursive elimination (more) Much simpler in a more functional-programming setting; one way of looking at this is to have “continuations” that look like this..evaluate-with- continuation (defun eval-w-c (expr env more) … ;; (Times x y)  evaluate x to X, then (eval-w-c y env #’(lambda(R)(* X R)) But don’t call eval-w-c, just reset expr, more. Also what about previous “more”) #’(lambda(R)(more (* X r)).

Prof. Fateman CS 164 Lecture 166 A change of pace Simple-interp.lisp is a collection of lisp programs that can be compiled or interpreted Figure that compiling a lisp program makes it about 10 or 20 times faster, but somewhat harder to debug. We can also implement MJ by rewriting the AST into lisp via simple-translate and Interpreting that as a lisp program. Or we can compile that rewritten program with the common lisp compiler. Semantics should be the same. What differences are there?

Prof. Fateman CS 164 Lecture Queens program benchmark Classic computing problem based on a chess game: how many ways can 8 queens be placed on a board without attacking each other?

Prof. Fateman CS 164 Lecture Queens program benchmark Classic computing problem based on a chess game: how many ways can 8 queens be placed on a board without attacking each other? One of 92 solutions

Prof. Fateman CS 164 Lecture Queens program benchmark (We did this 3 years ago in CS164. This is a Tiger program) let var N := 8 type intArray = array of int var row := intArray [ N ] of 0 var col := intArray [ N ] of 0 var diag1 := intArray [N+N-1] of 0 var diag2 := intArray [N+N-1] of 0 function printboard() = (for i := 0 to N-1 do (for j := 0 to N-1 do print(if col[i]=j then " O" else "."); print("\n")); print("\n")) function try(c:int) = ( /* for i:= 0 to c do print("."); print("\n"); flush();*/ if c=N then printboard() else for r := 0 to N-1 do if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 then (row[r]:=1; diag1[r+c]:=1; diag2[r+7-c]:=1; col[c]:=r; try(c+1); row[r]:=0; diag1[r+c]:=0; diag2[r+7-c]:=0) ) in try(0) end

Prof. Fateman CS 164 Lecture Queens program benchmark Remove almost all printing of the chess board, so that it runs at CPU speed. Do you expect these results?

Prof. Fateman CS 164 Lecture 1611 Speed/ compiled/ interpreted (8 Queens) running the tiger interpreter interpreted in common lisp: ; cpu time (total) 67,837 msec (00:01:07.837) 68 sec using 56,000,000 bytes of cons cells, 1.1 gigabytes of other memory compiling the interpreter but still interpreting queensc.tig cpu time (total) 721 msec user 0.7 sec using 181,000 bytes of cons cells translating the queensc.tig program into a lisp program ; cpu time (total) 2,795 msec user, 0 msec system 2.8 sec using 2,000,000 bytes of cons cells "compiling" the resulting lisp program ; cpu time (total) 10 msec user, 0 msec system 0.01 sec using 193 cons cells.

Prof. Fateman CS 164 Lecture 1612 Some typechecking hints.. Don’t be afraid of adding some more information fields, e.g. who uses this variable? The notion of “same” type is not “equal” type fields. Keep looking for type errors after the first one,even if they are likely to be bogus. Make sure that you are checking every piece of the code. Assume the AST is “well-formed” i.e. you don’t need to check that the parser was correct.

Prof. Fateman CS 164 Lecture 1613 Reminder: Exam coming up!! Nov 2, in class, Would you like 2 pages of notes? (2-sides?) this time? Topics: everything so far, but emphasis on material since exam 1. Certainly LR, LALR, interpreter, typechecker, subtleties of MiniJava semantics.