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.  Applicability of FP unchanging things to real    world changing
      things (Glurk)
   2. Re:  Applicability of FP unchanging things to     real    world
      changing things (Isaac Dupree)
   3. Re:  Applicability of FP unchanging things to     real world
      changing things (John Li)
   4.  Re: Re: How to create a monad instance (Maciej Piechotka)
   5.  Re: Applicability of FP unchanging things to real world
      changing things (Heinrich Apfelmus)
   6.  Re: Problem implementing a stack (Maciej Piechotka)
   7.  What about scratch pad for each library (Marc Weber)
   8. Re:  Re: Applicability of FP unchanging things    to real world
      changing things (Brent Yorgey)
   9. Re:  Re: Applicability of FP unchanging things to         real world
      changing things (Magnus Therning)


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

Message: 1
Date: Sun, 13 Dec 2009 02:11:28 +0000 (UTC)
From: Glurk <[email protected]>
Subject: [Haskell-beginners] 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 come from an OO background, but have recently started to see the light and 
think that functional programming has a lot to offer in real world, general 
software development - not just some small specific niche.

Having the OO background certainly has limited the way I view software 
development, and I guess in learning something new, I have a tendency to 
compare it with what I do know, looking for similarities and differences.

So, perhaps that's why I am having difficulty in seeing how some functional 
programming ideas apply.

What's got me a little puzzled at the moment is non mutable data.
On the one hand, I think it's great that I can mostly forget about errors 
caused by my variables having unexpected values, and that I can reason about 
my program much more easily...

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.

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 ?

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

Any thoughts appreciated ! :)

Thanks !




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

