Recursive Functions of Symbolic Expressions and Their Application, Part I

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

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

Functional Programming. Big Picture. Design of Programming Languages

FP Foundations, Scheme

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

Functional Programming. Pure Functional Languages

A Small Interpreted Language

CS 314 Principles of Programming Languages

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

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

Functional Languages. Hwansoo Han

SOFTWARE ARCHITECTURE 6. LISP

Introduction to lambda calculus Part 3

Introduction to Scheme

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Documentation for LISP in BASIC

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Discussion 12 The MCE (solutions)

Introduction to Functional Programming and basic Lisp

LECTURE 16. Functional Programming

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

1.3. Conditional expressions To express case distinctions like

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

15 Unification and Embedded Languages in Lisp

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

Denotational Semantics. Domain Theory

Programming Languages

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

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

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

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

Chapter 15 Functional Programming Languages

CPS 506 Comparative Programming Languages. Programming Language Paradigm

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

Chapter 11 :: Functional Languages

Chapter 15. Functional Programming Languages

CS 6110 S14 Lecture 1 Introduction 24 January 2014

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

User-defined Functions. Conditional Expressions in Scheme

Functional programming with Common Lisp

Programming Languages Third Edition

Functional programming in LISP

Building a system for symbolic differentiation

Functional Programming Lecture 1: Introduction

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

CS 314 Principles of Programming Languages. Lecture 16

Lambda Calculus LC-1

Lambda Calculus see notes on Lambda Calculus

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

An introduction to Scheme

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Programming Languages

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

Software Paradigms (Lesson 4) Functional Programming Paradigm

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

Scheme: Expressions & Procedures

Control in Sequential Languages

Imperative, OO and Functional Languages A C program is

A Brief Introduction to Scheme (II)

λ-calculus Lecture 1 Venanzio Capretta MGS Nottingham

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

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

Lambda Calculus. Gunnar Gotshalks LC-1

Imperative languages

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

Lambda Calculus. Variables and Functions. cs3723 1

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

Functional Programming Languages (FPL)

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

CS 314 Principles of Programming Languages

Building a system for symbolic differentiation

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

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

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

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

Programming Language Pragmatics

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Functional Programming

Chapter 1. Fundamentals of Higher Order Programming

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

CSCC24 Functional Programming Scheme Part 2

6.001: Structure and Interpretation of Computer Programs

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

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

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

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

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

n λxy.x n y, [inc] [add] [mul] [exp] λn.λxy.x(nxy) λmn.m[inc]0 λmn.m([add]n)0 λmn.n([mul]m)1

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

LISP Programming. (23 (this is easy) hello 821)

6.001 Notes: Section 8.1

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

Transcription:

Recursive Functions of Symbolic Expressions and Their Application, Part I JOHN MCCARTHY Review: Amit Kirschenbaum Seminar in Programming Languages Recursive Functions of Symbolic Expressions and Their Application, Part I p. 1/43

Historical Background LISP ( LISt Processor ) is the second oldest programming language and is still in widespread use today. Defined by John McCarthy from M.I.T. Development began in the 1950s at IBM as FLPL - Fortran List Processing Language. Implementation developped for the IBM 704 computer by the A.I. group at M.I.T. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 2/43

Historical Backgroung (Cont d) The main requirement was a programming system for manipulating expressions representing formalized declerative and imperative sentences so that the Advice Taker could make deductions. Many dialects have been developed from LISP: Franz Lisp, MacLisp, ZetaLisp... Two important dialects Common Lisp - ANSI Standard Scheme - A simple and clean dialect. Will be used in our examples. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 3/43

Imperative Programming Program relies on modfying a state, using a sequence of commands. State is mainly modified by assignment Commands can be executed one after another by writing them sequentially. Commands can be executed conditinonally using if and repeatedly using while Program is a series of instructions on how to modify the state. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 4/43

