[Haskell] Empty instance declaration

Benja Fallenstein benja.fallenstein at gmail.com
Fri Dec 28 11:30:28 EST 2007


On Dec 28, 2007 5:14 PM, Mike Haskel <mlh2131 at columbia.edu> wrote:
> You can define Show as a data type, rather than a type class:
>
> type Show a = Either (a -> String) (Int -> a -> String -> String)
...
> The constructors for Show make explicit the two ways to define an
> instance.  This technique also has the advantage of allowing multiple,
> non-conflicting instance declarations, selectable at runtime.  Using
> Show as an example, you might have instances representing both
> formatted and unformatted display.  An obvious disadvantage is that
> the instance needs a name and gets passed explicitly.

Seems to me that using Either is entirely orthogonal from type-vs-class :-)

class Show a where
    show0 :: Either (a -> String) (Int -> a -> String -> String)

show :: Show a => a -> String
show = case show0 of
    Left s -> s
    Right sp -> \x -> sp 0 x ""

etc.

- Benja


More information about the Haskell mailing list