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. Trying to find an alternative StateMonad (edgar klerks)
2. Re: Trying to find an alternative StateMonad (edgar klerks)
3. Re: Trying to find an alternative StateMonad
(Patrick LeBoutillier)
4. Re: Trying to find an alternative StateMonad (HASHIMOTO, Yusaku)
5. Re: Trying to find an alternative StateMonad (Daniel Fischer)
6. Re: Trying to find an alternative StateMonad (Ozgur Akgun)
7. Re: Trying to find an alternative StateMonad (Brent Yorgey)
----------------------------------------------------------------------
Message: 1
Date: Thu, 15 Apr 2010 00:15:09 +0200
From: edgar klerks <[email protected]>
Subject: [Haskell-beginners] Trying to find an alternative StateMonad
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi All,
I wanted to write the statemonad in a different way, because I want to
track mutations to the state. And also to be able to undo an done or
redo an undone mutation. Therefore I wrote a small dsl with an
evaluator, which finds out the current state.
The problem is, I don't know how to create a get and put function for
my monad. I have a function which evaluate the state and gives back
the current State.
Can someone help me a bit along? I have the feeling I do something
wrong, but I am not sure what.
With kind regards,
Edgar
{-# LANGUAGE GADTs,DeriveFunctor #-}
import Control.Monad
import Data.Maybe
data StateCmd a where
Put :: a -> StateCmd a
Undo :: StateCmd a
Redo :: StateCmd a
deriving (Show, Functor)
newtype State s a = State { unState :: ([StateCmd s],a) }
deriving Show
joinState :: State s a -> State s b -> State s b
joinState (State (xs, a)) (State (ys, b)) = State (xs ++ ys, b)
instance Monad (State s) where
return a = State $ ([], a)
(>>=) = bindState
-- m a -> ( a -> m b) -> m b
bindState st@(State (_,a)) f = st `joinState` st'
where st' = f a
unPut :: StateCmd a -> Maybe a
unPut (Put a) = Just a
unPut _ = Nothing
test = State $ ([Put 4, Undo, Redo, Undo, Put 5, Undo, Redo, Put 6,
Undo, Redo, Undo], ())
getCurrent = fromJust.unPut.head.snd.(foldr current'
([],[])).reverse.fst.unState
where current' x (ul,cl) = case x of
Put n -> (ul, Put n : cl)
Undo -> (head cl : ul, tail cl)
Redo -> (tail ul, head ul : cl)
------------------------------
Message: 2
Date: Thu, 15 Apr 2010 00:33:31 +0200
From: edgar klerks <[email protected]>
Subject: [Haskell-beginners] Re: Trying to find an alternative
StateMonad
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I think I already know, the monad is not a state monad, but a writer
monad. I have to implement the dsl in a state monad.
Sorry I was somewhat too fast sending to the list. But comments are of
course still welcome.
On 4/15/10, edgar klerks <[email protected]> wrote:
> Hi All,
>
> I wanted to write the statemonad in a different way, because I want to
> track mutations to the state. And also to be able to undo an done or
> redo an undone mutation. Therefore I wrote a small dsl with an
> evaluator, which finds out the current state.
>
> The problem is, I don't know how to create a get and put function for
> my monad. I have a function which evaluate the state and gives back
> the current State.
>
> Can someone help me a bit along? I have the feeling I do something
> wrong, but I am not sure what.
>
> With kind regards,
>
> Edgar
>
> {-# LANGUAGE GADTs,DeriveFunctor #-}
> import Control.Monad
> import Data.Maybe
>
> data StateCmd a where
> Put :: a -> StateCmd a
> Undo :: StateCmd a
> Redo :: StateCmd a
> deriving (Show, Functor)
>
> newtype State s a = State { unState :: ([StateCmd s],a) }
> deriving Show
>
> joinState :: State s a -> State s b -> State s b
> joinState (State (xs, a)) (State (ys, b)) = State (xs ++ ys, b)
>
> instance Monad (State s) where
> return a = State $ ([], a)
> (>>=) = bindState
>
> -- m a -> ( a -> m b) -> m b
> bindState st@(State (_,a)) f = st `joinState` st'
> where st' = f a
>
> unPut :: StateCmd a -> Maybe a
> unPut (Put a) = Just a
> unPut _ = Nothing
>
> test = State $ ([Put 4, Undo, Redo, Undo, Put 5, Undo, Redo, Put 6,
> Undo, Redo, Undo], ())
>
> getCurrent = fromJust.unPut.head.snd.(foldr current'
> ([],[])).reverse.fst.unState
> where current' x (ul,cl) = case x of
> Put n -> (ul, Put n : cl)
> Undo -> (head cl : ul, tail cl)
> Redo -> (tail ul, head ul : cl)
>
--
Flatliner ICT Service,
Email: [email protected],
Tel: +31727851429
Fax: +31848363080
Skype: edgar.klerks
Website: flatlinerict.nl
Adres: Koelmalaan 258,
1813JD, Alkmaar
Nederland
------------------------------
Message: 3
Date: Wed, 14 Apr 2010 18:49:26 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Trying to find an alternative
StateMonad
To: edgar klerks <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
Sorry I can't be of much help here, but I have a question though:
>
> data StateCmd a where
> Put :: a -> StateCmd a
> Undo :: StateCmd a
> Redo :: StateCmd a
> deriving (Show, Functor)
I'm not familiar with the above syntax (data ... where). What does it
do exactly?
Patrick
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 4
Date: Thu, 15 Apr 2010 08:10:51 +0900
From: "HASHIMOTO, Yusaku" <[email protected]>
Subject: Re: [Haskell-beginners] Trying to find an alternative
StateMonad
To: Patrick LeBoutillier <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
It is GADTs (Generalized Algebric Data Types) Notation.
>> data StateCmd a where
>> Â Â Â Â Put :: a -> StateCmd a
>> Â Â Â Â Undo :: StateCmd a
>> Â Â Â Â Redo :: StateCmd a
is same as
> data StateCmd a = Put a | Undo | Redo
-nwn
On 15 April 2010 07:49, Patrick LeBoutillier
<[email protected]> wrote:
> Hi,
>
> Sorry I can't be of much help here, but I have a question though:
>
>>
>> data StateCmd a where
>> Â Â Â Â Put :: a -> StateCmd a
>> Â Â Â Â Undo :: StateCmd a
>> Â Â Â Â Redo :: StateCmd a
>> Â Â Â Â Â Â Â Â deriving (Show, Functor)
>
> I'm not familiar with the above syntax (data ... where). What does it
> do exactly?
>
> Patrick
>
>
>
> --
> =====================
> Patrick LeBoutillier
> Rosemère, Québec, Canada
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Thu, 15 Apr 2010 01:15:51 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Trying to find an alternative
StateMonad
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Donnerstag 15 April 2010 00:49:26 schrieb Patrick LeBoutillier:
> Hi,
>
> Sorry I can't be of much help here, but I have a question though:
> > data StateCmd a where
> > Put :: a -> StateCmd a
> > Undo :: StateCmd a
> > Redo :: StateCmd a
> > deriving (Show, Functor)
>
> I'm not familiar with the above syntax (data ... where).
It's GADT syntax, read about it in the user's guide.
> What does it do exactly?
That is exactly equivalent to
data StateCmd a
= Put a
| Undo
| Redo
deriving (...)
The coolness of GADTs is that you can have different constructors in your
datatype for different type parameters:
data Expr p where
Plain :: a -> Expr a
Add :: Num a => Expr a -> Expr a -> Expr a
If :: Expr Bool -> Expr a -> Expr a -> Expr a
And :: Expr Bool -> Expr Bool -> Expr Bool
...
And you can pattern match on these constructors for different types in one
function:
eval :: Expr a -> a
eval (Plain x) = x
eval (Add e1 e2) = eval e1 + eval e2
eval (If eb t f) = if eval eb then eval t else eval f
eval (And e1 e2) = eval e1 && eval e2
...
>
> Patrick
------------------------------
Message: 6
Date: Thu, 15 Apr 2010 00:22:24 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Trying to find an alternative
StateMonad
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
On 15 April 2010 00:15, Daniel Fischer <[email protected]> wrote:
> ...
> And you can pattern match on these constructors for different types in one
> function:
>
> eval :: Expr a -> a
> eval (Plain x) = x
> eval (Add e1 e2) = eval e1 + eval e2
> eval (If eb t f) = if eval eb then eval t else eval f
> eval (And e1 e2) = eval e1 && eval e2
> ...
>
but sadly you cannot have template haskell on them.
sorry to distract the op's question, but i am in trouble with trying to use
them both.
see this* sad and lonely thread, if you want to know what i'm talking about.
* http://www.haskell.org/pipermail/haskell-cafe/2010-April/076267.html
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100414/feb46e53/attachment-0001.html
------------------------------
Message: 7
Date: Wed, 14 Apr 2010 20:09:03 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Trying to find an alternative
StateMonad
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Instead of keeping a list of Put/Undo/Redo commands, it may be easier
to keep a pair of stacks of states, representing past states (which
can be restored when you 'Undo') and "future" states (which are
restored when you 'Redo'). That is, you can think of the state as a
list of values with a "current" position; put and get operate on the
current position, and undo/redo let you move back/forward in the
list. (In other words - a list zipper!)
In fact, you can find this code already implemented on the Haskell wiki:
http://haskell.org/haskellwiki/New_monads/MonadUndo
Doesn't look like it's on Hackage though.
-Brent
On Thu, Apr 15, 2010 at 12:15:09AM +0200, edgar klerks wrote:
> Hi All,
>
> I wanted to write the statemonad in a different way, because I want to
> track mutations to the state. And also to be able to undo an done or
> redo an undone mutation. Therefore I wrote a small dsl with an
> evaluator, which finds out the current state.
>
> The problem is, I don't know how to create a get and put function for
> my monad. I have a function which evaluate the state and gives back
> the current State.
>
> Can someone help me a bit along? I have the feeling I do something
> wrong, but I am not sure what.
>
> With kind regards,
>
> Edgar
>
> {-# LANGUAGE GADTs,DeriveFunctor #-}
> import Control.Monad
> import Data.Maybe
>
> data StateCmd a where
> Put :: a -> StateCmd a
> Undo :: StateCmd a
> Redo :: StateCmd a
> deriving (Show, Functor)
>
> newtype State s a = State { unState :: ([StateCmd s],a) }
> deriving Show
>
> joinState :: State s a -> State s b -> State s b
> joinState (State (xs, a)) (State (ys, b)) = State (xs ++ ys, b)
>
> instance Monad (State s) where
> return a = State $ ([], a)
> (>>=) = bindState
>
> -- m a -> ( a -> m b) -> m b
> bindState st@(State (_,a)) f = st `joinState` st'
> where st' = f a
>
> unPut :: StateCmd a -> Maybe a
> unPut (Put a) = Just a
> unPut _ = Nothing
>
> test = State $ ([Put 4, Undo, Redo, Undo, Put 5, Undo, Redo, Put 6,
> Undo, Redo, Undo], ())
>
> getCurrent = fromJust.unPut.head.snd.(foldr current'
> ([],[])).reverse.fst.unState
> where current' x (ul,cl) = case x of
> Put n -> (ul, Put n : cl)
> Undo -> (head cl : ul, tail cl)
> Redo -> (tail ul, head ul : cl)
> _______________________________________________
> 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 22, Issue 22
*****************************************