Constructor or constructor function


Identity: ConsOrcons
Category: Data MultiModule
Classifiers: data abstraction
Internal cross references:
External cross references:

Language: This has been described in terms of Haskell, but will apply to any language with abstract data types.

Description: A constructor for a 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 Star and Plus. The calculation of literals is forced to use the definition of plus, which will duplicate the calculation of literals e.

On the other hand, the RHS allows a direct definition of literals (Plus e), thus avoiding the duplicated computation.

Right to left comment:

The RHS allows optimised computations of functions over Plus.

On the other hand, it requires the definition of the Plus case for every function; moving to the LHS means that the plus cases come for free.

Left to right conditions:

The function identified, plus in this case, needs to have return type the data type in question.

Right to left conditions:

There needs to be a semantic relationship between the constructor identified (Plus here) and the other constructors. Verifying this can only be done by the system modeller.