| 1 | = Tuple funpar = |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | Tupling the two arguments of function step: |
| 8 | |
| 9 | {{{ |
| 10 | step(A,B) -> {B, A rem B}. |
| 11 | |
| 12 | gcd(A,B) -> |
| 13 | if |
| 14 | B==0 -> A; |
| 15 | true -> |
| 16 | {X,Y} = step(A,B), |
| 17 | gcd(X,Y) |
| 18 | end. |
| 19 | }}} |
| 20 | |
| 21 | Result: |
| 22 | |
| 23 | {{{ |
| 24 | step({A,B}) -> {B, A rem B}. |
| 25 | |
| 26 | gcd(A,B) -> |
| 27 | if |
| 28 | B==0 -> A; |
| 29 | true -> |
| 30 | {X,Y} = step(A,B), |
| 31 | gcd(X,Y) |
| 32 | end. |
| 33 | }}} |
| 34 | |
| 35 | |
| 36 | == Side conditions == |
| 37 | |
| 38 | * The function must be declared at the top level of a module, not a function expression. |
| 39 | |
| 40 | * 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. |
| 41 | * If the function is not exported, it should not conflict with other functions defined in the same module or imported from other modules. |
| 42 | * 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. |
| 43 | |
| 44 | |
| 45 | == Transformation steps and compensations == |
| 46 | |
| 47 | 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. |
| 48 | |
| 49 | 2. 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. |
| 50 | |
| 51 | 3. If the function is exported and another module imports it, then the arity must be adjusted in the corresponding import list of that module. |
| 52 | |
| 53 | 4. Implicit function references are turned into fun expressions that call the function, and the result is handled as any other function call. |
| 54 | |
| 55 | 5. 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. |