0% found this document useful (0 votes)
110 views

Variable Scoping Rules in Erlang: László Lövei, Zoltán Horváth, Tamás Kozsik, Roland Király

This document discusses variable scoping rules in the Erlang programming language. It begins with an introduction to Erlang and refactoring. It then explains that in Erlang, variables are local to function clauses and cannot change value within a function call. The document defines variable scope as the part of the program affected by the variable. It presents examples of refactoring techniques like renaming variables, merging duplicated expressions, and extracting functions. It discusses conditions for these transformations based on variable binding and usage. Finally, it proposes using lookup tables to track variable information to implement transformations.

Uploaded by

mberrow
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Variable Scoping Rules in Erlang: László Lövei, Zoltán Horváth, Tamás Kozsik, Roland Király

This document discusses variable scoping rules in the Erlang programming language. It begins with an introduction to Erlang and refactoring. It then explains that in Erlang, variables are local to function clauses and cannot change value within a function call. The document defines variable scope as the part of the program affected by the variable. It presents examples of refactoring techniques like renaming variables, merging duplicated expressions, and extracting functions. It discusses conditions for these transformations based on variable binding and usage. Finally, it proposes using lookup tables to track variable information to implement transformations.

Uploaded by

mberrow
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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 real-
time 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
● Widely used in OO programming
– Extract interface or subclass, pull up a method
● Tool support is very useful
● Refactoring in Erlang heavily depends on
understading variables

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


Variables in Erlang
● Variables are local to longest([H|T]) ->
a function clause max(T, size(H)).
max([], M) -> M;
● When the function is max([H|T],M) ->
run, a value is bound S = size(H),
to the variable if S>M -> N=S;
not S>M -> N=M
● The value cannot be end,
changed in the same max(T, N).
run of the function error() ->
A = 1,
A = 2.

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


Variable Scoping
● Scope: program part max([H|T],M) ->
affected by the S = size(H),
if S>M -> N=S;
variable not S>M -> N=M
● Traditional definition: end,
max(T, N).
from the binding
until the last usage
● Formal semantics:
– Variable context: name to value mapping
– Expressions are evaluated in a context
– Pattern matching extends the context

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


Rename Variable
● Condition: the new max([H|T],M) ->
name is not used S = size(H),
if S>M -> N=S;
● This affects the whole not S>M -> N=M
function clause! end,
max(T, N).
● New definition:
– Scope ≡ fun.clause max([H|T],M) ->
– Variables are S = size(H),
contained in scopes if S>M -> New=S;
not S>M -> New=M
– Condition: the scope end,
does not contain the max(T, New).
new name
6 7th International Conference on Applied Informatics, January 28-31, 2007
Merge Expression Duplicates
● Same variable ≡ max([H|T],M) ->
same scope S = size(H),
if S>M -> N=S;
● Where may a variable not S>M -> N=M
be used? end,
max(T, N).
● Visibility:
– Expressions in the max([H|T],M) ->
scope of the variable S=size(H), Cond=S>M,
– After the binding if Cond -> N=S;
not Cond -> N=M
– Minus shadowing end,
constructs max(T, N).

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


Extract function
● Binding points: first max([H|T],M) ->
pattern matches for a S = size(H),
N = if S>M -> S;
variable not S>M -> M
● Other occurrences end,
max(T, N).
use the variable
● Conditions:
cmp(M,S) -> if S>M->S;
– Bound variables are not S>M -> M end.
not used elsewhere max([H|T],M) ->
– Used, but not bound S = size(H),
variables become N = cmp(M,S),
parameters 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)
– Unsafe variable end),
if not G -> long(T);
usage
G -> size(H) end.

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


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

You might also like