Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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:  parsec and a never terminating parser (Adam Flott)
   2.  Maybe and Just (Shishir Srivastava)
   3.  Type Parameters and Type Variables (Shishir Srivastava)
   4. Re:  Maybe and Just (Michael Alan Dorman)
   5. Re:  Maybe and Just (Lyndon Maydwell)
   6. Re:  Maybe and Just (Mike Meyer)


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

Message: 1
Date: Wed, 25 Mar 2015 17:53:46 -0400
From: Adam Flott <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] parsec and a never terminating parser
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

On Wed, 25 Mar 2015 17:10:00 -0400
David McBride <[email protected]> wrote:

> The problem is your show instance.
> 
> show x' = show x', means that when x' is a FullState, it shows it, which
> causes an infinite loop.
> 
> You need to take out the default case and add something for FullState.
> 

Doh! Nice catch. Thanks David!

For those keeping track, here's how to fix such a case:

    instance Show x => Show (FieldState x) where
        show (EmptyState)   = "empty ('-')"
        show (OmittedState) = "omitted ('^')"
        show (OtherState)   = "other ('~')"
        show (FullState x') = show x'


> On Wed, Mar 25, 2015 at 11:37 AM, Adam Flott <[email protected]> wrote:
> 
> > I'm having trouble understanding why my simple parser never terminates when
> > specific input is used.
> >
> > For example, let's say the first column is a field which can be in one of 4
> > states, empty, omitted, other, and any arbitrary value. That is,
> >
> >     data FieldState a = EmptyState | OmittedState | OtherState | FullState
> > a
> >       deriving (Eq, Ord)
> >
> > When attempting to use,
> >
> >     $ echo "- " | ./parser
> >     "- \n"
> >     empty ('-')
> >     $ echo "^ " | ./parser
> >     "^ \n"
> >     omitted ('^')
> >     $ echo "~ " | ./parser
> >     "~ \n"
> >     other ('~')
> >     [ all of this is as expected ]
> >     $ echo "1 " | ./parser
> >     "1 \n"
> >     [ computer twiddles it's thumbs here until I manually terminate it ...
> > ]
> >     ^C^C
> >     $
> >
> > Does anyone know what's happening and now to alleviate it?
> >
> >
> > -- begin full code --
> >
> > -- base
> > import Control.Applicative
> > import Data.Word
> >
> > -- Hackage
> > import qualified Data.Text.Lazy as TL
> > import qualified Data.Text.Lazy.IO as TLIO
> > import Text.Parsec (parse)
> > import Text.Parsec.Text.Lazy (Parser)
> > import Text.Parser.Combinators
> > import Text.Parser.Char
> >
> > data FieldState a = EmptyState | OmittedState | OtherState | FullState a
> >   deriving (Eq, Ord)
> >
> > instance Functor FieldState where
> >     fmap f (FullState a)    = FullState (f a)
> >     fmap _ EmptyState       = EmptyState
> >     fmap _ OmittedState     = OmittedState
> >     fmap _ OtherState       = OtherState
> >
> > instance Applicative FieldState where
> >     pure = FullState
> >     (FullState f) <*> (FullState x) = FullState (f x)
> >     _             <*> _             = EmptyState
> >
> > instance Monad FieldState where
> >     (FullState x) >>= k = k x
> >     EmptyState    >>= _ = EmptyState
> >     OmittedState  >>= _ = OmittedState
> >     OtherState    >>= _ = OtherState
> >
> >     (FullState _) >> k = k
> >     EmptyState    >> _ = EmptyState
> >     OmittedState  >> _ = OmittedState
> >     OtherState    >> _ = OtherState
> >
> >     return  = FullState
> >     fail _  = EmptyState
> >
> > instance Show (FieldState x) where
> >     show (EmptyState)   = "empty ('-')"
> >     show (OmittedState) = "omitted ('^')"
> >     show (OtherState)   = "other ('~')"
> >     show x' = show x'
> >
> > data Counter = Counter Word64 deriving (Eq, Ord, Show)
> >
> > parseNum :: (Num a) => Parser a
> > parseNum = do
> >     n <- rd <$> many digit
> >     return $ fromIntegral n
> >     where rd = read :: String -> Integer
> >
> > parseCounter :: Parser Counter
> > parseCounter = Counter <$> parseNum
> >
> > parseFieldStateOff :: Parser Char
> > parseFieldStateOff = char '-'
> >
> > parseFieldStateOmitted :: Parser Char
> > parseFieldStateOmitted = char '^'
> >
> > parseFieldStateOther :: Parser Char
> > parseFieldStateOther = char '~'
> >
> > parseFieldState :: Parser a -> Parser (FieldState a)
> > parseFieldState p = (parseFieldStateOff >> return EmptyState)
> >                   <|> (parseFieldStateOmitted >> return OmittedState)
> >                   <|> (parseFieldStateOther >> return OtherState)
> >                   <|> (p >>= return . FullState)
> >
> > main :: IO ()
> > main = do
> >     ls <- TLIO.getContents
> >     print ls
> >     mapM_ processLine (TL.lines ls)
> >
> > processLine :: TL.Text -> IO ()
> > processLine line = case (parse (parseFieldState parseCounter) "" line) of
> >   Left err -> print err
> >   Right xs -> print xs
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >
> >


-- 

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150325/883e9636/attachment-0001.sig>

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

Message: 2
Date: Thu, 26 Mar 2015 10:06:17 +0000
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Maybe and Just
Message-ID:
        <CALe5RTsU5_QDnh7RPyi+4VYMDgs0aM=fmvnxlkfzpb-a3cc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

After reading and re-reading the haskell tutorials I don't happen to see a
very convincing or appealing reason for having these data types.

Can anyone please explain where Maybe and Just provide the sort of
functionality that cannot be achieved in other languages which don't have
these kind.

Thanks,
Shishir Srivastava
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150326/926492ee/attachment-0001.html>

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

Message: 3
Date: Thu, 26 Mar 2015 10:21:30 +0000
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Type Parameters and Type Variables
Message-ID:
        <CALe5RTv-_nf=lwkkkamw3_xe_lisnfw6uhalev41rrtjh1l...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

What's the difference between the two.. ?

Shishir Srivastava
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150326/cc5a25c3/attachment-0001.html>

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

Message: 4
Date: Thu, 26 Mar 2015 06:26:15 -0400
From: Michael Alan Dorman <[email protected]>
To: Shishir Srivastava <[email protected]>
Cc: beginners <[email protected]>
Subject: Re: [Haskell-beginners] Maybe and Just
Message-ID: <[email protected]>
Content-Type: text/plain

Shishir Srivastava <[email protected]> writes:
> After reading and re-reading the haskell tutorials I don't happen to
> see a very convincing or appealing reason for having these data
> types.

To be clear: Maybe is the data *type*.  Just and Nothing are its data
*constructors*.

> Can anyone please explain where Maybe and Just provide the sort of
> functionality that cannot be achieved in other languages which don't
> have these kind.

The functionality can be achieved in other languages, certainly.  The
question is whether the clarity and safety is also achieved.

When I see (as a totally contrived example):

  fopen :: Maybe FileHandle

I know that that function may not be able to return a FileHandle value
all the time.  The compiler will, in fact, nag me if I do not write the
code that calls it in such a way that it acknowledges that possibility.

When I see:

  FILE * fopen ( const char * filename, const char * mode );

It is not immediately clear whether that can fail.  Sure, we can make
that inference, based on what we know about filesystems, etc., but the
compiler is never going to complain if I ignore the possibility.

In my experience, programmers in many languages end up resorting to
convention to try and work around these sorts of ambiguities.  Large
projects have strategies for naming functions that try to pass along
information out of band, or languages have a pervasive culture of "lint"
tools that try to use heuristics to make up for what the type system
doesn't make simple.

That said, I know that doing Maybe sorts of things in languages that
don't have, say, pattern matching, or the idea of a "failure monad",
gets to be a drag very quickly---manually unwrapping things is at best
awkward, having to re-wrap them just to unwrap them again in a sequence
of computations quickly leads one to believe "it's just not worth
it"---or you resort to exception handling, which has its own challenges
to do well.

Mike.


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

Message: 5
Date: Thu, 26 Mar 2015 21:27:53 +1100
From: Lyndon Maydwell <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe and Just
Message-ID:
        <CAM5QZtw0yvX4sQ9pAB2hJ-P-7guQH=vct2fmyee7q48qadr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Shishir,


Indeed, Maybe doesn't grant you additional operational functionality that
other languages lack. Most use-cases of Maybe can be matched in nearly all
mainstream languages through the use of NULL.

Maybe illustrates a desired lack of functionality in cases where it is
absent.

For example, in Java:

public static Integer myMethod(Integer a, Integer b) {
  // body}


What do we know about this method from the signature?

You would think it is a method that takes two integers and returns an
integer, but really it could take NULLs too, and could possibly return a
NULL.

Here is the equivalent Haskell signature:

myMethod :: Maybe Integer -> Maybe Integer -> IO (Maybe Integer)


We can see that Maybe allows us to replicate the possibility of NULL values
from other languages... But how is that special since other languages
already have this built-in.

Now look at the following Haskell signature:

myMethod :: Integer -> Integer -> Integer


Now what do we know about this?

We know that it takes two Integers and returns an Integer.

This is the power that comes from having Maybe as the canonical way to
represent the possibility of missing values.


On Thu, Mar 26, 2015 at 9:06 PM, Shishir Srivastava <
[email protected]> wrote:

> Hi,
>
> After reading and re-reading the haskell tutorials I don't happen to see a
> very convincing or appealing reason for having these data types.
>
> Can anyone please explain where Maybe and Just provide the sort of
> functionality that cannot be achieved in other languages which don't have
> these kind.
>
> Thanks,
> Shishir Srivastava
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150326/624cc397/attachment-0001.html>

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

Message: 6
Date: Thu, 26 Mar 2015 05:28:40 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Maybe and Just
Message-ID:
        <CAD=7u2anrdhqa34almnpsbpmptszhojgklvxq6dvg-whydb...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Mar 26, 2015 at 5:06 AM, Shishir Srivastava <
[email protected]> wrote:

> Hi,
>
> After reading and re-reading the haskell tutorials I don't happen to see a
> very convincing or appealing reason for having these data types.
>
> Can anyone please explain where Maybe and Just provide the sort of
> functionality that cannot be achieved in other languages which don't have
> these kind.
>

When a function fails to produce the return value, it can either return a
distinguished value (a NULL pointer, or -1 to signal an error, or ...) or
raise an exception. The latter is sufficiently clumsy that it's not
uncommon to see languages with two versions of some functions: one that
raises an exception, and one that returns a distinguished value.

Haskell uses the Maybe type for the distinguished value, raising it to the
type level. If I call a function that expects a value that is NOT the
distinguished value (a valid pointer, or a valid array index, or ...) in
languages without something like a Maybe type, I find out about it when my
unit tests fail if I'm lucky, or I just  get the wrong answer if I'm not.
When I call a function that expects an X with a value of type Maybe X in
Haskell, it's a type error at compile time.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150326/2c882605/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 81, Issue 61
*****************************************

Reply via email to