data
type may be expressible in terms of the other constructors. There are complementary advantages of keeping the constructor and in eliminating it in favour of its representation.
data Expr
= Epsilon | .... |
Then Expr Expr |
Star Expr
plus e = Then e (Star e)
literals Epsilon = emptySet
...
literals (Then e1 e2)
= literals e1 `union` literals e2
literals (Star e)
- literals e
literals (plus e)
= literals (Then e (Star e))
= literals e `union` literals e
= ...
|
data Expr
= Epsilon | .... |
Then Expr Expr |
Star Expr |
Plus Expr
literals Epsilon = emptySet
...
literals (Then e1 e2)
= literals e1 `union` literals e2
literals (Star e)
- literals e
literals (Plus e)
- literals e
|
General comment:
This is a very good example of a refactoring which could be applied in either direction, depending upon the circumstances. The particular example is a representation of regular expressions , with Star
corresponding to Kleene star and Plus
to 'one or more occurences of'.
Left to right comment:
The code on the LHS explicitly reflects the interrelatinship between On the other hand, the RHS allows a direct definition of |
Right to left comment:
The RHS allows optimised computations of functions over On the other hand, it requires the definition of the |
Left to right conditions:
The function identified, |
Right to left conditions:
There needs to be a semantic relationship between the constructor identified ( |