Andrew Cowie
Andrew Cowie

Reputation: 130

List Instances for higher-kinded types

If I have a MultiParamTypeClass, say Dictionary a k v (whatever, it's kind is * -> * -> *) and I would like to make an instance for lists (ie [(k,v)]), is that possible?

I get that the compiler is complaining that that [(k,v)] is only kind * but is is possible to make this list (in this example an association list) behave like Pair k v but still being able to use Haskell's list type?

In code,

class Dictionary a where
    method :: Thing k v -> a k v

I'd like to form

instance Dictionary [(k,v)] where
    method = ...

Can that be done? I realize there is the OverloadedLists extension but I would like something that works on things more than literals.

Upvotes: 0

Views: 93

Answers (1)

Li-yao Xia
Li-yao Xia

Reputation: 33429

Maybe you can change your type class to be indexed by the fully applied dictionary type, instead of a type constructor * -> * -> *:

{-# LANGUAGE TypeFamilies, FlexibleInstances #-}

class Dictionary d where
  type Key d :: Type
  type Value d :: Type
  f :: Thing (Key d) (Value d) -> d

instance Dictionary [(k, v)] where
  type Key [(k, v)] = k
  type Value [(k, v)] = v
  f = ...

instance Dictionary (Map k v) where
  type Key (Map k v) = k
  type Value (Map k v) = v
  f = ...

Upvotes: 5

Related Questions