| 1 | = Eliminate variable = |
| 2 | |
| 3 | In this refactoring, all instances of a variable are replaced with its bound value. Those instances of the variable where the value of the instance is not used can be dropped. |
| 4 | |
| 5 | Replacing all instances of variable {{{Y}}} with its value: |
| 6 | |
| 7 | {{{ |
| 8 | func(X) -> |
| 9 | Y=X+2, |
| 10 | Pid ! {value,Y}, |
| 11 | Y. |
| 12 | }}} |
| 13 | |
| 14 | Result: |
| 15 | |
| 16 | {{{ |
| 17 | func(X) -> |
| 18 | |
| 19 | Pid ! {value,X+2}, |
| 20 | X+2. |
| 21 | }}} |
| 22 | |
| 23 | == Side conditions == |
| 24 | |
| 25 | * The variable has exactly one binding occurrence on the left hand side of a pattern matching expression, and not a part of a compound pattern. |
| 26 | |
| 27 | * The expression bound to the variable has no side effects. |
| 28 | |
| 29 | * Every variable of the expression is visible (that is, not shadowed) at every occurrence of the variable to be eliminated. |
| 30 | |
| 31 | * If the selection is not specify a variable but it is inside a function clause, the tool gives a list to the user to select a variable. The list contains the reachable variables in the given function clause. |
| 32 | |
| 33 | |
| 34 | == Transformation steps and compensations == |
| 35 | |
| 36 | 1. Every occurrence of the variable is substituted with the expression bound to it at its binding occurrence, with parentheses around the. |
| 37 | |
| 38 | 2. If the result of the match expression that binds the variable is discarded, the whole match expression is removed. Otherwise, the match expression is replaced with its right hand side. |