Re: [Haskell-cafe] Generalizing IO

2009-10-06 Thread Gregory Crosswhite
Putting a constraint on the MonadIO class actually makes your code less general rather than more since it prevents your code from ever being used in a purely functional setting. If you leave out this constraint, you can actually turn your code into a pure function by doing things like feed

Re: [Haskell-cafe] Generalizing IO

2009-10-06 Thread Floptical Logic
> class StreamMonad m where >        fetchLine = m >        sendLine = String -> m () > > instance StreamMonad IO where >        fetchLine = getLine >        sendLine = putLine > > fetchLineFromStream = lift fetchLine > sendLineToStream = lift . sendLine This approach makes more sense to me. The

Re: [Haskell-cafe] Generalizing IO

2009-10-06 Thread Gregory Crosswhite
Oh, and I just thought of one more approach: class StreamMonad m where fetchLine = m sendLine = String -> m () instance StreamMonad IO where fetchLine = getLine sendLine = putLine fetchLineFromStream = lift fetchLine sendLineToStream = lift . sendLine type PDSta

Re: [Haskell-cafe] Generalizing IO

2009-10-06 Thread Gregory Crosswhite
It isn't clear what it is that you are trying to generalize the code to do. If you are trying to generalize it to work with an arbitrary input/output stream of lines, then unless you are doing arbitrary I/O it seems to me that all of these instance declarations are overkill. All that you

Re: [Haskell-cafe] Generalizing IO

2009-10-05 Thread David Menendez
On Mon, Oct 5, 2009 at 11:54 PM, Floptical Logic wrote: >> Instead of specifying the monad implementation, specify the interface. >> That is, you are using state operations (from MonadState) and IO >> operations (from MonadIO). Try removing all the type signatures that >> mention PDState and see w

Re: [Haskell-cafe] Generalizing IO

2009-10-05 Thread Antoine Latter
On Mon, Oct 5, 2009 at 10:54 PM, Floptical Logic wrote: > > If I were to make an instance of MonadIO be a parameter to StateT and > I wanted to use the Net monad (from Roll your own IRC bot on the wiki) > with it, I would need to make Net an instance of MonadIO.  What would > this instance look li

Re: [Haskell-cafe] Generalizing IO

2009-10-05 Thread Floptical Logic
> Instead of specifying the monad implementation, specify the interface. > That is, you are using state operations (from MonadState) and IO > operations (from MonadIO). Try removing all the type signatures that > mention PDState and see what you get. > > E.g., loop :: (MonadState PD m, MonadIO m) =

Re: [Haskell-cafe] Generalizing IO

2009-10-05 Thread Gregory Crosswhite
My thought is that you could simply drop the IO from your type definition, type PDState = StateT PD You will need to change all of your type signature from "PDState " to "PDState m " to make them all polymorphic over the choice of monad. Then all you should need to do is to generalize the

Re: [Haskell-cafe] Generalizing IO

2009-10-05 Thread David Menendez
On Mon, Oct 5, 2009 at 7:56 PM, Floptical Logic wrote: > The code below is a little interactive program that uses some state. > It uses StateT with IO to keep state.  My question is: what is the > best way to generalize this program to work with any IO-like > monad/medium?  For example, I would li

[Haskell-cafe] Generalizing IO

2009-10-05 Thread Floptical Logic
The code below is a little interactive program that uses some state. It uses StateT with IO to keep state. My question is: what is the best way to generalize this program to work with any IO-like monad/medium? For example, I would like the program to function as it does now using stdin but I woul