49
MINI ERLANG ID1218 Lecture 03 2009-11-02 Christian Schulte [email protected] Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Stockholm, Sweden

MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte [email protected] Software and Computer Systems School of Information and Communication Technology

  • View
    216

  • Download
    3

Embed Size (px)

Citation preview

Page 1: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MINI ERLANG

ID1218 Lecture 03 2009-11-02

Christian [email protected]

Software and Computer SystemsSchool of Information and Communication

TechnologyKTH – Royal Institute of Technology

Stockholm, Sweden

Page 2: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Reminder & Overview

L03, 2009-11-02ID1218, Christian Schulte

2

Page 3: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

3

Page 4: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Evaluating Values

L03, 2009-11-02

4

ID1218, Christian Schulte

Page 5: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Values

A MiniErlang value is an integer or a list other values are similar

In short notationV := int | [] | [ V1 | V2 ]

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 V1 and V2

L03, 2009-11-02ID1218, Christian Schulte

5

Page 6: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Expressions

A MiniErlang expression is a value, a variable, or a function call

E := int | [] | [ E1 | E2 ]

| X | F(E1,…, En)

expressions referred to by E variables referred to by X function names referred to by F

L03, 2009-11-02ID1218, Christian Schulte

6

Page 7: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Machine

MiniErlang machineEs ; 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, 2009-11-02ID1218, Christian Schulte

7

Page 8: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Stacks

We write stacks asX1 … Xn Xr

top of stack X1

n-th element Xn more elements Xr empty stack

Pushing X to stack Xr: X Xr Popping X from stack X Xr: Xr

L03, 2009-11-02ID1218, Christian Schulte

8

Page 9: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Execution Idea

Simple case: an integer evaluates to itself

the result of an integer expression……is an integer value

MiniErlang machinei 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, 2009-11-02ID1218, Christian Schulte

9

Page 10: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Instruction Idea

How to evaluate a list expression [ E1 | E2 ]

first evaluate E1 , to a value V1, … then evaluate E2 , to a value V2, … then construct a new value [ V1 | V2 ]

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 [ E1 | E2 ] is executed, E1 and E2 and the

instruction CONS are pushed on the expression stack

L03, 2009-11-02ID1218, Christian Schulte

10

Page 11: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Evaluating a List Expression

Evaluate a list expression[E1|E2]Er ; Vs

→ E1E2CONSEr ; Vs

Execute a CONS instructionCONSEr ; V1V2Vs

→ Er ; [V2|V1]Vs

L03, 2009-11-02ID1218, Christian Schulte

11

Page 12: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

12

Page 13: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Let’s Do It!

[1|[]] ; → …

L03, 2009-11-02ID1218, Christian Schulte

13

[E1|E2]Er ; Vs→ E1E2CONSEr ; Vs

Page 14: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Let’s Do It!

[1|[]] ; → 1 []CONS ; → …

L03, 2009-11-02ID1218, Christian Schulte

14

i Er ; Vs → Er ; i Vs

Page 15: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Let’s Do It!

[1|[]] ; → 1 []CONS ; → []CONS ; 1→ …

L03, 2009-11-02ID1218, Christian Schulte

15

i Er ; Vs → Er ; i Vs

Page 16: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Let’s Do It!

[1|[]] ; → 1 []CONS ; → []CONS ; 1→ CONS ; []1→ …

L03, 2009-11-02ID1218, Christian Schulte

16

CONSEr ; V1V2Vs→ Er ; [V2|V1]Vs

Page 17: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Let’s Do It!

[1|[]] ; → 1 []CONS ; → []CONS ; 1→ CONS ; []1→ ; [1|[]]

L03, 2009-11-02ID1218, Christian Schulte

17

Page 18: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

18

Page 19: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Executing Functions

L03, 2009-11-02

19

ID1218, Christian Schulte

Page 20: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

20

Page 21: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Evaluating Function Call

Evaluate call expressionF(E1, …, En)Er ; Vs

→ E1…EnCALL(F/n)Er ; Vs

L03, 2009-11-02ID1218, Christian Schulte

21

Page 22: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Patterns

Somehow: values + variables Or, crisply:

P := int | [] | [ P1 | P2 ] | X

L03, 2009-11-02ID1218, Christian Schulte

22

Page 23: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

23

Page 24: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

24

Page 25: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Substitution Examples

Application of substitution { X 2, Xr [1|[]]} to expression X+len(Xr) yields yields 2+len([1|[]])

We refer to substitutions by s application to expression E by s(E)

L03, 2009-11-02ID1218, Christian Schulte

25

Page 26: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Reminder: Call Expression

Evaluate call expressionF(E1, …, En)Er ; Vs

→ E1…EnCALL(F/n)Er ; Vs

L03, 2009-11-02ID1218, Christian Schulte

26

Page 27: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Executing CALL

CALL(F/n)Er ; V1…VnVs

→ s(E)Er ; Vs

F(P1, …, Pn) -> E is the first clause of F/n such that

the pattern [P1, …, Pn] matches…

…the list value [Vn, …, V1] with substitution s

L03, 2009-11-02ID1218, Christian Schulte

27

Page 28: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Example

Assume we want to evaluatef([1|[]])

where f/1 is defined by the single clausef([X|Xr]) -> [X|f(Xr)].

