wiki:RefactoringSteps/TupleFunpar

Tuple funpar

In this transformation, consecutive arguments of a function are contracted into a tuple. This transformation addresses the formal parameter list of all the clauses in the function definition as well as the actual parameter list in each perceptible (viz. by static analysis) call of the function. The transformation affects more than one module if the function is exported.

The example illustrates the operation of the transformation on a function with a single clause. Both the definition of step/2 and its application in gcd/2 are changed.

Tupling the two arguments of function step:

step(A,B) -> {B, A rem B}.

gcd(A,B) -> 
   if 
      B==0 -> A; 
      true -> 
         {X,Y} = step(A,B), 
         gcd(X,Y) 
   end. 

Result:

step({A,B}) -> {B, A rem B}.

gcd(A,B) ->
   if
      B==0 -> A;
      true ->
         {X,Y} = step(A,B),
         gcd(X,Y)
   end.

Side conditions

  • The function must be declared at the top level of a module, not a function expression.
  • If the number of parameters that should be contracted into tuple is greater than one the arity of function will be changed. In this case the function with new arity should not conflict with other functions.
    • If the function is not exported, it should not conflict with other functions defined in the same module or imported from other modules.
    • If the function is exported, then besides the requirement above, for all modules where it is imported, it should not conflict with functions defined in those modules or imported by those modules.

Transformation steps and compensations

  1. Change the formal parameter list in every clause of the function: contract the formal arguments into a tuple pattern from the first to the last argument that should be contracted.
  1. If the function is exported from the module, then the export list has to be modified: the arity of the function is updated with new arity.
  1. If the function is exported and another module imports it, then the arity must be adjusted in the corresponding import list of that module.
  1. Implicit function references are turned into fun expressions that call the function, and the result is handled as any other function call.
  1. For every application of the function, modify the actual parameter list by contracting the actual arguments into a tuple from the first to the last argument that should be contracted.
Last modified 12 years ago Last modified on Feb 19, 2012, 10:48:55 PM