Hacker News new | past | comments | ask | show | jobs | submit login

My most recent hack in this area is in [1]. We read in a bunch of type signatures from JSON and parse them with haskell-src-exts, but Haskell doesn't give us a way to turn these representations-of-types into actual types. The values we're constructing are to be passed into an API which requires all of its arguments to be Typeable, which it hides inside several layers of existentials (heterogeneous lists of heterogeneous maps of ...).

Thankfully for our purposes there's an escape hatch: the values we provide won't be evaluated, so they can be `undefined`, and the `TypeRep`s generated from these values are only used for determining whether two types are (intensionally) equal or different.

My solution was to define two new types, `Z` and `S a`, which can be used as peano numerals. We convert all of the distinct types in our JSON into distinct peano numeral types (`replaceTypes`), we generate `TypeRep` values for these numeral types (`getRep`), fabricate values of these types as arguments to the underlying library (`getVal`), then switch back all of the types which occur in the result (`restoreTypes`).

Very ugly, but it works, thanks to the wiggle room I had based on what these types and values would be used for.

Other projects have been even more icky, e.g. sending strings of auto-generated Haskell code through `runhaskell` [2] and throwing auto-generated template haskell into GHCi to see what sticks [3].

I don't recommend any of this for production code :)

[1] http://hackage.haskell.org/package/reduce-equations

[2] http://hackage.haskell.org/package/nix-eval

[3] http://chriswarbo.net/git/annotatedb/branches/master/annotat...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: