210 likes | 242 Views
Learn about refactoring Erlang programs, with example refactorings, general observations, challenges faced, implementations, and next steps from EUC 2006. Discover different design options and maintaining system flexibility as it evolves.
E N D
RefactoringErlang Programs Huiqing Li Simon Thompson University of Kent Zoltán Horváth Eötvös Loránd Univ.
Overview • Example refactorings • General observations • Challenges of Erlang • Our two implementations • Next 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
-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). Generalisation Generalisation and renaming -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). EUC 2006
-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]). Generalisation EUC 2006
-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). Generalisation EUC 2006
pid! {self(),msg} {Parent,msg} -> body pid! {self(),msg}, receive {pid, ok}-> ok {Parent,msg} -> Parent! {self(),ok}, body Asynchronous to synchronous EUC 2006
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? Refactoring = Transformation + Condition EUC 2006
Transformations full stop one EUC 2006
Condition > Transformation • Renaming 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 vsdynamic • Aim to check conditions statically. • Static analysis tools possible … but some aspects intractable: e.g. dynamically manufactured atoms. • Conservative vsliberal. • Compensation? EUC 2006
-export([oldFun/1, newFun/1]). oldFun(L) -> newFun(L). newFun(L) -> … … . -export([newFun/1]). newFun(L) -> … … . Compensate or fail? 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 • Concurrency • Refactoring within a design library: OTP. • Side-effects. EUC 2006
Haskell vsErlang refactorings Haskell Refactorings Erlang Refactorings Introduce/remove concurrency, Asynchronous/synchronous communication, Refactoring to design patterns, . . . . . . Type/type class-related refactorings. Monad-related refactorings . . . . . . 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
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? AST vsdatabase EUC 2006
Refactor concurrency styles introduction Refactor Erlang/OTP within OTP towards OTP Release systems by the end of January. Performance comparisons on real systems. Next steps EUC 2006
EPSRC ELTE IKKK, CNL Ericsson Hungary Bolyai Res Fellowship Ackonwledgements syntax-tools distel EUC 2006