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:  Implementing a Local Propagation Network
      (Stephen Blackheath [to Haskell-Beginners])
   2.  How to model this in haskell,    get rid of my OO thinking?
      ([email protected])
   3.  Are monads pure? (Amy de Buitl?ir)
   4. Re:  How to model this in haskell,        get rid of      my OO thinking?
      (Isaac Dupree)
   5. Re:  Are monads pure? (aditya siram)
   6. Re:  Are monads pure? (Amy de Buitl?ir)
   7. Re:  Are monads pure? (Andres Loeh)
   8. Re:  Are monads pure? (Stephen Tetley)


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

Message: 1
Date: Tue, 18 May 2010 14:41:09 +1200
From: "Stephen Blackheath [to Haskell-Beginners]"
        <[email protected]>
Subject: Re: [Haskell-beginners] Implementing a Local Propagation
        Network
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Patrick,

If you want to implement it in a functional style, you have to use an
association map of some sort.  Haskell only has values, but not any
concept of a reference (unless you count things like IORef, but I am not
counting those).  Generally speaking this is needed whenever you are
dealing with a data structure that has cycles.  (Generally speaking
because it's possible to make data structures lazily refer to themselves.)

People usually use IntMap, but there's a new package EnumMap on Hackage
which is really powerful.  It's like IntMap only typesafe.  You will
need a counter in your data structure as a source of unique ids.  You
can also use value-supply (from Hackage), which is a great bit of code.

On the face of it, this seems cumbersome, but the way to do it is to
create a data structure and access it through accessor functions like
"add node", "delete node", "follow wire", etc.  This way you can
abstract those details away.  People have done various directed/undirect
graph packages and so on on Hackage - I can't recommend anything.

Stick with it - this approach does work.  I've done things like
conversion of 3D models into triangle strips using this method, with
very satisfying results.


Steve

On 18/05/10 12:59, Patrick LeBoutillier wrote:
> Hi all,
> 
> After learning some Haskell recently, I decided to revisit a book
> about functional programming techniques for Perl: Higher Order Perl. I
> didn't fully understand the book at the time but now my Haskell
> experience has proved to be very insightful.
> 
> Towards the end of the book the author implements a local propagation network.
> 
> Here is the Perl source code:
> http://hop.perl.plover.com/Examples/Chap9/Local-Propagation/
> The PDF of the specific chapter is here:
> http://hop.perl.plover.com/book/pdf/09DeclarativeProgramming.pdf
> 
> I would like to experiment with something similar in Haskell, but the
> way this network is designed is all about state and references:
> 
> - Wires have a values that can change over time;
> - Wires have references to nodes;
> - Nodes have references to wires;
> 
> I'm a bit stuck as to how to approach the "object has a list
> references to other objects" situation from Haskell. I tried this:
> 
> type Name = String
> data Node = Node Name [Wire]
> data Wire = Wire Name Node Double [Node]
> 
> But that doesn't seem like it would work since when I change a Wire I
> must find all "copies" of it (in the Node objects) and update them
> also. Perhaps I should just refer to Wires/Nodes by name and use an
> association list to lookup them up, but that seems cumbersome.
> 
> Anybody have any suggestions?
> 
> 
> Thanks a lot,
> 
> Patrick
> 
> 
> 


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

Message: 2
Date: Mon, 17 May 2010 23:21:38 -0400
From: [email protected]
Subject: [Haskell-beginners] How to model this in haskell,      get rid of
        my OO thinking?
To: Biginners Haskell Mailinglist <[email protected]>
Message-ID: <20100517232138.6a5da...@samzwo>
Content-Type: text/plain; charset=US-ASCII

Hi,

I learning haskell and I am trying to understand how model certain
things in it.
As I have been doing a lot of C++ programming so far. Let's imagine we
want to write a game. In the game there are spaceships and rocks (image
something like astroids :) ). Now both spaceships and rocks have a
position and can move. Spaceships can shoot, while rocks can explode.
In C++, I would do (simplified):

class ScreenObject
{
   float x,y;
   void move(dx,dy){x+=dx;y+=dy;}
};

