Search code examples
haskellhlist

how do you annotate type in HList


I'm trying to use HList and I need to annotate it but I can't figure out how to do it.

If if type the following in ghci

>:t "hello" .*. HNil

I got the following type signature :

HList ((':) * [Char] ('[] *))

Which I don't really understand. If try to use this signature I got an error

"hello" .*. HNil :: HList ((':) * [Char] ('[] *))

I got an error

<interactive>:21:15: parse error on input `:'

What should I do ?

(I've seen things using :*: so I could in theory do String :*: HNil but it can't find :*:.


Solution

  • The way certain type signatures are printed in older versions of ghc are quite bad. The type HList ((':) * [Char] ('[] *)) really means HList ( ([Char] :: *) ': ('[] :: *) ). But this probably isn't very clear either, so lets go through it:

    • [Char] :: * A list of char, whose kind (the type of a type) is *, which is the kind of things which have values. This type comes from the type of "hello"

    • '[] :: * The type representing the empty type level list. The type that the compiler print is actually wrong - the kind of '[] isn't *, it is [*] - the same way that you cons a and [a] with :. This type comes from the type of HNil.

    • ': Equivalent of : but for type level lists. This comes from the type of .*.

    You can't give the type signature back because it is simple invalid. But any of the following would be correct:

    "hello" .*. HNil :: HList ([Char] ': '[])
    "hello" .*. HNil :: HList ( ([Char] :: *) ': ('[] :: [*]))
    "hello" .*. HNil :: HList '[ String ]
    "hello" .*. HNil :: HList '[ (String :: *) ]
    

    You can't write the type level list cons prefix - I would assume this is a bug in the parser:

    >"hello" .*. HNil :: HList ( (':) String '[])
    
    <interactive>:4:31: parse error on input `:'
    

    The explicit kind annotations are unnecessary, but you can put them in anyways.

    The printing was improved in ghc 7.8:

    >:t "hello" .*. HNil
    "hello" .*. HNil :: HList '[[Char]]