goldfirere / units

The home of the units Haskell package
95 stars 19 forks source link

Cannot make instances that mention `Qu` types #22

Closed goldfirere closed 10 years ago

goldfirere commented 10 years ago

Say we want

instance ToJSON (Carbs Volume) where ...
instance ToJSON (Carbs Mass) where ...

We're stuck, because Volume and Mass expand to types that mention type families (specifically, DimFactorsOf). Yet, the code above is quite reasonable. This is directly related to GHC#8828, which asks, essentially, for code like the above to work. But, I've decided (and SPJ agrees) that GHC#8828 is a bad idea, for other reasons.

What to do? Of course, wrapping in a newtype would work. Are there other ways? A few minutes' thought says no.

(Interesting, but ultimately failed idea: It seems straightforward(-ish) to write deepseq's force function at the type level, which would evaluate all type family applications in a type. The problem is that there's no way to get the evaluation started. Even if we had type Volume = Force (MkQu...), there's nothing forcing the RHS of the type synonym. I can't come up with a way around this. Any GHC feature request that comes to mind seems to be a stand-in for proper type pattern synonyms, as requested in the revised GHC#8828.)

Leaving this open as a ticket to either come up with a solution or to document a workaround.

goldfirere commented 10 years ago

Check out Data.Metrology.TH.evalType, which should fix this problem.