Skip to content
  • Ryan Scott's avatar
    Remove superfluous code when deriving Foldable/Traversable · a82956df
    Ryan Scott authored
    Currently, `-XDeriveFoldable` and `-XDeriveTraversable` generate
    unnecessary `mempty` and `pure` expressions when it traverses of an
    argument of a constructor whose type does not mention the last type
    parameter. Not only is this inefficient, but it prevents `Traversable`
    from being derivable for datatypes with unlifted arguments (see
    Trac #11174).
    
    The solution to this problem is to adopt a slight change to the
    algorithms for `-XDeriveFoldable` and `-XDeriveTraversable`, which is
    described in [this wiki
    page](https://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/DeriveFu
    nctor#Proposal:alternativestrategyforderivingFoldableandTraversable).
    The wiki page also describes why we don't apply the same changes to the
    algorithm for `-XDeriveFunctor`.
    
    This is techincally a breaking change for users of `-XDeriveFoldable`
    and `-XDeriveTraversable`, since if someone was using a law-breaking
    `Monoid` instance with a derived `Foldable` instance (i.e., one where `x
    <> mempty` does not equal `x`) or a law-breaking `Applicative` instance
    with a derived `Traversable` instance, then the new generated code could
    result in different behavior. I suspect the number of scenarios like
    this is very small, and the onus really should be on those users to fix
    up their `Monoid`/`Applicative` instances.
    
    Fixes #11174.
    
    Test Plan: ./validate
    
    Reviewers: hvr, simonpj, austin, bgamari
    
    Reviewed By: simonpj, bgamari
    
    Subscribers: thomie
    
    Differential Revision: https://phabricator.haskell.org/D1908
    
    GHC Trac Issues: #11174
    a82956df