Message: 2
Date: Sat, 12 Dec 2009 21:36:37 -0500
From: Isaac Dupree <[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; format=flowed

Computers think discretely.  Even the OO book is probably not going to 
recognize the book's every move as it falls into someone's hands, 
wanders around the library, perhaps sneaks out of the library or is 
reshelved somewhere else, is checked out and goes across the globe, has 
the cover torn and then taped together into pretty-good shape...

I prefer FP because it acknowledges that computers fundamentally operate 
  like this. They model and approximate situations.  Sometimes 
pretending to be those situations is not the best way to approximate them.

...[tried to give example but I'm too dizzy at the moment from finals-week]


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

Message: 3
Date: Sun, 13 Dec 2009 00:04:29 -0500
From: John Li <[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=us-ascii

On Sun, Dec 13, 2009 at 02:11:28AM +0000, Glurk wrote:
> What's got me a little puzzled at the moment is non mutable data.
[...]
> 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 ?

Mutability perhaps more directly matches how we look at the world, in
which objects have states that change over time.

Purely functional programming doesn't allow mutating data in place, so
we have the "evolution" of an object through time. We get multiple
snapshots. I like to think of it as having 1 dimensional objects
instead of having 0 dimensional objects, in direct analogy to thinking
of objects in the world as being 4 dimensional: 3 spatial and 1 time
dimension.

Rich Hickey (the Clojure guy) gave a keynote talk about this kind of
thing: http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

Here's an interview about the same thing:
http://www.artima.com/articles/hickey_on_time.html

I've only watched the keynote, which I found really interesting. Maybe
these will better help you get a grasp on a "functional" way of
thinking?


Hope that helps,
John


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

Message: 4
Date: Sun, 13 Dec 2009 09:50:22 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Re: How to create a monad instance
To: [email protected]
Message-ID: <1260697822.2847.83.ca...@picard>
Content-Type: text/plain; charset="UTF-8"


> Thanks for your help.
> 
> I have try your code ghci and I think there is a mistake in the
> definition of (>>=) operator.
> 

Hmm. I belive I tested code before (at least type-check) sending but it
is possible.

> test1 m f = IOException $ runIOException m >>= runIOException . f
> :t test1
> test1 :: IOException a
>          -> (Exception a -> IOException a)
>          -> IOException a
> 
> I think the defintion is:
> test :: IOException a -> ( a -> IOException a) -> IOException a
> 

Ups. You're right. Quick'n'dirty version with do:
instance Monad IOException where
   return = IOException . return . return
   m >>= f = IOException $ do m' <- runIOException m
                              case m' of
                                Success _ v -> runIOException $ f v
                                Exception e -> return $ Exception e

> 1. why I am not using monoid? I try to bind C code and the function
> return only one warning. But if I change i little the structure of
> IOException, it may have sense to use a List.
> newtype IOException a = IOException {runIOException :: ([Warning] ->
> IO (Exception a))}
> 

Well. The problem with returning one - what should be combination of
warnings. It is of course problem-dependent.

Regards




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

Message: 5
Date: Sun, 13 Dec 2009 11:24:06 +0100
From: Heinrich Apfelmus <[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=UTF-8

Glurk wrote:
> So, perhaps that's why I am having difficulty in seeing how some functional 
> programming ideas apply.
> 
> What's got me a little puzzled at the moment is non mutable data.
> On the one hand, I think it's great that I can mostly forget about errors 
> caused by my variables having unexpected values, and that I can reason about 
> my program much more easily...
> 
> 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.
> 
> 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 ?

The point is that the real world doesn't happen inside the computer,
only a model of it. The functional paradigm is based on the idea that
immutable variables make for great models, even when state changes are
involved.

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

where the data type  Library  is a giant table of book IDs that keeps
track of which ones are lent out and which are not. Together with a
notion of "the current library", this is enough to model your example.


In the end, even the library example is somewhat special in that it's
too much about reality. Most programming problems, like sorting,
compressing, type inference, etc. etc. are more abstract in nature and
there is no a priori reason why mutable variables would be a good way of
describing and modeling them.

In fact, physics has been very successful at modeling the real world
without mutable variables for centuries.


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



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

Message: 6
Date: Sun, 13 Dec 2009 10:56:05 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Problem implementing a stack
To: [email protected]
Message-ID: <1260701765.2847.155.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

>     data Stack a = Top (Stack a) | Layers [a] deriving (Show)

Sorry I'm asking - but why Stack is either a 'Stack or List' - shouldn't
it be either plain list or list-lile structure:

data Stack a = EmptyStack | Stack a (Stack a)
(unless you're training 'data' I'd say newtype Stack a = Stack [a])

Otherwise Top (Top (Layers [])) is a stack (yet it does not fit into
rest of the functions) or even fix Top (i.e. Top (Top (Top (Top (...)
Unless it have some reason of course.

Regards




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

Message: 7
Date: Sun, 13 Dec 2009 14:47:10 +0100
From: Marc Weber <[email protected]>
Subject: [Haskell-beginners] What about scratch pad for each library
To: [email protected]
Message-ID: <1260709552-sup-6...@nixos>
Content-Type: text/plain; charset=UTF-8

You can read about the complete proposal here:
http://haskell.org/haskellwiki/Hackage_wiki_page_per_project_discussion

Benefits: If something doesn't work for you should contact the author/
maintainer or the mailinglist. But if you find a workaround you can put
it online immediately. You should remove it again after having
discussed the issue with maintainers. However they may be on holidays..

So if you've had minor trouble getting started with a package
and if you think that some small examples would have helped you in the
past you should register at the haskellwiki and write about your
experiences so that I get a understanding of whether this idea
improves your experience or makes it worse.

Of course not all wiki pages will be maintained. So it's very likely
that you find outdated information. But I personally think that one tip
is worth 10 outdated wiki pages or such (?)

Additionally are you interested in helping removing outdated content when you
find such ?

You'll find a link to the haskell-cafe mailinglist thread on the wiki
page as well.

Hopefully most wiki pages are empty or contain links to the project
pages only because there are no issues and you understand everything
immediately :)

Thank you for your feedback
Marc Weber


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

Message: 8
Date: Sun, 13 Dec 2009 13:21:10 -0500
From: Brent Yorgey <[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=us-ascii

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.  In this particular example, most of
the "new" Library will probably be shared with the "old" Library, and
just the part that is changed needs to be updated (in fact, note that
this is only possible because Library is immutable! =) So in terms of
actual execution we get the same efficiency that we would have by
mutating the Library, but we get the cognitive benefit of being able
to *think* of it as if we have produced an entirely new Library.

-Brent


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

Message: 9
Date: Mon, 14 Dec 2009 14:50:59 +0000
From: Magnus Therning <[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 6: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.  In this particular example, most of
> the "new" Library will probably be shared with the "old" Library, and
> just the part that is changed needs to be updated (in fact, note that
> this is only possible because Library is immutable! =) So in terms of
> actual execution we get the same efficiency that we would have by
> mutating the Library, but we get the cognitive benefit of being able
> to *think* of it as if we have produced an entirely new Library.

Doesn't this immutability make some things very easy to implement?
For instance, it ought to be trivial to backup state to disk (for
recovery or rollback), one just hands it to a thread asynchronously
and then continues handling requests.  Doing that sort of thing with
mutable state requires a bit more work, I'd say.

>From watching a presentation on Happstack, I believe this is how it
achieves some of its nice features.

/M

-- 
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus@therning.org          Jabber: magnus@therning.org
http://therning.org/magnus         identi.ca|twitter: magthe


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

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


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

Reply via email to