MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte Software and Computer Systems School of Information and Communication Technology.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
1 Compiler Construction Intermediate Code Generation.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
CS 536 Spring Code generation I Lecture 20.
Fall 2008ACS-1805 Ron McFadyen1 Ch 8 Recursion Recursion occurs when a method (or function) calls itself.
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Stacks. 2 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.
SUMMARY ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and Communication Technology.
Catriel Beeri Pls/Winter 2004/5 environment1 1 The Environment Model  Introduction and overview  A look at the execution model  Dynamic scoping  Static.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ITERATIVE COMPUTATIONS CONCURRENCY ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
FUNCTIONAL PROGRAMMING IN ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and.
Functional Programming Element of Functional Programming.
Imperative Programming
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Runtime Environments Compiler Construction Chapter 7.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC Lecture 4 Procedures, last call optimization.
Chapter 3 Part II Describing Syntax and Semantics.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CMSC 330: Organization of Programming Languages Operational Semantics.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Run-Time Environments Chapter 7
Functions.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Introduction to Parsing
A lightening tour in 45 minutes
CS 3304 Comparative Languages
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
UNIT V Run Time Environments.
Lecture 2 מבוא מורחב.
Course Overview PART I: overview material PART II: inside a compiler
Lecture 2 מבוא מורחב.
CS210- Lecture 3 Jun 6, 2005 Announcements
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Presentation transcript:

MINI ERLANG ID1218 Lecture Christian Schulte Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Stockholm, Sweden

Reminder & Overview L03, ID1218, Christian Schulte 2

Roadmap: MiniErlang  What to compute with MiniErlang expressions and programs  What are the results MiniErlang Values  What are the instructions for compound value construction and function call  How are functions called parameters are passed by substitution considers only matching clauses clauses have patterns (we ignore guards) L03, ID1218, Christian Schulte 3

Evaluating Values L03, ID1218, Christian Schulte

MiniErlang Values  A MiniErlang value is an integer or a list other values are similar  In short notation V := int | [] | [ V 1 | V 2 ] known as BNF notation: discussed later so: values are referred to by V (possibly subscripted) can be: any integer, the empty list, a cons consisting of two values V 1 and V 2 L03, ID1218, Christian Schulte 5

MiniErlang Expressions  A MiniErlang expression is a value, a variable, or a function call E :=int | [] | [ E 1 | E 2 ] |X | F(E 1,…, E n ) expressions referred to by E variables referred to by X function names referred to by F L03, ID1218, Christian Schulte 6

MiniErlang Machine  MiniErlang machine Es ; Vs → Es’ ; Vs’ transforms a pair (separated by ;) of expression stack Es and value stack Vs into a new pair of expression stack Es’ and value stack Vs’  Initial configuration: expression we want to evaluate on expression stack  Final configuration: single value as result on value stack L03, ID1218, Christian Schulte 7

Stacks  We write stacks as X 1  …  X n  Xr top of stack X 1 n-th element X n more elementsXr empty stack   Pushing X to stack Xr: X  Xr  Popping X from stack X  Xr:Xr L03, ID1218, Christian Schulte 8

MiniErlang Execution Idea  Simple case: an integer evaluates to itself the result of an integer expression… …is an integer value  MiniErlang machine i  Er ; Vs → Er ; i  Vs if the expression stack has the integer i as top of stack… execution yields: the expression i is popped from the expression stack and pushed on to the value stack same for empty list L03, ID1218, Christian Schulte 9

MiniErlang Instruction Idea  How to evaluate a list expression [ E 1 | E 2 ] first evaluate E 1, to a value V 1, … then evaluate E 2, to a value V 2, … then construct a new value [ V 1 | V 2 ]  Use an instruction that says: build a list makes the assumption that values needed are on the value stack execution will pop two values, push a new list value when [ E 1 | E 2 ] is executed, E 1 and E 2 and the instruction CONS are pushed on the expression stack L03, ID1218, Christian Schulte 10

Evaluating a List Expression  Evaluate a list expression [ E 1 | E 2 ]  Er ; Vs → E 1  E 2  CONS  Er ; Vs  Execute a CONS instruction CONS  Er ; V 1  V 2  Vs → Er ; [ V 2 | V 1 ]  Vs L03, ID1218, Christian Schulte 11

Example  We want to evaluate the expression [1|[]] (that is, just the list [1] )  Start configuration of our machine [1|[]] ;  expression stack: [1|[]] empty value stack:   What should be the end configuration:  ; [1|[]] empty expression stack:  result on value stack: [1|[]] L03, ID1218, Christian Schulte 12

Let’s Do It! [1|[]] ;  → … L03, ID1218, Christian Schulte 13 [ E 1 | E 2 ]  Er ; Vs → E 1  E 2  CONS  Er ; Vs

Let’s Do It! [1|[]] ;  → 1  []  CONS ;  → … L03, ID1218, Christian Schulte 14 i  Er ; Vs → Er ; i  Vs