class Spaceship : public ScreenObject
{
    void shoot(){...}
};

class Rocket : public ScreenObject
{
    void explode(){...}
};

But what would I do in haskell? Ok, I can define a typeclass
"ScreenObjectType" that has a move function (taking an object, retuning
an moved object).
But I do not want to implement "move" for both Spaceship and Rocket.
Can I somehow give a default implementation for move that works on any
datatype having an "x" and "y" element? Or what would I do?
Can I somehow define a "base datatype" holding a x and y member form
which Spaceship and Rocket derive?
I feel like I am thinking to much OOP here.
But the point is, I guess, that I want to avoid code duplication!

So I guess, it comes down to the questions: How would you model the
scenario described above in haskell?

Thanks!
Nathan


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

Message: 3
Date: Tue, 18 May 2010 04:28:45 +0100
From: Amy de Buitl?ir <[email protected]>
Subject: [Haskell-beginners] Are monads pure?
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

When I first started learning Haskell, I was under the impression that
monads provided a way to encapsulate some imperative code so it didn't
"contaminate" your functional code. But I think that's a misconception that
I got because the first monad I dealt with was IO. Lately I've been reading
more about monads, and (aside from IO), they are purely functional, aren't
they? If they have state, they deal with it in a purely functional way.
Behind the scenes, they thread the state through your computations so that
you don't you don't have to bother with it. Is that correct? TIA.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100517/0e7d558e/attachment-0001.html

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

Message: 4
Date: Mon, 17 May 2010 23:31:56 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] How to model this in haskell,  get rid
        of      my OO thinking?
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 05/17/10 23:21, [email protected] wrote:
> So I guess, it comes down to the questions: How would you model the
> scenario described above in haskell?

how about,

data Position = Position { positionX :: Float, positionY :: Float }

move :: Position -> (Float, Float) -> Position

data Spaceship = Spaceship Position ...other data members...
data Rocket = Rocket Position ...other data members...
(or perhaps not separate data-types but rather
data SpaceThing =
    Spaceship { spacePosition :: Position, ...other data members...}
  | Rocket { spacePosition :: Position, ...other data members...}
  ...

-- this latter has advantages and disadvantages regarding code reuse and 
code reliability.  Experiment!


-Isaac


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

Message: 5
Date: Mon, 17 May 2010 23:43:48 -0400
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Are monads pure?
To: Amy de Buitl?ir <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Threading state is something that the State monad does and it is
purely functional - showing that a monad *can* be pure but don't have
to be. Other monads, like IO as you have stated, have side effects.

At the end of the day I found that the monad is very, very general and
it's best way to think about it as a piece of data wrapped in some
type. With a monad you can
1. takes some data and wraps it up in a type (return) :
a -> m a
2. apply a function to the data within the type (>>=).
m a -> (a -> m b) -> m b

hth,
-deech


On 5/17/10, Amy de Buitléir <[email protected]> wrote:
> When I first started learning Haskell, I was under the impression that
> monads provided a way to encapsulate some imperative code so it didn't
> "contaminate" your functional code. But I think that's a misconception that
> I got because the first monad I dealt with was IO. Lately I've been reading
> more about monads, and (aside from IO), they are purely functional, aren't
> they? If they have state, they deal with it in a purely functional way.
> Behind the scenes, they thread the state through your computations so that
> you don't you don't have to bother with it. Is that correct? TIA.
>


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

Message: 6
Date: Tue, 18 May 2010 05:37:20 +0100
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] Are monads pure?
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Threading state is something that the State monad does and it is

> purely functional - showing that a monad *can* be pure but don't have
> to be. Other monads, like IO as you have stated, have side effects.
>

Thank you, that helps.


> At the end of the day I found that the monad is very, very general and
> it's best way to think about it as a piece of data wrapped in some
> type. With a monad you can
> 1. takes some data and wraps it up in a type (return) :
> a -> m a
> 2. apply a function to the data within the type (>>=).
> m a -> (a -> m b) -> m b
>

But if that's all you need to do, you could just use an Applicative Functor,
right? The picture I have at the moment is:

