-
Notifications
You must be signed in to change notification settings - Fork 52
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
Uniform for Generic #70
Conversation
There's interesting question about efficiency for generation of pure product types. For large products (say |
I'm not particularly concerned about performance here - |
Since |
I disagree about performance. I certainly expect that performance of generic instances would be similar to naively written ones it also should avoid pathological performance cases if it's possible. Otherwise we get "this is standard way to derive instances except don't use it this and that case" (it will be used anyway). And generic deriving is place where it's reasonable to spend optimization effort even if makes things more complicated. Cost is paid once but benefit is spread over all users. |
Any ideas why |
@Bodigrim I am not sure, I've been seeing this with my own packages too, but I haven't investigated it much. Feels like something has changed with ghc-8.10 and Cabal combination which prevents doctests to function properly. I usually only care that doctests are executed with some ghc version, because this way there is an already pretty good chance that examples in haddock are still good. Really seems like an upstream bug and I try to stay away from digging into build tools too much and let someone else figure it out :) We could for now disable doctests with CPP for ghc-8.10, they are already disabled for < ghc-8.2 If you feel like investigating it further though, please let me know what you find. |
@lehins Yeah, I've seen this kind of arbitrary doctest failures with GHC 8.10 before, in my other projects. Disabling for now. @Shimuuar Well, one can certainly get more mileage generating made-to-measure instances via |
I thought a little more about it and come to conclusion that there's much better approach for product types. Current one defines uniform in terms of newtype Angle = Angle Double
instance Uniform Angle where
uniformM g = Angle . ((2*pi)*) <$> uniformM g It's perfectly valid uniform instance yet there's no sane I think better approach is to define generic class GUniform f where
guniformM :: StatefulGen g m => g -> m (f p)
instance GUniform f => GUniform (M1 i c f) where
guniformM = fmap M1 . guniformM
instance Uniform a => GUniform (K1 i a) where
guniformM = fmap K1 . uniformM
instance GUniform U1 where
guniformM _ = return U1
instance (GUniform f, GUniform g) => GUniform (f :*: g) where
guniformM g = (:*:) <$> guniformM g <*> guniformM g
instance (...) => GUniform (f :+: g) where
guniformM g = {- uses Finite -} I think it's better since it avoids introducing |
|
|
I actually thought a bit more and decided that it is better to follow |
I had this idea in mind and couldn't explain it properly. Thanks for articulating it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked generated core and GHC is able to specialize generated constructor. However earlier I found that generics were specialized away even at -O1. Now I they disappear even at -O1. Was that my mistake? We certainly need better tools for gazing into abyss^W GHC core.
Other that Enum instance LGTM
Co-authored-by: Aleksey Khudyakov <[email protected]>
@Shimuuar @lehins @idontgetoutmuch @curiousleo any more review suggestions? |
@Bodigrim Yes, please. The instance that gets derived for tuples in this PR is not something we want in this library. I don't care if it follows all the laws in the World, but |
@lehins done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll admit that am far from a Generics guru, but overall it seems legit and of course the functionality it provides is incredibly useful. 👍 from me. @Shimuuar you had a look at this PR before, is there anything you'd like to add before we merge it?
No everything is good |
Great work! |
Cf. #21 and partially #26