Open gelisam opened 6 years ago
transform covers $ \x -> \(Just x) -> [...]
In the comment, I was careful that the second appearance of the variable was in an expression context, not in a pattern context; if both uses are in a pattern, the TH won't have the same name for both variables.
I see! I'm a bit wary about using the (\x -> Just x)
syntax because this would require converting an expression to a pattern, which would require slightly more maintenance as more patterns are added to the language. Hawk keeps getting broken as new syntax is added to haskell-src-exts
for example, which is why I used the magic from Control.Lens.Plated
this time.
Hawk keeps getting broken as new syntax is added to haskell-src-exts for example
Would th-abstaction help?
turns out their OccNames are the same
ehh... I guess? That strikes me as more fragile, but I don't do much metaprogramming.
th-abstraction
indeed looks helpful for the kind of problems I am describing, but it looks like they aren't supporting patterns nor expressions yet.
Consider the following code:
There are two ways to make this
surjective
-friendly. We could list the three values separately:Or we could split out the
[True, False]
bit into its ownsurjective
-friendly definition:I personally like this second approach, but
surjective
shouldn't be in the business of recommending a coding style. To support the style of the original definition, we'd need to transform small patterns into bigger patterns:Semantically,
transform
is another macro which, likesurjective
, gathers the calls tocovers
within its scope. Then, it transforms them (in this case fromx
toJust x
), and passes them on to the surroundingsurjective
macro, who will gather both the patterns produced bytransform
(in this caseJust True
andJust False
) and the other pattern defined outside oftransform
(in this caseNothing
).Since
covers
has typeCovers a
,transform
must bind its owncovers'
function, of typeCovers Bool
.Suggested by @rpglover64 in the same reddit comment as #8's suggestion.