[Redirected to g-h-u, as this might be of general interest]
Jon Mountjoy <[EMAIL PROTECTED]> writes:
>
> I would like to inline >>=
>
> So, I would like to write:
>
> instance Monad Foo where
> (Bar p) >>= f = zapo
>
> I would then like to inline >>= at this type. How do I do this
> (important) inlining? ( The source code for the GHC cheats. There, >>=
> is not used and ugly 'then*''s appear all over the show.)
>
If you look at the sources for the prelude libraries, the Monad
instances in there do more or less what you're trying to do
(see ghc/IOBase.lhs and ghc/STBase.lhs)
(The reason for why the compiler source is not using >>= and
the like is that it is (still) Haskell 1.2(+), so there's no constructor
classes around).
To attach inlining pragmas you can either do as you suggest
> If I define thenS and do a {- INLINE thenS -} and then a
> (>>=) = thenS, will I only have one inlining?
>
or, if you define the `bind' and `unit' directly when declaring the
instance, you can put inline pragmas inside that decl, e.g.,
instance Monad Foo where
{-# INLINE (>>=) #-}
(Bar p) >>= f = ....p...f...
...
With 2.02/3, INLINE pragmas are not honoured across modules, i.e.,
if when compiling module Bar the compiler rules the unfolding of
Foo.foo as being too expensive, no unfolding is done - regardless
of whether the programmer supplied an inline pragma for Foo.foo
or not. (This doesn't imply that an INLINE pragma guarantees that
an unfolding will happen, just that the unfolding machinery is a
*lot* more lenient). The upcoming 2.04 fixes this shortcoming.
(For more info on inlining, see the section "Faster: producing a
program that runs quicker" in the user's guide)
--Sigbjorn