Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple usage samples for haskell Data.HList

Where can I find simple examples of usage for Data.HList? From what I read in the wiki, this tool is a "better" solution for heterogeneous lists than existential types, and I don't understand why.

like image 557
tohava Avatar asked Jul 18 '13 21:07

tohava


1 Answers

The article is saying HList better because it is typed.

If you use existential types, you've lost all the type information, and there's not much you can do with that data.

You can make them all instances of some class CanDoStuff that has useful functions (and using a GADT to do this would look so much nicer).

(However, that's running fast towards the existential typeclass antipattern, and you may prefer to avoid all the hassle and instead of writing a typeclass CanDoStuff, you can make a data type HandyStuff with the functions and data you actually use, and use the typeclass simply to overload the name of the function toHandyStuff. That way you could use a regular list.)

Simple HList usage

I think the easiest way to use HList is using the operators in Data.HList.GhcSyntax. For example

andrew = name .=. "Andrew" .*.
         awesomeness .=. 8000 .*.
         glasses .=. True .*.
         emptyRecord

I can use andrew .!. awesomeness to recover the number 8000 and andrew .!. name to get "Andrew". Joyously, these are all typed and thus handy.

We could do awesomeness .=. 4000000 .@. andrew to bump my awesomeness.

Unlike ordinary records, the HList record can be extended at any time with further data of whatever types you like.

Read More

Here's a link to Ralf Lämmel's page about HList, and here's a link to the paper itself.

like image 141
AndrewC Avatar answered Oct 17 '22 16:10

AndrewC