Skip to content

fix type signature of keepAlive - #375

Merged
andrewthad merged 2 commits into
masterfrom
chessai/fix-keepAlive-signature
Mar 22, 2023
Merged

andrewthad merged 2 commits into
masterfrom
chessai/fix-keepAlive-signature

Conversation

@chessai

@chessai chessai commented Mar 21, 2023 •

Copy link
Copy Markdown
Member

Addresses the issue discussed in the conversation starting with this comment.

cc @andrewthad @treeowl @mitchellwrosen for review

cc @bgamari for thoughts as well

@chessai
chessai requested a review from treeowl March 21, 2023 23:34
@chessai

chessai commented Mar 21, 2023

Copy link
Copy Markdown
Member Author

This function needs documentation. Does anyone have any wording in mind?

@mitchellwrosen

Copy link
Copy Markdown

Looks good, thanks.

But while we're making one breaking change, I think I'll propose another, though they are arguably orthogonal.

I should think that any user with a compiler older than GHC 9.0, for which this primitive did not exist, would not find a keepAlive symbol in this API at all. But instead, we export something with the same type signature, but subtly different and sometimes-unsafe semantics, which motivated the development of keepAlive# in the first place! 😎

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

@mitchellwrosen, I believe the NOINLINE makes that safe, because it prevents the compiler from using the fact that the action doesn't terminate (normally). Is there some additional risk?

@mitchellwrosen

mitchellwrosen commented Mar 22, 2023 •

Copy link
Copy Markdown

Oh, yes, thank you that makes sense. I'm not totally confident a sufficiently smart computer couldn't conclude that touch is sometimes unreachable regardless, but if this tricks GHC's simplifier, that's good enough.

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

@mitchellwrosen It only has to trick sufficiently old versions of GHC, so we should be okay.

@andrewthad

Copy link
Copy Markdown
Contributor

I think this looks good. I'll merge this shortly unless there are any concerns about it. I've got something in mind for documentation, but it's going to include a lengthy description of lifted types, unlifted types, and types with identity, so I'll do that in its own PR and we can discuss there.

@andrewthad
andrewthad merged commit cc2c769 into master Mar 22, 2023
@chessai

chessai commented Mar 22, 2023

Copy link
Copy Markdown
Member Author

Last thing I would add to this PR is that we should look into other outstanding PRs/TODOs if we're going to cut a major version release.

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

This is wronger now. Too liberal!

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

Unless you consider the primop type too conservative. Maybe it is?

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

It actually almost certainly is. @chessai, do you think you could open a GHC proposal to make keepAlive# work with arbitrary State# types? I don't imagine there's any reason to imagine such an operation wouldn't be useful and sometimes necessary in ST+FFI.

@andrewthad

Copy link
Copy Markdown
Contributor

@chessai I'm not sure why you think the new type is too liberal. What type do you think would be right?

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

@andrewthad That's what I thought for a few minutes, because it unsafely coerces the state token to work in contexts where the primop doesn't. But now I'm wondering if the primop is overly restrictive....

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

Really, there should be one decision for both the primop and this function.

@chessai

chessai commented Mar 22, 2023

Copy link
Copy Markdown
Member Author

@chessai I'm not sure why you think the new type is too liberal. What type do you think would be right?

I think @treeowl said this, unless I'm confused

@andrewthad

Copy link
Copy Markdown
Contributor

Sorry, I meant to address that last message to @treeowl

I now understand that @treeowl is suggesting

keepAlive :: (PrimBase m, PrimState m ~ RealWorld) => a -> m r -> m r

to match what GHC does. However, primitive already disagrees with ghc-prim in its treatment of touch. GHC wants a RealWorld token, and primitive wants it to work in ST, not just IO.

Here, I think that it's GHC that should change, not primitive. There are non-IO situations where it makes sense to use byteArrayContents or mutableByteArrayContents to get an pointer into a byte array, and we need to keep the byte array live until the end. It's the ST+FFI situations (which sadly GHC requires unsafeCoerce, another thing that I think could stand to be improved) where I see this the most.

@lehins

lehins commented Mar 22, 2023 •

Copy link
Copy Markdown
Contributor

@treeowl and @chessai It is totally ok for keepAlive to work in ST, it does not have to have RealWord state token. So the signature and implementation are absolutely fine IMHO.

In fact I've asked @bgamari to change the signature for keepAlive# when he was working on the implementation: https://gitlab.haskell.org/ghc/ghc/-/merge_requests/3131#note_274330 His response was:

I have no objection to generalizing over the state token type.

I guess he just probably forgot to do that.

@treeowl

treeowl commented Mar 22, 2023

Copy link
Copy Markdown
Contributor

Okay, please consider my concern entirely retracted. We should be sure to remove the in unsafe correction once it becomes unnecessary.

@simonpj

simonpj commented Mar 23, 2023

Copy link
Copy Markdown

I have not been following the details here, but I'd love to get a thumbs-up from @bgamari . He has spend ages getting keepAlive# right and knows where all the bodies are buried.

@andrewthad

Copy link
Copy Markdown
Contributor

I've opened GHC Issue 23163 to discuss there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants