Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

Similar documents
Recursive Functions of Symbolic Expressions and Their Application, Part I

CS 314 Principles of Programming Languages

Functional Programming

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

FP Foundations, Scheme

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Languages

Introduction to Functional Programming and basic Lisp

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

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

Programming Languages

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming. Big Picture. Design of Programming Languages

A Small Interpreted Language

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

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Lambda Calculus LC-1

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Lambda Calculus. Gunnar Gotshalks LC-1

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

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

Control in Sequential Languages

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

Functional Languages. Hwansoo Han

CS 314 Principles of Programming Languages. Lecture 16

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

User-defined Functions. Conditional Expressions in Scheme

SOFTWARE ARCHITECTURE 6. LISP

Introduction to Scheme

Documentation for LISP in BASIC

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

Lambda Calculus see notes on Lambda Calculus

CS 314 Principles of Programming Languages

Imperative, OO and Functional Languages A C program is

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

A Brief Introduction to Scheme (II)

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

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

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

ALISP interpreter in Awk

CSCC24 Functional Programming Scheme Part 2

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

by the evening of Tuesday, Feb 6

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

LECTURE 16. Functional Programming

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Heap storage. Dynamic allocation and stacks are generally incompatible.

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Project 2: Scheme Interpreter

CS 242. Fundamentals. Reading: See last slide

Review of Functional Programming

Building a system for symbolic differentiation

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

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS 415 Midterm Exam Spring 2002

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Nano-Lisp The Tutorial Handbook

Lisp. Versions of LISP

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

CSc 520 Principles of Programming Languages

John McCarthy IBM 704

Seminar in Programming Languages

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

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

Architecture of LISP Machines. Kshitij Sudan March 6, 2008

LISP. Introduction (some words about history...) Theoretical Assumptions Bridging the Gap Overview of Common Lisp Lisp versus ML Conclusion

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Introduction to lambda calculus Part 3

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

From Syntactic Sugar to the Syntactic Meth Lab:

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Programming Systems in Artificial Intelligence Functional Programming

Pierce Ch. 3, 8, 11, 15. Type Systems

4/19/2018. Chapter 11 :: Functional Languages

1.3. Conditional expressions To express case distinctions like

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

CSc 520 Principles of Programming Languages

CS 314 Principles of Programming Languages

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

CMSC 331 Final Exam Section 0201 December 18, 2000

Programming Languages

Functional Programming Lecture 1: Introduction

CS 360 Programming Languages Interpreters

Scope, Functions, and Storage Management

15 Unification and Embedded Languages in Lisp

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Lisp Basic Example Test Questions

Lecture 16: Object Programming Languages

MIDTERM EXAMINATION - CS130 - Spring 2005

Chapter 1. Fundamentals of Higher Order Programming

CS61A Midterm 2 Review (v1.1)

Sample Final Exam Questions

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

Transcription:

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I by John McCarthy Ik-Soon Kim Winter School 2005 Feb 18, 2005 Overview Interesting paper with By John Mitchell Interesting paper with Good language ideas, succinct presentation Good language ideas, succinct presentation Insight into language design process Insight into language design process Important concepts Important concepts Interest in symbolic computation influenced design Interest in symbolic computation influenced design Use of simple machine model Use of simple machine model Attention to theoretical considerations Attention to theoretical considerations Recursive function theory, Lambda calculus Recursive function theory, Lambda calculus Various good ideas: Various good ideas: Program as data, garbage collection Program as data, garbage collection Overview

Motivation for Lisp Advice Taker Advice Taker process declarative and imperative sentences process declarative and imperative sentences make logical reasoning make logical reasoning Lisp was designed to facilitate experiments with Advice Taker Lisp was designed to facilitate experiments with Advice Taker Motivating application part of good language design Motivating application part of good language design Lisp symbolic computation, logic, experimental Lisp symbolic computation, logic, experimental C Unix O/S C Unix O/S Simula simulation Simula simulation Java web applet Java web applet Introduction Lisp implements the following mathematical concepts: Partial functions Propositional expressions and predicates Conditional expressions Lambda functions and recursive functions

Partial functions A function defined on a subset of its domain Common in real computation since partial operations ex) division nontermination ex) f(x) = if x=1 then 1 else x*f(x-1) Propositional expressions and predicates Propositional expressions have T or F as possible values have logical connectives: ("and"), ("or") and ("not") ex) x < y (x < y) (b= c) A predicate is a function whose range consists of T or F ex) prime(x)

Conditional expressions (p e,,p e ) 1 1 n n Generalized if-then-else If p 1 then e 1 otherwise if p 2 then e 2,..., otherwise if p n then e n. ex) (1< 2 4, 1> 2 3) = 4 (2 < 1 4, T 3) = 3 (2 < 1 0, T 0 3) = 3 (2 < 1 3, T 0 ) 0 undefined (2 < 1 3, 4 < 1 4) undefined Lambda functions and recursive functions Express anonymous functions form x 2 +y function f f(x,y) = x 2 +y anonymous function 2 λ ((x,y) x + y) Inadequate for naming functions defined recursively label (fact, λ ((x,y) (n = 0 1,T nifact(n 1))) )

