Opened 5 years ago

Last modified 2 years ago

#9790 new feature request

Produce coercion rules for derived Functor instances

Reported by: dfeuer Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.9
Keywords: coercion, deriving Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

I don't think we do this yet, but I think we probably can. If we derive a Functor instance, we should, whenever possible and valid, produce some sort of fmap coerce = coerce rule for that type. This is done manually for lists in GHC.Base, but I imagine it probably applies (much) more generally.

Change History (5)

comment:1 Changed 5 years ago by goldfire

Such a rule is wrong for non-lawful functors, which is why we haven't done it.

It would be nice (perhaps even very nice) if we could give users a way to say that a particular instance is lawful w.r.t. the appropriate laws and would allow magic like the proposed idea. However, I'm not volunteering to design and/or implement such a system...

comment:2 in reply to:  1 Changed 5 years ago by dfeuer

Replying to goldfire:

Such a rule is wrong for non-lawful functors, which is why we haven't done it.

It would be nice (perhaps even very nice) if we could give users a way to say that a particular instance is lawful w.r.t. the appropriate laws and would allow magic like the proposed idea. However, I'm not volunteering to design and/or implement such a system...

Right, that's why I only mentioned derived Functor instances. Surely a derived instance that does not depend on any non-derived instances should be lawful!

comment:3 Changed 5 years ago by goldfire

Ah -- I missed the word "derive". But, unfortunately, this still doesn't pan out in all cases:

data BadFunctor a = MkBF a a
  deriving Show

instance Functor BadFunctor where
  fmap f (MkBF x _) = MkBF (f x) (f x)

data Wrapped f a = MkW (f a)
  deriving (Functor, Show)

Wrapped has a derived Functor instance, and yet fmap id (MkW (MkBF True False)) is not the same as id (MkW (MkBF True False)).

I do think your idea is sound as long as the derived Functor instance has no constraints.

comment:4 in reply to:  3 Changed 5 years ago by dfeuer

Replying to goldfire:

I do think your idea is sound as long as the derived Functor instance has no constraints.

That's what I was trying to get at with the bit about not depending on non-derived Functor instances.

comment:5 Changed 2 years ago by RyanGlScott

Keywords: deriving added
Note: See TracTickets for help on using tickets.