Let’s Do It! [1|[]] ;  → 1  []  CONS ;  → []  CONS ; 1 → … L03, ID1218, Christian Schulte 15 i  Er ; Vs → Er ; i  Vs

Let’s Do It! [1|[]] ;  → 1  []  CONS ;  → []  CONS ; 1 → CONS ; []  1 → … L03, ID1218, Christian Schulte 16 CONS  Er ; V 1  V 2  Vs → Er ; [ V 2 | V 1 ]  Vs

Let’s Do It! [1|[]] ;  → 1  []  CONS ;  → []  CONS ; 1 → CONS ; []  1 →  ; [1|[]] L03, ID1218, Christian Schulte 17

Summary  MiniErlang values expressions  MiniErlang machine operates on expression and value stack evaluates topmost expression on expr stack executes topmost instruction on expr stack  Start state: single expr on expr stack  Final state: single value on value stack L03, ID1218, Christian Schulte 18

Executing Functions L03, ID1218, Christian Schulte

Roadmap  How to evaluate arguments before executing function… shuffle arguments on expression stack have a call instruction executing call instruction picks values from value stack  How to find the right clause explain matching  How to pass parameters replace variables by substitution L03, ID1218, Christian Schulte 20

Evaluating Function Call  Evaluate call expression F(E 1, …, E n )  Er ; Vs → E 1  …  E n  CALL( F/n )  Er ; Vs L03, ID1218, Christian Schulte 21

MiniErlang Patterns  Somehow: values + variables  Or, crisply: P :=int | [] | [ P 1 | P 2 ] | X L03, ID1218, Christian Schulte 22

Pattern Matching  Pattern matching takes a pattern P and a value V and returns true, iff the value matches the pattern  If V matches P, a substitution is returned for each variable in the pattern P a matching value  Substitutions are applied to expressions replacing variables by the respective values  Details come later, just the big picture now L03, ID1218, Christian Schulte 23

Pattern Matching Examples  [X|Xr] matches [2|[1|[]]] substitution { X  2, Xr  [1|[]] }  [X|[X|Xr]] matches [2|[2|[]]] substitution { X  2, Xr  [] }  [X|[X|Xr]] does not match [2|[1|[]]] no substitution, of course L03, ID1218, Christian Schulte 24

Substitution Examples  Application of substitution { X  2, Xr  [1|[]] } to expression X+len(Xr) yields yields 2+len([1|[]])  We refer to substitutions bys application to expression E bys(E) L03, ID1218, Christian Schulte 25

Reminder: Call Expression  Evaluate call expression F(E 1, …, E n )  Er ; Vs → E 1  …  E n  CALL( F/n )  Er ; Vs L03, ID1218, Christian Schulte 26

Executing CALL CALL( F/n )  Er ; V 1  …  V n  Vs → s(E )  Er ; Vs  F(P 1, …, P n ) -> E is the first clause of F/n such that the pattern [ P 1, …, P n ] matches… …the list value [ V n, …, V 1 ] with substitution s L03, ID1218, Christian Schulte 27

Example  Assume we want to evaluate f([1|[]]) where f/1 is defined by the single clause f([X|Xr]) -> [X|f(Xr)]. L03, ID1218, Christian Schulte 28

Let’s Do It! f([1|[]]) ;  → [1|[]]  CALL(f/1) ;  → 1  []  CONS  CALL(f/1) ;  → []  CONS  CALL(f/1) ; 1 → CONS  CALL(f/1) ; []  1 → CALL(f/1) ; [1|[]] → [1|f([])] ;  → … L03, ID1218, Christian Schulte 29

What Do We Ignore?  Runtime errors what if no clause matches  Simplistic values  No un-nesting  No guards simple: just check guards on values  No case and if expressions rewrite to additional functions L03, ID1218, Christian Schulte 30

An Important Fact…  The expressions on the expression stack must have an essential property… hmmm… Look to the example again! L03, ID1218, Christian Schulte 31

Last Call Optimization  MiniErlang has last call optimization (LCO) built in remember what to do next on stack do not remember where to return to  What effect for recursive programs? L03, ID1218, Christian Schulte 32

MiniErlang Full Picture L03, ID1218, Christian Schulte 33

Making MiniErlang More Realistic  Substitution replaces variables in clause bodies by values values will be deconstructed and reconstructed over and over again  Add single rule that optimizes values an expression that is a value can be directly moved from the expression to the value stack subsumes the rules for integers and the empty list L03, ID1218, Christian Schulte 34

MiniErlang: Values, Expressions, Instructions  MiniErlang values V := int | [] | [ V 1 | V 2 ]  MiniErlang expressions E := int | [] | [ E 1 | E 2 ] | X | F(E 1,…, E n ) where X stands for a variable  MiniErlang instructions CONS, CALL L03, ID1218, Christian Schulte 35