Imperative Prog. (Cont d) Execution of program can be considered, abstractly as: s 0 s 1 s n Program starts at state s 0 including inputs Program passes through a finite sequence of state changes,by the commands, to get from s 0 to s n Program finishes in s n containing the outputs. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 5/43

Functional Programming A functional program is an expression, and executing a program means evaluating the expression. There is no state, meaning there are no variables. No assignments, since there is nothing to assign to. No sequencing. No repetition but recursive functions instead. Functions can be used more flexibly. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 6/43

Why use it? At first glance, a language without variables, assignments and sequencing seems very impractical Imperative languages have been developed as an abstraction of hardware from machine-code to assembler to FORTRAN and so on. Maybe a different approach is needed i.e, from human side. Perhaps functional languages are more suitable to people. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 7/43

Advantages of functional programming Clearer sematics. Programs correspond more directly to mathematical objects. More freedom in implementation e.g, parallel programs come for free. The flexible use of functions we gain elegance and better modularity of programs. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 8/43

Some Mathematical Concepts Partial Function - function that is defined only of part of its domain. Propositonal Expressions and Predicates - Expressions whose possible values are T (truth) and F (false). Conditional Expressions - Expressing the dependence of quantties on propositional quantities. Have the form (p 1 e 1,,p n e n ) Equivalent to If p 1 then e 1, else if p 2 then e 2, else if p n then e n Recursive Functions of Symbolic Expressions and Their Application, Part I p. 9/43

Mathematical Concepts (Cont d) Conditional expression can define noncommutative propositional connectives: p q = (p q,t F) p q = (p T,T q) p = (p F,T T) Recursive function definitions - Using conditional expressions, we can define recursive functions n! = (n = 0 1,T n (n 1)!) Functions are defined and used, using λ-notation. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 10/43

Brief intro to λ-calculus A formal system designed to investigate function definition function application recursion Can be called the smallest universal programming language. It is universal in the sense that any computable function can be expressed within this formalism. Thus, it is equivalent in expressive power to Turing machines. λ-calculus was developed by Alonzo Church in the 1930s Recursive Functions of Symbolic Expressions and Their Application, Part I p. 11/43

λ-notation Defining a function in mathematics means: Giving it a name. The value of the function is an expression in the formal arguments of the function. e.g., f(x) = x + 1 Using λ-notation we express it as a λ-expression λx. (+ x 1) It has no name. prefix notation is used. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 12/43

λ-notation (Cont d) The function f may be applied to the argument 1 : f(1) Similarly, the λ-expression may be applied to the argument 1 (λx. (+ x 1))1 Application here means Subtitue 1 for x: (+ x 1) (+ 1 1) Evaluate the function body: make the addition operation. Return the result: 2 Recursive Functions of Symbolic Expressions and Their Application, Part I p. 13/43

Syntax of λ-calculus Pure λ-calculus contains just three kinds of expressions variables (identifiers) function applications λ-abstractions (function defintions) It is convinient to add predefined constants (e.g., numbers) and operations (e.g., arithmetic operators) exp ::= var const ( exp exp ) (λ var. exp ) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 14/43

Function Application Application is of the form (E 1 E 2 ) E 1 is expected to be evaluated to a function. The function may be either a predefined one or one defined by a λ-abstraction. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 15/43

λ-abstractions The expression (λx. ( x 2)) is the function of x which multiplies x by 2 The part of the expression that occurs after λx is called the body of the expression. When application of λ-abstraction occurs, we return the result of the body evaluation. The body can be any λ-expression, therefore it may be a λ-abstraction. The parameter of λ-abstraction can be a function itself Recursive Functions of Symbolic Expressions and Their Application, Part I p. 16/43

λ-abstractions In mathematics there are also functions which return functions as values and have function arguments. Usually they are called operators or f unctionals For example: the differentiation operator. d dx x2 = 2x Recursive Functions of Symbolic Expressions and Their Application, Part I p. 17/43

Constants Pure λ-calculus doesn t have any constants like 0, 1, 2,... or built in functions like +,,,..., since they can be defined by λ-expressions. For the purpose of this discussion we ll assume we have them. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 18/43

