This is an introduction to a programming idiom for placing extra
constraints on the construction of values by using smart
constructors. Sometimes you need guarantees about the values in
your program beyond what can be accomplished with the usual type
system checks. Smart constructors can be used for this purpose.
Consider the following problem: we want to be able to specify a
data type for electronic resistors. The resistors come in two
forms, metal and ceramic. Resistors are labelled with a number
of bands, from 4 to 8. We'd like to be able to - ensure only
resistors with the right number of bands are constructed. 1
Runtime checking : smart constructors 1.1 A first attempt Code
up the a typical data type describing a resistor value: data
Resistor = Metal Bands | Ceramic Bands deriving Show type Bands
= Int This has a problem however, that the constructors of type
Resistor are unable to check that only bands of size 4 to 8 are
built. It is quite legal to say: *Main> :t Metal 23 Metal 23 ::
Resistor 1.2 Smart(