1 / 49

# Mini Erlang - PowerPoint PPT Presentation

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 03 2009-11-02. Reminder & Overview. Roadmap: MiniErlang. What to compute with

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

## PowerPoint Slideshow about ' Mini Erlang' - kemp

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Software and Computer Systems

School of Information and Communication Technology

KTH – Royal Institute of Technology

Stockholm, Sweden

### Mini Erlang

ID1218 Lecture 03 2009-11-02

ID1218, Christian Schulte

• 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)

• ID1218, Christian Schulte

ID1218, Christian Schulte

• A MiniErlang value is an integer or a list

• other values are similar

• In short notation

V := 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

• ID1218, Christian Schulte

• 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

ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

• We write stacks as

X1 …  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

• ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

• 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 andE2 and the instruction CONS are pushed on the expression stack

• ID1218, Christian Schulte

• Evaluate a list expression

[E1|E2]Er ; Vs

→ E1E2CONSEr ; Vs

• Execute a CONS instruction

CONSEr ; V1V2Vs

→ Er ; [V2|V1]Vs

ID1218, Christian Schulte

• 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|[]]

• ID1218, Christian Schulte

[1|[]] ; 

→ …

[E1|E2]Er ; Vs

→ E1E2CONSEr ; Vs

ID1218, Christian Schulte

[1|[]] ; 

→ 1 []CONS ; 

→ …

i Er ; Vs→ Er ; iVs

ID1218, Christian Schulte

[1|[]] ; 

→ 1 []CONS ; 

→ []CONS ;1

→ …

i Er ; Vs→ Er ; iVs

ID1218, Christian Schulte

[1|[]] ; 

→ 1 []CONS ; 

→ []CONS ;1

→ CONS ;[]1

→ …

CONSEr ; V1V2Vs

→ Er ; [V2|V1]Vs

ID1218, Christian Schulte

[1|[]] ; 

→ 1 []CONS ; 

→ []CONS ;1

→ CONS ;[]1

→ ;[1|[]]

ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

• Evaluate call expression

F(E1, …, En)Er ; Vs

→ E1…EnCALL(F/n)Er ; Vs

ID1218, Christian Schulte

• Somehow: values + variables

• Or, crisply:

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

ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

• [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

• ID1218, Christian Schulte

• 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)

• ID1218, Christian Schulte

• Evaluate call expression

F(E1, …, En)Er ; Vs

→ E1…EnCALL(F/n)Er ; Vs

ID1218, Christian Schulte

Executing CALL

CALL(F/n)Er ; V1…VnVs

→ 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

ID1218, Christian Schulte

• Assume we want to evaluate

f([1|[]])

where f/1 is defined by the single clause

f([X|Xr]) -> [X|f(Xr)].

ID1218, Christian Schulte

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([])] ; 

→ …

ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

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

hmmm… Look to the example again!

ID1218, Christian Schulte

• 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?

• ID1218, Christian Schulte

MiniErlang Full Picture

ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

• MiniErlang values

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

• MiniErlang expressions

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

| X | F(E1,…, En)

• where X stands for a variable

• MiniErlang instructions

CONS, CALL

• ID1218, Christian Schulte

• Evaluate values

V Er ; Vs→ Er ; VVs

• provided V is a value

• Evaluate list expression

[E1|E2]Er ; Vs

→ E1E2CONSEr ; Vs

• Evaluate function call

F(E1, …, En)Er ; Vs

→ E1…EnCALL(F/n)Er ; Vs

• ID1218, Christian Schulte

• CONS instruction

CONSEr ; V1V2Vs

→ Er ; [V2|V1]Vs

• CALL instruction

CALL(F/n)Er ; V1…VnVs

→ s(E)Er ; Vs

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

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

ID1218, Christian Schulte

• Patterns

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

• match(P,V)

s:=try(P,V)

iferrorsorXV1,XV2 s withV1≠V2 thenno elses

where

try(i,i) = 

try([],[]) = 

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

try(X,V) = {XV}

otherwise = {error}

ID1218, Christian Schulte

• 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|[[]|[]]]])

ID1218, Christian Schulte

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[]}

ID1218, Christian Schulte

• 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|[[]|[]]]])

ID1218, Christian Schulte

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!

ID1218, Christian Schulte

• Defined over structure of expressions

s(i) = i

s([]) = []

s([E1|E2]) = [s(E1)|s(E2)]

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

s(X) = ifXV  s thenVelse X

ID1218, Christian Schulte

• If P matches V with substitution s

s=match(P,V)

then

s(P)=V

ID1218, Christian Schulte

• 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|[[]|[]]]]

ID1218, Christian Schulte

• Built-in expressions, for example…

• evaluate addition

E1+E2Er ; Vs → E1E2ADDEr ; Vs

• execute ADD instruction

ADDEr ; V1V2Vs → Er ; V2+V1Vs

• Comma operator

• sequence of expressions

• replace substitution by environment

• lookup value for variable from environment

• ID1218, Christian Schulte

• 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!

• ID1218, Christian Schulte

• 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

• ID1218, Christian Schulte

• Hand execute app/2 in MiniErlang!

• Try all examples

ID1218, Christian Schulte