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:  Re: Applicability of FP unchanging things to         real world
      changing things (Gregg Reynolds)
   2. Re:  Applicability of FP unchanging things to     real    world
      changing things (Tommy M. McGuire)
   3. Re:  writing many1Till combinator for Parsec (Vlad Skvortsov)
   4. Re:  writing many1Till combinator for Parsec (Daniel Fischer)
   5.  Re: Applicability of FP unchanging things        to      real    world
      changing things (Glurk)
   6. Re:  Re: Applicability of FP unchanging things to         real world
      changing things (Stephen Tetley)
   7.  Large tables and GHC. (Sherard D.)


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

Message: 1
Date: Mon, 14 Dec 2009 10:54:53 -0600
From: Gregg Reynolds <[email protected]>
Subject: Re: [Haskell-beginners] Re: Applicability of FP unchanging
        things to       real world changing things
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

On Sun, Dec 13, 2009 at 12:21 PM, Brent Yorgey <[email protected]>wrote:

> On Sun, Dec 13, 2009 at 11:24:06AM +0100, Heinrich Apfelmus wrote:
> >
> > In other words, the book in your example is not a real book, it's just a
> > few bytes in the computer that model it for the purpose of tracking
> > whether it's lent out or not. For instance, these few bytes can be a
> > unique ID and a possible  lend  function is
> >
> >     lend :: BookID -> Library -> Library
>
> I should also point out that people coming from an OO or imperative
> background often look at a function like this and think it looks
> incredibly inefficient --- why throw away the entire *library* and
> build a new one *from scratch* when all we want to do is modify one
> little part of it?  This seems a huge price to pay for immutability!
>
> But this line of thinking confuses abstract semantics with concrete
> implementation (something that imperative programming unfortunately
> tends to encourage, in my experience).  The runtime is free to
> implement operations like "lend" any way it likes, as long as the
> abstract semantics are preserved.
>
+1

One of the turning points in learning the FP way is the realization that the
conception of "function" as a "machine" or "transformation process" is
misleading or even harmful.  For most programmers coming from an imperative
language background that's going to mean returning to a more mathematically
pure notion of function, which means discarding the idea of mutating parts
of an object in favor of establishing relations among different "objects"
and letting the compiler worry about the gory details.  Youschkevitch's
fascinating history of the
concept<http://www.springerlink.com/content/p3x21230u404p13h/>might be
useful.

-gregg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091214/f0253117/attachment-0001.html

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

Message: 2
Date: Mon, 14 Dec 2009 14:33:54 -0600
From: "Tommy M. McGuire" <[email protected]>
Subject: Re: [Haskell-beginners] Applicability of FP unchanging things
        to      real    world changing things
To: Glurk <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Glurk wrote:
> However, on the other hand, I wonder how it ties in with real world systems 
> that do in fact do deal with objects that change state.

Do they change state? Or do you see a new object with a different state than
the previous object? Can you step into the same river twice?

> For example, if I was building a library system - I might have book objects, 
> and if a book gets borrowed, there remains the same book object, but it now 
> just has a different state - from "on shelf" say, to "borrowed".
> 
> Now, in Haskell, I might have a function to lend a book :-
> 
> lend book1
> 
> and this is going to return a new book, with the appropriate status change..
> ..but what of the old book ? I don't want 2 book objects floating around..
> ...to my way of thinking, it's really not a new book, it really is a change 
> of 
> state.
> 
> How do people see this kind of situation ?
> Is it just the way of looking at it... ?
> Do I just accept that I toss out my old library object, and have a new one, 
> which happens to be the same as the old one, except for the "new" book which 
> is now borrowed ?
> 
> Do I really need to get over thinking of objects in the first place, even 
> though it's what I see exist in the real world ?

As Master Apfelmus has written, what you see (or what you think you see, if
that is different) is almost completely irrelevant to what is going on in your
program in the computer. If you lend a book, the book doesn't change. The
library doesn't change. What changes is a model of the library, which now
indicates that a book is lent to someone, and the real question is how best to
build that model of the library.

Most object oriented languages are inherently procedural; this is equivalent
to writing all of your code in the IO monad. Haskell gives you more options
than that, including writing pure functional code with the type

lend :: Book -> Library -> Library

which has all sorts of neat properties that help you build the model without
falling into some of the potholes that go along with procedural programming.

> Is there where we part company with non mutability and venture into the 
> monads 
> where we do have side effects ?

At some point, you will ask, "What about these two Libraries floating around?"
and realize that what you really need is a monad and ideally one specific to
your Library (rather than the general IO monad). Then you will be enlightened.


-- 
Tommy "Gotta cut down on the cold meds" McGuire
[email protected]


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

Message: 3
Date: Mon, 14 Dec 2009 14:35:13 -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

Daniel Fischer wrote:
> You want a nonempty sequence of 'p's which aren't 'end's, followed by an 
> 'end'.
> So you could do for example
> a)
> many1Till p end = do
>     ps <- manyTill p end
>     guard (not $ null ps)
>     return ps
>
> , i.e. check whether the result of manyTill is a nonempty list after the 
> fact, or check 
> whether manyTill p end will return an empty list before manyTill is run, like
> b)
> many1Till p end = do
>     notFollowedBy end
>     manyTill p end
>
> (i.e. many1Till p end = notFollowedBy end >> manyTill p end).
>   

