[Haskell-cafe] Re: Has anyone looked into adding subtyping to
Haskell?
Tomasz Zielonka
tomasz.zielonka at gmail.com
Thu May 31 15:04:30 EDT 2007
On Thu, May 31, 2007 at 06:16:20PM +0100, Jon Harrop wrote:
> > I can't think of a lightweight way to encode overlapping enumerations in
> > Haskell.
>
> I'd like to know if this is possible in Haskell.
Maybe this way using GADTs and typeclasses? I haven't used such code in
practice - there may be some hidden traps.
{-# OPTIONS -fglasgow-exts #-}
module Enum where
data Height
data Size
class HasMEDIUM t
instance HasMEDIUM Height
instance HasMEDIUM Size
data ENUM t where
LOW :: ENUM Height
MEDIUM :: HasMEDIUM t => ENUM t
HIGH :: ENUM Height
SMALL :: ENUM Size
BIG :: ENUM Size
Example use:
*Enum> :t [MEDIUM, LOW]
[MEDIUM, LOW] :: [ENUM Height]
*Enum> :t [MEDIUM, SMALL]
[MEDIUM, SMALL] :: [ENUM Size]
*Enum> :t [MEDIUM, SMALL, LOW]
<interactive>:1:16:
Couldn't match expected type `Size' against inferred type `Height'
Expected type: ENUM Size
Inferred type: ENUM Height
In the expression: LOW
Best regards
Tomek
More information about the Haskell-Cafe
mailing list