I could use TH but not without all of its drawbacks (I already listed some in my post).
TH would not allow me to write zipField
and use it in the same file. I would like to be able to do that.
TH splits your files into sections forcing you to write things in a certain order. I would like to be able to get rid off that.
If we could modify TH somehow to be able sort those two things, then I agree with you, we don’t need a new macro system. Maybe “untyped string” is not the best solution and I’m open to anything.
It is when you can use it, but it stops being useful when you are working with a unknown number of element. Let’s go back to zipField
, which take a list of arguments (just not two). So I could do
data Point = Point {x,y :: Double}
data Point3 = Point3 {x,y,z :: double}
addPoint = Point $((zipField ["x", "y"])
addPoint3 = Point3 $((zipField ["x", "y", "z")
How would you write the equivalent in TH using TH Quotation ? (I’ve done similar things and the code end up being something like foldl AppE (VarE $ mkName fnFame) fs
(which looking at it now, I have no idea what it does).
What I want to create a HKD like
works
data PointF f = PointF { x :: f Double
, y :: f Double
}
data Point3F f ...
With “untyped string macro” in could write (in a single file), something along
$((
makeF :: String -> String -> String
pointF cons fields =
"data $cons f = $cons {"
++ intercalate "\n, " [ "$field :: f Double | field <- words fields ]
++ "\n }"
makeF "Point" "x y"
makeF "Point "x y z"
))
It tooks me 2mn to write. How would be the equivalent in TH ?
makeF cons field s=
DataD [] (mkName cons) ? ?? ? ( foldl AppT ???) []
Every time something is added to GHC, DataD
is getting more arguments which I need to fill in with []
or Nothing
etc …
Probably but I think TH has show is limits which is why since TH has been introduced we got type families, generics (which wouldn’t have been needed if TH has been easy enough to use).
The last proof of limitation of TH is the multiline proposal.
We already have one native way to do multilines, (using \
), then there are plenty of Template Haskell nice solutions to it, with or without string interpolation, with or without leading spaces stripping etc …
If TH was good enough, everybody would be using a TH solution to this problem, but nobody is, thus the need for multilines syntax.