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: How to "insert" a cross reference into a tree structure
(Stephen Tetley)
2. Re: How to "insert" a cross reference into a tree structure
(Tim Baumgartner)
3. Re: How to "insert" a cross reference into a tree structure
(Stephen Tetley)
4. Re: RandT (Brent Yorgey)
5. Re: RandT (Amy de Buitl?ir)
6. Re: How to "insert" a cross reference into a tree structure
(Tim Baumgartner)
----------------------------------------------------------------------
Message: 1
Date: Sun, 19 Dec 2010 11:28:59 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] How to "insert" a cross reference
into a tree structure
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 19 December 2010 09:54, Edward Z. Yang <[email protected]> wrote:
>
> Related to editing values embedded deep in structures, but not related
> to cross-references, you might find this post interesting:
>
> ? ?http://conal.net/blog/posts/semantic-editor-combinators/
Zippers are another view on editing nested structures, try to find
Gerard Huet's original Zipper functional pearl if you can (I think it
is probably accessible via Citeseer), although the code is in OCaml
its a better eplanation than the haskellwiki page.
For the original problem, it may be more appealing to have
cross-references outside the syntax tree and put them in a "relational
structure" like a finite map (e.g. Data.Map or a variant that is
better for one-to-many relations). As information then gets separated
into two places, there is perhaps more book-keeping to be done than
with tying-the-knot, however the book-keeping itself will be simpler.
------------------------------
Message: 2
Date: Sun, 19 Dec 2010 15:06:17 +0100
From: Tim Baumgartner <[email protected]>
Subject: Re: [Haskell-beginners] How to "insert" a cross reference
into a tree structure
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thanks for the answers. It seems this is a point where I can't easily
translate an OO program to Haskell. I was afraid this might be the
case. In my job, I work with heavy use of EMF (Eclipse Modeling
Framework, for Java) which allows editing of models like the
MusicCollection with generic support for features like Displaying in
lists/trees, Moving, Deleting, Creating new child elements, Undo/Redo,
Drag & Drop, lazyness via proxies. I'd really like to have (or even
implement) something similar in Haskell, but this seems to be a very
difficult task.
Regards
Tim
------------------------------
Message: 3
Date: Sun, 19 Dec 2010 15:40:33 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] How to "insert" a cross reference
into a tree structure
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Tim
Trees - even complex ones like abstract syntax trees for programming
languages - are straight-forward to manipulate. Its easy to manipulate
simple trees, it isn't too hard to manipulate complex trees - though
you might want a "boilerplate removal library" like Uniplate,
Scrap-Your-Boilerplate, or even an attribute grammar (UUAG).
The difficulty comes when you add links / references between parts of
the tree as this turns the tree into a graph. Graph manipulation
without pointers is convoluted. The "moral" is to avoid turning trees
into graphs if you possibly can.
Best wishes
Stephen
------------------------------
Message: 4
Date: Sun, 19 Dec 2010 12:29:46 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] RandT
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
On Sat, Dec 18, 2010 at 04:11:02AM +0000, Amy de Buitl?ir wrote:
>
> Q2: The "Animal" type includes a "brain" component. I implemented that
> as a simple list, but perhaps it would be better to use a monad
> here?
I am not 100% sure I understand this question, but I'm pretty sure the
answer is "no", the way you have defined the Animal type looks fine to
me.
> Q1: My implementation of "simulateBrain" seems clumsy. Is there a
> better way to do this?
Well, it's fine for what it is, but I would generalize it a bit, to
make it more widely applicable.
The key challenge is that stimulateBrain is supposed to be a
computation using a whole Animal as state, but it is to be defined in
terms of a computation operating simply on the Brain as state.
Unfortunately that means the types will not match, so we need some
sort of adapter, as you figured out. But this problem is not specific
to Animal and Brain: what if later we wanted to use a computation
which has access only to the Pancreas, etc.?
We can define a generic adapter as follows:
withComponent :: (RandomGen g) =>
-- | "Extractor" function to allow us to extract the piece of state s'
-- from the larger state s
(s -> s') ->
-- | "Setter" function to update the larger state with a new s'
(s' -> s -> s) ->
-- | The state computation to run over the smaller state...
RandT g (State s') a ->
-- | ...which we can now use as a state computation over the larger state
RandT g (State s) a
withComponent extract set m = do
s <- get
g <- getSplit
let (a, s') = runState (evalRandT m g) (extract s)
put $ set s' s
return a
Now stimulateBrain is just
stimulateBrain :: (RandomGen g) => Int -> [Double] -> RandT g (State Animal) ()
stimulateBrain n xs = withComponent brain (\b a -> a { brain = b }) (stimulate
n xs)
and you can also easily write stimulatePancreas or whatever.
We can generalize this further in two ways, which I will leave for you
to explore if you are interested:
(1) use something like data-accessor for automatically deriving the
'extractor' and 'setter' functions.
(2) generalizing withComponent so that it works with other monad stacks.
-Brent
>
> Thank you,
> Amy
>
> ----- SAMPLE CODE -----
>
> {-# LANGUAGE PackageImports, RankNTypes, FlexibleContexts #-}
>
> import "mtl" Control.Monad.State
> import Control.Monad.Random
>
> type Neuron = Int -- The real type is more complex
>
> -- An "Alife" animal
> data Animal = Animal
> {
> brain :: [Neuron]
> -- There are other fields too, of course
> } deriving (Show, Read)
>
> -- | Stimulates an animal's brain, and allows it to react.
> stimulateBrain :: (RandomGen g)
> -- | The number of cycles
> => Int
> -- | The signals to apply to the sensor neurons
> -> [Double]
> -- | The animal
> -> RandT g (State Animal) ()
> stimulateBrain n xs = do
> c <- get
> g <- getSplit
> let b' = execState (evalRandT (stimulate n xs) g) (brain c)
> put $ c{brain=b'}
>
>
> -- | Feeds some input signals into a brain, and allow it to react.
> stimulate :: (RandomGen g)
> -- | The number of cycles
> => Int
> -- | The signals to apply to the sensor neurons
> -> [Double]
> -- | The neuron states
> -> RandT g (State [Neuron]) ()
> stimulate k xs = return () -- The real implementation is more complex
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Sun, 19 Dec 2010 17:51:13 +0000
From: Amy de Buitl?ir <[email protected]>
Subject: Re: [Haskell-beginners] RandT
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thank you so much, deech and Brent. You've both given me good ideas,
not just for this particular component, but for other parts of my code
too.
------------------------------
Message: 6
Date: Sun, 19 Dec 2010 18:56:35 +0100
From: Tim Baumgartner <[email protected]>
Subject: Re: [Haskell-beginners] How to "insert" a cross reference
into a tree structure
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thanks for your help. I read the introduction to Uniplate. It looks
very interesting, just like Template Haskell does.
I think I have (again) to forget how I used trees/graphs in Java and
try to learn more about the Haskell way(s), which up to now always
were very charming.
Regards
Tim
2010/12/19 Stephen Tetley <[email protected]>:
> Hi Tim
>
> Trees - even complex ones like abstract syntax trees for programming
> languages - are straight-forward to manipulate. Its easy to manipulate
> simple trees, it isn't too hard to manipulate complex trees - though
> you might want a "boilerplate removal library" like Uniplate,
> Scrap-Your-Boilerplate, or even an attribute grammar (UUAG).
>
> The difficulty comes when you add links / references between parts of
> the tree as this turns the tree into a graph. Graph manipulation
> without pointers is convoluted. The "moral" is to avoid turning trees
> into graphs if you possibly can.
>
> Best wishes
>
> Stephen
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 30, Issue 38
*****************************************