akborder
akborder

Reputation: 207

Automatic HEq instances for members of an HList

I'm experimenting with HList based typed heterogeneous lists.

I have defined the following:

import Data.HList

data ATag
data BTag

type TagList = ATag :*: BTag :*: HNil

bIndex :: Int
bIndex = hNat2Integral (hFind (undefined :: BTag) (undefined :: TagList))

I was expecting bIndex to have value 1. Instead, I get the following error:

  No instances for (HEq BTag ATag b,
                    HFind' b BTag (HCons BTag HNil) n0)
  arising from a use of `hFind'

It seems that GHC (7.4.1) is not able to automatically infer the instance

  HEq BTag ATag HFalse

Is there any way to make that happen?

Upvotes: 1

Views: 124

Answers (1)

Chris Kuklewicz
Chris Kuklewicz

Reputation: 8153

There are several flavors of HList's labels. You are trying to use your own 'ATag' and 'BTag' phantoms as labels. The HList flavor you are using expects its 'HNat' types as labels: 'HZero' and 'HSucc *'.

You need to import one of the Data.HList.Label1 to Label5 modules. And you will need to choose a TypeEq flavor, and the TypeCast flavor to match the TypeEq flavor:

{-# LANGUAGE TypeOperators #-}

import Data.HList
import Data.HList.Label5
import Data.HList.TypeCastGeneric1
import Data.HList.TypeEqGeneric1

data ATag
data BTag

type TagList = ATag :*: BTag :*: HNil

bIndex :: Int
bIndex = hNat2Integral (hFind (undefined :: BTag) (undefined :: TagList))

The above works and give 'bIndex' the value 1.

Upvotes: 4

Related Questions