[Haskell] Global Variables and IO initializers: A proposal andsemantics

John Meacham john at repetae.net
Fri Oct 15 20:17:02 EDT 2004

On Fri, Oct 15, 2004 at 04:40:09PM -0700, Erkok, Levent wrote:
> >> I wondered if something like that could work, but I wasn't sure that
> >> mdo allowed recursion in its let-bindings...
> >
> >The mdo implementation in ghc does not actually...
> I think there's a misunderstanding there. let expressions inside an mdo
> can of course be recursive; both Hugs and ghc support this:
> -----------------------------
> import Control.Monad.Fix
> main = mdo x <- return y
>            let y = 1:y
>            z <- return y
>            print (take 1 x, take 2 y, take 3 z)
> ------------------------------
> will happily print ([1],[1,1],[1,1,1]); no problems there. Note that the
> let defined variable 'y' is used both before and after its definition;
> and it's also defined recursively.
> May be you are thinking of polymorphism problem. The restriction is
> this: let bound variables in an mdo cannot be use polymorphically if
> they are referred to before their definition. (It's also OK to use them
> polymorphically after their definition.) This is merely there to ensure
> that the translation will be a well-typed Haskell-98 expression; nothing
> more than that. If we choose, we can make it polymorphic everywhere, by
> typing mdo-expressions separately. As John pointed out, there're no
> technical difficulties there. The only thing we lose is that the
> translation may fail to type-check. This was a compromise we had to do,
> and we chose the light-weight view that mdo is only syntactic-sugar. 

Ah yes, that is exactly what I meant. I misread recursion as


John Meacham - ⑆repetae.net⑆john⑈ 

More information about the Haskell mailing list