Refactoring Erlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös...

Preview:

Citation preview

RefactoringErlang Programs

Huiqing LiSimon Thompson University of

Kent

Zoltán Horváth

Eötvös Loránd Univ.

EUC 2006

Overview

Example refactoringsGeneral observationsChallenges of ErlangOur two implementationsNext steps … and conclusion

EUC 2006

Soft-ware

There’s no single correct design …

… different options for different situations.

Maintain flexibility as the system evolves.

EUC 2006

Generalisation

-module (test).-export([f/1]). add_one ([H|T]) -> [H+1 | add_one(T)];

add_one ([]) -> [].

f(X) -> add_one(X).

-module (test).-export([f/1]). add_one (N, [H|T]) -> [H+N | add_one(N,T)];

add_one (N,[]) -> [].

f(X) -> add_one(1, X).

-module (test).-export([f/1]). add_int (N, [H|T]) -> [H+N | add_int(N,T)];

add_int (N,[]) -> [].

f(X) -> add_int(1, X).

Generalisation and renaming

EUC 2006

Generalisation

-export([printList/1]).

printList([H|T]) -> io:format("~p\n",[H]), printList(T);printList([]) -> true.

printList([1,2,3])

-export([printList/2]).

printList(F,[H|T]) -> F(H), printList(F, T);printList(F,[]) -> true.

printList( fun(H) -> io:format("~p\n", [H]) end, [1,2,3]).

EUC 2006

Generalisation

-export([printList/1]).

printList([H|T]) -> io:format("~p\n",[H]), printList(T);printList([]) -> true.

-export([printList/1]).

printList(F,[H|T]) -> F(H), printList(F, T);printList(F,[]) -> true.

printList(L) -> printList( fun(H) -> io:format("~p\n", [H]) end, L).

EUC 2006

Asynchronous to synchronous

pid! {self(),msg}

{Parent,msg} -> body

pid! {self(),msg}, receive {pid, ok}-> ok

{Parent,msg} -> Parent! {self(),ok}, body

EUC 2006

Refactoring = Transformation + Condition

Transformation

Ensure change at all those points needed.

Ensure change at only those points needed.

Condition

Is the refactoring applicable?

Will it preserve the semantics of the module? the program?

EUC 2006

Transformations

full stop one

EUC 2006

Condition > TransformationRenaming an identifier"The existing binding structure should not be affected. No binding for the new name may intervene between the binding of the old name and any of its uses, since the renamed identifier would be captured by the renaming. Conversely, the binding to be renamed must not intervene between bindings and uses of the new name."

EUC 2006

Tool support

Bureaucratic and diffuse.

Tedious and error prone.

Semantics: scopes, types, modules, …

Undo/redo

Enhanced creativity

EUC 2006

Static vs dynamic

Aim to check conditions statically.

Static analysis tools possible … but some aspects intractable: e.g. dynamically manufactured atoms.

Conservative vs liberal.

Compensation?

EUC 2006

Compensate or fail?

-export([oldFun/1, newFun/1]).

oldFun(L) -> newFun(L).

newFun(L) -> … … .

-export([newFun/1]).

newFun(L) -> … … .

or ?

EUC 2006

Erlang refactoring: challenges Multiple binding occurrences of variables.Indirect function call or function spawn: apply (lists, rev, [[a,b,c]]) Multiple arities … multiple functions: rev/1

ConcurrencyRefactoring within a design library: OTP.Side-effects.

EUC 2006

Haskell vs Erlang refactorings

Haskell Refactorings Erlang Refactorings

Type/type class-related refactorings.Monad-related refactorings. . . . . .

Introduce/remove concurrency,Asynchronous/synchronous

communication,Refactoring to design patterns, . . . . . .

Renaming,Removing unused definitions/parameters,Swapping arguments,Introduce new definitions, . . . . . .

EUC 2006

Architectures

EUC 2006

Wrangler in Emacs

EUC 2006

Wrangler in Emacs

EUC 2006

AST vs database

Lightweight.

Better integration with interactive tools (e.g. emacs).

Undo/redo external?

Ease of implementing conditions?

Higher entry cost.

Better for a series of refactorings on a large project.

Transaction support.

Ease of implementing transformations?

EUC 2006

Next steps

Refactor concurrency

• styles• introduction

Refactor Erlang/OTP

• within OTP• towards OTP

Release systems by the end of January.

Performance comparisons on real systems.

EUC 2006

Ackonwledgements

EPSRC

ELTE IKKK, CNL

Ericsson Hungary

Bolyai Res Fellowship

syntax-tools

distel

Recommended