haskell-benign icon indicating copy to clipboard operation
haskell-benign copied to clipboard

Auto-derive SeqIsEval for Generic type

Open aspiwack opened this issue 2 years ago • 3 comments

Idea suggested by @profpatsch on twitter

We could add an instance instance (Generic a, …) => SeqIsEval a. This instance would check that every field is strict (is it possible in the generic framework? I expect it is. Though I imagine that it's a little bit of work).

This instance is overlapping, but there is no risk in overlapping instances here: SeqIsEval has trivial evidence, so is automatically coherent. On the other hand, we need to be a little careful with error messages, because the instance will always trigger. It needs to point the user in the right direction if it eventually fails.

aspiwack avatar Nov 09 '22 07:11 aspiwack

There's a similar idea in this issue https://github.com/haskell/deepseq/issues/3 . I developed the gist from there a bit to handle recursion here: https://gist.github.com/TeofilC/a85da6a388ec94e8acb1519886fbd2a7

A slightly different approach is taken here: https://gist.github.com/anka-213/440cfef8c8712f5df747029640cfe170

One thing to also watch out for is that newtypes need to be handled as a special case as their field is always DecidedLazy (cf. https://gitlab.haskell.org/ghc/ghc/-/issues/21380)

Hope these links help!

TeofilC avatar Nov 09 '22 22:11 TeofilC

Beautiful! This is all very helpful, thank you. Especially the newtype thing. Newtypes are easy to detect in Generic if I remember correctly, but it's would be quite easy to overlook.

aspiwack avatar Nov 12 '22 11:11 aspiwack

I've also recently published a library that uses TH to do this stuff that you might find useful: https://tracsis.github.io/th-deepstrict/

It's not on Hackage yet but should be in a couple of months

TeofilC avatar Feb 14 '24 11:02 TeofilC