[Haskell-beginners] Ambiguous type of WriterT result I am not using

Kim-Ee Yeoh ky3 at atamo.com
Fri Nov 20 17:21:52 UTC 2015


Hi Martin,

But the compiler complains about ambiguous type for the writer reult I
> am ignoring (message in the above lpaste).
>

There are a number of types going on here, and the one that's ambiguous is
the monoid w in WriterT w m a.


> Normally I think the way around this is to provide explicit type
> annotation for "foldIt", but in this case the result type depends on the
> type of "a" in foldrEntries type and I don't know how to express this
> and make the compiler happy.
>

David's ScopedTypeVariables solution is perfectly cromulent. Here's another
one without any pragmas:

foldrEntries ::
      (Entry -> a -> a)
   -> a
   -> (String -> a)
   -> Entries
   -> a
foldrEntries next done fail' =
   isoR . foldrEntriesW (isoL .: next) (isoL done) (isoL . fail')
   where
   isoL :: a -> WriterT () Identity a
   isoL = return
   isoR :: WriterT () Identity a -> a
   isoR = fst . runIdentity . runWriterT

The (.:) is from Data.Composition.

The isoL and isoR witness the isomorphism, and foldrEntries is written in a
point-minimized form that makes it transparent that it's a specialization
of foldrEntriesW.

Do eschew verbosities like "return . fst =<< runWriterT". (Hlint should be
programmed to catch this. It doesn't yet.) A return that's too near a
monadic bind sets off alarms among professional haskell engineers.


> (I was able to make it work by calling "tell ()", basically writing a
> dummy value, which lets compiler know what the type is, but this is not
> so good - I don't want to make artificial function calls like this.)
>

Good call.

-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151121/3f604bf1/attachment.html>


More information about the Beginners mailing list