[Haskell-cafe] NoMonomorphismRestriction forces odd situation when using contexts

Noon Silk noonslists at gmail.com
Mon Dec 21 01:01:54 UTC 2015


Hello,

  Consider the following program:

-------------------------------------------------------------------
-- bar.hs
{-# LANGUAGE NoMonomorphismRestriction #-}

data Status = Foo | Bar

data Rec m a = Rec {
             get    :: m a
           , status :: Status
           }

defRec :: (Monad m) => Rec m a
defRec = undefined

myRec :: (Monad m) => Rec m a
myRec = Rec x y
  where
      Rec x y = defRec
-------------------------------------------------------------------

  It doesn't compile (under GHC 7.10.2), the error is:

bar.hs:16:7:
    No instance for (Monad t0)
    The type variable ‘t0’ is ambiguous
    When checking that ‘y’ has the inferred type
      y :: Status
    Probable cause: the inferred type is ambiguous
    In an equation for ‘myRec’:
        myRec
          = Rec x y
          where
              Rec x y = defRec
Failed, modules loaded: none.


  If you remove the language extension, it does.

  Keeping the NoMonomorphismRestriction, it can be forced to typecheck by
putting in a "dummy" type that satifies the constraint when obtaining the
field `status` (that doesn't depend on `m`.

-------------------------------------------------------------------
-- bar.hs
{-# LANGUAGE NoMonomorphismRestriction #-}

data Status = Foo | Bar

data Rec m a = Rec {
             get    :: m a
           , status :: Status
           }

defRec :: (Monad m) => Rec m a
defRec = undefined

myRec :: (Monad m) => Rec m a
myRec = Rec x y where
    s :: Rec Maybe a -> Status
    s = status
    x = get defRec
    --
    y = s defRec
-------------------------------------------------------------------


  So, what's going on here? Is this a feature of this extension? It seems
like the ability to destructure and then recombine is pretty fundamental,
and it shouldn't break in the way that it does. (I.e. it's kinda crazy to
have to put in a dummy type satisfying the constraint; is there some way to
pass down the error

-- 
Noon Silk, ن

https://silky.github.io/

"Every morning when I wake up, I experience an exquisite joy — the joy
of being this signature."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151221/eb88a7e0/attachment.html>


More information about the Haskell-Cafe mailing list