Shayan Ehsani Hessameddin Akhlaghpour. Remembrance History Eval funciton Applications.

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
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.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Storage Reclamation Explicit Erasure Reference Count Garbage collection.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
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.
Principles of Programming Chapter 1: Introduction  In this chapter you will learn about:  Overview of Computer Component  Overview of Programming 
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Functional Programming in Scheme and Lisp.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Functional Programming: Lisp MacLennan Chapter 10.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Tail Recursion.
Introduction to Scheme
Scheme : variant of LISP
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
Env. Model Implementation
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
Cons operation calls for the allocation of a new cons-cell, like new in Pascal. How to make it free?
Abstraction and Repetition
Today’s topics Abstractions Procedural Data
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

Shayan Ehsani Hessameddin Akhlaghpour

Remembrance History Eval funciton Applications

 (defun sqr (x) (times x x) )  List processors : car,cdr,cons,mapcar.  Functions : bu,rev,assoc.  (lambda (x y) (sqrt (plus (sqr x) (sqr y) (sqr z) ) ) )  e.g. (sqr 3) = ( (lambda (x) (times x x)) 3 ) 3

 LISP was first implemented by Steve Russell on an IBM 704 computer.Steve RussellIBM 704  It was a working LISP interpreter which could be used to run LISP programs, or more properly, 'evaluate LISP expressions.‘  Since it is written in LISP, it makes use of the facilities of LISP such as car,cdr,etc. 4

 The LISP universal function is conventionally called eval since it evaluates a LISP expression.  Eval function shows how strong LISP is ! 5

 General form : (eval ‘E A)  Example : (eval ‘(cons (quote A ) (quote ( B C ) ) ) nil ) (eval ‘(sqr x) ( (x 3) (y 4) )  A is a data structure representing the context in which the evaluation to be done. 6

The first step is to classify LISP expressions:  Numeric atom 2  Nonnumeric atom val  Quotation (quote (B C D ))  Conditional (if (null x) nil 1)  Primitive (cons x y)  User defined (mtable text nil) 7

 (defun eval (e a) (if (atom e) Handle atoms Handle lists ))  There are two kinds of atoms-numeric and nonnumeric. 8

 The value of a numeric atoms is that atom !  (eval 2 a) = 2  (( and (atome e) (numberp e)) e )  Nonnumeric atoms (val,text,etc) must be looked up in the enviroment.  The result of evaluating them is the value to which thay are bound. 9

 The value which an atom is bound is determined by the environment.  This is the purpose of the second parameter.  One of the simplest way to represent an environment is an association list.  ( (val 2) (text (to be or not to be )) ) 10

 We have to look up nonnumeric atoms in the association list representing current environment.  (eval e a) = (assoc e a)  (defun eval (e a) (cond (( and (atom e) (numberp e)) e) (( atom e) (assoc e a) ) …. )) 11

 Quote  Quote : The whole purpose of quote is to prevent the evaluation of its argument.  (eval ‘(quote x ) a) = x  ((eq (car e ) ‘quote) (cadr e)) 12

 Conditional  The conditional expression is different from other built-in functions : it’s argument is not evaluate unitl its value is needed.  (if P T F) : First evaluate P if it is t, then evaluate T; it is nil evaluate F.  (if (eval (cadr e) a) (eval (caddr e) a) (eval (cadddr e) a))  We are using if to interpret if!  We are calling eval recursively! 13

 Primitives and user-defined  General form : (f x1 x2 … xn)  First we should evaluate arguments.  (evargs (cdr e) a) : will be the list of argument values.  We need to construct a list, the ith element of which is (eval xi a)  We need to use mapcar. 14

 Primitives and user-defined  ((bu (rev ‘eval) a) xi) = (eval xi a)  (defun evargs (x a) (mapcar (bu (rev ‘eval) a) x ))  We define (apply f x a) in out interpreter.  (apply (car e) (evargs (cdr e) a) a) ) 15

 Primitives :  The natural structure for the apply function is a cond that handles primitives.  Again we use plus to interpret plus and car to interpret car...  (defun apply (f x a) (cond ( (eq f ‘plus) (plus (car x) (cadr x)) ) ( (eq f ‘car) (car (car x)) ) ( ( eq f ‘eq) (eq (car x) (cadr x) ) ) …. ) 16

 User-defined  The steps required to handle user-defined functions is very similar to the steps to invoke a procedure in other languages.  The environment must bind all of the names used in the expression.  The environment is composed of locals and non- locals. 17

 User-defined : 1.Evaluate the actual parameters. 2.Bind the formal parameters to the actual parameters. 3.Add these new bindings to the environment of evaluation. 4.Evaluate the body of the function in this envirnment. 18

 User-defined : Constructing the environment  (consval text)  (lambda (x) (cons val x ) )  (apply f (x 1, x 2, x 3,…, x n ) a)  (apply ‘consval (ab dad) ((val baba) (consval (lambda (x) (cons val x) ) ) )  L = ( lambda (v 1, v 2, …,v n ) B )  LE = ((v 1,x 1 )…(v n,x n ))  LE = (mapcar ‘list (cadr L) x )  EE = (append LE a)  (eval (caddr L) EE ) 19

 We use the let function  e.g. (let ((yek 1) (do 2)) (times yek do) )  (let ((L (eval f a) )) (let (( LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a ) ) )) 20

(defun eval (e a) (cond ((and (atom e) (numberp e)) e) ((atom e) (assoc e a)) ((eq (car e) ‘quote) (cadr e)) ((eq (car e) ‘if (if (eval (cadr e) a) (eval (caddr e) a) (eval (cadddr e) a)) ) (t (apply (car e) (evargs (cdr e) a) a) ) ) (defun evargs (x a) (mapcar (bu (rev ‘eval) a) x)) 21

(defun apply (f x a) (cond ((eq f ‘car) (car (car x)) ) ((eq f ‘cdr) (cdr (car x)) ) ((eq f ‘atom) (atom (car x)) ) ((eq f ‘null) (null (car x)) ) ((eq f ‘cons) (cons(car x)) ) ((eq f ‘eq) (eq(car x)) ). (t (let ((L (eval f a) )) (let ((LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a)) ) 22

 (function (lambda (x) (times val x))  We use closures for such cases  Closures consists of two parts:  An ip (instruction part) which points to the piece of program  An ep (environment part) which points to the environment in which the piece of program must be evaluated.  (closure ip ep) 23

(defun eval (e a) (cond ((and (atom e) (numberp e)) e). ((eq (car e) ‘function) (list ‘closure (cadr e) a)). (t (apply (car e) (evargs (cdr e) a) a) ) ) 24

 L = (closure (lambda (x1 … xn) B) ED)  LE = (mapcar ‘list (cadadr L) x)  EE = (append LE (caddr L)) (t (let ((L (eval f a) )) (let ((LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a)) ) 25

(defun apply (f x a) (cond ((eq f ‘car) (car (car x)) ). (t (let ((L (eval f a) )) (if (eq (car f) ‘closure) (let ((LE (mapcar ‘list (cadadr L) x) )) (eval (caddadr L) (append LE (caddr L))) ) (let ((LE (mapcar ‘list (cadr L) x) )) (eval (caddr L) (append LE a)) ) 26

Explicit Erasure Reference Count Garbage collection

 Adopted by Pascal (Dispose function)  Still used in C  Releasing each cell by Programmer 28

 Programmers must work harder  Who is the “Good” Programmer? ▪ Remember the words against Fortran.  Computers must take care of bookkeeping details.  Violating the Security Principle  Dangling Pointers (pointers that do not point to an allocated cell) 29 C Dangling Reference

 Using Reference Count  Include a reference count field in each cell 30

 Using Reference Count 31 decrement (C) : reference count (C) := reference count (C) -1; if reference count (C) = 0 then decrement (C.left); decrement (C.right); return C to free-list; end if.

 One Solution is to disallow cyclic structures by eliminating rplaca and rplacd pseudo- functions.  These pseudo-functions have side-effects and do not belong in an applicative programming language. Cycles are prone and difficult to understand. 32

 After the exhaustion of the free space, the system enters a “Garbage Collection Phase” in which it identifies all of the inaccessible cells and returns them to free storage.  The “mark-sweep” garbage collector operates in two phases: 1. The mark phase in which all accessible cells are identified and marked 2. The sweep phase in which all inaccessible cells are disposed 33

Mark phase: for each root R, mark(R) mark (R) : if R is not marked then: set mark bit of R; mark (R^. left); mark (R^. right); end if. 34

 Problem:  Mark phase is recursive and requires space for it’s activation records.  Garbage Collector is called only in crisis (no free storage available)  Solution:  Start garbage collection before the last cell is allocated and there is enough space for the stack.  Encode stack in a clever way (reversing link in the marked nodes). 35

 Expensive in a large address space  It should trace down all of the lists  Visit every cell in the memory  There is a high-speed execution of the program until a garbage collection take place.  The system will stop for minutes while a garbage collection is in the progress.  Solutions:  Parallel garbage collection 36

Flexibility Interlisp

 Lisp has simple structure syntax.  The representation of the Lisp programs as Lisp lists.  It has simplified writing programs that process other Lisp programs such as compilers and optimizer.  It is very easy to manage Lisp programs using other Lisp programs. ( As we saw in the eval interpreter )  Lisp programmers write many programming tools in Lisp  It has encouraged special–purpose extensions to Lisp for pattern matching, text processing, editing, type checking …  This tools are provided by conventional languages  So why? ▪ In conventional languages they are complicated because Pascal and … have character-oriented syntax 38

 Programming Environment  A system that supports all phases of programming including design, program entry, debugging, documentation, maintenance  Lisp programs can manipulate other Lisp programs has led to the development of a wide variety of Lisp programming tools.  The Interlisp programming environment  1966: BBN( Bolt Beraneck Newman)  1972: a joint project with Xerox PARC (Palo Alto Research Center) that was renamed Interlisp.  Grew during 1970s in response to the needs and ideas of implementers and users.  The combination of Lisp language and an experimental approach to tool developmentproduced a highly integrated but loosely coupled and flexible programming environment. 39

 LISP problems:  It is interpreted and often runs two orders of magnitude slower than the code produced by the compiler.  Recursion was inefficient on most machines and in LISP everything (even loops) are implemented recursively.  Dynamic Storage Allocation and Reclamation is slow.  Solutions:  LISP Compilers  LISP Machines 40

 “Do What I Mean”; Intelligent Error Correction  Macros  Undo and Redo 41

 Imperative languages :  Dependent heavily on assignment statements and a changeable memory for accomplishing a programming task.  Applicative languages:  The central idea is function application that is applying a function to it’s arguments. 42

 Lisp is the closest thing to an applicative language in widespread use.  The Lisp experience is evidence in favor of the practicality of functional programming languages. 43

 There is an emphasis on the use of pure functions  Syntactic structure:  Prefix notation  Data structure:  List is the principle data structure  Control structure:  Conditional expression and recursion are basic control structures. 44