MiniErlang: Expressions  Evaluate values V  Er ; Vs → Er ; V  Vs provided V is a value  Evaluate list expression [ E 1 | E 2 ]  Er ; Vs → E 1  E 2  CONS  Er ; Vs  Evaluate function call F(E 1, …, E n )  Er ; Vs → E 1  …  E n  CALL( F/n )  Er ; Vs L03, ID1218, Christian Schulte 36

MiniErlang: Instructions  CONS instruction CONS  Er ; V 1  V 2  Vs → Er ; [ V 2 | V 1 ]  Vs  CALL instruction CALL( F/n )  Er ; V 1  …  V n  Vs → s(E )  Er ; Vs F(P 1, …, P n ) -> E first clause of F/n such that [ P 1, …, P n ] matches [ V n, …, V 1 ] with substitution s L03, ID1218, Christian Schulte 37

MiniErlang Pattern Matching  Patterns P :=int | [] | [ P 1 | P 2 ] | X  match(P,V) s:=try(P,V) if error  s or X  V 1, X  V 2  s withV 1 ≠V 2 then no else s where try( i, i ) =  try( [], [] ) =  try( [ P 1 | P 2 ], [ V 1 | V 2 ] )= try(P 1,V 1 )  try(P 2,V 2 ) try(X,V) = {X  V} otherwise= {error} L03, ID1218, Christian Schulte 38

Pattern Matching  Example match( [A,A|[B|B]],[1,1,[]] )  Uniform list notation match( [A|[A|[B|B]]],[1|[1|[[]|[]]]] )  Evaluate try try( [A|[A|[B|B]]],[1|[1|[[]|[]]]] ) L03, ID1218, Christian Schulte 39

Evaluating try try( [A|[A|[B|B]]],[1|[1|[[]|[]]]] ) = try( A,1 )  try( [A|[B|B]],[1|[[]|[]]] ) = { A  1 }  try( A,1 )  try( [B|B],[[]|[]] ) = { A  1 }  { A  1 }  try( B,[] )  try( B,[] ) = { A  1 }  { B  [] }  { B  [] } = { A  1, B  [] }  Matches with substitution: { A  1, B  [] } L03, ID1218, Christian Schulte 40

Pattern Matching  Example match( [A,A|[B|B]],[1,2,[]] )  Uniform list notation match( [A|[A|[B|B]]],[1|[2|[[]|[]]]] )  Evaluate try try( [A|[A|[B|B]]],[1|[2|[[]|[]]]] ) L03, ID1218, Christian Schulte 41

Evaluating try try( [A|[A|[B|B]]],[1|[2|[[]|[]]]] ) = try( A,1 )  try( [A|[B|B]],[2|[[]|[]]] ) = { A  1 }  try( A,2 )  try( [B|B],[[]|[]] ) = { A  1 }  { A  2 }  try( B,[] )  try( B,[] ) = { A  1, A  2 }  { B  [] }  { B  [] } = { A  1, A  2, B  [] }  Does not match! L03, ID1218, Christian Schulte 42

Substitution  Defined over structure of expressions s( i ) = i s( [] ) = [] s( [ E 1 | E 2 ] ) = [ s(E 1 ) | s(E 2 ) ] s(F(E 1, …, E n )) = F(s(E 1 ), …, s(E n )) s(X)= if X  V  s then V else X L03, ID1218, Christian Schulte 43

Matching and Substitutions  If P matches V with substitution s s=match(P,V) then s(P)=V L03, ID1218, Christian Schulte 44

Substitution  Assume s= { A  1, B  [] }  s( [A|[A|[B|B]]] ) = [ s( A ) | s( [A|[B|B]] ) ] = [1|[ s( A ) | s( [B|B] ) ]] = [1|[1|[ s( B ) | s( B ) ]]] = [1|[1|[[]|[]]]] L03, ID1218, Christian Schulte 45

Extending MiniErlang  Built-in expressions, for example… evaluate addition E 1 + E 2  Er ; Vs → E 1  E 2  ADD  Er ; Vs execute ADD instruction ADD  Er ; V 1  V 2  Vs → Er ; V 2 + V 1  Vs  Comma operator sequence of expressions replace substitution by environment lookup value for variable from environment L03, ID1218, Christian Schulte 46

What Did We Actually Do?  Blueprint of MiniErlang implementation operational semantics capable of explaining how calls are handelled stack machine  A real implementation would statically replace (compilation) call and list construction to the respective instructions would replace substitution by environments (registers) …of a stack machine is the JVM! L03, ID1218, Christian Schulte 47

What Can We Answer  Faithful model for runtime measure: number of function calls all remaining operations are constant wrt function calls  Faithful model for memory MiniErlang does not use heap memory: value stack stack space: number of entries on either stack space: size of entries on either stack L03, ID1218, Christian Schulte 48

Homework  Hand execute app/2 in MiniErlang!  Try all examples L03, ID1218, Christian Schulte 49