Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to help with strict containers #97

Open
treeowl opened this issue Apr 3, 2023 · 2 comments
Open

Add method to help with strict containers #97

treeowl opened this issue Apr 3, 2023 · 2 comments

Comments

@treeowl
Copy link
Contributor

treeowl commented Apr 3, 2023

I propose an NFData method (by some name)

  whnfIsNf :: proxy a -> Bool
  whnfIsNf _ = False

Then we can have, for example,

instance NFData Integer where
  whnfIsNf _ = True
instance NFData a => NFData (Set a) where
  whnfIsNf _ = whnfIsNf (Proxy :: Proxy a)
  rnf
    | whnfIsNf (Proxy :: Proxy a)
    = \ !_ -> ()
    | otherwise
    = ....

This can avoid a bunch of unnecessary traversals.

@treeowl
Copy link
Contributor Author

treeowl commented Apr 4, 2023

We could also offer a function to derive this for Generic instances without the risk of going out of date if the type changes.

@TeofilC
Copy link

TeofilC commented Feb 14, 2024

You might find my th-deepstrict library helpful https://tracsis.github.io/th-deepstrict/

It uses TH to allow making the whnf is nf assertion you mention, which the library calls deep strictness.

In terms of Generics, @bgamari wrote up some code a while ago that does something like this, and I modified it to work with (mutual) recursive types (the trick is you have to treat this as an inductive proof): https://gist.github.com/TeofilC/a85da6a388ec94e8acb1519886fbd2a7
See some earlier discussion here: #3

In the end, the trickiness of doing this with Generics led me towards TH.

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

No branches or pull requests

2 participants