[Haskell-cafe] Newbie Q: Overloading and type classes
Dmitri O.Kondratiev
dokondr at gmail.com
Sat Jun 7 12:44:35 EDT 2008
{--
I try to define class Store that will group types implmenting different
storage mechanisms .
All types should support two functions:
1) put (key, value) pair into storage
2) get value from storage corresponding to the given key
As an example I start with the following storage types:
--}
-- simple memory cell
data OneCell k v = Cell(k, v)
-- list of cells
data CellList k v = CLst [(k, v)]
{--
First, without grouping these types into any class, just to illustrate what
these types should do, I define the following functions:
--}
{--
put::(k, v) -> OneCell k v -> OneCell k v
put (k1, v1) (Cell(k, v)) = Cell(k1, v1)
get :: Eq k => k -> OneCell k v -> Maybe v
get k (Cell (_, v)) = Just v
c1 ::OneCell String Int
c1 = Cell("one", 1)
putLst :: (k,v) -> CellList k v -> CellList k v
putLst (k,v) (CLst xs) = CLst ((k,v) : xs)
getLst :: Eq k => k -> CellList k v -> Maybe v
getLst k (CLst xs) = lookup k xs
cl1 = CLst [("one",1),("two",2),("three",3)]
--}
{--
These work as expected.
Now I try to define Store class that should allow me to overload put & get
functions:
--}
class Store s where
put :: Eq k => (k, v) -> s -> s
get :: k s -> Maybe v
{--
instance Store OneCell where
put (k1, v1) (Cell(k, v)) = Cell(k1, v1)
get k (Cell (_, v)) = Just v
--}
{--
I get the following error:
`OneCell' is not applied to enough type arguments
Expected kind `*', but `OneCell' has kind `* -> * -> *'
In the instance declaration for `Store OneCell'
--}
instance Store CellList where
put (k,v) (CLst xs) = CLst ((k,v) : xs)
get k (CLst xs) = lookup k xs
{--
I get the following error:
`CellList' is not applied to enough type arguments
Expected kind `*', but `CellList' has kind `* -> * -> *'
In the instance declaration for `Store CellList'
--}
What should I do to create Store class?
Thanks!
--
Dmitri O. Kondratiev
dokondr at gmail.com
http://www.geocities.com/dkondr
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080607/ddd86072/attachment.htm
More information about the Haskell-Cafe
mailing list