Re: [Haskell-cafe] using an external application
On 11/2/07, Petr Hoffmann [EMAIL PROTECTED] wrote: import System.Cmd main = do System.Cmd.system echo hello output.txt -- use the external application to create an output file o1 - readFile output.txt System.Cmd.system echo bye output.txt -- the second call to the external application o2 - readFile output.txt putStr o1 -- hello expected, but bye printed return 0 Can you please give me some hint to solve this problem? I'm a beginning haskell developer and I'm still a bit confused by the IO monad. This looks like yet another case of the lazy-I/O goblins. The readFile function uses evil magic to avoid actually performing any I/O until the contents are actually used. In your case, I suspect that by the time o1 is used -- i.e. in the putStr call -- the file contents have already changed, so the lazy I/O reads the new contents without complaining. The solution would be to use a version of readFile that works in a stricter way, by reading the file when it's told to, but I don't have an implementation handy. Stuart ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
On 11/2/07, Petr Hoffmann [EMAIL PROTECTED] wrote: I'm solving the following problem - I need to use an external application - give it the input data and receive its output. Check out: The HSH library: HSH is designed to let you mix and match shell expressions with Haskell programs. With HSH, it is possible to easily run shell commands, capture their output or provide their input, and pipe them to and from other shell commands and arbitrary Haskell functions at will. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/HSH-1.2.4 regards, Bas van Dijk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
On Fri, 2 Nov 2007, Petr Hoffmann wrote: Hi, I'm solving the following problem - I need to use an external application - give it the input data and receive its output. However, when multiple calls are made, the results are not as expected. The simplified version of the problem is given below: import System.Cmd main = do System.Cmd.system echo hello output.txt -- use the external application to create an output file o1 - readFile output.txt System.Cmd.system echo bye output.txt -- the second call to the external application o2 - readFile output.txt putStr o1 -- hello expected, but bye printed return 0 You fell into the trap, that 'readFile' works lazily, that is, data is only read when needed. If o1 is not completely evalutated until the second write to output.txt, it will not be read correctly from the disk. I think it was not a good choice to make the lazy version of 'readFile' the default. I think it would have been better to provide a strict 'readFile' and another 'readFileLazy' with warnings in the documentation. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
On 11/2/07, Petr Hoffmann [EMAIL PROTECTED] wrote: import System.Cmd main = do System.Cmd.system echo hello output.txt -- use the external application to create an output file o1 - readFile output.txt System.Cmd.system echo bye output.txt -- the second call to the external application o2 - readFile output.txt putStr o1 -- hello expected, but bye printed return 0 Can you please give me some hint to solve this problem? I'm a beginning haskell developer and I'm still a bit confused by the IO monad. I'm puzzled - when I run this on GHCi (v6.4, Windows XP) I get the following outcome *Mainmain The process cannot access the file because it is being used by another process. hello *Main This is certainly the behaviour I would expect - seeing bye being printed seems to me to be an error and may even constitute a violation of referential transparency. What implementation of GHC are you using ? This looks like yet another case of the lazy-I/O goblins. Yes, but not quite what everyone has being addressing -- Andrew Butterfield Tel: +353-1-896-2517 Fax: +353-1-677-2204 Foundations and Methods Research Group Director. Course Director, B.A. (Mod.) in CS and ICT degrees, Year 4. Department of Computer Science, Room F.13, O'Reilly Institute, Trinity College, University of Dublin, Ireland. http://www.cs.tcd.ie/Andrew.Butterfield/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
Can you please give me some hint to solve this problem? I'm a beginning haskell developer and I'm still a bit confused by the IO monad. Other people have explained to the OP why unsafe lazy IO is breaking his code. Yet another piece of evidence, in my opinion, that unsafe-lazy-by-default is the wrong basic API. Provide that API as an option, sure. But as a default? Not IMO. Jules ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
Andrew Butterfield [EMAIL PROTECTED] writes: I'm puzzled - when I run this on GHCi (v6.4, Windows XP) I get the following outcome^^ The process cannot access the file because it is being used by another process. Isnt' this a difference between Windows and Unix? Windows typically locks files, Unix typically does not. -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
Hello Petr, Friday, November 2, 2007, 11:17:23 AM, you wrote: o1 - readFile output.txt add return $! length o1 here to evaluate whole list System.Cmd.system echo bye output.txt -- the second call to -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
Petr Hoffmann writes: I'm solving the following problem - I need to use an external application - give it the input data and receive its output. However, when multiple calls are made, the results are not as expected. The simplified version of the problem is given below: System.Cmd.system echo hello output.txt -- use the external ... System.Cmd.system echo bye output.txt -- the second call to the external application o2 - readFile output.txt putStr o1 -- hello expected, but bye printed Can you please give me some hint to solve this problem? I'm a beginning haskell developer and I'm still a bit confused by the IO monad. I have the impression that your problem has nothing to do do with Haskell, you just rewrite your file, instead of appending to it. But perhaps I didn't look correctly... Jerzy Karczmarczuk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] using an external application
Hi, I'm solving the following problem - I need to use an external application - give it the input data and receive its output. However, when multiple calls are made, the results are not as expected. The simplified version of the problem is given below: import System.Cmd main = do System.Cmd.system echo hello output.txt -- use the external application to create an output file o1 - readFile output.txt System.Cmd.system echo bye output.txt -- the second call to the external application o2 - readFile output.txt putStr o1 -- hello expected, but bye printed return 0 Can you please give me some hint to solve this problem? I'm a beginning haskell developer and I'm still a bit confused by the IO monad. Thank you in advance. Petr ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
On 11/2/07, Stuart Cook [EMAIL PROTECTED] wrote: The solution would be to use a version of readFile that works in a stricter way, by reading the file when it's told to, but I don't have an implementation handy. I guess this does the job: readFile' fp = do contents - readFile fp let ret (x:xs) = x `seq` ret xs ret [] = return contents ret contents Maybe the x `seq` part isn't necessary at all. -- Felipe. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
On Fri, 2 Nov 2007, Felipe Lessa wrote: On 11/2/07, Stuart Cook [EMAIL PROTECTED] wrote: The solution would be to use a version of readFile that works in a stricter way, by reading the file when it's told to, but I don't have an implementation handy. I guess this does the job: readFile' fp = do contents - readFile fp let ret (x:xs) = x `seq` ret xs ret [] = return contents ret contents Maybe the x `seq` part isn't necessary at all. Awful. It reminds me on MatLab where the developers have implemented many automatisms which do arbitrary clever things in every corner case, but nothing consistent, because they thought programmers want it that way. Then the programmers must write wrappers in order to get functions with consistent behaviour around the fully automated functions. The unlazying procedure looks much like the lazying one, and I wonder whether it would be a good idea to eventually add readFileStrict, getContentStrict and hGetContentStrict to the standard library. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
On 11/2/07, Andrew Butterfield [EMAIL PROTECTED] wrote: I'm puzzled - when I run this on GHCi (v6.4, Windows XP) I get the following outcome *Mainmain The process cannot access the file because it is being used by another process. hello *Main Under GHCi 6.6 I get this: *Main main bye 0 My guess is that 6.4's readFile always opens the file as soon as it is called, whereas 6.6's readFile delays opening until the list is actually forced. Either way, goblins all round! Stuart ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] using an external application
lemming: On Fri, 2 Nov 2007, Felipe Lessa wrote: On 11/2/07, Stuart Cook [EMAIL PROTECTED] wrote: The solution would be to use a version of readFile that works in a stricter way, by reading the file when it's told to, but I don't have an implementation handy. I guess this does the job: readFile' fp = do contents - readFile fp let ret (x:xs) = x `seq` ret xs ret [] = return contents ret contents Maybe the x `seq` part isn't necessary at all. Awful. It reminds me on MatLab where the developers have implemented many automatisms which do arbitrary clever things in every corner case, but nothing consistent, because they thought programmers want it that way. Then the programmers must write wrappers in order to get functions with consistent behaviour around the fully automated functions. The unlazying procedure looks much like the lazying one, and I wonder whether it would be a good idea to eventually add readFileStrict, getContentStrict and hGetContentStrict to the standard library. Yes, I have often thought System.IO.Strict should be added to the strict package on hackage, maybe in terms of bytestring.readFile = return . unpack, since we're being strict anyway. The above is also a rather odd implementation of readFile', which is usually implemented by forcing the last element of the list instead. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe