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:  mayBe stuck (Brent Yorgey)
   2. Re:  Parsec (David Virebayre)
   3. Re:  mayBe stuck (Colin Paul Adams)
   4. Re:  Parsec (Patrick LeBoutillier)
   5. Re:  mayBe stuck (Brent Yorgey)
   6. Re:  mayBe stuck (aditya siram)
   7. Re:  mayBe stuck (Daniel Fischer)
   8. Re:  mayBe stuck (Colin Paul Adams)


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

Message: 1
Date: Fri, 6 Aug 2010 10:24:19 +0100
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] mayBe stuck
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Aug 06, 2010 at 09:40:05AM +0100, Colin Paul Adams wrote:
> >>>>> "Brent" == Brent Yorgey <[email protected]> writes:
> 
>     Brent> On Thu, Aug 05, 2010 at 06:20:06PM -0500, aditya siram wrote:
>     >> Normally yes, but here we are guaranteed to get a 'Just ...'
>     >> value because of the 'isNothing' guard.  -deech
> 
>     Brent> You are correct, but that's not the point.  Every time you
>     Brent> use fromJust (or head, or unsafePerformIO...) you shift the
>     Brent> burden of proving that it is safe from the compiler onto
>     Brent> yourself.
> 
> But the compiler could indeed prove that it's safe, if the typing system
> reflected the precondition. 

Sure. But it doesn't.  

My concern is a pragmatic one rather than theoretical.  Maybe for the
benefit of other beginners reading this, I should spell it out a bit
more clearly rather than trying to be too cute: if you ever find
yourself using fromJust, (or head, or any other functions that can
sometimes cause your program to crash), you have to convince yourself
that such a use is safe -- but this probably means you are doing too
much work yourself, rather than letting the compiler do the work for
you.  You should see if there is a way to refactor your code to use
pattern matching, or 'maybe', or 'fromMaybe'.  In deech's particular
case there was a way to pattern-match on the Maybe value (and also
make the code a lot shorter at the same time) rather than using
isNothing and fromJust.

> Since Haskell allows programming with
> partial functions, you always have this burden at present.

Yes.  But that doesn't mean there isn't value in the discipline of
avoiding them.  The burden of making sure you never use fromJust (or
head, or ...) is much lighter than the burden of proving that every
such use is safe.

-Brent


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

Message: 2
Date: Fri, 6 Aug 2010 11:41:00 +0200
From: David Virebayre <[email protected]>
Subject: Re: [Haskell-beginners] Parsec
To: C Gosch <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Fri, Aug 6, 2010 at 11:03 AM, C Gosch <[email protected]> wrote:

> You're right, I probably used the wrong words .. I meant that apparently the
> tokens Parsec uses are of type Char, and I would actually at some point
> like to continue parsing, but using different tokens. Sorry if I still got
> it wrong, I'm new :)  I can post some code later, as I don't have it here
> right now.

Parsec (at least version 3) uses any type of token you want.

Quick example off the top of my head, I didn't check if it compiles:

-- you have to make lists of your token type an instance of Stream :

instance Stream [ MyTokenType ] Identity MyTokenType where
   uncons []     = return Nothing
   uncons (x:xs) = return $ Just (x,xs)

-- your parser type is going to look like this :

type MyParser a = ParsecT [MyTokenType] () Identity a

-- assuming your toke type looks like this

Data MyTokenType = A Char
                 | B Word8
    deriving (Show)


-- you need a basic parser from which you can make more complicated ones

satisChar :: ( Char -> Bool ) -> MyParser Char
satisChar f = tokenPrim prt pos match
    where
      prt           = show
      pos p _l _cs  = incSourceLine p 1
      match (A c)   = if f c then Just c else Nothing
      match _       = Nothing

satisBin :: ( Word8 -> Bool ) -> MyParser Word8
satisBin f = tokenPrim prt pos match
    where
      prt           = show
      pos p _l _cs  = incSourceLine p 1
      match (B w)   = if f w then Just w else Nothing
      match _       = Nothing

-- You can define basic parsers like this

-- parse any letter
letter = satisChar (const True)

-- parse a specific char, it will return
char c = satisChar (==c)

-- parse any binary word
binary = satisBin (const True)

-- parse a specific binary
word w = satisBin (==w)

-- now you can combine this to make more complicated parsers.

...


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

Message: 3
Date: Fri, 06 Aug 2010 11:15:52 +0100
From: Colin Paul Adams <[email protected]>
Subject: Re: [Haskell-beginners] mayBe stuck
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

>>>>> "Brent" == Brent Yorgey <[email protected]> writes:

    >> Since Haskell allows programming with partial functions, you
    >> always have this burden at present.

    Brent> Yes.  But that doesn't mean there isn't value in the
    Brent> discipline of avoiding them.  The burden of making sure you
    Brent> never use fromJust (or head, or ...) is much lighter than the
    Brent> burden of proving that every such use is safe.

The burden is knowing whether or not a function is partial. It's that or
... that is the problem. This isn't flagged anywhere.
-- 
Colin Adams
Preston Lancashire
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments


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

Message: 4
Date: Fri, 6 Aug 2010 07:59:03 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Parsec
To: C Gosch <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I started using Data.Binary yesterday for a project and it's nice. I'm
not sure it's possible, but perhaps you could switch tools along the
way:

- Parse the ASCII bit with Parsec ;
- Give the remaining ByteString to Data.Binary for the binary part ;
- Switch back and forth as required.

Patrick


On Fri, Aug 6, 2010 at 5:03 AM, C Gosch <[email protected]> wrote:
>
>
> 2010/8/6 Dean Herington <[email protected]>
>>
>> At 11:42 PM +0200 8/5/10, C Gosch wrote:
>>>
>>> Hi,
>>> does anyone here know their way around in Parsec?
>>> I'm trying to parse a file which contains some binary parts too.
>>> I have been using Parsec 3.0.1 to parse the first ASCII part, but all the
>>> parsers are
>>> returning Char type tokens, so it would not work with the binary parts ..
>>> is there any way to do this?
>>> I am using Text.Parsec.ByteString.Lazy.
>>>
>>> Note that I'm new to Haskell and Parsec, and doing this within a small
>>> project that
>>> I basically do to get a grip on Haskell (and Parsec, because it appears
>>> to be really nifty and I want to learn more about it).
>>>
>>> Thanks for any hints,
>>> Christian
>>
>> Parsing binary parts of a file is not inherently a problem.  A parser can
>> return any type; different parsers in your application will likely return
>> different types.
>>
>> If you include some code we'll be able to help you more specifically.
>>
>> Dean
>
> You're right, I probably used the wrong words .. I meant that apparently the
> tokens Parsec uses are of type Char, and I would actually at some point
> like to continue parsing, but using different tokens. Sorry if I still got
> it wrong, I'm new :)  I can post some code later, as I don't have it here
> right now.
>
> Cheers,
> Christian
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 5
Date: Fri, 6 Aug 2010 13:38:56 +0100
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] mayBe stuck
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Aug 06, 2010 at 11:15:52AM +0100, Colin Paul Adams wrote:
> >>>>> "Brent" == Brent Yorgey <[email protected]> writes:
> 
>     >> Since Haskell allows programming with partial functions, you
>     >> always have this burden at present.
> 
>     Brent> Yes.  But that doesn't mean there isn't value in the
>     Brent> discipline of avoiding them.  The burden of making sure you
>     Brent> never use fromJust (or head, or ...) is much lighter than the
>     Brent> burden of proving that every such use is safe.
> 
> The burden is knowing whether or not a function is partial. It's that or
> ... that is the problem. This isn't flagged anywhere.

That's a very good point.  But it's the sort of thing you pick up
pretty quickly, I think.  Here are some off the top of my head, for
beginners reading this who might not already know:

  fromJust
  head
  tail
  init
  last
  (!!)

For a fuller list, take a look at the 'safe' package on Hackage [1],
which also provides many different safe alternatives.

-Brent

[1] http://hackage.haskell.org/package/safe


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

Message: 6
Date: Fri, 6 Aug 2010 07:52:06 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] mayBe stuck
To: Colin Paul Adams <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Doesn't the -Wall flag pick that up?
-deech

On 8/6/10, Colin Paul Adams <[email protected]> wrote:
>>>>>> "Brent" == Brent Yorgey <[email protected]> writes:
>
>     >> Since Haskell allows programming with partial functions, you
>     >> always have this burden at present.
>
>     Brent> Yes.  But that doesn't mean there isn't value in the
>     Brent> discipline of avoiding them.  The burden of making sure you
>     Brent> never use fromJust (or head, or ...) is much lighter than the
>     Brent> burden of proving that every such use is safe.
>
> The burden is knowing whether or not a function is partial. It's that or
> ... that is the problem. This isn't flagged anywhere.
> --
> Colin Adams
> Preston Lancashire
> ()  ascii ribbon campaign - against html e-mail
> /\  www.asciiribbon.org   - against proprietary attachments
>


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

Message: 7
Date: Fri, 6 Aug 2010 15:18:17 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] mayBe stuck
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

On Friday 06 August 2010 14:52:06, aditya siram wrote:
> Doesn't the -Wall flag pick that up?
> -deech

Not if there's an explicit error call for the undefined cases, e.g.

head :: [a] -> a
head (x:_) = x
head _ = error "Prelude.head: empty list"

compiles without warning.


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

Message: 8
Date: Fri, 06 Aug 2010 14:38:27 +0100
From: Colin Paul Adams <[email protected]>
Subject: Re: [Haskell-beginners] mayBe stuck
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

>>>>> "Daniel" == Daniel Fischer <[email protected]> writes:

    Daniel> On Friday 06 August 2010 14:52:06, aditya siram wrote:
    >> Doesn't the -Wall flag pick that up?  -deech

    Daniel> Not if there's an explicit error call for the undefined
    Daniel> cases, e.g.

    Daniel> head :: [a] -> a head (x:_) = x head _ = error
    Daniel> "Prelude.head: empty list"

    Daniel> compiles without warning.

Ah. So it's safer not to use defensive programming then.

Good. As an Eiffel programmer, that's what I'm used to. 
-- 
Colin Adams
Preston Lancashire
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments


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

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


End of Beginners Digest, Vol 26, Issue 14
*****************************************

Reply via email to