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: Problem installing graphalyze with Cabal (John Velman)
2. Re: writing many1Till combinator for Parsec (Vlad Skvortsov)
3. in which monad am I? (Francesco Guerrieri)
4. Re: in which monad am I? (Rahul Kapoor)
5. Re: in which monad am I? (Francesco Guerrieri)
6. Re: in which monad am I? (Maciej Piechotka)
7. Re: in which monad am I? (Maciej Piechotka)
8. Re: in which monad am I? (Felipe Lessa)
----------------------------------------------------------------------
Message: 1
Date: Fri, 1 Jan 2010 08:03:41 -0800
From: John Velman <[email protected]>
Subject: Re: [Haskell-beginners] Problem installing graphalyze with
Cabal
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thanks!
John V.
On Fri, Jan 01, 2010 at 10:44:14AM +0100, Henk-Jan van Tuyl wrote:
> On Fri, 01 Jan 2010 05:08:52 +0100, John Velman <[email protected]> wrote:
>
>> What gives? How do I get rid of the extra copies?
>
> You can unregister packages with commands like:
> ghc-pkg unregister binary-0.5.0.1 --user
>
> --
> Regards,
> Henk-Jan van Tuyl
>
>
> --
> http://Van.Tuyl.eu/
> http://members.chello.nl/hjgtuyl/tourdemonad.html
> --
------------------------------
Message: 2
Date: Fri, 01 Jan 2010 19:49:39 -0800
From: Vlad Skvortsov <[email protected]>
Subject: Re: [Haskell-beginners] writing many1Till combinator for
Parsec
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Thanks Daniel!
Just for the record, here is what I ended up with:
many1Till :: Show end => Parser a -> Parser end -> Parser [a]
many1Till p end = do
notFollowedBy' end
p1 <- p
ps <- manyTill p end
return (p1:ps)
{-
Workaround for an overly restrictive type of notFollowedBy.
See also:
http://www.haskell.org/pipermail/haskell-cafe/2005-November/012318.html
-}
notFollowedBy' :: Show a => GenParser tok st a -> GenParser tok st ()
notFollowedBy' p = try $ join $ do a <- try p
return (unexpected (show a))
<|> return (return ())
Daniel Fischer wrote:
> Am Montag 14 Dezember 2009 23:35:13 schrieb Vlad Skvortsov:
>
>> Thanks Daniel! I missed out on 'guard' and somewhy was under impression
>> that 'notFollowedBy' can only deal with Chars.
>>
>
> That's correct, its type is
>
> notFollowedBy :: Show tok => GenParser tok st tok -> GenParser tok st ()
>
> I didn't lookup the type. However, you can easily generalize it:
>
> nFB p = ((try p >> pzero) <|> return ()) <?> ""
>
> Be aware, however, that the use of try may have adverse effects on
> performance.
>
>
>>>> many1Till :: Parser a -> Parser end -> Parser [a]
>>>> many1Till p end = do
>>>> try (end >> (unexpected "sequence terminator"))
>>>> <|> (do { p1 <- p; ps <- manyTill p end; return (p1:ps) })
>>>>
>>>> Here there are two disadvantages:
>>>>
>>>> 1) I don't like hardcoding "sequence terminator" here;
>>>>
>
> Understandable, but I haven't a better idea either.
>
>
>>>> 2) the error output should say that only 'p' parser is expected, while
>>>> it says (technically correct) that either 'p' or 'end' is expected:
>>>>
>
> Remove expectations of end by labelling the test with an empty string:
>
> many1Till p end =
> ((try end >> unexpected "whatever") <?> "")
> <|> do { p1 <- p; ps <- manyTill p end; return (p1:ps) }
>
>
>>>> Prelude Main Text.ParserCombinators.Parsec> parseTest (many1Till letter
>>>> (char '.')) "1"
>>>> parse error at (line 1, column 1):
>>>> unexpected "1"
>>>> expecting "." or letter
>>>>
>>>> (What I want here is to say "expecting letter")
>>>>
>>> For that, you need the slightly clumsier
>>> c)
>>> many1Till p end = do
>>> notFollowedBy end
>>>
>
> make that nFB end or better ((try end >> unexpected "whatever") <?> "")
>
>
>>> p1 <- p
>>> ps <- manyTill p end
>>> return (p1:ps)
>>>
>> Yep, that works but still provides incorrect diagnostics when fed with
>> an empty string:
>>
>> Prelude Main Text.ParserCombinators.Parsec> parseTest (many1Till letter
>> (char '.')) ""
>> parse error at (line 1, column 1):
>> unexpected end of input
>> expecting "." or letter
>>
>> It's not a showstopper, but I'd still like to understand how to make it
>> provide better error messages.
>>
>> Thanks!
>>
>
>
--
Vlad Skvortsov, [email protected], http://vss.73rus.com
------------------------------
Message: 3
Date: Sun, 3 Jan 2010 22:38:28 +0100
From: Francesco Guerrieri <[email protected]>
Subject: [Haskell-beginners] in which monad am I?
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hello, this is my first post to the list.
I am learning Haskell and I think I understand monads (well, that
doesn't mean that I actually understand them :-) )
Nevertheless, I would like to check something with you.
My question is: how do you know in which monad you are computing at a
given time?
What I mean is that, when dealing with more than a monad at the same
time (Maybe, List, IO come to my mind) if I understand correctly you
have to lift values back and forth from a monad to the other. How do
you "keep" track of the monad you are in a given time? I think that
the type system will help to resolve to the relevant function (eg,
thinking to liftM signature, liftM :: (Monad m) => (a1 -> r) -> m a1
-> m r
But doesn't ever happen that poor liftM be confused? and do poor
haskell programmers never feel the need to explicitly state to which
monad they "wish to lift" ?
I hope it's not a too silly question. If so, please point me to the
relevant resouces (Are monad transformers relevant to my issue?)
Thanks in advance,
Francesco
------------------------------
Message: 4
Date: Sun, 3 Jan 2010 17:06:27 -0500
From: Rahul Kapoor <[email protected]>
Subject: Re: [Haskell-beginners] in which monad am I?
To: Francesco Guerrieri <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I am not sure I completely understand your question (I think I do)
> But doesn't ever happen that poor liftM be confused?
The compiler is able to figure out which Monad you are working in
via type inference. This is true for all overloaded functions not
just the monadic ones. For example in this ghci session:
Prelude> :m Control.Monad
Prelude Control.Monad> liftM id [1]
[1]
Prelude Control.Monad> liftM id (Just "qaz")
Just "qaz"
ghci is able to infer that the Monad's in question are the Maybe and
List respectively.
In (almost?) all cases type inference allows the compiler to pick
which overloaded
implementation is to be used.
>and do poor
> haskell programmers never feel the need to explicitly state to which
> monad they "wish to lift" ?
It is probably a matter of taste - but adding type signatures even in
the presence
of type inference is usually a good thing except for the most trivial cases It
serves as a documentation and a quick test by making sure that the
compiler and you
infer the same type since it is a compile error to specify a type
which is not compatible
with what the compiler infers.
The haskell wiki book though incomplete in places does a good job and
so does the
wikipedia entry for Type Inference
http://en.wikibooks.org/wiki/Haskell/Type_basics#Type_inference
http://en.wikipedia.org/wiki/Type_inference
http://stackoverflow.com/questions/463870/when-to-exploit-type-inference-in-haskell
On Sun, Jan 3, 2010 at 4:38 PM, Francesco Guerrieri
<[email protected]> wrote:
> Hello, this is my first post to the list.
>
> I am learning Haskell and I think I understand monads (well, that
> doesn't mean that I actually understand them :-) )
> Nevertheless, I would like to check something with you.
> My question is: how do you know in which monad you are computing at a
> given time?
> What I mean is that, when dealing with more than a monad at the same
> time (Maybe, List, IO come to my mind) if I understand correctly you
> have to lift values back and forth from a monad to the other. How do
> you "keep" track of the monad you are in a given time? I think that
> the type system will help to resolve to the relevant function (eg,
> thinking to liftM signature, liftM :: (Monad m) => (a1 -> r) -> m a1
> -> m r
>
> But doesn't ever happen that poor liftM be confused? and do poor
> haskell programmers never feel the need to explicitly state to which
> monad they "wish to lift" ?
>
> I hope it's not a too silly question. If so, please point me to the
> relevant resouces (Are monad transformers relevant to my issue?)
>
> Thanks in advance,
> Francesco
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Sun, 3 Jan 2010 23:21:20 +0100
From: Francesco Guerrieri <[email protected]>
Subject: Re: [Haskell-beginners] in which monad am I?
To: [email protected], Rahul Kapoor <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On Sun, Jan 3, 2010 at 11:06 PM, Rahul Kapoor <[email protected]> wrote:
> I am not sure I completely understand your question (I think I do)
It appears that you did. Thanks!
> ghci is able to infer that the Monad's in question are the Maybe and
> List respectively.
> In (almost?) all cases type inference allows the compiler to pick
> which overloaded
> implementation is to be used.
Why did you put the "almost"? Given the types on which the function is acting,
either it finds a good match or not, in which case it is a
compile-time error. What am I missing?
>>and do poor
>> haskell programmers never feel the need to explicitly state to which
>> monad they "wish to lift" ?
>
> It is probably a matter of taste - but adding type signatures even in
> the presence
> of type inference is usually a good thing except for the most trivial cases It
> serves as a documentation and a quick test by making sure that the
> compiler and you
> infer the same type since it is a compile error to specify a type
> which is not compatible
> with what the compiler infers.
Great, I didn't recall that a wrong annotation was a compile error.
Francesco
------------------------------
Message: 6
Date: Sun, 03 Jan 2010 23:21:45 +0100
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: in which monad am I?
To: [email protected]
Message-ID: <1262557304.5358.74.ca...@picard>
Content-Type: text/plain; charset="UTF-8"
On Sun, 2010-01-03 at 22:38 +0100, Francesco Guerrieri wrote:
> Hello, this is my first post to the list.
>
> I am learning Haskell and I think I understand monads (well, that
> doesn't mean that I actually understand them :-) )
> Nevertheless, I would like to check something with you.
> My question is: how do you know in which monad you are computing at a
> given time?
> What I mean is that, when dealing with more than a monad at the same
> time (Maybe, List, IO come to my mind) if I understand correctly you
> have to lift values back and forth from a monad to the other. How do
> you "keep" track of the monad you are in a given time? I think that
> the type system will help to resolve to the relevant function (eg,
> thinking to liftM signature, liftM :: (Monad m) => (a1 -> r) -> m a1
> -> m r
>
> But doesn't ever happen that poor liftM be confused? and do poor
> haskell programmers never feel the need to explicitly state to which
> monad they "wish to lift" ?
>
> I hope it's not a too silly question. If so, please point me to the
> relevant resouces (Are monad transformers relevant to my issue?)
>
> Thanks in advance,
> Francesco
'In which monad' question is the same as 'can type system be confused'.
Except for syntax sugar (do) monads are nothing special in Haskell. Not
more special then Read or Show classes.
For example (for type system):
> f :: [String] -> [String]
> f = map show . map read
>
> main = interact (unlines . f . lines)
test.lhs:6:21:
Ambiguous type variable `a' in the constraints:
`Read a' arising from a use of `read' at test.lhs:6:21-24
`Show a' arising from a use of `show' at test.lhs:6:10-13
Probable fix: add a type signature that fixes these type variable(s)
With monad it is harder (as monad is one-way) but it is possible to do
it:
> extract $ liftM (+1) (return 0)
results in:
Ambiguous type variable `f' in the constraints:
`Monad f' arising from a use of `liftM' at <interactive>:1:10-30
`Copointed f'
arising from a use of `extract' at <interactive>:1:0-6
Probable fix: add a type signature that fixes these type variable(s)
We only know that we operate on some monad which is copointed. But we
have no idea what is it. However in 99% of cases we don't have to.
(You man notice the signature of extract is - extract :: (Copointed f)
=> f a -> a so the problem is when we remove type [type does not occures
on RHS of last ->])
Regards
------------------------------
Message: 7
Date: Sun, 03 Jan 2010 23:28:17 +0100
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: in which monad am I?
To: [email protected]
Message-ID: <1262557696.5358.80.ca...@picard>
Content-Type: text/plain; charset="UTF-8"
On Sun, 2010-01-03 at 23:21 +0100, Francesco Guerrieri wrote:
> On Sun, Jan 3, 2010 at 11:06 PM, Rahul Kapoor <[email protected]> wrote:
> > I am not sure I completely understand your question (I think I do)
>
> It appears that you did. Thanks!
>
> > ghci is able to infer that the Monad's in question are the Maybe and
> > List respectively.
> > In (almost?) all cases type inference allows the compiler to pick
> > which overloaded
> > implementation is to be used.
>
> Why did you put the "almost"? Given the types on which the function is acting,
> either it finds a good match or not, in which case it is a
> compile-time error. What am I missing?
>
That further it may be lost (if I misunderstood the question then
sorry). liftM a b will now the correct monad as long as:
1. It knows monad in second parameter
or
2. It knows monad in result
Knowing (or defaulting in case of GHCi) is sufficient.
> >>and do poor
> >> haskell programmers never feel the need to explicitly state to which
> >> monad they "wish to lift" ?
> >
> > It is probably a matter of taste - but adding type signatures even in
> > the presence
> > of type inference is usually a good thing except for the most trivial cases
> > It
> > serves as a documentation and a quick test by making sure that the
> > compiler and you
> > infer the same type since it is a compile error to specify a type
> > which is not compatible
> > with what the compiler infers.
>
Well. As far as 'global' signatures (i.e. the ones directly in module)
are concerned it is correct. However there is sometimes need to write
something like:
> sizeOf (undefined :: CInt)
or
> map (show :: Int -> String) . map read
> Great, I didn't recall that a wrong annotation was a compile error.
>
That's strong type system :)
Regards
------------------------------
Message: 8
Date: Sun, 3 Jan 2010 20:30:59 -0200
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] in which monad am I?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sun, Jan 03, 2010 at 10:38:28PM +0100, Francesco Guerrieri wrote:
> But doesn't ever happen that poor liftM be confused? and do poor
> haskell programmers never feel the need to explicitly state to which
> monad they "wish to lift" ?
If by "explicitly state to which monad" you mean specializing
liftM or fmap, then the answer is no. It is not uncommon to see
fmap (fmap function)
being used to map two layers of structures. If by stating you
mean giving a type signature, then yes, it is a good practice to
give type signatures to all top-level definitions.
The compiler never gets "confused". The only thing that may
happen is having more than one possible type that fulfills your
needs. The classical example is Num overloading:
$ ghci
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :s -Wall
Prelude> :m Data.Int
Prelude Data.Int> let e :: Int; e = 31
Prelude Data.Int> 2^e
<interactive>:1:0:
Warning: Defaulting the following constraint(s) to type `Integer'
`Num t' arising from a use of `^' at <interactive>:1:0-2
In the expression: 2 ^ e
In the definition of `it': it = 2 ^ e
<interactive>:1:0:
Warning: Defaulting the following constraint(s) to type `Integer'
`Num t' arising from a use of `^' at <interactive>:1:0-2
In the expression: 2 ^ e
In the definition of `it': it = 2 ^ e
2147483648
Prelude Data.Int> 2^e :: Int32
-2147483648
Note that using just '2^e' in the GHCi prompt defaults to
Integer. If I say I want Int32 as a type, then the result is
completely different, in this case it overflows.
Another case when you arrive at those oddities is when dealing
with exceptions. The function you use to catch the exceptions
need to explicitly say which type of exceptions it wants to
catch. Failures to list the correct type will cause the
exception to fall through and propagate.
HTH,
--
Felipe.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 19, Issue 2
****************************************