
Take a look at the following: data Writer o v = Writer [o] v instance Monad (Writer o) where return v = Writer [] v (Writer os v) >>= f = let (Writer os' v') = f v in Writer (os ++ os') v' write :: o -> Writer o () write o = Writer [o] () writes :: [o] -> Writer o () writes os = Writer os () By this code, I define a new monad where I can "write" stuff into a big list. (Although I notice this is going to get slower and slower as the list grows...) For example, test = do write 1 write 2 write 3 yields "Writer [1,2,3] ()". Now, how do I implement the following? data Reader i v = Reader [i] v instance Monad (Reader i) where return = ??? (>>=) = ??? read :: Reader o o such that a Reader is created with an initial list, and the read function fetches 1 element out of that list. That is, the expression "x <- read" will take the head element of the list and put it into x, keeping the tail to be read later. (Oh yeah - and apparently that clashes with Prelude.read. Oh well!) I can't figure out how to implement this... The closest I managed was to make a Reader object also contain a function that tells (>>=) what to do to the Reader object you're binding against... But that seems to be horribly buggy. Any thoughs?

On Sun, May 27, 2007 at 02:43:32PM +0100, Andrew Coppin wrote:
Any thoughs?
Take a look at MonadSupply on the wiki: http://haskell.org/haskellwiki/New_monads/MonadSupply Stefan

Hi @all I am quite new to haskell ... But need to get the HaXml to work... Unfortunatly I dont know why ghc doesnt find the lib... Ghc is installed on my linux and it works it appears to be the ghc 6.6 I managed to compile the little "hello world" programm ... But as soon as i try to import HaXml ghc tells me that it could not find it... I followed the installation steps on the haxml homepage and it seemed to work just fine.... Thanx in advanced Yours Bjoern No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.472 / Virus Database: 269.8.0/819 - Release Date: 26.05.2007 10:47

Björn wrote:
Hi @all
I am quite new to haskell ... But need to get the HaXml to work... Unfortunatly I dont know why ghc doesnt find the lib...
Ghc is installed on my linux and it works it appears to be the ghc 6.6 I managed to compile the little "hello world" programm ...
But as soon as i try to import HaXml ghc tells me that it could not find it...
I followed the installation steps on the haxml homepage and it seemed to work just fine....
Thanx in advanced Yours Bjoern
1. To "ghc-pkg list" and see if HaXmL shows up in the listing. If yes, GHC knows it's installed. If no, something is wrong. 2. How are you compiling your program? IIRC, if you don't use the "--make" switch, you must manually specify that you want specific packages installed, whereas with --make it happens automatically.

Hey, Ghc-pkg list says: "... HaXml-1.13.2, ..." so that should be alright. yes I've compiled using the --make switch... My Programm is something simple right now: Module bjoern where Import HaXml Import IO Main = putStrLn "hello world" But it always fails to import the lib... This is what I type to compile it in the shell: ghc --make bjoern -----Ursprüngliche Nachricht----- Von: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] Im Auftrag von Andrew Coppin Gesendet: Sonntag, 27. Mai 2007 17:06 An: haskell-cafe@haskell.org Betreff: Re: [Haskell-cafe] Problems with HaXml -- ghc cant find the libs Björn wrote:
Hi @all
I am quite new to haskell ... But need to get the HaXml to work... Unfortunatly I dont know why ghc doesnt find the lib...
Ghc is installed on my linux and it works it appears to be the ghc 6.6
I managed to compile the little "hello world" programm ...
But as soon as i try to import HaXml ghc tells me that it could not find it...
I followed the installation steps on the haxml homepage and it seemed to work just fine....
Thanx in advanced Yours Bjoern
1. To "ghc-pkg list" and see if HaXmL shows up in the listing. If yes, GHC knows it's installed. If no, something is wrong. 2. How are you compiling your program? IIRC, if you don't use the "--make" switch, you must manually specify that you want specific packages installed, whereas with --make it happens automatically. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.472 / Virus Database: 269.8.0/819 - Release Date: 26.05.2007 10:47 No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.472 / Virus Database: 269.8.0/819 - Release Date: 26.05.2007 10:47

Björn wrote:
Hey,
Ghc-pkg list says: "... HaXml-1.13.2, ..." so that should be alright.
That's a good start - you've got further than I ever did!
My Programm is something simple right now:
Module bjoern where Import HaXml Import IO Main = putStrLn "hello world"
But it always fails to import the lib...
This is what I type to compile it in the shell: ghc --make bjoern
OK, well, try changing it to import Text.XML.HaXml and see what that does...

On Sun, May 27, 2007 at 04:48:10PM +0100, Andrew Coppin wrote:
Björn wrote:
My Programm is something simple right now:
Module bjoern where
Keywords like "module" must be lower case. Module names must be capitalized. module Bjoern where
Import HaXml
import Text.XML.HaXml
Import IO
import IO (which isn't needed here, but it's harmless)
Main = putStrLn "hello world"
Variable names such as "main" must be lowercase. main = putStrLn "hello world"
But it always fails to import the lib...
This is what I type to compile it in the shell: ghc --make bjoern
The argument to --make must be a file name, and the file must have a .hs or .lhs extension. Stefan

Hi Now it finally worked... Atleast it does not complain anymore about the import... I guess one has to import Text.XML.HaXml and it seems to work... Thanx a lot !!! Cu B -----Ursprüngliche Nachricht----- Von: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] Im Auftrag von Stefan O'Rear Gesendet: Sonntag, 27. Mai 2007 17:56 An: Andrew Coppin Cc: haskell-cafe@haskell.org Betreff: Re: AW: [Haskell-cafe] Problems with HaXml -- ghc cant find the libs On Sun, May 27, 2007 at 04:48:10PM +0100, Andrew Coppin wrote:
Björn wrote:
My Programm is something simple right now:
Module bjoern where
Keywords like "module" must be lower case. Module names must be capitalized. module Bjoern where
Import HaXml
import Text.XML.HaXml
Import IO
import IO (which isn't needed here, but it's harmless)
Main = putStrLn "hello world"
Variable names such as "main" must be lowercase. main = putStrLn "hello world"
But it always fails to import the lib...
This is what I type to compile it in the shell: ghc --make bjoern
The argument to --make must be a file name, and the file must have a .hs or .lhs extension. Stefan _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.472 / Virus Database: 269.8.0/819 - Release Date: 26.05.2007 10:47 No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.472 / Virus Database: 269.8.0/819 - Release Date: 26.05.2007 10:47

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Andrew Coppin wrote:
Take a look at the following:
data Writer o v = Writer [o] v
instance Monad (Writer o) where return v = Writer [] v
(Writer os v) >>= f = let (Writer os' v') = f v in Writer (os ++ os') v'
write :: o -> Writer o () write o = Writer [o] ()
writes :: [o] -> Writer o () writes os = Writer os ()
By this code, I define a new monad where I can "write" stuff into a big list. (Although I notice this is going to get slower and slower as the list grows...)
Try the dlist library instead of plain lists, for the speed issue.
For example,
test = do write 1 write 2 write 3
yields "Writer [1,2,3] ()".
Now, how do I implement the following?
data Reader i v = Reader [i] v
instance Monad (Reader i) where return = ??? (>>=) = ???
read :: Reader o o
such that a Reader is created with an initial list, and the read function fetches 1 element out of that list. That is, the expression "x <- read" will take the head element of the list and put it into x, keeping the tail to be read later.
(Oh yeah - and apparently that clashes with Prelude.read. Oh well!)
You could call it Unwriter/unwrite since it's in some way the opposite in time of Writer (and since Reader monad conventionally refers to one where the input state isn't consumed when it's read).
I can't figure out how to implement this... The closest I managed was to make a Reader object also contain a function that tells (>>=) what to do to the Reader object you're binding against... But that seems to be horribly buggy.
you could try this (I haven't tested it but it looks okay to me) newtype Unwriter i v = Unwriter ([i] -> (v, [i])) instance Monad (Unwriter i) where return v = \_ -> v (Unwriter m) >>= f = Unwriter $ \i -> let (v, i') = m i in let Unwriter m' = f v in m' i' unwrite :: Unwriter i i - --well, what do you want it to do when there is nothing more left in the input list? Isaac -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGWZERHgcxvIWYTTURAtN4AJ4zXKL97Pg9jttb27hPxJiPhibpjQCgp4AA GWsftHiS6OL1VKINkZW1bGc= =3T/G -----END PGP SIGNATURE-----

On 5/27/07, Andrew Coppin
such that a Reader is created with an initial list, and the read function fetches 1 element out of that list. That is, the expression "x <- read" will take the head element of the list and put it into x, keeping the tail to be read later.
Your intended behavior for Reader indicates stateful computational features. The "read later" roughly expands to "be read by some monadic action on the rhs of a >>=" as in (read >>= \x -> read {-this is later-} >>= ...) Recognizing the stateful nature gives you two options: 1) Implement yours as a restricted version of Control.Monad.State type ReaderAC = State readAC = get >>= \x -> put (tail x) >> return (head x) 2) or (as Isaac demonstrated) look to the definition of Control.Monad.State.State for guidance own how to structure your monad.