Theoretical consideration Lisp is based on scheme for representing the partial recursive functions of a certain class of symbolic expressions Lisp uses Concept of computable (partial recursive) functions Want to express all computable functions Function expressions known from lambda calculus (developed A. Church) lambda calculus equivalent to Turing Machines, but provide useful syntax and computation rules Presents the Lisp syntax and semantics S-expressions S-functions Translation of S-functions into S-expressions Universal function eval (meta-circular interpter for Lisp)

S-expressions Atoms are distinguishable symbols Atomic symbols are S-expressions If e 1 and e 2 are S-expressions, so is (e 1. e 2 ) ex) A (A. B) ((A. B) C) Lists can be represented by S-expressions (e) (e. NIL) (e 1 e 2 e m ) (e 1. (e 2. ( (e m. NIL) ))) (e 1 e 2 e m. x) (e 1. (e 2. ( (e m. x) ))) S-functions S-functions are written in M-expressions fname[arg 1 ;arg 2 ; ;arg n ] Elementary S-functions atom[x] check whether x is an atomic symbol eq[x;y] check whether x and y are the same symbol car[x] car[(e 1. e 2 )] = e 1 cdr[x] cdr[(e 1. e 2 )] = e 2 cons[x;y] cons[x;y] = (x. y)

Recursive and Higher-order S-functions Recursive S-functions append[x;y] = (null[x] y,t cons[car[x];append[cdr[x];y]]) null[x] = atom[x] eq[x;nil] Higher-order functions takes a function as an argument or returns a function as a result compose[f;g] = [ λ [x] f[g[x]]] maplist[x;f]=(null[x] NIL,T cons[f[car[x]];maplist[cdr[x];f]) Translating S-functions into S-expressions Translating an M-expression M into M * If M is an S-expression, M is (QUOTE M) Variable and function names are converted into upper case letters f[e 1 ; ; e n ] is translated into (f * e 1* e n* ) * * * * * [p1 e 1,,pn e n] is (COND (p1 e ) (p e )) 1 n n * * * * { λ [[x 1; ;x n]; M]} is (LAMBDA (x x ) M ) 1 n * * * {label [f;m]} is (LABEL f M ) Regard program as data

Translation example label[append; λ [[x;y];(null[x] y,t cons[car[x];append[cdr[x];y]])]] Translation (LABEL APPEND (LAMBDA (X Y) (COND ((NULL X) Y) (T (CONS (CAR X) (APPEND (CDR X) Y)))))) Program as data Program and data have same representation Symbolic computation such as integration and differentiation Lisp handles program (or functions) as input or output ex) find integration or differentiation of input funciton (INTEGRAL (QUOTE (LAMBDA (X) (* 3 SQUARE X))))) Staged computations Manipulate code at runtime Macro processing Runtime code generation

Universal function eval (eval exp env) Compute exp under the env environment exp an S-expression translated from an S-function env a list of pairs of variable and its value A Lisp interpreter based on essential S-functions (meta-circular interpreter) An operational semantics for Lisp using Lisp We will skip the detailed code for eval Lisp programming system Present implementation issues for Lisp Representation of S-expressions Free Storage List (Garbage Collection) Public push-down list

Representation of S-expressions Memory cells Address Decrement Atoms and lists represented by cells Atom A 0 Atom B Prohibit circular lists for printing problem (allowed later) Shared lists A B A B A B Both structures could be printed as ((A. B). (A. B)) Whether lists are shared or not depends on the history of program execution (cons (cons 1 2) (cons 1 2)) (cons a a) where a = (cons 1 2)

Free-storage list Lisp keeps the free-storage list of free cells automatically Assume tag bits associated with data Need list of heap locations referred by program Algorithm: Set all tag bits to 0. Start from each location used directly in the program. Follow all links, changing tag bit to 1 Place all cells with tag = 0 on free-storage list Mark-and-sweep garbage collection algorithm Public push-down list A recursive function uses itself as a subroutine When a recursive function begins, it saves registers into public push-down list When a recursive functions exits, it restores registers from public push-down list It is called a stack today

Innovation in the Design of Lisp Expression-oriented function oriented conditional expressions recursive functions Abstract view of memory Cells instead of array of indexed locations Garbage colletion Public push-down list (stack) for recursive calls Programs as data Higher-order functions Conclusions Successful language symbolic computation, experimental programming Specific language ideas Expression-oriented: functions and recursion Lists as basic data structures Programs as data, with universal function eval Garbage collection

References McCathy, and their McCathy, and their computation by machine, CACM, Vol 3, No 4, 1960 computation by machine, CACM, Vol 3, No 4, 1960 John Mitchell s CS242 lecture note John Mitchell s CS242 lecture note