Naming Expressions Expressions can be given names, for later reference: square (λx. ( x x)) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 19/43

Free and bound variables Consider the expression (λx. ( x y))2 x is bound: it is just the formal parameter of the function. y is free: we have to know its value in advance. A variable v is called bound in an expression E if there is some use of v in E that is bound by a decleration λv of v in E. A variable v is called free in an expression E if there is some use of v in E that is not bound by any decleration λv of v in E. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 20/43

Reduction rule The main rule for simplifiying expressions in λ-calculus is called β-reduction. Applying a λ-abstraction to an argument is an instance of its body in which free occurences of the formal parameter are substituted by the argument. parameter may occur multiple times in the body (λx. ( x x))4 ( 4 4) 16 Recursive Functions of Symbolic Expressions and Their Application, Part I p. 21/43

Reduction rule Functions may be arguments (λf.(f 3))(λx.( x 1)) (λx.( x 1))3 ( 3 1) 2 (λf.(f 3))(λx.( x 1)) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 22/43

Expressions for Recursive Functions The λ-notation is inadequte for defining functions recursively the function n! = (n = 0 1,T n (n 1)!) should be converted into! = λ((n)(n = 0 1,T n (n 1)!)) There is no clear reference from! inside the λ-clause, to the expression as a whole. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 23/43

Expressions for Recursive Functions A new notation: label(a, E) denotes the expression E, provided that occurences of a within Eare to be referred as a whole. For example, for the latter function the notation would be label(!,λ((n)(n = 0 1,T n (n 1)!) (There is a way to describe recursion in λ-calculus, using Y-combinator, but McCarthy doesn t use it.) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 24/43

S-Expressions A new class of Symbolic expressions. S-Expression are composed of the special characters ( - start of composed expression ) - end of composed expression - composition and an infinite set of distinguishable atomic symbols. e.g., A ABA APPLE-PIE-NUMBER-3 Recursive Functions of Symbolic Expressions and Their Application, Part I p. 25/43

S-Expression : Definition Atomic symbols are S-expression. if e 1 and e 2 are S-expressions then so is (e 1 e 2 ) examples AB (A B) ((AB C) D) S-expression is then simply an ordered pair. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 26/43

S-Expression : Lists The list (m 1,m 2,...,m n ) is represented by the S-expression (m 1 (m 2 ( (m n NIL) ))) NIL is an atomic symbol, used to terminate lists, also known as the empty list. (m) stands for (m NIL) (m 1,m 2,...,m n x) stands for (m 1 (m 2 ( (m n x) ))) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 27/43

M-expressions Meta-expressions are functions of S-expressions, also called S-functions. Written in conventional functional notation. There are some elementry S-functions and predicates Recursive Functions of Symbolic Expressions and Their Application, Part I p. 28/43

M-expressions atom - atom[x] has the value T or F according to whether x is atomic symbol. atom[x] = T. atom[(x A)] = F. eq - eq[x;y] is defined iff both x and y are symbols. eq[x;y] = T if x and y are the same symbol and eq[x;y] = F otherwise eq[x;x] = T. eq[x;a] = F. eq[x;(a B)] is undefined Recursive Functions of Symbolic Expressions and Their Application, Part I p. 29/43

M-expressions car - car[x] is defined iff x is not atomic. car[(e 1 e 2 )]=e 1 car[(x A)] = X. car[(x A) Y)]= (X A). cdr - cdr[x] is also defined iff x is not atomic. cdr[(e 1 e 2 )]=e 2 cdr[(x A)] = A. cdr[(x A) Y)]= Y. cons - cons[x;y] is defined for any x and y. It is the list constructor cons[(e 1 ;e 2 )]=(e 1 e 2 ) cons[x;a] = (X A). cons[(x A);Y]= ((X A) Y). Recursive Functions of Symbolic Expressions and Their Application, Part I p. 30/43

