You are on page 1of 14

Refactoring Erlang Programs

Zoltán Horváth, László Lövei, Tamás Kozsik,


Anikó Víg, Tamás Nagy

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
Contents
● Introduction to Refactoring
● Introduction to Erlang
● Refactoring Examples
● Refactoring Implementation
● Prototype Tool

2 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Introduction to Refactoring
● Restructuring program code without altering its
external behaviour
● Simple refactorings are used every day
– Rename functions, variables, types, packages
● Widely used in OO programming
– Extract interface or subclass, pull up a method
● Tool support is needed
– Available for Java, not for functional languages

3 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Erlang 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

4 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Refactoring Example
● Rename a variable new(A) ->
– Condition: new name Id = newId(),
is unused add(Id, A).
● Semantical analysis add(Id, A) ->
is required to db:store(Id, A).
– Determine used
variable names new(Host) ->
Id = newId(),
– Find every occurrence
add(Id, Host).
but leave others with
the same name intact add(Id, A) ->
db:store(Id, A).

5 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Generalization
● Conditions: notify(P) ->
– The expression does P ! {notif, 1}.
not use external add(Id, A) ->
variables db:store(Id, A),
– New variables are not notify(A).
used outside the
expression
notify(P, X) ->
● Compensation: P ! {notif, X}.
– Every call of the add(Id, A) ->
function must provide db:store(Id, A),
the new parameter notify(A, 1).

6 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Issues with generalization
● An Erlang expression can have a side effect
– The new parameter is used as a function which can
incorporate the side effect
● Function references can be hard to track
– Function names can be passed as a parameter –
flow analysis is needed to track that
– Function names can even be constructed at run
time – that is outside the scope of a refactoring tool
– This influences “easy” refactorings like function
renaming

7 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Implementation of Refactoring
● Transformations are new(A) ->
syntax tree-based Id = newId(),
add(Id, A).
● Refactoring needs
extra information add(Id, A) ->
db:store(Id, A).
Module
Function: new Function: add

Parameter: A Parameter: Id
Parameter: A
Binding Variable: Id

Call: add Call: db:store

Param: Id Param: A Param: Id Param: A

8 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Representation in a Database 
● Extra information (like variable scoping) can be
represented by links in the syntax tree
● The edges and links of the tree are relations
between the nodes of the tree
● A relational database can easily represent them
– Every link is bidirectional
– Tree traversal steps are simulated by joining tables
– SQL statements can express complex graph
operations easily

9 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Database Example
● Every node type has new(A) ->
its own table Id = newId(),
add(Id, A).
● Every node gets a
globally unique ID add(Id, A) ->
db:store(Id, A).

Occurence Variable Call Function Body


11 11 11 A 21 0 32 31 new 31 1 41
12 12 12 Id 22 0 33 32 newId 31 2 22
13 12 13 Id 22 1 13 33 add 34 1 23
14 11 14 A 22 2 14 34 add
15 15 15 Id 23 0 35 35 db:store Binding
16 16 16 A 23 1 17 41 1 12
17 15 17 Id 23 2 18 41 2 21
18 16 18 A

10 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Prototype tool
● Refactorings are implemented in Erlang
● Emacs is used as a user interface
– Connection using Distel
● Database backend: MySQL
– Accessed using ODBC
● Parser: standard library
– Slight modifications: comments, position information
● Only changed modules are reparsed between
consecutive refactorings

11 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Benefits and drawbacks
✔ Tree traversal is needed only for analysis
✔ Subgraphs with fixed depth can be manipulated
by singe joint statements – easy and efficient
✔ Large systems do not cause problems

✗ Long, variable length paths are hard to handle


– They can be pre-computed like extra links
✗ In case of smaller programs, database
overhead ruins efficiency

12 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
Further Work
● Design and implement more refactorings
● Speed optimization
● Non-refactoring transformations and syntax-
driven editing capabilities
● Semantic model for correctness proof
● Transformation language: describe refactorings
formally

13 The Fifth Conference of PhD Students in Computer Science, Szeged, Hungary, June 27-30, 2006
The End

Thank you for your attention!

You might also like