L03, 2009-11-02ID1218, Christian Schulte

28

Page 29: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Let’s Do It!

f([1|[]]) ; → [1|[]]CALL(f/1) ; → 1[]CONSCALL(f/1) ; → []CONSCALL(f/1) ; 1→ CONSCALL(f/1) ; []1→ CALL(f/1) ; [1|[]]→ [1|f([])] ; → …

L03, 2009-11-02ID1218, Christian Schulte

29

Page 30: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

30

Page 31: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

An Important Fact…

The expressions on the expression stack must have an essential property…

hmmm… Look to the example again!

L03, 2009-11-02ID1218, Christian Schulte

31

Page 32: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

32

Page 33: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Full Picture

L03, 2009-11-02ID1218, Christian Schulte

33

Page 34: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

34

Page 35: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang: Values, Expressions, Instructions MiniErlang values

V := int | [] | [ V1 | V2 ] MiniErlang expressions

E := int | [] | [ E1 | E2 ]

| X | F(E1,…, En) where X stands for a variable

MiniErlang instructionsCONS, CALL

L03, 2009-11-02ID1218, Christian Schulte

35

Page 36: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang: Expressions

Evaluate valuesV Er ; Vs → Er ; V Vs

provided V is a value Evaluate list expression

[E1|E2]Er ; Vs

→ E1E2CONSEr ; Vs Evaluate function call

F(E1, …, En)Er ; Vs

→ E1…EnCALL(F/n)Er ; VsL03, 2009-11-02ID1218, Christian Schulte

36

Page 37: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang: Instructions

CONS instructionCONSEr ; V1V2Vs

→ Er ; [V2|V1]Vs CALL instruction

CALL(F/n)Er ; V1…VnVs

→ s(E)Er ; Vs F(P1, …, Pn) -> E first clause of F/n such that

[P1, …, Pn] matches [Vn, …,V1] with substitution s

L03, 2009-11-02ID1218, Christian Schulte

37

Page 38: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

MiniErlang Pattern Matching

PatternsP := int | [] | [ P1 | P2 ] | X

match(P,V)s:=try(P,V)if errors or XV1, XV2 s withV1≠V2 then no else s

wheretry(i,i) = try([],[]) = try([P1|P2],[V1|V2])= try(P1,V1)try(P2,V2)

try(X,V) = {XV}otherwise = {error}

L03, 2009-11-02ID1218, Christian Schulte

38

Page 39: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Pattern Matching

Examplematch([A,A|[B|B]],[1,1,[]])

Uniform list notationmatch([A|[A|[B|B]]],[1|[1|[[]|[]]]])

Evaluate trytry([A|[A|[B|B]]],[1|[1|[[]|[]]]])

L03, 2009-11-02ID1218, Christian Schulte

39

Page 40: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Evaluating try

try([A|[A|[B|B]]],[1|[1|[[]|[]]]]) =try(A,1) try([A|[B|B]],[1|[[]|[]]]) ={A1} try(A,1) try([B|B],[[]|[]]) = {A1} {A1} try(B,[]) try(B,[]) ={A1} {B[]} {B[]} ={A1, B[]}

Matches with substitution: {A1, B[]}

L03, 2009-11-02ID1218, Christian Schulte

40

Page 41: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Pattern Matching

Examplematch([A,A|[B|B]],[1,2,[]])

Uniform list notationmatch([A|[A|[B|B]]],[1|[2|[[]|[]]]])

Evaluate trytry([A|[A|[B|B]]],[1|[2|[[]|[]]]])

L03, 2009-11-02ID1218, Christian Schulte

41

Page 42: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Evaluating try

try([A|[A|[B|B]]],[1|[2|[[]|[]]]]) =try(A,1) try([A|[B|B]],[2|[[]|[]]]) ={A1} try(A,2) try([B|B],[[]|[]]) = {A1} {A2} try(B,[]) try(B,[]) ={A1, A2} {B[]} {B[]} ={A1, A2, B[]}

Does not match!

L03, 2009-11-02ID1218, Christian Schulte

42

Page 43: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Substitution

Defined over structure of expressionss(i) = is([]) = []s([E1|E2]) = [s(E1)|s(E2)]

s(F(E1, …, En)) = F(s(E1), …, s(En))

s(X) = if XV s then V else X

L03, 2009-11-02ID1218, Christian Schulte

43

Page 44: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Matching and Substitutions

If P matches V with substitution ss=match(P,V)

thens(P)=V

L03, 2009-11-02ID1218, Christian Schulte

44

Page 45: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Substitution

Assume s= {A1, 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, 2009-11-02ID1218, Christian Schulte

45

Page 46: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Extending MiniErlang

Built-in expressions, for example… evaluate addition

E1+E2Er ; Vs → E1E2ADDEr ; Vs execute ADD instructionADDEr ; V1V2Vs → Er ; V2+V1Vs

Comma operator sequence of expressions replace substitution by environment lookup value for variable from environment

L03, 2009-11-02ID1218, Christian Schulte

46

Page 47: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

47

Page 48: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

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, 2009-11-02ID1218, Christian Schulte

48

Page 49: MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology

Homework

Hand execute app/2 in MiniErlang! Try all examples

L03, 2009-11-02ID1218, Christian Schulte

49