Перейти до Домашньої сторінки Груп Google    comp.lang.haskell
Re: reads

Frank Poettgen <frank.poett...@post.rxwytxh-ayazcyhzeyn.de>

Dirk Thierbach schrieb:

> Mark T.B. Carroll <Mark.Carr...@aetion.com> wrote:
>> Frank Poettgen <Frank.Poett...@Post.RxWyTxH-AyAzCyHzEyN.de> writes:

>>> I would expect

>>> reads "1.2" :: [(Int,String)]

>>> to be evaluated to

>>> [(1,".2")]

>>> but using Hugs (Version Sep 2006) it is just

>>> []

>>> Bug or feature?

>> Good question. GHCi, version 6.8.2, agrees with Hugs.

> The reason at least in GHC is that all numeric instances use the same
> function to parse the string. The string is then converted, unless
> conversion fails.

Thanks for your explanation.

> Because of that, one also gets e.g.
>> reads "1e2" :: [(Int,String)]
> [(100,"")]

Really? -Hugs says [].

:type 1e2
100.0 :: Fractional a => a

> AFAIK the report doesn't specify exactly how to parse a number, so I
> strongly suspect "feature". But I never noticed that before, too.

It can be a problem doing parsing with polymorphism. Consider '.' to be
a binary infix symbol describing the branching of a tree of arbitrary
type. Then one gets e.g.

parseTree "'a'.'b'.'c'.'d'" :: (Tree Char)
   /\
  /\d
/\c
ab

parseTree "1.2.3.4" :: (Tree Float)
    /\
   /  \
1.2  3.4

but

parseTree "1.2.3.4" :: (Tree Int)
no parse

You can't parse a tree of type String as well (without escape codes),
but that's obvious.