Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  bugs or lack thereof (Brent Yorgey)
   2.  combining two monads (Ovidiu Deac)
   3. Re:  combining two monads (Daniel Schoepe)
   4.  why is the :type different in ghci? (Ovidiu Deac)
   5. Re:  why is the :type different in ghci? (Brandon Allbery)
   6. Re:  why is the :type different in ghci? (Ovidiu Deac)


----------------------------------------------------------------------

Message: 1
Date: Mon, 22 Aug 2011 10:08:22 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] bugs or lack thereof
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sun, Aug 21, 2011 at 04:53:43PM -0700, Dennis Raddle wrote:
> I love Haskell for the fact that my programs have relatively few bugs
> other than typos or obvious mistakes in types that the compiler
> catches. I just wrote about 100 lines of code to do a fairly complex
> task, which is to make a map of changing loudness in a musical
> document (including slopes, not just flat level changes) and after I
> got the typos out and it compiled, it just worked.
> 
> I have been a professional programmer in imperative languages for 20
> years, so I can think pretty accurately about what I am doing, but I'm
> new to Haskell, and it's a marked difference in the ease of getting
> something to work right.

This has been my experience too.  Haskell is not magic, but it does
help eliminate a large class of silly mistakes: the kind of mistakes
that everyone makes no matter how experienced.  So it frees you up to
only make more interesting mistakes; and if you are an experienced
programmer you don't make as many of those.  So you often get programs
that work the first time, which is (as you know!) a singularly
unsettling and heady experience.

-Brent



------------------------------

Message: 2
Date: Mon, 22 Aug 2011 19:51:18 +0300
From: Ovidiu Deac <[email protected]>
Subject: [Haskell-beginners] combining two monads
To: beginners <[email protected]>
Message-ID:
        <CAKVsE7s8Jjo5-wSD9of_w5PsS==50+22N=wjhsfnaomjuwh...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I wrote some code and I want to refactor it to use the Error monad
combined with IO operations. I found some examples of monad
transformers but I didn't really get them.

So I have a function in IO monad which does several operations which
might fail. These operations work with files so they have to be in IO
monad but on the other hand I would like to use Error monad such that
I can throw errors.

So far I came up with something like this.

data Config = Config { root :: String }

data ParsedData = ParsedData {...}

data MyError = ConfigError | ParseError | OtherError String

instance Error MyError where
    strMsg = OtherError

loadConfig :: String -> Either MyError (IO Config)
loadConfig fn -> ...might throwError ConfigError

parseFile :: String -> Either MyError (IO ParsedData)
parseFile fn -> .. might throwError ParseError

main -> do
   config <- loadConfig "mycfg.cfg"
   parsedData <- parseFile (root config)
   ...

Can somebody please enlighten me about how to apply monad transformers
in this situation?

Thanks,
Ovidiu



------------------------------

Message: 3
Date: Tue, 23 Aug 2011 02:48:41 +0200
From: Daniel Schoepe <[email protected]>
Subject: Re: [Haskell-beginners] combining two monads
To: Ovidiu Deac <[email protected]>, beginners
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Mon, 22 Aug 2011 19:51:18 +0300, Ovidiu Deac <[email protected]> wrote:
> Hi,
> 
> I wrote some code and I want to refactor it to use the Error monad
> combined with IO operations. I found some examples of monad
> transformers but I didn't really get them.
> 
> So I have a function in IO monad which does several operations which
> might fail. These operations work with files so they have to be in IO
> monad but on the other hand I would like to use Error monad such that
> I can throw errors.

For this you need to use a monad transformer instead, in this case
ErrorT. Monad transformers are given an "inner monad", in this case IO,
and usually the same parameters the "original" monad, in this case
Either, expected. A monad transformer normally comes with a runFooT
function that returns a value in the inner monad. RWH has a chapter on
monad transformers that explains them more clearly:

http://book.realworldhaskell.org/read/monad-transformers.html

Here's a shortened version of your example:

import Control.Monad.Error

data Config = Config deriving Show

data MyError = ConfigError | ParseEerror | OtherError String deriving Show

instance Error MyError where
  strMsg = OtherError

loadConfig :: String -> ErrorT MyError IO Config
loadConfig _ = do
  lift $ putStrLn "foo" -- `lift' is necessary to turn an IO action into an 
ErrorT action
  throwError ConfigError

main = print =<< runErrorT (loadConfig "foo")

The result type in your code, "Either MyError (IO Config)", would mean
that the function decides, without doing any IO, whether or not to
return an error and, if not, return an IO action that will read the
config.

Cheers,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110823/b3b2874e/attachment-0001.pgp>

------------------------------

Message: 4
Date: Tue, 23 Aug 2011 06:26:52 +0300
From: Ovidiu Deac <[email protected]>
Subject: [Haskell-beginners] why is the :type different in ghci?
To: beginners <[email protected]>
Message-ID:
        <CAKVsE7sMh6jrQAYkXeizP=qqkfefte_sr5q7zmq-akxcc_+...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Can somebody explain the following situation?

$ ghci
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :type map show
map show :: Show a => [a] -> [String]
Prelude> map show [1,2,3]
["1","2","3"]
Prelude> let f = map show
Prelude> :type f
f :: [()] -> [String]
Prelude> f [1,2,3]

<interactive>:1:8:
    No instance for (Num ())
      arising from the literal `3'
    Possible fix: add an instance declaration for (Num ())
    In the expression: 3
    In the first argument of `f', namely `[1, 2, 3]'
    In the expression: f [1, 2, 3]