Nicolas Frisby wrote:
Your intended behavior for Reader indicates stateful computational features. The "read later" roughly expands to "be read by some monadic action on the rhs of a >>=" as in
(read >>= \x -> read {-this is later-} >>= ...)
Recognizing the stateful nature gives you two options:
1) Implement yours as a restricted version of Control.Monad.State
type ReaderAC = State readAC = get >>= \x -> put (tail x) >> return (head x)
I'll at least have a look at that...
2) or (as Isaac demonstrated) look to the definition of Control.Monad.State.State for guidance own how to structure your monad.
Ah yes. I just sat down to do this, and... oh look. There in the first few lines, a non-standard language extension that I don't understand. ...which neatly brings us back to the reason I'm trying to reimplement this by hand in the first place. ;-)

On Sun, 27 May 2007, Andrew Coppin wrote:
such that a Reader is created with an initial list, and the read function fetches 1 element out of that list. That is, the expression "x <- read" will take the head element of the list and put it into x, keeping the tail to be read later.
(Oh yeah - and apparently that clashes with Prelude.read. Oh well!)
I can't figure out how to implement this... The closest I managed was to make a Reader object also contain a function that tells (>>=) what to do to the Reader object you're binding against... But that seems to be horribly buggy.
Confusingly MonadReader and MonadWriter in the Monad Template Library are not quite counterparts. Reader does not consume its input, Writer does not overwrite its output.
participants (6)
-
Andrew Coppin
-
Björn
-
Henning Thielemann
-
Isaac Dupree
-
Nicolas Frisby
-
Stefan O'Rear