
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Now after my earlier soap box tirade I'm trying to put my code where my mouth is. >>blush<< It's harder than I thought. I've broken it down into a State Model of a light bulb. The goal, to find out just how much Haskell code does it take to change the state of a light bulb? Here's a very crippled version. Is there an easy way to introduce state without changing the type of main??? I've written a much more complex example, but I got into things like main :: STT(IO) using a monad transformer. So really, the big question is, must I use a monad transformer? - ------------------------------------------------------------------------------------------------ - -- State model of a light bulb import Monad import IO main :: IO () main = do hSetBuffering stdin NoBuffering process process :: IO () process = do i <- assign initialState putChar '\n' print(s) if (s /= Exit) then process else return () data State = Dark | Light initialState :: State initialState = Dark data Stimulus = On | Off | Exit deriving (Show, Eq, Enum) charToStimulus :: Char -> Stimulus charToStimulus '1' = On charToStimulus '0' = Off charToStimulus c = Exit getStimulus :: IO Stimulus getStimulus = liftM charToStimulus getChar - -- State transformer newtype St a = MkSt (State -> (a, State)) - -- State transformer applied to state apply :: St a -> State -> (a, State) apply (MkSt f) s = f s - -- State monad instance Monad St where return x = MkSt f where f s = (x,s) p >>= q = MkSt f where f s = apply (q x) s' where (x, s') = apply p s - -- Useful State operations fetch :: St State fetch = MkSt f where f s = (s,s) assign :: State -> St () assign s' = MkSt f where f s = ((),s') done :: St () done = return () - ----------------------------------------------------------------------------------------------------------------------- - -- You're in a maze of twisty little statements, all alike. Public Key available from http://www.garbett.org/public-key -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQE8fqdpDtpPjAQxZ6ARAkCWAJ9pGcS/hQpoK12AdcEmt8Y/0rEv9ACfWGtO 1FL9VwopmgsAyUAv9RL++T8= =a9xu -----END PGP SIGNATURE-----

On Thu, 28 Feb 2002, Shawn P. Garbett wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Now after my earlier soap box tirade I'm trying to put my code where my mouth is. >>blush<< It's harder than I thought.
I've broken it down into a State Model of a light bulb. The goal, to find out just how much Haskell code does it take to change the state of a light bulb?
Here's a very crippled version. Is there an easy way to introduce state without changing the type of main???
Rhe IO monad IS a state monad without fetch and assign. (I'd hate to see it written in the traditional \s -> (s,v) sense, one fetch of the state gives you gigabytes of information that is on the computer, and then you'd have to include at least part of the 2^whatever bytes on all the other computers on the internet. Oh yeah, did i mention if you actually interface such a monad with reality, the state would have to include every bit of information with in the universe! Imagine, all of that dumped out, all with one single fetch!) :-) </Joke>
I've written a much more complex example, but I got into things like
main :: STT(IO)
using a monad transformer.
I did similar not to long ago on one of these lists, but it was more of an example of using a monad transformer for another goal.
So really, the big question is, must I use a monad transformer?
I'm confused as to what your goal is :) (plus I don't know where your previous post is). It doesn't seem like all that much code. Much of it is the definition of the state monad. Jay Cox
participants (2)
-
Jay Cox
-
Shawn P. Garbett