<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p>What about phantom types?</p>
<pre> {-# LANGUAGE KindSignatures, DataKinds #-}
data IsNormalized = Normalized | NotNormalized
data Sum (n :: IsNormalized) = Value Int | Sum (Sum n) (Sum n)
</pre>
<p>You could still say</p>
<pre> instance MyClass (Sum n) where…</pre>
<p>but you could also write</p>
<pre> normalizeSum :: Sum NotNormalized -> Sum Normalized -- or even create a class with two inhabitants for this
instance Eq (Sum NotNormalized) where (==) = (==) `on` normalizeSum
instance Eq (Sum Normalized) where … -- real implementation</pre>
<p>It's not ideal. For example if you want to sort a list, it would
still be better to normalize the whole list before sorting, but at
least you could still use the other operators afterwards without a
"denormalization" step. So maybe it would help reduce some
boilerplate?<br>
</p>
<p>Cheers,<br>
MarLinn<br>
</p>
</body>
</html>