[Haskell-cafe] Newbie Q: Overloading and type classes

Luke Palmer lrpalmer at gmail.com
Sat Jun 7 15:08:11 EDT 2008

2008/6/7 Dmitri O.Kondratiev <dokondr at gmail.com>:
> class Store s where
>     put :: Eq k => (k, v) -> s  -> s
>     get :: k s -> Maybe v

I suspect you want this to be a constructor class.  That is, you want
to make explicit the fact that the type s depends on k and v.

class Store s where
    put :: Eq k => (k,v) -> s k v -> s k v
    get :: s k v -> Maybe v

If instead you have cell types which are restricted in what they can
store in different ways, you might explore fundeps or associated

-- fundeps
class Store s k v | s -> k v where
    put :: (k,v) -> s -> s
    get :: s -> Maybe v

-- associated types
class Store s where
    type Key s :: *
    type Value s :: *
    put :: (Key s, Value s) -> s -> s
    get :: s -> Maybe (Value s)

But if you can get away with the former, I would recommend that before
looking into these advanced extensions.


More information about the Haskell-Cafe mailing list