Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte Software and Computer Systems School of Information and Communication Technology."— Presentation transcript:

1 MINI ERLANG ID1218 Lecture 032009-11-02 Christian Schulte cschulte@kth.se Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Stockholm, Sweden

2 Reminder & Overview L03, 2009-11-02ID1218, Christian Schulte 2

3 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

4 Evaluating Values L03, 2009-11-02 4 ID1218, Christian Schulte

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

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

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

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

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

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

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

12 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

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

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

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

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

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

18 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

19 Executing Functions L03, 2009-11-02 19 ID1218, Christian Schulte

20 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

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

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

23 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

24 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

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

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

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

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

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

30 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

31 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

32 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

33 MiniErlang Full Picture L03, 2009-11-02ID1218, Christian Schulte 33

34 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

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

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

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

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

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

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

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

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

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

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

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

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

47 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

48 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

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


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

Similar presentations


Ads by Google