CS321 Functional Programming 2 © JAS 2005 6-1 The SECD Machine The SECD machine has become a classic way of implementing functional languages. It is based.

Slides:



Advertisements
Similar presentations
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Advertisements

CPU Review and Programming Models CT101 – Computing Systems.
Register Allocation CS 671 March 27, CS 671 – Spring Register Allocation - Motivation Consider adding two numbers together: Advantages: Fewer.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
The University of Adelaide, School of Computer Science
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
COSC 2006 Chapter 7 Stacks III
Arithmetic Expressions
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Infix, Postfix, Prefix.
CS 536 Spring Code generation I Lecture 20.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Lecture 17 Today’s Lecture –Instruction formats Little versus big endian Internal storage in the CPU: stacks vs. registers Number of operands and instruction.
Chapter 3 Introduction to Collections – Stacks Modified
CS321 Functional Programming 2 © JAS Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC Lecture 4 Procedures, last call optimization.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
CS321 Functional Programming 2 © JAS The λ Calculus This is a formal system to capture the ideas of function abstraction and application introduced.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CS321 Functional Programming 2 © JAS Implementation using Combinators This approach builds upon the λ-calculus model. The aim is to translate.
Lecture 7 Macro Review Stack Frames
Formal Languages, Automata and Models of Computation
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Functions.
Edited by Original material by Eric Grimson
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
PDAs Accept Context-Free Languages
© Craig Zilles (adapted from slides by Howard Huang)
6.001 SICP Compilation Context: special purpose vs. universal machines
PDAs Accept Context-Free Languages
Closures and Streams cs784(Prasad) L11Clos
Stacks Chapter 4.
Stack application: postponing data usage
NPDAs Accept Context-Free Languages
Declarative Computation Model Kernel language semantics (Non-)Suspendable statements (VRH ) Carlos Varela RPI October 11, 2007 Adapted with.
Variables, Environments and Closures
The Metacircular Evaluator
The Metacircular Evaluator
Programming Language Principles
Streams, Delayed Evaluation and a Normal Order Interpreter
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
6.001 SICP Environment model
(Part 2) Infix, Prefix & Postfix
… NPDAs continued.
© Craig Zilles (adapted from slides by Howard Huang)
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

CS321 Functional Programming 2 © JAS The SECD Machine The SECD machine has become a classic way of implementing functional languages. It is based on an automaton designed by Peter Landin in the early sixties. As with the Combinator approach it assumes that the program is expressed as a λ-expression which is one of A variable An abstraction (bound variable part and body) An application (operator and operand)

CS321 Functional Programming 2 © JAS The SECD machine uses four stacks. These are The Stack which is used to store the partial results of evaluating an expression, and which will hold the final result of the machine's execution. The Environment which consists of a series of pairs that give values to the free variables of an expression; the value of a constant in any environment being that constant. The first element of any pair is an identifier and the second element is the associated value. The Control which stores the expression that is being evaluated. The Dump which is used to store copies of the four stacks whilst a sub-function is being evaluated.

CS321 Functional Programming 2 © JAS We can now present an outline algorithm for the implementation of the SECD machine. Since the design was intended to be used for implementation on a conventional sequential machine it is appropriate to present the algorithm in a sequential style.

CS321 Functional Programming 2 © JAS WHILE there is an expression to be evaluated OR there is a suspended computation to be resumed DO IF the evaluation of the current expression is complete THEN resume the last suspended computation with the newly computed value on S ELSE C is not empty D is not empty C is empty remember the value on S; recover S,E,C,D from D; push the value from the old S onto S

CS321 Functional Programming 2 © JAS CASE the next expression to be evaluated OFidentifier: push value of this identifier onto S and pop C λ-exp: push the appropriate closure onto S and pop C top of C find value of identifier in E; push it onto S; pop C push the triple (body, bvpart, E) onto S; pop C

CS321 Functional Programming 2 © JAS application: replace top of C by the expressions representing this application pop C; push the ap operator onto C; push the operator to be applied onto C; push the operand onto C

CS321 Functional Programming 2 © JAS ap: cause the operator on S to be applied to the operand below it IF hd(S) is a closure THEN push 4-tuple (tail(tail(S)),E,(tail(C),D) onto D; push body of closure onto new empty C; make E the environment from closure prefixed by pair associating bvpart of closure with hd(tail(S)); create a new empty S ELSE { the operator is a primitive function } pop C; find result of applying hd(S) to hd(tail(S)); pop S twice; push result onto S ENDC

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD (λx.λy.+xy) 34 ap

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD 4 ap (λx.λy.+xy)3 ap cl(λy.+xy,x,())

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD 4 ap 3 cl(λy.+xy,x,()) (,(),,()) λy.+xy (x,3)

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD 4 ap (,(),,()) λy.+xy (x,3) cl(+xy,y,(x,3))

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD 4 ap (x,3) cl(+xy,y,(x,3)) (y,4) + x y ((),(),(),())

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD (x,3) (y,4) ((),(),(),()) +xy ap 4 3

CS321 Functional Programming 2 © JAS An example evaluation – (λx.λy.+xy) 3 4 SECD (x,3) (y,4) ((),(),(),()) ap

CS321 Functional Programming 2 © JAS The SECD machine implements eager (applicative order) evaluation since it evaluates the arguments first. The evluation of the body of the function is delayed by the closure mechanism. A lazy SECD machine can be defined by introducing the concept of a suspension. Primitive functions may be regarded as strict, but other functions are lazy so their parameters are saved as a suspension which consists of the unevaluated parameter and the environment in which it is to be evaluated.

CS321 Functional Programming 2 © JAS The evaluation algorithm needs to add an extra case for a suspended computation, and also modify the actions required for an application and an ap operator. We will consider first the modification for the application case: application: replace top of C by the expressions representing this application pop C; push the ap operator onto C; push the operator to be applied onto C; push the operand onto C IF operand is function THEN push operand onto C ELSE create suspension – push pair (operand, E) onto S

CS321 Functional Programming 2 © JAS When evaluating the ap operator we have another option to consider:- If the operator is a closure, or has its necessary operands then the actions are as before. If the operator is a strict primitive function and its operand is a suspension we need to create a new set of stacks to evaluate the suspension. push the 4-tuple (S,E,C,D) onto D; push the sp operator onto C; push the body of the suspension onto C; push the environment of the suspension onto E; create a new empty S

CS321 Functional Programming 2 © JAS Finally we need to be able to evaluate the sp operator when it is at the top of the C stack. This involves resuming the last incomplete suspended computation with all references to the completed suspension replaced by the computed value. remember hd(S); recover S, E, C and D from hd(D); throughout S and E replace all occurrences of the suspension that is hd(tl(S)) by what was hd(S)

CS321 Functional Programming 2 © JAS S E C D (y,6) (λx.4) y (y,6) ap, (λx.4), y 6 (y,6) ap, (λx.4) 6,cl(4,x,((y,6)) (y,6) ap (y,6),(x,6) 4 ((),(y,6),(),()) 4 (y,6),(x,6) ((),(y,6),(),()) 4 (y,6) Consider an example evaluation using the standard eager machine

CS321 Functional Programming 2 © JAS S E C D (y,6) (λx.4) y su(y,(y,6)) (y,6) ap, (λx.4) su(y,(y,6)), cl(4,x,((y,6)) (y,6) ap (y,6),(x,su(y,(y,6))) 4 ((),(y,6),(),()) 4 (y,6),(x,su(y,(y,6))) ((),(y,6),(),()) 4 (y,6) The same example evaluation using a lazy machine