ekmett / profunctors

Haskell 98 Profunctors
http://hackage.haskell.org/package/profunctors
Other
70 stars 43 forks source link

Why is unsafe unsafe? #106

Open Jashweii opened 1 year ago

Jashweii commented 1 year ago

Edit - rephrased since I originally wrote this really unclearly What exactly is unsafe about profunctor.unsafe? The reasons given by the documentation are 1) An implementation might unsafe coerce 2) You might not give it a function that is operationally identity

But seemingly: 1) This is the instance's/implementation's concern. 2) It almost certainly can't make use of the function anyway, beyond seq. It has a coercible constraint, meaning it knows a function with the same type that IS operationally identity.

And even if 2 didn't apply, or if you really wanted something that looks like NewtypeConstructor #. p, with the "unsafe" bit being NewtypeConstructor isn't called and it's misleading if you pass in some other function, why couldn't this be a free function in an unsafe module which calls perfectly safe methods lcoerce/rcoerce, which either A) Take Coercions without the coercible constraint B) Take Proxys with the coercible constraint C) Don't take any argument at all with the coercible constraint

This way at least the whole type class is perfectly safe. Off topic addendum: Would it be better to have something like (Maybe (Coercion a b -> Coercion (p a x) (p b x)))? This way you can compose coercions where possible. But I don't know if there's any situation where you can more efficiently map a coercion but can't get a coercion back.