Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 ● ● 2 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 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 – ● Widely used in OO programming – ● ● 3 Rename variables or types, split functions Extract interface or subclass, pull up a method Tool support is very useful Refactoring in Erlang heavily depends on understading variables 7th International Conference on Applied Informatics, January 28-31, 2007 Variables in Erlang ● ● ● 4 Variables are local to a function clause longest([H|T]) -> max(T, size(H)). When the function is run, a value is bound to the variable max([], M) -> M; max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N). The value cannot be changed in the same run of the function error() -> A = 1, A = 2. 7th International Conference on Applied Informatics, January 28-31, 2007 Variable Scoping ● ● ● 5 Scope: program part affected by the variable Traditional definition: from the binding until the last usage max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N). Formal semantics: – 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 Rename Variable ● ● ● 6 Condition: the new name is not used This affects the whole function clause! New definition: – 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 -> N=S; not S>M -> N=M end, max(T, N). max([H|T],M) -> S = size(H), if S>M -> New=S; not S>M -> New=M end, max(T, New). 7th International Conference on Applied Informatics, January 28-31, 2007 Merge Expression Duplicates ● ● ● 7 Same variable ≡ same scope Where may a variable be used? Visibility: – Expressions in the scope of the variable – After the binding – Minus shadowing constructs max([H|T],M) -> S = size(H), if S>M -> N=S; not S>M -> N=M end, max(T, N). max([H|T],M) S=size(H), if Cond not Cond end, max(T, N). -> Cond=S>M, -> N=S; -> N=M 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: – – 8 max([H|T],M) -> S = size(H), N = if S>M -> S; not S>M -> M end, max(T, N). cmp(M,S) -> if S>M->S; not S>M -> M end. Bound variables are not used elsewhere Used, but not bound variables become parameters max([H|T],M) -> S = size(H), N = cmp(M,S), max(T, N). 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. 9 7th International Conference on Applied Informatics, January 28-31, 2007 Implementation ● ● 10 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 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