M-expressions Compositions of car and cdr arise very frequently. Many expressions can be written more concisely if we abbreviate. cadr[x] car[cdr[x]] caddr[x] car[cdr[cdr[x]]] cdadr[x] cdr[car[cdr[x]]] expressions are not defined for every x. depends on the list structure. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 31/43

Recursive S-functions Forming new functions of S-expression by conditional expression and recursive definition gives us much larger class of functions. In fact all computable functions. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 32/43

Recursive S-function examples ff[x] - returns the first atomic symbol of the S-expression x, ignoring the parentheses. ff[x] = [atom[x] x ; T ff[car[x]]] ff[(a B)] = [atom[(a B)] (A B) ; T ff[car[(a B)]]] = [F (A B);T ff[car[(a B)]]] = ff[car[(a B)]] = ff[a] = ff[atom[a] A ; T ff[car[a]]] = [T A ; T ff[car[a]]] = A Recursive Functions of Symbolic Expressions and Their Application, Part I p. 33/43

ransform M-expressions to S-expressions There is a transformation mechanism that translate an M-expression E into S-expression E if E is an S-expression, E is (QUOTE E). M-expression f[e 1 ;...;e n ] is translated to (f e 1...e n). Thus, {cons[a;b]} is (CONS (QUOTE A) (QUOTE B)) {[p 1 e 1 ];...;[p n e n ]} is (COND (p 1 e 1 )...(p n e n)) {λ[x 1 ;...;x n ]E} is (LAMBDA(x 1... x n ) E. {label[a; E]} is (LABEL a E ) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 34/43

What do we gain? Unifying Symbol-level and Meta-level, gives us a way to treat expressions over symbols exactly the same as symbols. Functions and data are the same. Thus we can write a program, which write another program and evaluating it. This is useful in AI. Furthermore, we can expand the language with new features. LISP interpreters are easily implemented in LISP. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 35/43

S-function apply Plays the theoretical role of a universal Turing machine and the practical role of an interpreter. Formally, If f is an S-expression for an S-function f and args is a list of arguments of the form (arg 1,...,arg n ) where arg 1,...,arg n are S-expressions, Then apply[f;args] and f [arg 1,...,arg n ] are defined for the same values of arg 1,...,arg n and are equal when defined. example: λ[[x;y];cons[car[x];y]] [(A,B); (C,D)] apply[(lambda, (X,Y )(CONS(CARX)Y ))((A B)(C D))] = (A C D) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 36/43

S-function eval serves both as a formal definition of the language and as an interpreter Before apply applies the function f on the list of arguments (arg 1,...,arg n ), it sends them to eval for evaluating the S-expressions which represents them. > (eval (lambda (x) (+ x 1))) #<procedure> (lambda (x) (+ x 1))) is an S-expression which repersents a function. eval evalutes it and return its value, which is indeed a function Recursive Functions of Symbolic Expressions and Their Application, Part I p. 37/43

Implementing eval (define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -- EVAL" exp)))) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 38/43

Strength of the mechanism Extending the language is done easily by adding required forms to eval. Just add syntax and evaluation rules. Paraphrasing Oscar Wilde: LISP programmers know the value of everything but the cost of nothing. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 39/43

The cost Performance of LISP systems became a growing issue Garbage Collection. Representation of internal structures. Became difficult to run on the memory-limited hardware of that time. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 40/43

LISP Machines The solution was LISP machine - a computer which has been optimized to run LISP efficiently and provide a good environment for programming in it. Typical optimizations to LISP machines Fast function calls. Efficient representation of lists. Hardware garbage collection. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 41/43

LISP in the real world de-facto standard in AI NLP Modelling speech and vision Some more AutoCAD Yahoo Store Emacs Mirai, the 3d animation package was used to create Gollum in Lord Of The Rings. Recursive Functions of Symbolic Expressions and Their Application, Part I p. 42/43

The End ((lambda(x)(x x)) (lambda(x)(x x))) Recursive Functions of Symbolic Expressions and Their Application, Part I p. 43/43