jamshidh
jamshidh

Reputation: 12070

Can one convert an "Either" list to an "HList" in Haskell?

For example, say I have an [Either Int Bool], and I want to convert it to an HList.... so

[Left 1, Right False, Left 2]

would become

1 .*. False .*. 2 .*. HNil

(I actually think this is impossible, but would love to hear otherwise.... even writing the type for such a function seems impossible, although perhaps there is a way to do this that involves more than just writing a function).

Upvotes: 3

Views: 179

Answers (2)

barsoap
barsoap

Reputation: 3376

See this post by Oleg on how to do extensible variants with HList.

Upvotes: 1

Nikita Volkov
Nikita Volkov

Reputation: 43330

You cannot convert [Either Int Bool] to HList because it is a dynamic value, but an HList has a static type which depends on its value. Consider the following:

  • 1 .*. False .*. 2 .*. HNil has type HCons 1 (HCons False (HCons 2 HNil))
  • 1 .*. HNil has type HCons 1 HNil

Both of those values are possible results of your supposed conversion function, but they have different types.

The above is all because the information about what value the HList has must be available to the compiler to figure out its type. In your case you can only get this value in runtime, i.e. when the program is already compiled.

Upvotes: 1

Related Questions