Why can't arguments be levity polymorphic for inline functions?

Simon Peyton Jones simonpj at microsoft.com
Fri Oct 8 15:17:33 UTC 2021

We do have a few such functions, and we give them a "compulsory unfolding" which means they MUST be inlined at EVERY call site.  But

  *   Usually if a module exports a function, it generates code for that function. But for these guys it can't.   We don't have a mechanism for *not* generating code for user-defined functions.  We could add an INLINE-COMPULSORY pragma perhaps.
  *   Even then we'd have to check that every call of such a function is applied to enough arguments to get rid of all levity/representation polymorphism; so that when it is inlined all is good. It's not clear how to do that in general.

That's the kind of thing Richard means by "templates".


PS: I am leaving Microsoft at the end of November 2021, at which point simonpj at microsoft.com<mailto:simonpj at microsoft.com> will cease to work.  Use simon.peytonjones at gmail.com<mailto:simon.peytonjones at gmail.com> instead.  (For now, it just forwards to simonpj at microsoft.com.)

From: ghc-devs <ghc-devs-bounces at haskell.org> On Behalf Of Clinton Mead
Sent: 08 October 2021 00:37
To: ghc-devs at haskell.org
Subject: Why can't arguments be levity polymorphic for inline functions?

Hi All

Not sure if this belongs in ghc-users or ghc-devs, but it seemed devy enough to put it here.

Section<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdownloads.haskell.org%2F~ghc%2F9.0.1%2Fdocs%2Fhtml%2Fusers_guide%2Fexts%2Flevity_polymorphism.html&data=04%7C01%7Csimonpj%40microsoft.com%7C74a80049a03e46c9fcb008d989eb68f7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637692467888653138%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=0Rrxrp3SHoh6%2F5Y1C7enO5b58GkAHQlyHjnmnMp94MA%3D&reserved=0> of the GHC user manual points out, if we allowed levity polymorphic arguments, then we would have no way to compile these functions, because the code required for different levites is different.

However, if such a function is {-# INLINE #-} or {-# INLINABLE #-} there's no need to compile it as it's full definition is in the interface file. Callers can just compile it themselves with the levity they require. Indeed callers of inline functions already compile their own versions even without levity polymorphism (for example, presumably inlining function calls that are known at compile time).

The only sticking point to this that I could find was that GHC will only inline the function if it is fully applied<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdownloads.haskell.org%2Fghc%2F9.0.1%2Fdocs%2Fhtml%2Fusers_guide%2Fexts%2Fpragmas.html%23inline-pragma&data=04%7C01%7Csimonpj%40microsoft.com%7C74a80049a03e46c9fcb008d989eb68f7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637692467888653138%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=W4kpJY9sjkeXWhOEK7iScDm0mv9KvMApStdwITr3SxA%3D&reserved=0>, which suggests that the possibility of partial application means we can't inline and hence need a compiled version of the code. But this seems like a silly restriction, as we have the full RHS of the definition in the interface file. The caller can easily create and compile it's own partially applied version. It should be able to do this regardless of levity.

It seems to me we're okay as long as the following three things aren't true simultaneously:

1. Blah has levity polymorphic arguments
2. Blah is exported
3. Blah is not inline

If a function "Blah" is not exported, we shouldn't care about levity polymorphic arguments, because we have it's RHS on hand in the current module and compile it as appropriate. And if it's inline, we're exposing it's full RHS to other callers so we're still fine also. Only when these three conditions combine should we give an error, say like:

"Blah has levity polymorphic arguments, is exported, and is not inline. Please either remove levity polymorphic arguments, not export it or add an  {-# INLINE #-} or {-# INLINABLE #-} pragma.

I presume however there are some added complications that I don't understand, and I'm very interested in what they are as I presume they'll be quite interesting.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20211008/452ed6cc/attachment.html>

More information about the ghc-devs mailing list