Opened 2 years ago

Closed 2 years ago

#13058 closed task (fixed)

Use coercions to implement fmapDefault and foldMapDefault

Reported by: dfeuer Owned by: dfeuer
Priority: normal Milestone: 8.2.1
Component: Core Libraries Version: 8.0.1
Keywords: Cc: ekmett
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D2916
Wiki Page:

Description

We should be able to use

fmapDefault :: Traversable t => (a -> b) -> t a -> t b
fmapDefault = (getId .) #. traverse .# (Id .)

foldMapDefault :: (Traversable t, Monoid m) => (a -> m) -> t a -> m
foldMapDefault = (getConst .) #. traverse .# (Const .)

where .# and #. are coercion operators found in Data.Profunctor.Unsafe. This should help when the function passed in doesn't inline.

Change History (7)

comment:1 Changed 2 years ago by RyanGlScott

Sounds reasonable to me.

We already have most of the machinery needed to do this, since (#.) is already used in base, located within the internal Data.Functor.Utils module. David, would you care to add (.#) as well and refactor fmapDefault and foldMapDefault as you propose?

comment:2 Changed 2 years ago by dfeuer

Owner: set to dfeuer

comment:3 Changed 2 years ago by ekmett

Sounds good to me.

comment:4 Changed 2 years ago by dfeuer

Differential Rev(s): Phab: D2916
Status: newpatch

comment:5 Changed 2 years ago by dfeuer

Differential Rev(s): Phab: D2916Phab:D2916

comment:6 Changed 2 years ago by Ben Gamari <ben@…>

In 5f91ac8/ghc:

Coerce for fmapDefault and foldMapDefault

Define `fmapDefault = coerce traverse` and `foldMapDefault = coerce
traverse`.  This ensures that we won't get unnecessary allocation and
indirection when the arguments don't inline.

Fixes #13058

Reviewers: ekmett, RyanGlScott, austin, hvr, bgamari

Reviewed By: RyanGlScott

Subscribers: simonpj, RyanGlScott, thomie

Differential Revision: https://phabricator.haskell.org/D2916

GHC Trac Issues: #13058

comment:7 Changed 2 years ago by bgamari

Resolution: fixed
Status: patchclosed
Note: See TracTickets for help on using tickets.