haskelloptimizationghc# Does GHC optimize the monoid operation over mempty?

If I write an instance of `Monoid`

with a horrible complexity for its operation `(<>)`

, will GHC know that

```
mempty <> x = x
x <> mempty = x
```

and avoid computing `(<>)`

?

I'm also interested in *how* you got that information, and, if this optimization does not exits, whether this could be done or has been discussed previously.

Solution

Here's a sketch for how a rewrite rule can accomplish what you've layed out in your comment:

```
{-# LANGUAGE UnicodeSyntax #-}
module RewriteMempty where
data Big = Big String
instance Semigroup Big where
Big s <> Big t = Big . reverse $ reverse s++reverse t
instance Monoid Big where
mempty = Big ""
class Inflatable a where
inflate :: a -> Big
instance Inflatable Double where
inflate = Big . show . replicate 100000
{-# NOINLINE poppedBalloon #-}
poppedBalloon :: Big
poppedBalloon = mempty
{-# RULES "OmitPopped" ∀ x . poppedBalloon <> x = x #-}
instance Inflatable Int where
{-# INLINE inflate #-}
inflate _ = poppedBalloon
big :: Big
big = inflate (pi :: Double)
debig :: Big -> String
debig (Big s) = show $ length s
{-# SPECIALIZE busy :: Int -> String #-}
busy :: Inflatable a => a -> String
busy x = debig $ inflate x <> big
```

- Haskell fails to infer the return type of a monad after using the sequence operator
- Does extracting values from a multiple Value return in Haskell invoke the function more than once?
- How to specify c/c++ compiler on stack install command
- Why do I get "Unexpected reply type" from notify-send when using this Haskell notification server?
- Don't understand notation of morphisms in Monoid definition
- Foldln in haskell
- Is this property of a functor stronger than a monad?
- How to Instantiate a Custom Data Type with Record Syntax and Multiple Constructors
- How do I make a minimal working example for the a DBus server?
- Is it safe to downgrade Haskell stack version?
- Haskell, list of natural number
- unfamiliar syntax in Haskell / Clash function type signature
- foldM with monad State does not type check
- Why does my Runge-Kutta implementation oscillate to 0?
- How do I get the desired behavior in my TCP server?
- Why does the Haskell PVP describe new functions as non-breaking?
- How do I correctly use toLower in Haskell?
- How can I write a notification server in Haskell?
- Every Lens' is a Traversal'... how?
- How do I crate a value of type a{sv} for a call to org.freedesktop.Notifications.Notify via DBus?
- Web Scraping With Haskell
- Double exclamation marks in Haskell
- Haskell Servant POST FormUrlEncoded for (Vector String) field
- Confusion about list types in Haskell
- Idiomatic way to define new vs persisted types in Haskell
- Why does Cabal, unlike GHC, not automatically enable GeneralizedNewtypeDeriving if I explicitly enabled DerivingStrategies?
- What is the proper way of wrapping an Int (not a general type) in another type if type safety is the only motive?
- Parsing inside `between` with Megaparsec
- takeWhile implementation in JavaScript - Looking for better ideas
- How to setup MINGW environment variables for Haskell language server in vscode?