<div dir="ltr">At least up to the core representation (generated with -O2) the optimization you suggest has not happened (see the marked line below).  Does it happen further down the pipeline?  Is there some flag I can use to get some lower level representation to see that it actually does happen? How does one get the final STG representation?<div><br></div><div>P.$fFunctorExpr_$cfmap<br>  = \ (@ a_a5Tu)<br>      (@ b_a5Tv)<br>      (f_a5Sb :: a_a5Tu -> b_a5Tv)<br>      (ds_d5Vm :: Expr a_a5Tu) -><br>      case ds_d5Vm of {<br>        Var a1_a5Sc -> P.Var @ b_a5Tv (f_a5Sb a1_a5Sc);<br>        Val x_a5Sg -> P.Val @ b_a5Tv x_a5Sg;       <-- *****************************************<br>        Add e0_a5Se e1_a5Sf -><br>          P.Add<br>            @ b_a5Tv<br>            (P.$fFunctorExpr_$cfmap @ a_a5Tu @ b_a5Tv f_a5Sb e0_a5Se)<br>            (P.$fFunctorExpr_$cfmap @ a_a5Tu @ b_a5Tv f_a5Sb e1_a5Sf)<br>      }<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 2, 2020 at 5:22 PM David Feuer <<a href="mailto:david.feuer@gmail.com" target="_blank">david.feuer@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>You could use unsafeCoerce, but I don't think it's worth the risk. With the plain code, GHC will *try* to recover sharing in cases like this late in compilation, after the types are erased.<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 2, 2020, 10:12 AM Brent Walker <<a href="mailto:brenthwalker@gmail.com" target="_blank">brenthwalker@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">In the following code, function fmap does not compile because variable 'y' on line marked <***> has type (Expr a) where an (Expr b) is expected.  The code can be fixed simply by returning (Val x) on the rhs of the function but then we are allocating a new object for something we could potentially reuse since (Val n) has the same runtime representation in (Expr a) and (Expr b) (it has no dependence on the type variable).<div><br></div><div>I was reading about coerce (Data.Coerce) recently and thought this could be a place it could be used but simply replacing the 'y' on the rhs with 'coerce y' does not compile (error message below).</div><div><br></div><div>Is it possible to use coerce in this context?</div><div><br></div><div>Thanks for any help,</div><div>Brent<br><div><br></div><div><div>=======================================</div><div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif"></div></div></div><div><font face="arial, sans-serif">data Expr a = Var a | Val Int | Add (Expr a) (Expr a)<br>  deriving Show<br><br>instance Functor Expr where<br>  fmap :: (a -> b) -> Expr a -> Expr b<br>  fmap f (Var a) = Var (f a)<br>  fmap f (Add e0 e1) = Add (fmap f e0) (fmap f e1)<br>  fmap _ y@(Val x) = y   -- <***></font><br></div></div><div><font face="arial, sans-serif"><br></font></div><div>=======================================</div><div><div class="gmail_default" style="font-family:"comic sans ms",sans-serif">/dev/proj/src/Ex12.hs:69:22: error:<br>    • Couldn't match representation of type ‘a’ with that of ‘b’<br>        arising from a use of ‘coerce’<br>      ‘a’ is a rigid type variable bound by<br>        the type signature for:<br>          fmap :: forall a b. (a -> b) -> Expr a -> Expr b<br>        at /dev/proj/src/Ex12.hs:66:11-38<br>      ‘b’ is a rigid type variable bound by<br>        the type signature for:<br>          fmap :: forall a b. (a -> b) -> Expr a -> Expr b<br>        at /dev/proj/src/Ex12.hs:66:11-38<br>    • In the expression: coerce y<br>      In an equation for ‘fmap’: fmap _ y@(Val _) = coerce y<br>      In the instance declaration for ‘Functor Expr’<br>    • Relevant bindings include<br>        y :: Expr a (bound at /dev/proj/src/Ex12.hs:69:10)<br>        fmap :: (a -> b) -> Expr a -> Expr b<br>          (bound at /dev/proj/src/Ex12.hs:67:3)<br>   |<br>69 |   fmap _ y@(Val _) = coerce y<br>   |                      ^^^^^^^^<font color="#888888"><br></font></div><font color="#888888"><div><div>=======================================</div></div></font></div></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div></div></div>
</blockquote></div>