Thanks Daniel! I missed out on 'guard' and somewhy was under impression 
that 'notFollowedBy' can only deal with Chars.

>> 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;
>> 2) the error output should say that only 'p' parser is expected, while
>> it says (technically correct) that either 'p' or 'end' is expected:
>>
>> 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
>     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: 4
Date: Tue, 15 Dec 2009 00:57:15 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] writing many1Till combinator for
        Parsec
To: Vlad Skvortsov <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

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!



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

Message: 5
Date: Tue, 15 Dec 2009 07:53:31 +0000 (UTC)
From: Glurk <[email protected]>
Subject: [Haskell-beginners] Re: Applicability of FP unchanging things
        to      real    world changing things
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I agree with the points some people have raised - as I said in the original 
post, I certainly like some of the things that FP gives me.

> Do they change state? Or do you see a new object with a different state than
> the previous object? Can you step into the same river twice?

I think a lot of people, looking at the real world, would answer, yes, it 
clearly IS the same book, now in a different state. And yes, I CAN step into 
the same river twice. This is how people see and think about the real world.

So, while I understand and like the benefits that thinking of things as 
mathematical concepts gives me, it seems to me that it does take your program 
further away from the real word, and thus can make it less understandable 
because of this.

There is a certain adavantage in having a close mapping from the real world to 
your program - if your program is meant to be dealing with real world things.

> As Master Apfelmus has written, what you see (or what you think you see, if
> that is different) is almost completely irrelevant to what is going on in 
your
> program in the computer. If you lend a book, the book doesn't change. The
> library doesn't change. What changes is a model of the library, which now
> indicates that a book is lent to someone, and the real question is how best 
to
> build that model of the library.

I agree with you, that yes, it's our model that we're really working with, not 
the real world, and that the question is how best to build the model.

However, in answering that question, as I said, I think that there are some 
advantages in there being a close relationship between the real world, and our 
model -  in terms of understandability, which, personally, I believe is one of 
the most important things in any system.

So, I'm not sure I agree that what we see in the real world is "almost 
completely irrelevant". If the mapping from real world to program is hard to 
understand, then it can make it harder to maintain. To make a farfetched 
example, if we model our book as an apple, and each time it is lent a bite 
gets taken out of it...... well..that's just completely ridiculous, right ?
It's ridiculous because what we see in the real world IS important to how we 
model. It certainly doesn't mean that we have to model things exactly as we 
see them - obviously, we drop things we think are irrelevant etc ...but, I 
think in generaly, it CAN make visualization and understanding easier, if our 
model has a certain similarity to the thing it is modelling !

So, I'm just thinking that in the real world, we typically do think of 
changing objects, so a model which also has this property, can help in 
understanding.

Of course, if we train ourselves to think differently...then a model 
containing immutable objects might have more benefits than drawbacks - and I 
think that's what most FP supporters are claiming.

> At some point, you will ask, "What about these two Libraries floating 
around?"
> and realize that what you really need is a monad and ideally one specific to
> your Library (rather than the general IO monad). Then you will be 
enlightened.

I guess that brings up another point - that eventually we are drawn into 
monads, which to me seems a bit unfortunate, because it seems that it leads us 
back to imperative programming with mutable state...

As someone else suggested, perhaps FRP is an answer here..
..I must admit I don't understand much about that, I'll look into it a bit 
more ! 

Glurk





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

Message: 6
Date: Tue, 15 Dec 2009 11:24:37 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Re: Applicability of FP unchanging
        things to       real world changing things
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

2009/12/15 Glurk <[email protected]>:

> So, I'm just thinking that in the real world, we typically do think of
> changing objects, so a model which also has this property, can help in
> understanding.


Ah but Glurk, a model that has this property can often greatly
_hinder_ understanding:
Its well know in OO circles that state change is problematic for
'modelling' - vis the damage done to a circle or square object by the
otherwise standard operation of a non-uniform scale, where the circle
is obliged to become an ellipse, the square an oblong. [1]

Typestate object oriented languages exist where one can migrate e.g a
closed file object into an open file and back again, but are somewhat
off the beaten path, seemingly doomed to be curios every once in a
while at POPL, OOPSLA...

Best wishes

Stephen


[1] http://en.wikipedia.org/wiki/Circle-ellipse_problem
[2] http://www.cs.cmu.edu/~aldrich/papers/onward2009-state.pdf


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

Message: 7
Date: Wed, 16 Dec 2009 23:05:10 -0500
From: "Sherard D." <[email protected]>
Subject: [Haskell-beginners] Large tables and GHC.
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

I have a couple of rather large byte tables that I'd like to use. I 
decided to use them as arrays because I figured such large tables 
wouldn't work well as lists.

table1 = listArray (0, 25599) [...]
table2 = listArray (0, 25599) [...]

This is how my code looks, minus the large tables. However, the problem 
is that the large tables are loaded as a list first, and then converted 
to an array. This is indeed a very big problem, as trying to compile 
with GHC takes an unacceptably long time.

Is there any way I can get around this?


- Sherard


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

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


End of Beginners Digest, Vol 18, Issue 12
*****************************************

Reply via email to