[Haskell-cafe] can't figure out a type

Ryan Ingram ryani.spam at gmail.com
Wed Feb 11 15:16:52 EST 2009

You can do this with another type class.

class (Chunkable c1 el1, Chunkable c2 el2) => ChunkMap c1 el1 c2 el2 where
    cMap :: (el1 -> el2) -> c1 -> c2

instance ChunkMap [a] a [b] b where cMap = map

If you want to assert that c1 and c2 are really related, you can add
functional dependencies to specify the relation:

class ... | c1 el2 -> c2, c2 el1 -> c1 where ...

Combined with the dependencies in the superclass, this says that if we
have c1 and el2 we can determine c2 and el1, and vice versa.

Also, if "chunkable" has a notion of "cons", "empty", and "fold", you
can write a generic map between any two chunkable instances:

genericCMap :: (Chunkable c1 el1, Chunkable c2 el2) => (el1 -> el2) -> c1 -> c2
genericCMap f = cFold (\x xs -> cCons (f x) xs) cEmpty

  -- ryan

P.S. Check out Data.Traversable.

On Wed, Feb 11, 2009 at 8:52 AM, John Lato <jwlato at gmail.com> wrote:
> Hello,
> I'm working on some code like the following:
>> class Chunkable c el | c -> el where
>>   cLength :: c -> Int
>>   cHead :: c -> Maybe el
> I want to be able to map over this type, like this:
>>  cMap :: Chunkable c' el' => (el -> el') -> c -> c'
> but this isn't quite right.  c' shouldn't be any instance of
> Chunkable, it should be the same instance except parameterized over a
> different type.  Another approach would be something like:
> class (Functor c) => Chunkable c el
> ...
> except that's not right either.  I think c has the wrong kind to be a
> Functor instance.
> I expect there's something very basic I'm missing.  Could anyone point
> in the proper direction of how to do this?  Can this be expressed with
> associated types, perhaps?
> Thanks,
> John Lato
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list