Variable Scoping Rules in Erlang

László Lövei, Zoltán Horváth, Tamás Kozsik, Roland Király
Department of Programming Languages and Compilers Faculty of Informatics ELTE, Budapest, Hungary
Supported by Ericsson Hungary, ELTE CNL and ELTE IKKK*
* GVOP-3.2.2-2004-07-0005/3.0

Erlang Programming Language

Functional programming language and runtime environment developed by Ericsson Designed to build distributed, reliable, soft realtime concurrent systems (telecommunication) No static type system Lightweight processes and message passing Highly dynamic nature

● ● ●

2

7th International Conference on Applied Informatics, January 28-31, 2007

Introduction to Refactoring

Restructuring program code without altering its external behaviour Simple refactorings are used every day

Rename variables or types, split functions Extract interface or subclass, pull up a method

Widely used in OO programming

● ●

Tool support is very useful Refactoring in Erlang heavily depends on understading variables
7th International Conference on Applied Informatics, January 28-31, 2007

3

Variables in Erlang

Variables are local to a function clause When the function is run, a value is bound to the variable The value cannot be changed in the same run of the function

longest([H|T]) -> max(T, size(H)). max([], M) -> M; max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N). error() -> A = 1, A = 2.

4

7th International Conference on Applied Informatics, January 28-31, 2007

Variable Scoping

Scope: program part affected by the variable Traditional definition: from the binding until the last usage Formal semantics:
– – –

max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N).

Variable context: name to value mapping Expressions are evaluated in a context Pattern matching extends the context
7th International Conference on Applied Informatics, January 28-31, 2007

5

Rename Variable

Condition: the new name is not used This affects the whole function clause! New definition:
– –

max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N).

Scope ≡ fun.clause Variables are contained in scopes Condition: the scope does not contain the new name

max([H|T],M) -> S = size(H), if S>M -> New=S; not S>M -> New=M end, max(T, New).

6

7th International Conference on Applied Informatics, January 28-31, 2007

Merge Expression Duplicates

Same variable ≡ same scope Where may a variable be used? Visibility:

max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N).

Expressions in the scope of the variable After the binding Minus shadowing constructs

– –

max([H|T],M) S=size(H), if Cond not Cond end, max(T, N).

-> Cond=S>M, -> N=S; -> N=M

7

7th International Conference on Applied Informatics, January 28-31, 2007

Extract function

Binding points: first pattern matches for a variable Other occurrences use the variable Conditions:

max([H|T],M) -> S = size(H), N = if S>M -> S; not S>M -> M end, max(T, N).

Bound variables are not used elsewhere Used, but not bound variables become parameters

cmp(M,S) -> if S>M->S; not S>M -> M end. max([H|T],M) -> S = size(H), N = cmp(M,S), max(T, N).

8

7th International Conference on Applied Informatics, January 28-31, 2007

Eliminate variable

Conditions:

long([H|T]) -> S = size(H), – Variables used in the G = all(T, fun expression are visible (H)-> size(H)<S end), at substitution points if not G -> long(T); G -> S end. –

Unique binding

Constructs that limit visibility:

long([H|T]) -> G = all(T, fun – Function expressions (X)-> size(X)<size(H) end), – Unsafe variable if not G -> long(T); usage G -> size(H) end.
7th International Conference on Applied Informatics, January 28-31, 2007

9

Implementation

These concepts can be implemented by lookup tables that contain
– – – –

the scope for every variable visible variables for every expression binding points for every variable usage points for every variable

These lookup tables can be used directly to implement the transformations

10

7th International Conference on Applied Informatics, January 28-31, 2007

The End

Thank you for your attention!

11

7th International Conference on Applied Informatics, January 28-31, 2007