Functors can apply a function to a value inside a container.

Applicative functors provide pure expressions and sequencing, but no
binding. All applicative functors are also functors.

Arrows provide a way to set up more complicated pipelines with "tee"
junctions, etc. All arrows are also applicative functors (?)

Monads add binding. All monads are also arrows.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100518/310ed213/attachment-0001.html

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

Message: 7
Date: Tue, 18 May 2010 06:51:03 +0200
From: Andres Loeh <[email protected]>
Subject: Re: [Haskell-beginners] Are monads pure?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

> When I first started learning Haskell, I was under the impression that
> monads provided a way to encapsulate some imperative code so it didn't
> "contaminate" your functional code. But I think that's a misconception that
> I got because the first monad I dealt with was IO. Lately I've been reading
> more about monads, and (aside from IO), they are purely functional, aren't
> they? If they have state, they deal with it in a purely functional way.
> Behind the scenes, they thread the state through your computations so that
> you don't you don't have to bother with it. Is that correct? TIA.

You are right. IO is an exception, because it is a built-in type that
cannot be implemented in Haskell itself. Monads that are implemented in
Haskell without mentioning IO or interfacing via the FFI are generally
pure. Examples are state, errors, reader/writer, continuations,
lists, ... Nevertheless, people often refer to features such as state
and exceptions as "effects", and hence sometimes call code that makes
use of them "effectful" and code that does not "pure".

Cheers,
  Andres

-- 

Andres Loeh, Universiteit Utrecht

mailto:[email protected]     mailto:[email protected]
http://www.andres-loeh.de


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

Message: 8
Date: Tue, 18 May 2010 09:24:17 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Are monads pure?
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 18 May 2010 05:37, Amy de Buitléir <[email protected]> wrote:

[SNIP]
>> [aditya siram]
>> With a monad you can
>> 1. takes some data and wraps it up in a type (return) :
>> a -> m a
>> 2. apply a function to the data within the type (>>=).
>> m a -> (a -> m b) -> m b
>
>
> But if that's all you need to do, you could just use an Applicative Functor,
> right? The picture I have at the moment is:
>
> Functors can apply a function to a value inside a container.
>
> Applicative functors provide pure expressions and sequencing, but no
> binding. All applicative functors are also functors.

Hello all


With bind, monads are a more powerful than applicative functors as
they give you the ability to perform context sensitive operations
whereas you could consider Applicative functors to be "context-free".

This means you can write more control operations for monads than
applicative functors, for instance you can have a State applicative
functor just as you can have a State monad. However with just
applicative you can't write the useful bracket function, which first
takes a snapshot of the state, performs the stateful function, then
restores the original state:


> newtype State st a = State { getStateF :: st -> (a,st) }


Functor instance:

> instance Functor (State st) where
>   fmap f (State sf) = State $ \st ->
>                         let (a,st1) = sf st in (f a,st1)

Applicative instance

> instance Applicative (State st) where
>   pure a = State $ \st -> (a,st)
>   (State sfab) <*> (State sf) = State $ \st ->
>                                 let (fab,st1) = sfab st
>                                     (a,  st2) = sf st1
>                                 in (fab a,st2)

Monad instance

> instance Monad (State st) where
>   return a = State $ \st -> (a,st)
>   (State sf) >>= smf = State $ \st ->
>                           let (a1,st1) = sf st1
>                               sfun     = getStateF (smf a1)
>                               (a2,st2) = sfun st2
>                           in (a2,st2)


> get :: State a a
> get = State $ \st -> (st,st)

> put :: a -> State a a
> put a = State $ \ _st -> (a,a)


> mbracket :: State st a -> State st a
> mbracket ma = do { st  <- get
>                  ; ans <- ma
>                  ; put st
>                  ; return ans
>                  }

Or without the do notation:

> mbracket' :: State st a -> State st a
> mbracket' ma = get    >>= \st  ->
>                ma     >>= \ans ->
>                put st >>= \_   -> return ans

No bracket with just the applicative machinery...


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

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


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

Reply via email to