[Haskell-cafe] DataKinds + KindSignatures question

Frank Staals frank at fstaals.net
Thu Nov 27 09:06:30 UTC 2014


Wojtek Narczyński <wojtek at power.com.pl> writes:

> Hello,
>
> The following code:
>
>> {-# LANGUAGE DataKinds #-}
>> {-# LANGUAGE KindSignatures #-}
>>
>> import Data.ByteString
>>
>> data CmdKind = GET | SET
>>
>> class Serialize (cmd :: CmdKind) where
>>    serialize :: cmd -> ByteString
>
> Results in the following error:
>
>> wojtek at wojtek-desktop:~/src/he/snip$ ghc dk.hs
>> [1 of 1] Compiling Main             ( dk.hs, dk.o )
>>
>> dk.hs:9:17:
>>     Expected a type, but ‘cmd’ has kind ‘CmdKind’
>>     In the type ‘cmd -> ByteString’
>>     In the class declaration for ‘Serialize’
>
> Is this so by design, or should I write it in a different way, or is it just
> not implemented?

As Karl explained, type parameters of (->) need to have kind *, whereas
your argument cmd has kind CmdKind. A ``workaround'' to this is using
Proxy (or any other type that has something of kind CmdKind as (phantom)
type). You can then define something like:

import Data.Proxy

class Serialize (cmd :: CmdKind) where
    serialize :: Proxy cmd -> ByteString

myByteString = serialize (Proxy :: Proxy GET)

Depending on what you are trying to do that may be applicable. 

Regards,

-- 

- Frank


More information about the Haskell-Cafe mailing list