7 Oct 14:11
Type classes question
From: Roly Perera <roly.perera <at> dynamicaspects.org>
Subject: Type classes question
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-10-07 12:13:25 GMT
Subject: Type classes question
Newsgroups: gmane.comp.lang.haskell.cafe
Date: 2008-10-07 12:13:25 GMT
Hi,
I'm reasonably well versed in Haskell but fairly new to defining type classes.
In particular I don't really understand how to arrange for all instances of X
to also be instances of Y.
It's quite possibly that my question is ill-posed, so I'll make it as concrete
as possible: in the following code, I define a Stream class, with two
instances, Stream1 and Stream2. How do I arrange for there to be one
implementation of Functor's fmap for all Stream instances? I currently rely on
delegation, but in the general case this isn't nice.
I guess I'm either misunderstanding what it is I'm trying to achieve, or how to
do this kind of thing in Haskell. Any help would be greatly appreciated.
many thanks,
Roly Perera
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances,
ExistentialQuantification, FunctionalDependencies #-}
module Test where
-------------------------------------------------------------------------------
-- Just some helpers.
-------------------------------------------------------------------------------
-- Product map.
prod :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
f `prod` g = \(a, c) -> (f a, g c)
(Continue reading)
RSS Feed