Uri Braun | 24 Jan 22:14 2013
Picon

Manually instantiating Typeable w/DataKinds

I've read the recent posting titled "Non-derivable Typeable"
(http://www.mail-archive.com/haskell-cafe <at> haskell.org/msg103616.html) which
explains that Typeable cannot be automatically derived for cases where the
kind is constrained.

I'm very impressed that a solution is imminent.  In the interim, can
somebody kindly suggest a workaround? I'm okay with a manual instance, but
I'd appreciate some help as to how to write one. I'm looking for a Typeable
instance for TaggedVar for the following example below (extracted from my
code).

Thank you in advance!

+Uri

{-# LANGUAGE DeriveDataTypeable, DataKinds, KindSignatures #-}
module Example where

import           Data.Typeable

data Tag = TagV | TagE | TagA | TagL
         deriving Typeable

newtype TaggedVar (t :: Tag) = TaggedVar Int
Roman Cheplyaka | 25 Jan 07:40 2013

Re: Manually instantiating Typeable w/DataKinds

Hi Uri,

Here's how it might look.

  {-# LANGUAGE DeriveDataTypeable, DataKinds, KindSignatures, ScopedTypeVariables #-}
  module Example where

  import Data.Typeable
  import Data.Proxy

  data Tag = TagV | TagE | TagA | TagL
           deriving Typeable

  class TypeableTag (t :: Tag) where
    tagRep :: Proxy t -> TypeRep

  instance TypeableTag TagV where
    tagRep _ = mkTyConApp (mkTyCon3 "mypkg" "Example" "'TagV") []

  -- ... same for the other tags

  newtype TaggedVar (t :: Tag) = TaggedVar Int

  instance TypeableTag t => Typeable (TaggedVar t) where
    typeOf _ =
      mkTyConApp
        (mkTyCon3 "mkpkg" "Example" "TaggedVar")
        [tagRep (Proxy :: Proxy t)]

Roman
(Continue reading)


Gmane