Opened 2 years ago
Last modified 18 months ago
#10327 new feature request
Devise workaround for how infinite types prevent closed type family reduction
Reported by: | goldfire | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | |
Component: | Compiler | Version: | 7.10.1 |
Keywords: | TypeFamilies | Cc: | garrett |
Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |
Type of failure: | None/Unknown | Test Case: | |
Blocked By: | Blocking: | ||
Related Tickets: | Differential Rev(s): | ||
Wiki Page: |
Description
Suppose we have
data a :+: b type family Inj x y where Inj a a = True Inj b (b :+: c) = False
When we try to reduce Inj f (f :+: g)
, it looks like we should just use the second equation. Instead, we fail to reduce. This is because GHC is worried about the possibility of the first equation firing, in the event that f ~ (f :+: g)
. This fact can happen only if f
is infinitely large. On the surface, this seems impossible, but shenanigans in this area can cause unsafeCoerce
. See #8162.
I don't see an easy way to fix this, but the fact that GHC can't cope (well) with this example tells me something is wrong. Here is one idea of how to proceed:
If we somehow ensure at reduction time that f
is finite, we're OK. If we need finiteness in terms, we use deepseq
. Can we do this in types? I tentatively say "yes".
Imagine the following type family:
type family Seq (x :: a) (y :: b) :: b type instance Seq True y = y type instance Seq False y = y
To reduce, say, b `Seq` 5
, we'd need to know concretely what b
is. We can then build Deepseq
similarly to how deepseq
at the term level works.
The closed type family mechanism could then detect cases like Inj
, where the whole infinite-type thing is causing trouble. (I conjecture that detecting this is not hard, as there's a specific line in the Unify
module that triggers in the worry-about-infinite-types case.) In the case of Inj
, something like Inj f (f :+: g)
would reduce to f `Deepseq` False
. Note that the call to Seq
wouldn't be written in the closed type family definition, but would be inserted during reduction as appropriate.
This solution is ugly. And it requires magic to define Seq
in types (we need an instance for every type!) and weird magic in closed type family reduction. The definition of Deepseq
might also benefit from being magical. It would be annoying to explain to users, but no more so than the current crazy story. In general, I don't like this idea much, but I do think it would work.
In any case, this ticket is mainly to serve as a placeholder for any future thoughts in this direction. It's quite annoying to have the specter of infinite types cripple otherwise-sensible closed type families.
Change History (4)
comment:1 Changed 2 years ago by
Cc: | garrett added |
---|
comment:2 follow-up: 3 Changed 2 years ago by
comment:3 Changed 2 years ago by
Replying to simonpj:
Very unconvinced.
As, rest assured, am I.
I think to get this to work we would also need a constraint Finite f
that means (via magic) that f `Seq` x
reduces to x
. (Without magic, this would be spelled forall x. f `Seq` x ~ x
, but that's a higher-order constraint!)
But to really get it to work, we need a totally different idea, as this one is terrible.
comment:4 Changed 18 months ago by
Keywords: | TypeFamilies added |
---|
I must be missing something.
Inf f (f :+: g)
will certainly reduce for any groundf
(such asTrue
orFalse
) because then the call will be apart from the first equation so the second can fire.If
f
is not ground, then it'll still reduce, iff
is anything other thanf2 :+: g
, for the same reason.If
f
is a variable then yes the reduction is stuck, but that seems fair enough. Reducing it tof `Seq` rhs
doesn't get us any further forward (e.g. if we want to unify this with another type) but it does add real new complication.Very unconvinced.