Changes between Initial Version and Version 1 of RefactoringSteps/GeneralizeFunction


Ignore:
Timestamp:
Feb 19, 2012, 10:31:38 PM (13 years ago)
Author:
manualwiki
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • RefactoringSteps/GeneralizeFunction

    v1 v1  
     1= Generalize function = 
     2 
     3This refactoring generalizes a function definition by selecting an expression (or an expression sequence), and makes this the value of a new parameter added to the definition of the function. The actual parameter at every call site becomes the selected part with the corresponding compensation. 
     4 
     5The generalized function will not be exported from the module. If the original function is exported from the module, a new function definition with the original arity will be created which calls the generalized function with the corresponding arguments. 
     6 
     7In the example a function for double the elements of a list is generalized by selecting the subexpression {{{2}}}. The subexpression is replaced with the variable {{{N}}} and the subexpression is added to the argument-list of function call in the body of {{{f(Z)}}} function. 
     8 
     9Introduce a new parameter to function {{{double/1}}}: 
     10 
     11{{{ 
     12-module(gen1). 
     13-export([double/1]). 
     14 
     15double(List) -> 
     16   [2*X || X <- List]. 
     17 
     18f(Z)-> 
     19   double(Z). 
     20}}} 
     21 
     22Result: 
     23 
     24{{{ 
     25-module(gen1). 
     26-export([double/1]). 
     27 
     28double(List) -> 
     29   double(List,2) 
     30 
     31double(List, N) -> 
     32   [N*X || X <- List] 
     33 
     34f(Z)-> 
     35   double(Z, 2). 
     36}}} 
     37 
     38 
     39== Side conditions == 
     40 
     41 * The name of the function with its arity increased by one should not conflict with another function, either defined in the same module, imported from another module, or being an auto-imported built-in function. 
     42 
     43 * The new variable name does not exist in the scope of the selected expression(s) and must be a legal variable name. If the new variable name does not keep these conditions, the transformation starts an interaction to ask for a new variable name. 
     44 
     45 * The starting and ending positions should delimit an expression or a sequence of expressions. 
     46 
     47 * The selected expressions do not bind variables that are used outside the selection. 
     48 
     49 * Variable names bound by the expressions do not exist in the scopes of the generalized function calls. 
     50 
     51 * The expressions to generalize are not patterns and they do not call their containing function. 
     52 
     53 * If the selection is part of a list comprehension, it must be a single expression and must not be the generator of the comprehension. 
     54 
     55 * The extracted sequence of expressions are not part of a macro definition, and are not part of macro application parameters. 
     56 
     57 
     58== Transformation steps and compensations == 
     59 
     60 1. If the selected expression does not contain any variables: 
     61  * Give an extra argument (a simple variable) to the function. The name of this argument is the name given as a parameter of the transformation. 
     62  * Replace the expression with a variable expression. 
     63  * Add the selected expression to the argument list of every call of the function. 
     64 
     65 2. If the selected expression contains variable(s) or contains more than one expression or has a side-effect: 
     66  * Add a new argument (a simple variable) to the function definition with the given variable name. 
     67  * Replace the expression with an application. The name of the application is the given variable name, the arguments of the application are the contained variables. 
     68  * Add a fun expression to the argument list of every call of the function. The parameters of the fun expression are the contained variables and the body is the selected expression. 
     69 
     70 3. If the selected expression is part of a guard expression: 
     71  * Give an extra argument (a simple variable) to the function. The name of this argument is the name given as a parameter of the transformation. 
     72  * Replace the guard expression with a variable expression with that new name. 
     73  * Add the selected expression to the argument list of the function calls. 
     74  * If the selected expression contain formal parameter, replace this with the actual parameter. 
     75  * If the selected guard is a conjunction or a disjunction, create an andalso or an orelse expression and add this to the argument list of the function call. 
     76 
     77 4. If the generalized function is recursive, than instead of add the selection to the argument list of the function calls in the body, add the new parameter to the argument list. 
     78 
     79 5. If the generalized function is exported from the module, add a new function definition to the current module with the same parameters and guard. The body of this function is a call of the generalized function. 
     80 
     81 6. If the selection contains macro application/record expression move the definition of the macro/record before the first call of function.