------------------------------

Message: 5
Date: Tue, 23 Aug 2011 00:04:30 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] why is the :type different in ghci?
To: Ovidiu Deac <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <CAKFCL4UWGr2t-fb=cpqu-53fazvqxwnqxmnjbvtobym3k34...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Aug 22, 2011 at 23:26, Ovidiu Deac <[email protected]> wrote:

> Prelude> :type map show
> map show :: Show a => [a] -> [String]
> Prelude> map show [1,2,3]
> ["1","2","3"]
> Prelude> let f = map show
> Prelude> :type f
> f :: [()] -> [String]
>

Ah, another victim of the MMR and extended defaulting....

Very briefly, the Haskell spec says that a binding without arguments is
monomorphic, i.e. must have a single specific type.  Your "let f =" is such
a binding.

Extended defaulting is how ghci gets itself out of that situation:  it looks
for a type in the list of default types which can be used to build a
concrete type.  Under standard defaulting rules it would fail, since the
default types are Double and Integer and the result would be an inability to
pick one; under extended defaulting, () is added and breaks the deadlock.
 So you get a defaulted monomorphic type ([()] -> [String]).

If you disable the monomorphism restriction, you will get the most general
type which is the same as you got for ":t map show".  You can also
explicitly type f:

> let f :: Show a => [a] -> String; f = map show

<interactive>:1:8:
>    No instance for (Num ())
>      arising from the literal `3'
>    Possible fix: add an instance declaration for (Num ())
>    In the expression: 3
>    In the first argument of `f', namely `[1, 2, 3]'
>    In the expression: f [1, 2, 3]
>

And this lovely bizarro-land error is because the Haskell spec causes
numeric literals to be translated to applications of fromInteger (so you
don't have to explicitly specify whether it's Int, Integer, Double, Word16,
etc.), so ghci tries to resolve

> f [fromIntegral 1, fromIntegral 2, fromIntegral 3] :: [()] -> [String]

in which fromIntegral must be (Integral a => a -> ()), but can't find a
fromIntegral instance that produces a ().

Common wisdom these days is to disable the monomorphism restriction when
working with ghci; there's a growing belief that it has outlived its
usefulness.

> :set -XNoMonomorphismRestriction

-- 
brandon s allbery                                      [email protected]
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110823/ee3cffe8/attachment-0001.htm>

------------------------------

Message: 6
Date: Tue, 23 Aug 2011 10:36:26 +0300
From: Ovidiu Deac <[email protected]>
Subject: Re: [Haskell-beginners] why is the :type different in ghci?
To: Brandon Allbery <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <CAKVsE7tV_VUj=DGXtm1=UdNKGnf8yA4tvBh_kYAQXXC=ep=p...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Thanks for the explanation.

I kind of understand what you mean but I don't understand why would
anybody want this functionality.


On Tue, Aug 23, 2011 at 7:04 AM, Brandon Allbery <[email protected]> wrote:
> On Mon, Aug 22, 2011 at 23:26, Ovidiu Deac <[email protected]> wrote:
>>
>> Prelude> :type map show
>> map show :: Show a => [a] -> [String]
>> Prelude> map show [1,2,3]
>> ["1","2","3"]
>> Prelude> let f = map show
>> Prelude> :type f
>> f :: [()] -> [String]
>
> Ah, another victim of the MMR and extended defaulting....
> Very briefly, the Haskell spec says that a binding without arguments is
> monomorphic, i.e. must have a single specific type. ?Your "let f =" is such
> a binding.
> Extended defaulting is how ghci gets itself out of that situation: ?it looks
> for a type in the list of default types which can be used to build a
> concrete type. ?Under standard defaulting rules it would fail, since the
> default types are Double and Integer and the result would be an inability to
> pick one; under extended defaulting, () is added and breaks the deadlock.
> ?So you get a defaulted monomorphic type ([()] -> [String]).
> If you disable the monomorphism restriction, you will get the most general
> type which is the same as you got for ":t map show". ?You can also
> explicitly type f:
>> let f :: Show a => [a] -> String; f = map show
>>
>> <interactive>:1:8:
>> ? ?No instance for (Num ())
>> ? ? ?arising from the literal `3'
>> ? ?Possible fix: add an instance declaration for (Num ())
>> ? ?In the expression: 3
>> ? ?In the first argument of `f', namely `[1, 2, 3]'
>> ? ?In the expression: f [1, 2, 3]
>
> And this lovely bizarro-land error is because the Haskell spec causes
> numeric literals to be translated to applications of fromInteger (so you
> don't have to explicitly specify whether it's Int, Integer, Double, Word16,
> etc.), so ghci tries to resolve
>> f [fromIntegral 1, fromIntegral 2, fromIntegral 3] :: [()] -> [String]
> in which fromIntegral must be (Integral a => a -> ()), but can't find a
> fromIntegral instance that produces a ().
> Common wisdom these days is to disable the monomorphism restriction when
> working with ghci; there's a growing belief that it has outlived its
> usefulness.
>> :set -XNoMonomorphismRestriction
> --
> brandon s allbery ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [email protected]
> wandering unix systems administrator (available) ? ? (412) 475-9364 vm/sms
>
>



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 38, Issue 41
*****************************************

Reply via email to