[Haskell-cafe] [ANN]: traverse-code

David Feuer david.feuer at gmail.com
Sun Jan 23 02:32:56 UTC 2022

I've put up a tiny little package[1], traverse-code, offering a
generic way to "traverse" in Template Haskell's `Code m`.

class TraverseCode t where
  traverseCode :: Quote m => (a -> Code m b) -> t a -> Code m (t b)

I wrote this primarily as a way to "lift" operations on polymorphic
data structures into Template Haskell. For example, for Data.Sequence,
you can write

triple :: a -> a -> a -> Seq a
triple a b c = $$(sequenceCode $ fromList [ [||a||], [||b||], [||c||] ])

The `triple` function will not form its arguments into a list and then
convert it to a sequence; rather, it will build the result sequence
directly, using the underlying constructors.

A generic default definition is available for all instances of
Generics.Linear.Generic1. See linear-generics[2] for information about
how to derive those. There's nothing particularly linear about
traverse-code, but linear-generics has a more general Generic1 than
GHC.Generics offers.

[1] https://hackage.haskell.org/package/traverse-code
[2] https://hackage.haskell.org/package/linear-generics

More information about the Haskell-Cafe mailing list