[GHC] #1132: Data.ByteString.Lazy.Char8's readInt does no bounds checking
#1132: Data.ByteString.Lazy.Char8's readInt does no bounds checking +--- Reporter: bos | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: hslibs/data | Version: 6.6 Severity: normal |Keywords: Difficulty: Easy (1 hr) |Testcase: Architecture: Multiple | Os: Multiple +--- I think it should be failing with Nothing if given a string of digits that's too long. {{{~ $ ghci ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.6, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \/\/ /_/\/|_| Type :? for help. Loading package base ... linking ... done. Prelude :m +Data.ByteString.Lazy.Char8 Prelude Data.ByteString.Lazy.Char8 readInt (pack ) Just (7766279631452241919,LPS [])}}} -- Ticket URL: http://cvs.haskell.org/trac/ghc/ticket/1132 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler___ Glasgow-haskell-bugs mailing list Glasgow-haskell-bugs@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
Re: [GHC] #1132: Data.ByteString.Lazy.Char8's readInt does no bounds checking
#1132: Data.ByteString.Lazy.Char8's readInt does no bounds checking -+-- Reporter: bos | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: hslibs/data |Version: 6.6 Severity: normal | Resolution: Keywords: | Difficulty: Easy (1 hr) Testcase: | Architecture: Multiple Os: Multiple | -+-- Comment (by bos): Hmm, guess I can't use WikiFormatting after all. Sorry for the mess! -- Ticket URL: http://cvs.haskell.org/trac/ghc/ticket/1132 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler___ Glasgow-haskell-bugs mailing list Glasgow-haskell-bugs@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
Re: [GHC] #1132: Data.ByteString.Lazy.Char8's readInt does no bounds checking
#1132: Data.ByteString.Lazy.Char8's readInt does no bounds checking -+-- Reporter: bos | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: hslibs/data |Version: 6.6 Severity: normal | Resolution: Keywords: | Difficulty: Easy (1 hr) Testcase: | Architecture: Multiple Os: Multiple | -+-- Comment (by bos): Don points out that this behaviour is consistent with that of read, so depending on your perspective, we have either one bug (in two places), two bugs, or no bugs :-) -- Ticket URL: http://cvs.haskell.org/trac/ghc/ticket/1132 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler___ Glasgow-haskell-bugs mailing list Glasgow-haskell-bugs@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC appears to loop
When I try to compile to the following program, GHC seems to loop: data Evil = Evil (Evil - Evil) instance Show Evil where show _ = t apply :: Evil - Evil - Evil apply (Evil f) x = f x delta :: Evil delta = Evil (\x - x `apply` x) omega :: Evil omega = delta `apply` delta main = print omega The program codes up a non-terminating term omega - very similar to (\x - x x) (\x - x x) - without using recursion. The trick is to define an Evil data type which has a negative occurrence of Evil. I realize it's a contrived example, but I wasn't sure if it's a known issue. In case it matters, I'm using GHC 6.6 on a PowerPC Mac. All the best, Wouter ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: GHC appears to loop
Would you say this the same as the one described in the user guide? http://www.haskell.org/ghc/docs/latest/html/users_guide/bugs.html#bugs-ghc GHC's inliner can be persuaded into non-termination using the standard way to encode recursion via a data type: data U = MkU (U - Bool) russel :: U - Bool russel u@(MkU p) = not $ p u x :: Bool x = russel (MkU russel) We have never found another class of programs, other than this contrived one, that makes GHC diverge, and fixing the problem would impose an extra overhead on every compilation. So the bug remains un-fixed. There is more background in Secrets of the GHC inliner. Duncan On Sun, 2007-02-04 at 13:49 +, Wouter Swierstra wrote: When I try to compile to the following program, GHC seems to loop: data Evil = Evil (Evil - Evil) instance Show Evil where show _ = t apply :: Evil - Evil - Evil apply (Evil f) x = f x delta :: Evil delta = Evil (\x - x `apply` x) omega :: Evil omega = delta `apply` delta main = print omega The program codes up a non-terminating term omega - very similar to (\x - x x) (\x - x x) - without using recursion. The trick is to define an Evil data type which has a negative occurrence of Evil. I realize it's a contrived example, but I wasn't sure if it's a known issue. In case it matters, I'm using GHC 6.6 on a PowerPC Mac. All the best, Wouter ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: GHC appears to loop
On 4 Feb 2007, at 14:12, Duncan Coutts wrote: Would you say this the same as the one described in the user guide? http://www.haskell.org/ghc/docs/latest/html/users_guide/ bugs.html#bugs-ghc You're right of course. I checked Trac before hitting send, but didn't think to look any further. Thanks for the link, Wouter ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: Become a GHC build slave!
I'm in the processing of setting up a OSX powerpc buildbot on my end. Maybe we can split the effort to increase powerpc coverage? On 2/3/07, Richard Giraud [EMAIL PROTECTED] wrote: Hello I have a G4 PowerPC Mac Mini and an AMD 64 bit box that I can donate to the cause. What OS would be the most useful on the G4? Mac OS X? Linux? BSD? QNX (not sure if it's supported)? What OS would be the most useful on the AMD box? BSD? QNX? Or something else? Richard Simon Marlow wrote: Thanks largely to Ian Lynagh, GHC now has a BuildBot infrastructure to automate nightly builds on multiple platforms. This replaces the old set of shell scripts that we used to run nightly builds; now adding new clients to the setup is relatively easy, instructions are here: http://hackage.haskell.org/trac/ghc/wiki/BuildBot So far we have various Windows builds running, and I'll be moving over the existing Linux nightly builds in due course. If you have a spare machine or a machine that is idle overnight, and you'd like to use it to run automated GHC builds, then please take a look at the above page for how to get started. We especially need platforms that we don't use regularly (i.e. not Windows or Linux/x86/x86_64). Cheers, Simon ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users -- Omnem crede diem tibi diluxisse supremum. ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: Become a GHC build slave!
Thanks for the info; I'll set up the following: - Gentoo Linux on the G4 PPC - OpenBSD for AMD64 for the AMD box. It would be nifty to have an easy-to-view list showing which platforms are covered and which platforms are desired. Richard Wilson MacGyver wrote: I'm in the processing of setting up a OSX powerpc buildbot on my end. Maybe we can split the effort to increase powerpc coverage? ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: Become a GHC build slave!
You can see that here. http://darcs.haskell.org:8010/ and it looks like Thomas Davie already has both OSX PPC and OSX intel covered. there is no reason for multiple build-bot for the same platform is there? On 2/4/07, Richard Giraud [EMAIL PROTECTED] wrote: Thanks for the info; I'll set up the following: - Gentoo Linux on the G4 PPC - OpenBSD for AMD64 for the AMD box. It would be nifty to have an easy-to-view list showing which platforms are covered and which platforms are desired. -- Omnem crede diem tibi diluxisse supremum. ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
ghc 6.6 for mac os x (intel)
Hello, I tried to install ghc 6.6 but apparently I have done something wrong since whenever I type ghc I obtain: $ /usr/local/bin/ghc dyld: Library not loaded: /opt/local/lib/libreadline.5.1.dylib Referenced from: /usr/local/lib/ghc-6.6/ghc-6.6 Reason: image not found Trace/BPT trap can anyone help please? ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
Re: ghc 6.6 for mac os x (intel)
On 2/4/07, Ariel Apostoli [EMAIL PROTECTED] wrote: Hello, I tried to install ghc 6.6 but apparently I have done something wrong since whenever I type ghc I obtain: $ /usr/local/bin/ghc dyld: Library not loaded: /opt/local/lib/libreadline.5.1.dylib Referenced from: /usr/local/lib/ghc-6.6/ghc-6.6 Reason: image not found Trace/BPT trap Hi, Ariel-- Have you seen the page on building GHC on Mac OS X? http://cvs.haskell.org/trac/ghc/wiki/Building/MacOSX In particular, it explains how to set up the readline library so that GHC can find it. If you try the instructions there and something still doesn't work, feel free to post here again. Cheers, Kirsten -- Kirsten Chevalier* [EMAIL PROTECTED] *Often in error, never in doubt The astonishment of life is the absence of any appearance of reconciliation between the theory and practice of life.--Emerson ___ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
[Haskell] Haskell Xlib bindings
Hello everyone, This question probably belongs in GUI, but I tried posting there a week ago and have yet to get a response. My question is this: Why do the Haskell Xlib bindings have no way to extract the property event from an event pointer? For example, the following functions are available: get_KeyEvent, get_ButtonEvent, get_MotionEvent, etc. but there's no get_PropertyEvent. Why was this left out? It seems pretty critical to the functionality of an Xlib program to someone who's been programming in C with Xlib for quite some time. Thanks, Robert Hoelz ___ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Re: [Haskell] Haskell Xlib bindings
On Sun, 2007-02-04 at 16:00 -0600, Rob Hoelz wrote: Hello everyone, This question probably belongs in GUI, but I tried posting there a week ago and have yet to get a response. My question is this: Why do the Haskell Xlib bindings have no way to extract the property event from an event pointer? For example, the following functions are available: get_KeyEvent, get_ButtonEvent, get_MotionEvent, etc. but there's no get_PropertyEvent. Why was this left out? It seems pretty critical to the functionality of an Xlib program to someone who's been programming in C with Xlib for quite some time. I think the sad fact is that nobody seriously uses the Xlib bindings. If you want to help improve them, I'm sure that'd be most welcome. Perhaps these days binding xcb might be the way to go for low level X11 stuff. Duncan ___ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
[Haskell] ANNOUNCE: Data.CompactString 0.1 - my attempt at a Unicode ByteString
Hello all, I would like to announce my attempt at making a Unicode version of Data.ByteString. The library is named Data.CompactString to avoid conflict with other (Fast)PackedString libraries. The library uses a variable length encoding (1 to 3 bytes) of Chars into Word8s, which are then stored in a ByteString. The structure is very much based on Data.ByteString, most of the implementation is copied from there. Hopefully this means that fusion rules could be copied as well. This is kind of a pre-release, many functions are still missing, and I have not benchmarked yet. I am releasing this in the hopes of getting some feedback on the general idea. Homepage: http://twan.home.fmf.nl/compact-string/ Haddock: http://twan.home.fmf.nl/compact-string/doc/html/ Source:darcs get http://twan.home.fmf.nl/repos/compact-string Twan van Laarhoven ___ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
[Haskell] HSXML version 1.13. Overloading polyvariadic functions
This message announces the version 1.13 of HSXML. HSXML is a library for writing and transforming typed semi-structured data in Haskell -- in S-expression syntax, with the extensible set of `tags', and statically enforced content model restrictions. A particular application is writing web pages in Haskell. We obtain HTML, XHTML or other output formats by running the Haskell web page in an appropriate rendering monad. The benefit of representing XML-like documents as a typed data structure/Haskell code is static rejection of bad documents -- not only those with undeclared tags but also those where elements appear in wrong contexts. The web page http://pobox.com/~oleg/ftp/Scheme/xml.html#typed-SXML gives further motivation and description. It points to an example of authoring web pages in HSXML and a complex example of context-sensitive HSXLT transformations: producing structurally distinct HTML and XML/RSS from the same master file. The complete source code with several examples is available at http://pobox.com/~oleg/ftp/Haskell/HSXML.tar.gz The new version of HSXML contains more examples, many of which have been very kindly suggested by shelarcy. One example in particular, quote.hs, demonstrates overloading of a polyvariadic function. That may seem impossible: a polyvariadic function typically has the type forall t. C t = t. How overload on t? How to overload on arguments or the result of the function if we don't know even the number of arguments let alone their types. It's all in the future, when the function is applied. As it turns out, if we make preparations in the present, we can overload the future. ___ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Re: [Haskell-cafe] Suggestions for a hReadUntilStr implementation
Matt, should finish evaluating when either the timer has run out or I recommend changing my implementation of hReadUntilStr so that the deadline is calculated upfront (have a look at System.Time), and then reducing the number of milliseconds for hReadUntilChar with each call to it. Thanks, Greg On 2/4/07, Matt Revelle [EMAIL PROTECTED] wrote: Thanks for the responses. Greg, your implementation looks useful but it's a little different than what I was thinking (my apologies, I wasn't very clear). In the implementation you posted, the timeout parameter is used to limit the amount of time spent waiting to read an individual character - I was hoping to use the timeout as an initial value for a timer that should start running when hReadUntilStr is evaluated and the function should finish evaluating when either the timer has run out or when the string match has been found. Martin, thanks for the link. Cheers, Matt On 2/3/07, Greg Fitzgerald [EMAIL PROTECTED] wrote: Hi Matt, hReadUntilStr - that is, a function that takes a Handle as an input source, a String to match, and a Num a as the number of seconds to wait before returning a (String, Bool) where the String is all the text read from the Handle until either matching or timing out and the Bool is true if the input String was matched This might work for you: http://hpaste.org/289 . It throws an IO exception if hWaitForChar times out, and makes use of lazy evaluation to schedule all the IO upfront so that grabbing the string prefix can be done in pure code. Thanks, Greg ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
On Saturday 03 February 2007 19:56, Pepe Iborra wrote: pad :: [Word8] - [Word8] pad xs = pad' xs 0 pad' (x:xs) l = x : pad' xs (succ l) pad' [] l = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) Pepe, Thanks but this gives me a different problem [EMAIL PROTECTED]:~/sha1 ./allInOne 101 +RTS -hc -RTS [2845392438,1191608682,3124634993,2018558572,2630932637] [2224569924,473682542,3131984545,4182845925,3846598897] Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize' to increase it. Someone suggested pad :: Num a = [a] - [a] pad = pad' 0 where pad' !l [] = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) pad' !l (x:xs) = x : pad' (l+1) xs but that didn't compile *Main :r [2 of 2] Compiling Main ( allInOne.hs, interpreted ) allInOne.hs:83:14: Parse error in pattern Failed, modules loaded: Codec.Utils. Dominic. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
On Sun, Feb 04, 2007 at 08:20:23AM +, Dominic Steinitz wrote: Someone suggested pad :: Num a = [a] - [a] pad = pad' 0 where pad' !l [] = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) pad' !l (x:xs) = x : pad' (l+1) xs but that didn't compile *Main :r [2 of 2] Compiling Main ( allInOne.hs, interpreted ) allInOne.hs:83:14: Parse error in pattern Failed, modules loaded: Codec.Utils. Dominic. The '!' is a GHC extension, enabled using the flag '-fbang-patterns'. Equivalently, you can use Haskell 98's seq : pad :: Num a = [a] - [a] pad = pad' 0 where pad' l [] | l `seq` False = undefined pad' l [] = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) pad' l (x:xs) = x : pad' (l+1) xs The first alternative never succeeds, but to see that the compiler must force the evaluation of 'l'. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
On Saturday 03 February 2007 19:42, [EMAIL PROTECTED] wrote: I have re-written SHA1 so that is more idiomatically haskell and it is easy to see how it implements the specification. The only problem is I now have a space leak. I can see where the leak is but I'm less sure what to do about getting rid of it. Here's the offending function: pad :: [Word8] - [Word8] pad xs = xs ++ [0x80] ++ ps ++ lb where l = length xs pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) I would try something along the following lines (untested): \begin{spec} catWithLen xs f = xs ++ f (length xs) \end{spec} \begin{code} catWithLen :: [a] - (Int - [a]) - [a] catWithLen xs f = h 0 xs where h k [] = f k h k (x : xs) = case succ k of-- forcing evaluation k' - x : h k' xs \end{code} \begin{code} pad :: [Word8] - [Word8] pad xs = catWithLen xs f where f l = 0x80 : ps lb where -- we know that |l = length xs| pl = (64-(l+9)) `mod` 64 ps = funPow pl (0x00 :) lb = i2osp 8 (8*l) \end{code} If you are lucky, then the replicate and the (++lb) in the original code might be fused by the compiler as an instance of foldr-build or something related --- check the optimised core code. Wolfram, Thanks but this gives a different problem: [EMAIL PROTECTED]:~/sha1 ./allInOne 101 +RTS -hc -RTS [2845392438,1191608682,3124634993,2018558572,2630932637] [2224569924,473682542,3131984545,4182845925,3846598897] Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize' to increase it. Dominic. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
On Sun, Feb 04, 2007 at 08:30:44AM +, Dominic Steinitz wrote: On Saturday 03 February 2007 19:42, [EMAIL PROTECTED] wrote: I would try something along the following lines (untested): \begin{spec} catWithLen xs f = xs ++ f (length xs) \end{spec} \begin{code} catWithLen :: [a] - (Int - [a]) - [a] catWithLen xs f = h 0 xs where h k [] = f k h k (x : xs) = case succ k of-- forcing evaluation k' - x : h k' xs Nice try. k', as a variable binding, is irrefutable. \end{code} \begin{code} pad :: [Word8] - [Word8] pad xs = catWithLen xs f where f l = 0x80 : ps lb where -- we know that |l = length xs| pl = (64-(l+9)) `mod` 64 ps = funPow pl (0x00 :) lb = i2osp 8 (8*l) \end{code} Thanks but this gives a different problem: [EMAIL PROTECTED]:~/sha1 ./allInOne 101 +RTS -hc -RTS [2845392438,1191608682,3124634993,2018558572,2630932637] [2224569924,473682542,3131984545,4182845925,3846598897] Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize' to increase it. expected result of the excessive laziness above. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
On Sunday 04 February 2007 08:28, Stefan O'Rear wrote: On Sun, Feb 04, 2007 at 08:20:23AM +, Dominic Steinitz wrote: Someone suggested pad :: Num a = [a] - [a] pad = pad' 0 where pad' !l [] = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) pad' !l (x:xs) = x : pad' (l+1) xs but that didn't compile *Main :r [2 of 2] Compiling Main ( allInOne.hs, interpreted ) allInOne.hs:83:14: Parse error in pattern Failed, modules loaded: Codec.Utils. Dominic. The '!' is a GHC extension, enabled using the flag '-fbang-patterns'. The test program runs to completion but still has a space leak consuming over 25m. Equivalently, you can use Haskell 98's seq : pad :: Num a = [a] - [a] pad = pad' 0 where pad' l [] | l `seq` False = undefined pad' l [] = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) pad' l (x:xs) = x : pad' (l+1) xs The first alternative never succeeds, but to see that the compiler must force the evaluation of 'l'. [EMAIL PROTECTED]:~/sha1 ./allInOne 101 +RTS -hc -RTS [2845392438,1191608682,3124634993,2018558572,2630932637] [2224569924,473682542,3131984545,4182845925,3846598897] Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize' to increase it. Dominic. PS I appreciate all the help I'm getting. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
On Sun, Feb 04, 2007 at 09:45:12AM +, Dominic Steinitz wrote: pad :: Num a = [a] - [a] pad = pad' 0 where pad' l [] | l `seq` False = undefined Stupid typo, that should be: where pad' l _ | l `seq` False = undefined pad' l [] = [0x80] ++ ps ++ lb where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 lb = i2osp 8 (8*l) pad' l (x:xs) = x : pad' (l+1) xs The first alternative never succeeds, but to see that the compiler must force the evaluation of 'l'. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Alternate instance Show (Maybe a)?
G'day all. Quoting Sergey Zaharchenko [EMAIL PROTECTED]: Yes, I think another Show-like class will probably be a better solution... This is the one that I use. Very simple. import Text.PrettyPrint.HughesPJ class Pretty a where -- Equivalent of showsPrec prettyP :: Int - a - Doc prettyP _ x = pretty x -- Equivalent of shows pretty :: a - Doc pretty x = prettyP 0 x Cheers, Andrew Bromage ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Space Leak Help
If anyone wants to play with this, here's a version of the leak that doesn't need any libraries or extensions. pad causes a stack overflow and pad1 uses up about 6m of heap. Dominic. module Main(main) where import Data.Word import Data.Bits import Data.List pad = pad' 0 where pad' l [] = [0x80] ++ ps where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 pad' l (x:xs) = x : pad' (l+1) xs pad1 xs = xs ++ [0x80] ++ ps where l = length xs pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 test :: Int - Word8 test n = foldl' xor 0x55 (pad (replicate n 0x55)) test1 :: Int - Word8 test1 n = foldl' xor 0x55 (pad1 (replicate n 0x55)) main = putStrLn (show (test1 101)) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Space Leak Help
pad causes a stack overflow and pad1 uses up about 6m of heap. pad1 xs = xs ++ [0x80] ++ ps where l = length xs pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 wild guess: if you compute the length when the consumer reaches ps, you hold on to a copy of xs longer than needed, whereas if you compute the length upfront, you unfold xs too early. the zip-trick you mentioned might work around this, allowing you to consume xs lazily while still having its length at the end. but if you're only interested in the **length modulo 64**, you should be able to process and let go of xs in chunks of length 64, too small for overflows? Claus ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Space Leak Help
On 2/4/07, Dominic Steinitz [EMAIL PROTECTED] wrote: pad causes a stack overflow and pad1 uses up about 6m of heap. pad = pad' 0 where pad' l [] = [0x80] ++ ps where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 pad' l (x:xs) = x : pad' (l+1) xs pad = pad' 0 where pad' l [] = [0x80] ++ ps where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 pad' l (x:xs) = x : (pad' $! l+1) xs -- otherwise (l+1) it will be deferred until replicate -- Tolik ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] nested maybes
I have a Data.Map.Map String - (Layout, [String]) as follows: type Anagrams = [String] type Cell = (Layout, Anagrams) type WordMap = Map.Map String Cell exists str wmap = let a = Map.lookup (sort str) wmap in case a of Nothing - False Just x - case (find (== str) (snd x)) of Nothing - False _ - True the existence test looks ugly - any more compact way to write it? martin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
On 2/4/07, Martin DeMello [EMAIL PROTECTED] wrote: I have a Data.Map.Map String - (Layout, [String]) as follows: type Anagrams = [String] type Cell = (Layout, Anagrams) type WordMap = Map.Map String Cell exists str wmap = let a = Map.lookup (sort str) wmap in case a of Nothing - False Just x - case (find (== str) (snd x)) of Nothing - False _ - True the existence test looks ugly - any more compact way to write it? How about: exists str = fromMaybe False . fmap (elem str.snd) . Map.lookup (sort str) -- Cheers, Lemmih ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
Maybe has a Monad instance, so you can write this as follows (untested): exists str wmap = boolFromMaybe exists' where exists' = do x - Map.lookup (sort str) wmap find (== str) (snd x) boolFromMaybe (Just _) = True boolFromMaybe Nothing = False /g On 2/4/07, Martin DeMello [EMAIL PROTECTED] wrote: I have a Data.Map.Map String - (Layout, [String]) as follows: type Anagrams = [String] type Cell = (Layout, Anagrams) type WordMap = Map.Map String Cell exists str wmap = let a = Map.lookup (sort str) wmap in case a of Nothing - False Just x - case (find (== str) (snd x)) of Nothing - False _ - True the existence test looks ugly - any more compact way to write it? martin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- It is myself I have never met, whose face is pasted on the underside of my mind. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
On Sun, 2007-02-04 at 19:54 +0530, Martin DeMello wrote: I have a Data.Map.Map String - (Layout, [String]) as follows: type Anagrams = [String] type Cell = (Layout, Anagrams) type WordMap = Map.Map String Cell exists str wmap = let a = Map.lookup (sort str) wmap in case a of Nothing - False Just x - case (find (== str) (snd x)) of Nothing - False _ - True the existence test looks ugly - any more compact way to write it? Using the Maybe Monad is one solution i think (as in: i _think_ this should work): findIt str wmap = do a - Map.lookup (sort str) wmap return $ find (== str) (snd a) exists str wmap = case findIt str wmap of Nothing - False Just _ - True The Maybe monad is very nice for abstracting away all those case-expressions. signature.asc Description: This is a digitally signed message part ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Strange behaviour with writeFile
Hi, I am observing some rather strange behaviour with writeFile. Say I have the following code: answer - AbstractIO.readFile filename let (answer2, remainder) = parseAnswer answer if remainder == answer2 == then do AbstractIO.putStrLn $ completed else do AbstractIO.putStrLn answer2 AbstractIO.writeFile filename remainder With the above I get an error saying the resources to filename are locked. If I add the line AbstractIO.putStrLn $ show (answer2, remainder) before I call writeFile it suddenly magically works! Has anyone seen strange behaviour like this before? Regards, Chris. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Strange behaviour with writeFile
Its about the lazyness of reading the file. The handles on the file associated (underlying readFile) is still open - hence the resource being in use. When you add that extra line the act of writing out the remainer causes the rest of the input to be fully evaluated and hence the filehandle is closed. If you wish to overwrite the existing file you have to assure that the file is not open for reading - just like with any file interface. Neil On 04/02/07, C.M.Brown [EMAIL PROTECTED] wrote: Hi, I am observing some rather strange behaviour with writeFile. Say I have the following code: answer - AbstractIO.readFile filename let (answer2, remainder) = parseAnswer answer if remainder == answer2 == then do AbstractIO.putStrLn $ completed else do AbstractIO.putStrLn answer2 AbstractIO.writeFile filename remainder With the above I get an error saying the resources to filename are locked. If I add the line AbstractIO.putStrLn $ show (answer2, remainder) before I call writeFile it suddenly magically works! Has anyone seen strange behaviour like this before? Regards, Chris. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Strange behaviour with writeFile
Hi Neil, When you add that extra line the act of writing out the remainer causes the rest of the input to be fully evaluated and hence the filehandle is closed. Ah, yes of course :) I've found that: let (answer2, remainder) = parseAnswer (force answer) where force :: Eq a = a - a force x = if x==x then x else x Seems to do the trick. Thanks! Chris. On 04/02/07, C.M.Brown [EMAIL PROTECTED] wrote: Hi, I am observing some rather strange behaviour with writeFile. Say I have the following code: answer - AbstractIO.readFile filename let (answer2, remainder) = parseAnswer answer if remainder == answer2 == then do AbstractIO.putStrLn $ completed else do AbstractIO.putStrLn answer2 AbstractIO.writeFile filename remainder With the above I get an error saying the resources to filename are locked. If I add the line AbstractIO.putStrLn $ show (answer2, remainder) before I call writeFile it suddenly magically works! Has anyone seen strange behaviour like this before? Regards, Chris. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Space Leak Help
\begin{code} catWithLen :: [a] - (Int - [a]) - [a] catWithLen xs f = h 0 xs where h k [] = f k h k (x : xs) = case succ k of-- forcing evaluation k' - x : h k' xs \end{code} Thanks but this gives a different problem: [EMAIL PROTECTED]:~/sha1 ./allInOne 101 +RTS -hc -RTS [2845392438,1191608682,3124634993,2018558572,2630932637] [2224569924,473682542,3131984545,4182845925,3846598897] Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize' to increase it. Does it still do that if you youse seq instead of case? \begin{code} catWithLen :: [a] - (Int - [a]) - [a] catWithLen xs f = h 0 xs where h k [] = f k h k (x : xs) = let k' = succ k in k' `seq` x : h k' xs \end{code} Wolfram ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Win32 help please
On Sun, Feb 04, 2007 at 10:42:23PM +1100, John Ky wrote: # hsc2hs mywin32.hsc # ghc -fffi mywin32.hs C:/system/ghc/ghc-6.6/libHSrts.a(Main.o):Main.c:(.text+0x1b): undefined reference to `__stginit_ZCMain' C:/system/ghc/ghc-6.6/libHSrts.a(Main.o):Main.c:(.text+0x3f): undefined reference to `ZCMain_main_closure' collect2: ld returned 1 exit status What am I missing? A function named main. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Another Space Leak
Many thanks for the help on the original space leak which is now fixed -see the function pad below and test runs in small constant space. However, that has merely revealed the next space leak. The problem appears to be blockWord8sIn512 :: [Word8] - [[Word8]] blockWord8sIn512 = unfoldr g where g [] = Nothing g xs = Just (splitAt 64 xs) or its alternative bws :: [Word8] - [[Word8]] bws [] = [] bws xs = ys:(bws zs) where (ys,zs) = splitAt 64 xs But I can't see why a big expression is being built up. Shouldn't these produce elements of the list when they are required and they are then consumed by foldl'? Dominic. module Main(main) where import Data.Word import Data.Bits import Data.List ss = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0] pad = pad' 0 where pad' l [] = [0x80] ++ ps where pl = (64-(l+9)) `mod` 64 ps = replicate pl 0x00 pad' l (x:xs) = x : (pad' $! l+1) xs -- otherwise (l+1) it will be deferred until replicate blockWord8sIn512 :: [Word8] - [[Word8]] blockWord8sIn512 = unfoldr g where g [] = Nothing g xs = Just (splitAt 64 xs) bws :: [Word8] - [[Word8]] bws [] = [] bws xs = ys:(bws zs) where (ys,zs) = splitAt 64 xs test :: Int - Word8 test n = foldl' xor 0x55 (pad (replicate n 0x55)) test1 :: Int - [Word8] test1 n = foldl' (zipWith xor) [0x01..0x40] (blockWord8sIn512 (pad (replicate n 0x55))) test2 :: Int - [Word8] test2 n = foldl' (zipWith xor) [0x01..0x40] (bws (pad (replicate n 0x55))) main = do putStrLn (show (test 100)) putStrLn (show (test1 100)) putStrLn (show (test2 100)) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: nested maybes
Maybe has a Monad instance, so you can write this as follows (untested): exists str wmap = boolFromMaybe exists' where exists' = do x - Map.lookup (sort str) wmap find (== str) (snd x) boolFromMaybe (Just _) = True boolFromMaybe Nothing = False import isJust boolFromMaybe = isJust -- WBR, Max Vasin. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] List operation question
Hi, I'm still somewhat new to Haskell, so I'm wondering if there are better ways I could implement the following functions, especially shiftl: moves the first element to the end of the list shiftr :: [a] - [a] shiftr [] = [] shiftr (x:y) = y ++ [x] moves the last element to the head of the list shiftl :: [a] - [a] shiftl [] = [] shiftl x = [last x] ++ init x -Eric ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] List operation question
Not much better. You could define shiftl such that is does a single traversal and returns both the last element and all but the last. That will save you one traversal. On Feb 4, 2007, at 18:44 , Eric Olander wrote: Hi, I'm still somewhat new to Haskell, so I'm wondering if there are better ways I could implement the following functions, especially shiftl: moves the first element to the end of the list shiftr :: [a] - [a] shiftr [] = [] shiftr (x:y) = y ++ [x] moves the last element to the head of the list shiftl :: [a] - [a] shiftl [] = [] shiftl x = [last x] ++ init x -Eric ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] mixing wxhaskell state and file io
I'm having a lot of trouble mixing file io and wxhaskell's varCreate/Get/Set functions. I have functions readWords :: String - IO WordMap wordGrid :: WordMap - Layout And within my GUI code, the following compiles (ignores the variable, basically): words - varCreate (do {w - readWords words; return w}) wGrid - do w - readWords words return $ wordGrid w but I can't get the following (noncompiling code, but it shows what I'm trying to do) working: wGrid - do w - varGet words return $ wordGrid w Could someone give me a minimal example of reading in a list of words from a file, binding a wxHaskell variable to the list, and then mapping some GUI code over it? (Also, I'm making the base assumption that varSet and varGet are wxHaskell's analogue of the State monad - should I be looking at using StateT instead?) martin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Another Space Leak
On 2/4/07, Dominic Steinitz [EMAIL PROTECTED] wrote: test1 :: Int - [Word8] test1 n = foldl' (zipWith xor) [0x01..0x40] (blockWord8sIn512 (pad (replicate n 0x55))) test2 :: Int - [Word8] test2 n = foldl' (zipWith xor) [0x01..0x40] (bws (pad (replicate n 0x55))) The problem really is here: foldl' demands the value of 'zipWith xor xs ys' (which is a list) but not its elemants. So we need a modified zipWith: zipWith' f xs ys = forceList (zipWith f xs ys) where forceList zs = foldr seq zs zs -- Tolik ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] List operation question
Nicolas Frisby wrote: I've always thought that when certain operations are of particular interest, it's time to use more appropriate data structures, right? Lists are great and simple and intuitive, but if you need such operations as shifts, something like a deque is the way to go. This sounds like a job for Data.Sequence. -Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] List operation question
I agree. If performance is important enough to worry about is shiftl traverses the list once or twice then it's time to switch to a better data type. On Feb 4, 2007, at 19:27 , Yitzchak Gale wrote: Nicolas Frisby wrote: I've always thought that when certain operations are of particular interest, it's time to use more appropriate data structures, right? Lists are great and simple and intuitive, but if you need such operations as shifts, something like a deque is the way to go. This sounds like a job for Data.Sequence. -Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] List operation question
Eric Olander wrote: Hi, I'm still somewhat new to Haskell, so I'm wondering if there are better ways I could implement the following functions, especially shiftl: moves the first element to the end of the list shiftr :: [a] - [a] shiftr [] = [] shiftr (x:y) = y ++ [x] moves the last element to the head of the list shiftl :: [a] - [a] shiftl [] = [] shiftl x = [last x] ++ init x If you use Data.Sequence (new in 6.6, I think), these can be O(1): import qualified Data.Sequence as Seq import Data.Sequence ( (|), (|), (:), (:) ) shiftr seq = go (Seq.viewl seq) where go (EmptyL) = Seq.empty go (e : remain) = remain | e shiftl seq = go (Seq.viewr seq) where go (EmptyR) = Seq.empty go (remain : e) = e | remain Decomposing by elements like this is a bit unwieldy, but using the functions in Data.Traversable and Data.Foldable it shouldn't be too bad, depending on what you're doing. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Win32 help please
On 4 Feb 2007, at 17:59, Stefan O'Rear wrote: On Sun, Feb 04, 2007 at 10:42:23PM +1100, John Ky wrote: # hsc2hs mywin32.hsc # ghc -fffi mywin32.hs C:/system/ghc/ghc-6.6/libHSrts.a(Main.o):Main.c:(.text+0x1b): undefined reference to `__stginit_ZCMain' C:/system/ghc/ghc-6.6/libHSrts.a(Main.o):Main.c:(.text+0x3f): undefined reference to `ZCMain_main_closure' collect2: ld returned 1 exit status What am I missing? A function named main. s/function/constant/ In a module called Main. Bob ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] List operation question
On Sunday 04 February 2007 14:24, Nicolas Frisby wrote: I've always thought that when certain operations are of particular interest, it's time to use more appropriate data structures, right? Lists are great and simple and intuitive, but if you need such operations as shifts, something like a deque is the way to go. I'm not a data structure pro, but I'm sure someone on this list could post a neat example. Or you could look for work by Osaki - he seems to be the reference for functional data structures. Finger trees and tries also get a lot of attention around here. Also, take a look at Edison. It has a variety of sequence implementations with different properties. Several of them have efficient access to both ends of the sequence. http://www.eecs.tufts.edu/~rdocki01/edison.html Enjoy. On 2/4/07, Lennart Augustsson [EMAIL PROTECTED] wrote: Not much better. You could define shiftl such that is does a single traversal and returns both the last element and all but the last. That will save you one traversal. On Feb 4, 2007, at 18:44 , Eric Olander wrote: Hi, I'm still somewhat new to Haskell, so I'm wondering if there are better ways I could implement the following functions, especially shiftl: moves the first element to the end of the list shiftr :: [a] - [a] shiftr [] = [] shiftr (x:y) = y ++ [x] moves the last element to the head of the list shiftl :: [a] - [a] shiftl [] = [] shiftl x = [last x] ++ init x -Eric ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote: J. Garrett Morris wrote: Small improvement (Data.Maybe is underappreciated): exists str wmap = isJust exists' where exists' = do x - Map.lookup (sort str) wmap find (== str) (snd x) This is true. Some time ago I swore off the use of fromRight and fromLeft in favor of maybe, and have been forgetting about the other functions in Data.Maybe ever since. and maybe another improvement, though this is dependent on your tastes: exists s wmap = isJust $ Map.lookup (sort s) wmap = find (== s) . snd If you're going to write it all on one line, I prefer to keep things going the same direction: exists s wmap = isJust $ find (==s) . snd = Map.lookup (sort s) wmap Normally, from there I would be tempted to look for a points-free implementation, but in this case I have a strong suspicion that would simply be unreadable. /g -- It is myself I have never met, whose face is pasted on the underside of my mind. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
Hi This is true. Some time ago I swore off the use of fromRight and fromLeft in favor of maybe, and have been forgetting about the other functions in Data.Maybe ever since. I think you mean you swore off fromJust. Unfortunately when people started to debate adding fromLeft and fromRight they decided against logic and consistency, and chose not to add them... Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Generalizing three programs
Hi everyone, I've got an interesting problem here I'm trying to solve. Actually, I've got several problems which seem to have a very similar structure. I want to find a way to abstract them to solve other problems which can be thought about in the same way. Here they are: http://hpaste.org/307 http://hpaste.org/308 http://hpaste.org/309 Note that these are just sketches, the programs aren't done yet. The general structure of the problem is that an object enters a system, interacts with different parts of the system, and eventually leaves, and we want to monitor some statistics about the interaction, so that we can then make some changes, and run it again, and hopefully improve it. Thanks in advance for any suggestions! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] (no subject)
_ FREE online classifieds from Windows Live Expo buy and sell with people you know http://clk.atdmt.com/MSN/go/msnnkwex001001msn/direct/01/?href=http://expo.live.com?s_cid=Hotmail_tagline_12/06 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] mixing wxhaskell state and file io
martindemello: I'm having a lot of trouble mixing file io and wxhaskell's varCreate/Get/Set functions. I have functions readWords :: String - IO WordMap wordGrid :: WordMap - Layout And within my GUI code, the following compiles (ignores the variable, basically): words - varCreate (do {w - readWords words; return w}) wGrid - do w - readWords words return $ wordGrid w but I can't get the following (noncompiling code, but it shows what I'm trying to do) working: wGrid - do w - varGet words return $ wordGrid w Could someone give me a minimal example of reading in a list of words from a file, binding a wxHaskell variable to the list, and then mapping some GUI code over it? Here's an efficient read-words-from-file function: $ cat A.hs import qualified Data.ByteString.Char8 as S (words,readFile) import Data.ByteString (ByteString) getWords :: FilePath - IO [ByteString] getWords f = fmap S.words (S.readFile f) main = print . length = getWords /usr/share/dict/words $ time ./A 234979 ./A 0.09s user 0.01s system 94% cpu 0.107 total If you wanted to turn this list of words into a Map, something like: buildMap :: [ByteString] - M.Map ByteString Int buildMap xs = M.fromListWith (+) (zip xs (repeat 1)) (untested). As for wxHaskell, I'll have to defer to the ui hackers for that. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Strange behaviour with writeFile
cmb21: fo/haskell-cafe, mailto:[EMAIL PROTECTED] Errors-To: [EMAIL PROTECTED] Status: O Content-Length: 778 Lines: 27 Hi, I am observing some rather strange behaviour with writeFile. Say I have the following code: answer - AbstractIO.readFile filename let (answer2, remainder) = parseAnswer answer if remainder == answer2 == then do AbstractIO.putStrLn $ completed else do AbstractIO.putStrLn answer2 AbstractIO.writeFile filename remainder With the above I get an error saying the resources to filename are locked. If I add the line AbstractIO.putStrLn $ show (answer2, remainder) before I call writeFile it suddenly magically works! lazy IO at play. One quick fix would be to use strict IO: import qualified Data.ByteString.Char8 as S import Data.ByteString (ByteString) main = do ans - S.readFile t -- strict file IO print (S.length ans)-- current size let (x,xs) = S.splitAt 10 ans -- parse S.writeFile t xs ans' - S.readFile t print (S.length ans')-- new size $ time ./a.out 2487212 2487202 This comes up often enough that I think we should have a strict readFile for Strings -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
J. Garrett Morris wrote: On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote: exists s wmap = isJust $ Map.lookup (sort s) wmap = find (== s) . snd If you're going to write it all on one line, I prefer to keep things going the same direction: Hey, doing it this way saved me a full two keystrokes!!!1 Sure, you're right, everything flowing in the same direction is usually nicer, and in central Europe, that order is from the left to the right. What a shame that the Haskell gods chose to give the arguments to (.) and ($) the wrong order! exists s wmap = isJust $ find (==s) . snd = Map.lookup (sort s) wmap Normally, from there I would be tempted to look for a points-free implementation, but in this case I have a strong suspicion that would simply be unreadable. Well, depends on whether we are allowed to define new combinators. I sometimes use -- Kleisli composition infixl 1 @@ (@@) :: Monad m = (a - m b) - (b - m c) - (a - m c) f @@ g = join . liftM g . f and the resulting exists s = Map.lookup (sort s) @@ find (== s) . snd isJust isn't all that bad. (To be read as: one can get used to it.) I also think, (@@) and () belong in the Prelude and () at type ((a-b) - (b-c) - (b-c)) should be known under a shorter name. Unfortunately, everything short but (?) is already taken... Of course, the remaining variable s could also be transformed away, but that's really pointless. -Udo -- Never confuse motion with action. -- Ernest Hemingway signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
u.stenzel: J. Garrett Morris wrote: On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote: exists s wmap = isJust $ Map.lookup (sort s) wmap = find (== s) . snd If you're going to write it all on one line, I prefer to keep things going the same direction: Hey, doing it this way saved me a full two keystrokes!!!1 Sure, you're right, everything flowing in the same direction is usually nicer, and in central Europe, that order is from the left to the right. What a shame that the Haskell gods chose to give the arguments to (.) and ($) the wrong order! exists s wmap = isJust $ find (==s) . snd = Map.lookup (sort s) wmap Normally, from there I would be tempted to look for a points-free implementation, but in this case I have a strong suspicion that would simply be unreadable. Well, depends on whether we are allowed to define new combinators. I sometimes use -- Kleisli composition infixl 1 @@ (@@) :: Monad m = (a - m b) - (b - m c) - (a - m c) f @@ g = join . liftM g . f By the way, this is now in Control.Monad (in darcs). Though since we also want the flipped version, it becomes: -- | Left-to-right Kleisli composition of monads. (=) :: Monad m = (a - m b) - (b - m c) - (a - m c) f = g = \x - f x = g -- | Right-to-left Kleisli composition of monads. '(=)', with the arguments flipped (=) :: Monad m = (b - m c) - (a - m b) - (a - m c) (=) = flip (=) Cheers, Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] nested maybes
On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote: J. Garrett Morris wrote: On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote: Well, depends on whether we are allowed to define new combinators. I sometimes use -- Kleisli composition infixl 1 @@ (@@) :: Monad m = (a - m b) - (b - m c) - (a - m c) f @@ g = join . liftM g . f I was responding to this, but Dons beat me to it. Personally, I use this combinator quite a bit. (As much as I would rather use , the Kleisli arrow is a bit verbose to use for my taste.) and the resulting exists s = Map.lookup (sort s) @@ find (== s) . snd isJust isn't all that bad. (To be read as: one can get used to it.) I also think, (@@) and () belong in the Prelude and () at type ((a-b) - (b-c) - (b-c)) should be known under a shorter name. Unfortunately, everything short but (?) is already taken... Presumably you mean (a - b) - (b - c) - (a - c)? I would personally be fine with Arrows being in the prelude (and, for instance, (.) defined as flip ()). I'd support your shorter name idea if I could think of one... /g -- It is myself I have never met, whose face is pasted on the underside of my mind. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: nested maybes
Hi, I've often got the same pattern with nested Maybes but inside the IO monad (sure this could be every other monad too). Assuming that I've got functions: getInput :: IO (Maybe Input) processInput :: Input - IO (Maybe Result) printError :: IO () printResult :: Result - IO () I observed me writing something like main :: IO () main = do minput - getInput case minput of Nothing - printError Just input - do mresult - processInput input case mresult of Nothing - printError Just result - printResult result several times. But to my mind this looks very imperative and I hope it can be done more functional. If there is any way, please let me know. Regards, Martin. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: nested maybes
On 2/4/07, Martin Huschenbett [EMAIL PROTECTED] wrote: Hi, I've often got the same pattern with nested Maybes but inside the IO monad (sure this could be every other monad too). Assuming that I've got functions: This is where my favorite part of the mtl steps in: monad transformers. First, we'll create a transformed version of the IO monad, which encompasses the idea of failure. I've made the failures somewhat more general by allowing String typed error messages, but you can replace String with whatever type you'd like (including () if you really don't want any such information). newtype MyIO a = MyIO { runMyIO :: ErrorT String IO a } deriving (Functor, Monad, MonadError String) This uses GHC's newtype deriving mechanism, and thus requires -fglasgow-exts. The same effect can be achieved in Haskell 98 by using a type synonym instead of a newtype. Then, we need to have your operations produce their results in MyIO a instead of IO (Maybe a): getInput :: MyIO Input processInput :: Input - MyIO Result printError :: String - MyIO () printResult :: Result - MyIO () Finally, we can rewrite your main function without the case statements: main = runErrorT . runMyIO $ (do input - getInput result - processInput input printResult result) `catchError` printError However, in this case you don't really need do notation at all. You have a very nice pipeline of operations, and we can express it that way: main' = runErrorT . runMyIO $ (getInput = processInput = printResult) `catchError` printError which should remove the last vestiges of imperative-feeling code. /g -- It is myself I have never met, whose face is pasted on the underside of my mind. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] mixing wxhaskell state and file io
Martin DeMello said: I'm having a lot of trouble mixing file io and wxhaskell's varCreate/Get/Set functions. I have functions readWords :: String - IO WordMap wordGrid :: WordMap - Layout And within my GUI code, the following compiles (ignores the variable, basically): words - varCreate (do {w - readWords words; return w}) I'm not familiar with wxHaskell, but I don't think wxHaskell is your problem here. It looks like you are confusing yourself with overuse of do notation, and perhaps a lack of understanding of the monad laws. Whenever you see this: v - expr return v You can replace it with just expr. Your code above reduces to: words - varCreate (readWords words) So the variable you are creating has type (IO WordMap), when you probably wanted it to have type WordMap. By itself, this compiles, because IO actions are values as good as any other. The problem only manifests when you combine it with code that tries to read the variable as if it had type WordMap. This is probably what you wanted (untested): w - readWords words words - varCreate w Now, the variable has type WordMap. wGrid - do w - readWords words return $ wordGrid w but I can't get the following (noncompiling code, but it shows what I'm trying to do) working: wGrid - do w - varGet words return $ wordGrid w With the above change, this should now work, though it might be clearer to write this as: wGrid - liftM wordGrid $ varGet words Could someone give me a minimal example of reading in a list of words from a file, binding a wxHaskell variable to the list, and then mapping some GUI code over it? (Also, I'm making the base assumption that varSet and varGet are wxHaskell's analogue of the State monad - should I be looking at using StateT instead?) The wxHaskell Var type is not quite like the State monad. It is a type of mutable variable in the IO monad, much like the IORef type. Try to avoid gratuitous use of mutable variables, as they can weaken your ability to reason about your code. I'm not convinced you really need one here. What problem are you trying to solve by using a mutable variable? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Win32 help please
Hi, I tried as suggested: hsc2hs mywin32.hsc ghc -c -O -fffi mywin32.hs which allows me to use ghci. And if I add a main function, I can also do this: hsc2hs mywin32.hsc ghc -fffi mywin32.hs -package Win32 Thanks everyone for the help. -John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Connected!
Bulat Ziganshin wrote: Hello haskell-cafe, i've just got ADSL connection here! it's slow (64k) and not cheap, but at least it is completely different from dial-up i've used before That's great! I'm using GPRS, so you can imagine how painful it is :-) ps Ru = Добро пожаловать в Декларативное программирование: rsdn.ru/forum/?group=decl ps En = Welcome to Declarative programming (forum on Russian software developer network) ;-) Best regards, Nick. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] How is laziness defined?
I would think that with 100% laziness, nothing would happen until the Haskell program needed to output data to, e.g. the console. Quite obviously that's not it. So how is laziness defined in Haskell? I remember vaguely someone saying that pattern matching on a value forces it to be evaluated. Is that right? What else? This is one of the things that just boggles my mind everytime I try to wrap it around this thing called Haskell ;) Cheers, TJ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
G'day all. Quoting TJ [EMAIL PROTECTED]: I would think that with 100% laziness, nothing would happen until the Haskell program needed to output data to, e.g. the console. Quite obviously that's not it. So how is laziness defined in Haskell? It means that the program behaves as if things are evaluated if and only if they are needed. Needed in the Haskell sense, means needed to do I/O. It does not, of course, guarantee the order of evaluations, merely that the program acts as if it will only be evaluated if it's needed. It also doesn't guarantee that unneeded evaluation won't take place, it just means that if it does, it will happen in such a way that it won't destroy the illusion. Full laziness, which Haskell does not guarantee but does allow, goes one step further: A thing will be evaluated AT MOST once if it's ever needed. I remember vaguely someone saying that pattern matching on a value forces it to be evaluated. Is that right? What else? Remember that the pattern match code itself will only be evaluated if it needs to be for some other reason, which eventually boils down to I/O. (Note: We're assuming the absence of seq, which confuses everything.) This is one of the things that just boggles my mind everytime I try to wrap it around this thing called Haskell ;) The cool part is that for the most part, it doesn't matter. It just works. If you ever come across a case where it doesn't just work (e.g. if you need a tilde pattern), you'll be ready for it. Cheers, Andrew Bromage ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
I found it useful to work through an example where lazy evaluation was important, and wrote it up in a tutorial. It may or may not help you, no guarantees, but here it is: http://www.haskell.org/haskellwiki/Haskell/Lazy_Evaluation Any comments are welcome! Andrew On 2/4/07, TJ [EMAIL PROTECTED] wrote: I would think that with 100% laziness, nothing would happen until the Haskell program needed to output data to, e.g. the console. Quite obviously that's not it. So how is laziness defined in Haskell? I remember vaguely someone saying that pattern matching on a value forces it to be evaluated. Is that right? What else? This is one of the things that just boggles my mind everytime I try to wrap it around this thing called Haskell ;) Cheers, TJ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
On 2/5/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Quoting TJ [EMAIL PROTECTED]: I would think that with 100% laziness, nothing would happen until the Haskell program needed to output data to, e.g. the console. Quite obviously that's not it. So how is laziness defined in Haskell? It means that the program behaves as if things are evaluated if and only if they are needed. Needed in the Haskell sense, means needed to do I/O. So it's just IO which makes things run huh? OK that's basically what I said there. Cool. This is one of the things that just boggles my mind everytime I try to wrap it around this thing called Haskell ;) The cool part is that for the most part, it doesn't matter. It just works. If you ever come across a case where it doesn't just work (e.g. if you need a tilde pattern), you'll be ready for it. I despise using what I don't understand. It's a big problem but one which is more insurmountable than understanding Haskell, I think... On 2/5/07, Andrew Wagner [EMAIL PROTECTED] wrote: I found it useful to work through an example where lazy evaluation was important, and wrote it up in a tutorial. It may or may not help you, no guarantees, but here it is: http://www.haskell.org/haskellwiki/Haskell/Lazy_Evaluation With the code from your tutorial, magic :: Int - Int - [Int] magic 0 _ = [] magic m n = m : (magic n (m+n)) getIt :: [Int] - Int - Int getIt [] _ = 0 getIt (x:xs) 1 = x getIt (x:xs) n = getIt xs (n-1) and the example expression, getIt (magic 1 1) 3 It only gets run (starts pattern matching and all) if I do a print on it, or run it from GHCi (which will do theprint for me), right? Thanks, TJ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
I would think that with 100% laziness, nothing would happen until the Haskell program needed to output data to, e.g. the console. In many cases, that's exactly what it's like. Quite obviously that's not it. So how is laziness defined in Haskell? In fact, Haskell is not defined as lazy, it is defined as non-strict. From what I understand, non-strict semantics are (very informally) about choosing an order of evaluation that, where possible, avoids non-termination or error. Laziness is about evaluating only what's needed. In any case, I think all of the mainstream Haskell compilers and interpreters implement the non-strict semantics by means of lazy evaluation, so unless you're working on a radical new Haskell implementation, you probably don't need to worry about the difference. There's a wiki stub about this, though it doesn't say much more than what I've just said: http://haskell.org/haskellwiki/Lazy_vs._non-strict I remember vaguely someone saying that pattern matching on a value forces it to be evaluated. Is that right? What else? Yes, but you need to be more precise about when this pattern matching occurs, what you mean by it, and the extent to which it is evaluated. See below about weak head normal form. This is one of the things that just boggles my mind everytime I try to wrap it around this thing called Haskell ;) If it's any comfort, you're not alone. It takes some discipline to forget your previous notions of computation, and to start thinking lazily. Get familiar with the notion of the thunk: a record which the implementation stores on the heap in place of the result of some computation which has not yet been performed. A thunk typically records a reference to a function and some arguments, but remember that the arguments (and indeed, the function) in all likelihood haven't been evaluated yet, so they might well be thunks too. A thunk sits on the heap until either the garbage collector decides it's no longer needed (in which case the computation is never performed), or until the implementation finds it needs the value (or part of it). Also get familiar with weak head normal form (WHNF). I'm not 100% on this, so hopefully someone will correct me if I'm wrong. Let's say the implementation gets to a point where it needs to perform a pattern match on a value. So far, the value is just an unevaluated thunk on the heap. To perform the pattern match, the implementation needs to evaluate that thunk to its top-level data constructor. It doesn't need to go any further than that just yet (unless the pattern also includes a sub-pattern inside the top-level data constructor). So a value that is evaluated to its top-level data constructor is said to be in weak head normal form. The result of the pattern match (aside from allowing the implementation to choose between alternative patterns based on the data constructor) is typically that one or more variables are bound to values inside the data constructor. Of course, those values are most likely unevaluated thunks, at least until further pattern matching is necessary. Hopefully, that's clear enough to be of some use. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
tjay.dreaming: On 2/5/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Quoting TJ [EMAIL PROTECTED]: I would think that with 100% laziness, nothing would happen until the Haskell program needed to output data to, e.g. the console. Quite obviously that's not it. So how is laziness defined in Haskell? It means that the program behaves as if things are evaluated if and only if they are needed. Needed in the Haskell sense, means needed to do I/O. So it's just IO which makes things run huh? OK that's basically what I said there. Cool. Exactly, no IO, no computation required: $ cat A.hs main = do let v = last [1..] -- could be very slow... return () $ time ./a.out ./a.out 0.00s user 0.00s system 0% cpu 0.003 total :) Laziness: you know it makes sense. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
G'day all. tjay.dreaming: So it's just IO which makes things run huh? OK that's basically what I said there. Cool. Yeah, but you said output. Sending a signal to another process in Unix is I/O, which would force the process id to be evaluated, but there's no output as such. Cheers, Andrew Bromage ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
I went through the entry on laziness on the wikipedia wikibook. Very nice. The wikibook sure has grown a lot since I last visited. http://en.wikibooks.org/wiki/Haskell/Laziness I believe I've got it now. By it I mean the understanding of laziness in Haskell. Even though Haskell is, strictly speaking, not lazy, but non-strict. It being but read and thought about, and not practiced, might prove _itself_ to become Undefined as I evaluate it further. :D Cheers, TJ On 2/5/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: G'day all. tjay.dreaming: So it's just IO which makes things run huh? OK that's basically what I said there. Cool. Yeah, but you said output. Sending a signal to another process in Unix is I/O, which would force the process id to be evaluated, but there's no output as such. Cheers, Andrew Bromage ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How is laziness defined?
TJ said: I went through the entry on laziness on the wikipedia wikibook. Very nice. The wikibook sure has grown a lot since I last visited. http://en.wikibooks.org/wiki/Haskell/Laziness Thanks for the link. I hadn't seen that before. Although it covers irrefutable (lazy) pattern matching in the second section, it does appear to miss the point that let bindings are always irrefutable. Thus, there is no difference between these two: let (x,y) = foo in ... let ~(x,y) = foo in ... I need to have a closer look when I have some more time, but I think this might have lead to some inaccuracies in the first section (Thunks and WHNF). FYI, the Haskell 98 Report defines the non-recursive let binding in terms of a case expression with an irrefutable pattern match. So, the following are equivalent: let (x,y) = foo in ... case foo of ~(x,y) - ... But note that function arguments are defined in terms of case expressions (whose patterns are refutable), so the following are *not* equivalent (example from the wikibook): let f (x,y) = 1 in f undefined let f ~(x,y) = 1 in f undefined Confused again? I believe I've got it now. By it I mean the understanding of laziness in Haskell. Even though Haskell is, strictly speaking, not lazy, but non-strict. It being but read and thought about, and not practiced, might prove _itself_ to become Undefined as I evaluate it further. :D Nicely put. I think you are getting it. :-) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] mixing wxhaskell state and file io
On 2/5/07, Matthew Brecknell [EMAIL PROTECTED] wrote: I'm not familiar with wxHaskell, but I don't think wxHaskell is your problem here. It looks like you are confusing yourself with overuse of do notation, and perhaps a lack of understanding of the monad laws. Whenever you see this: v - expr return v You can replace it with just expr. Your code above reduces to: words - varCreate (readWords words) Okay - was confused about that. Thanks for the tip. So the variable you are creating has type (IO WordMap), when you probably wanted it to have type WordMap. By itself, this compiles, because IO actions are values as good as any other. The problem only manifests when you combine it with code that tries to read the variable as if it had type WordMap. This is probably what you wanted (untested): w - readWords words words - varCreate w That didn't work, because (as someone on #haskell explained to me) readWords has type IO and the wx actions have type Layout The wxHaskell Var type is not quite like the State monad. It is a type of mutable variable in the IO monad, much like the IORef type. Try to avoid gratuitous use of mutable variables, as they can weaken your ability to reason about your code. I'm not convinced you really need one here. What problem are you trying to solve by using a mutable variable? You're right, I don't need mutable variables, just some sort of state. I'm writing a game which presents the user with a list of anagrams (read in from a file) and an input box in which to enter guesses. The input box's on command is (pseudocode) a - get input text if exists a wordmap remove a wordmap update display else say wrong So what I need is for the gui code to have a single wordmap threaded through it. I thought I could do that by reading in the wordmap within the gui do sequence and binding it to a variable. martin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe