Re: [Haskell-cafe] Why is it so different between 6.12.1 and 6.10.4_1 ?
Hmm, When a ghci was started, there should be the only loaded module (Prelude.) And in both 6.10 and 6.12, such instance is not defined or exported in its Prelude. So please try `ghci -ignore-dot-ghci`. It invokes ghci without reading ~/.ghci and ./.ghci. And `((->) a)` is known as the Reader Monad, `a` can be regarded as the environment. My typical usage of that is like following: import Control.Monad data Vec = Vec { x :: Int, y :: Int } absolute :: Vec -> Double absolute = sqrt . fromIntegral . liftM2 (+) (square . x) (square . y) where square a = a * a The definition of `absolute` above can be rewritten as absolute p = sqrt . fromIntegral $ square (x p) + square (y p) where square a = a * a How `square . x` and `square . y` share the argument? Because `Monad ((->) a)` is defined as instance Monad ((->) a) where return x = \a -> x m >>= f = \a -> f (m a) a Note `(>>=)` propagates `a` into both of its arguments. That's why the functions read same argument. HTH -nwn On Sat, Mar 27, 2010 at 3:31 PM, zaxis wrote: > > I just start ghci from shell and do nothing else. In fact, i really donot > know `Monad ((->) a) ` . Would you mind expplain it ? > > > Yusaku Hashimoto wrote: >> >> Did you import the module includes the instance of Monad ((->) e) >> somewhere in your code loaded in ghci? >> >> I tried this on a fresh ghci 6.12, but I got "No instance" error. >> >> -nwn >> >> On Sat, Mar 27, 2010 at 9:20 AM, zaxis wrote: >>> >>> In 6.12.1 under archlinux >>>>let f x y z = x + y + z >>>> :t f >>> f :: (Num a) => a -> a -> a -> a >>> >>>> :t (>>=) . f >>> (>>=) . f :: (Num a) => a -> ((a -> a) -> a -> b) -> a -> b >>>> ((>>=) . f) 1 (\f x -> f x) 2 >>> 5 >>> >>> In 6.10.4_1 under freebsd >>>> let f x y z = x + y + z >>> *Money> :t f >>> f :: (Num a) => a -> a -> a -> a >>> >>>> :t (>>=) . f >>> (>>=) . f :: (Monad ((->) a), Num a) => a -> ((a -> a) -> a -> b) -> a >>> -> b >>>> ((>>=) . f) 1 (\f x -> f x) 2 >>> >>> :1:1: >>> No instance for (Monad ((->) a)) >>> arising from a use of `>>=' at :1:1-5 >>> Possible fix: add an instance declaration for (Monad ((->) a)) >>> In the first argument of `(.)', namely `(>>=)' >>> In the expression: ((>>=) . f) 1 (\ f x -> f x) 2 >>> In the definition of `it': it = ((>>=) . f) 1 (\ f x -> f x) 2 >>> >>> Sincerely! >>> >>> >>> - >>> fac n = let { f = foldr (*) 1 [1..n] } in f >>> -- >>> View this message in context: >>> http://old.nabble.com/Why-is-it-so-different-between-6.12.1-and-6.10.4_1---tp28049329p28049329.html >>> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. >>> >>> ___ >>> 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 >> >> > > > - > fac n = let { f = foldr (*) 1 [1..n] } in f > -- > View this message in context: > http://old.nabble.com/Why-is-it-so-different-between-6.12.1-and-6.10.4_1---tp28049329p28050535.html > Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. > > ___ > 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] Why is it so different between 6.12.1 and 6.10.4_1 ?
>> fac n = let { f = foldr (*) 1 [1..n] } in f > > Why do you bother with the interior definition of f in there? > > fac = product . enumFromTo 1 let fac = do is_zero <- (==0); if is_zero then return 1 else liftM2 (*) id (fac . pred) -nwn On Sat, Mar 27, 2010 at 9:59 AM, Ivan Lazar Miljenovic wrote: > zaxis writes: >> In 6.10.4_1 under freebsd >>> let f x y z = x + y + z >> *Money> :t f >> f :: (Num a) => a -> a -> a -> a >> >>> :t (>>=) . f >> (>>=) . f :: (Monad ((->) a), Num a) => a -> ((a -> a) -> a -> b) -> a -> b >>> ((>>=) . f) 1 (\f x -> f x) 2 >> >> :1:1: >> No instance for (Monad ((->) a)) >> arising from a use of `>>=' at :1:1-5 >> Possible fix: add an instance declaration for (Monad ((->) a)) >> In the first argument of `(.)', namely `(>>=)' >> In the expression: ((>>=) . f) 1 (\ f x -> f x) 2 >> In the definition of `it': it = ((>>=) . f) 1 (\ f x -> f x) 2 >> > > Some definitions and exports got changed, so in 6.12 the (-> a) Monad > instance is exported whereas in 6.10 it isn't. > >> fac n = let { f = foldr (*) 1 [1..n] } in f > > Why do you bother with the interior definition of f in there? > > fac = product . enumFromTo 1 > > -- > Ivan Lazar Miljenovic > ivan.miljeno...@gmail.com > IvanMiljenovic.wordpress.com > ___ > 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] Why is it so different between 6.12.1 and 6.10.4_1 ?
Did you import the module includes the instance of Monad ((->) e) somewhere in your code loaded in ghci? I tried this on a fresh ghci 6.12, but I got "No instance" error. -nwn On Sat, Mar 27, 2010 at 9:20 AM, zaxis wrote: > > In 6.12.1 under archlinux >>let f x y z = x + y + z >> :t f > f :: (Num a) => a -> a -> a -> a > >> :t (>>=) . f > (>>=) . f :: (Num a) => a -> ((a -> a) -> a -> b) -> a -> b >> ((>>=) . f) 1 (\f x -> f x) 2 > 5 > > In 6.10.4_1 under freebsd >> let f x y z = x + y + z > *Money> :t f > f :: (Num a) => a -> a -> a -> a > >> :t (>>=) . f > (>>=) . f :: (Monad ((->) a), Num a) => a -> ((a -> a) -> a -> b) -> a -> b >> ((>>=) . f) 1 (\f x -> f x) 2 > > :1:1: > No instance for (Monad ((->) a)) > arising from a use of `>>=' at :1:1-5 > Possible fix: add an instance declaration for (Monad ((->) a)) > In the first argument of `(.)', namely `(>>=)' > In the expression: ((>>=) . f) 1 (\ f x -> f x) 2 > In the definition of `it': it = ((>>=) . f) 1 (\ f x -> f x) 2 > > Sincerely! > > > - > fac n = let { f = foldr (*) 1 [1..n] } in f > -- > View this message in context: > http://old.nabble.com/Why-is-it-so-different-between-6.12.1-and-6.10.4_1---tp28049329p28049329.html > Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. > > ___ > 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] Re: Books for "advanced" Haskell
On Sun, Mar 7, 2010 at 10:53 PM, Stephen Tetley wrote: > Hi All > > What is the state-of-the-practice in type-level programming? > > I know Günther started this thread about monads, but I seem to > remember him having a long running problem with "typeful" database > programming, and I wonder if some of his problems are really in the > later area. Compared to monads, type-level programming seems much more > a wild frontier - scattered docs, fewer definitive examples, no > de-facto standard library. So I want the book describes how to make type-level programming up to the practice. I think this would cover how to provide simple interface for types and contexts with full benefits, how to produce friendly error message in type-errors, when to use UndecidableInstances, and how to get rid of such itchy things. -nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Two GET HTTP requests
Try to reinstall HTTP package also. I think your HTTP package is still linked with old broken network package. HTTP depends on network. And network is a binding for network API of OS. These API is for C-language. When ghc builds such binding packages, It runs gcc for some purpose. gcc thinks you need 64bit binary (from SL, I believe.) and works for 64bit environment. But ghc on Mac can only build 32bit binaries. So it causes the problem. You can check if network package was correctly built by running this. This takes a host name, and gets the root document of the host via HTTP using a socket. Build and try `./this_program haskell.org` import Network.Socket import System.IO import System.Environment getAddr :: HostName -> IO AddrInfo getAddr host = head `fmap` (getAddrInfo (Just defaultHints { addrSocketType = Stream }) (Just host) (Just "http")) connected :: HostName -> IO Socket connected host = do addrinfo <- getAddr host sock <- socket (addrFamily addrinfo) (addrSocketType addrinfo) (addrProtocol addrinfo) connect sock (addrAddress addrinfo) return sock httpGet :: HostName -> IO String httpGet host = do h <- flip socketToHandle ReadWriteMode =<< connected host hSetBuffering h NoBuffering hPutStr h "GET / HTTP/1.0\r\n\r\n" hGetContents h main = fmap head getArgs >>= httpGet >>= putStr I should have mentioned them in my last mail. Sorry. By the way, ghc-6.12 on Mac still can not build 64bit binaries. So upgrading ghc won't solve it. --nwn On Mon, Feb 8, 2010 at 12:50 AM, Chris Eidhof wrote: > Thanks. Unfortunately, it didn't help. The thing that frustrates me is that > it's quite hard to debug. I guess I'll upgrade my GHC to 6.12, hopefully > that'll solve it. > > -chris > > On 7 feb 2010, at 16:07, Yusaku Hashimoto wrote: > >> Hello, >> >> On Sat, Feb 6, 2010 at 2:51 AM, Chris Eidhof wrote: >>> Approach 3: I used the simpleHTTP function from the HTTP package. This >>> crashed, after I dug a little deeper into the code, it threw an error on >>> calling the parseURI function (openFile: no such file exists). I installed >>> the latest network package and upgraded my HTTP package, and the parseURI >>> error went away. I felt like I was almost there, and tried the following: >>> >>>> simpleHTTP (getRequest "http://haskell.org";) >>> >>> This failed with just the text "Bus error". I searched the HTTPBis git >>> repository, but couldn't find the text "Bus error". I don't have a clue of >>> how to fix this. >> >> Try reinstall network package with `cabal install --reinstall >> --hsc2hs-options="--cflag=-m32 --lflag=-m32"`. >> >> See also: http://hackage.haskell.org/trac/ghc/ticket/3681 >> >> Hope this helps. >> --nwn > > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Two GET HTTP requests
Hello, On Sat, Feb 6, 2010 at 2:51 AM, Chris Eidhof wrote: > Approach 3: I used the simpleHTTP function from the HTTP package. This > crashed, after I dug a little deeper into the code, it threw an error on > calling the parseURI function (openFile: no such file exists). I installed > the latest network package and upgraded my HTTP package, and the parseURI > error went away. I felt like I was almost there, and tried the following: > >> simpleHTTP (getRequest "http://haskell.org";) > > This failed with just the text "Bus error". I searched the HTTPBis git > repository, but couldn't find the text "Bus error". I don't have a clue of > how to fix this. Try reinstall network package with `cabal install --reinstall --hsc2hs-options="--cflag=-m32 --lflag=-m32"`. See also: http://hackage.haskell.org/trac/ghc/ticket/3681 Hope this helps. --nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] poor perfomance of indexU in uvector package
>> Also, I would recomend using criterion. > > I tried to do so.. But it depends on gtk2hs and it is too difficult > to install You can install with the flag to skip gtk2hs installation. i.e. Try `cabal install criterion -f-chart` -~nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Applicative but not Monad
> Some parsers are not monads, allowing for optimizations. Could you elaborate on them or give me some pointers? I think I heard about it two or three times, but I couldn't find any resource of them. -~nwn On Sat, Oct 31, 2009 at 5:35 AM, Martijn van Steenbergen wrote: > Yusaku Hashimoto wrote: >> >> Hello cafe, >> Do you know any data-type which is Applicative but not Monad? > > The Except datatype defined in the Applicative paper. > > Some parsers are not monads, allowing for optimizations. > > Martijn. > > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Applicative but not Monad
Thank you for your correction. I tried your (>>=) and replaced return's definition with return = ZipList . repeat then as you said this works fine for infinite lists. Cheers, -~nwn On Sat, Oct 31, 2009 at 2:39 AM, David Menendez wrote: > On Fri, Oct 30, 2009 at 1:33 PM, Yusaku Hashimoto wrote: >> Thanks for fast replies! Examples you gave explain why all >> Applicatives are not Monads to me. >> >> And I tried to rewrite Bob's Monad instance for ZipList with (>>=). >> >> import Control.Applicative >> >> instance Monad ZipList where >> return = ZipList . return >> (ZipList []) >>= _ = ZipList [] >> (ZipList (a:as)) >>= f = zlHead (f a) `zlCons` (ZipList as >>= f) > > This is taking the first element of each list, but you need to take > the nth element. Try > > (ZipList (a:as)) >>= f = zlHead (f a) `zlCons` (ZipList as >>= zlTail . f) > > > -- > Dave Menendez > <http://www.eyrie.org/~zednenem/> > ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Applicative but not Monad
Thanks for fast replies! Examples you gave explain why all Applicatives are not Monads to me. And I tried to rewrite Bob's Monad instance for ZipList with (>>=). import Control.Applicative instance Monad ZipList where return = ZipList . return (ZipList []) >>= _ = ZipList [] (ZipList (a:as)) >>= f = zlHead (f a) `zlCons` (ZipList as >>= f) zlHead :: ZipList a -> a zlHead (ZipList (a:_)) = a zlCons :: a -> ZipList a -> ZipList a zlCons a (ZipList as) = ZipList $ a:as zlTail :: ZipList a -> ZipList a zlTail (ZipList (_:as)) = ZipList as I understand if this instance satisfies the laws, we can replace <$> with `liftM` and <*> and `ap`. And I found a counterexample (correct me if I'm wrong). *Main Control.Monad> getZipList $ (*) <$> ZipList [1,2] <*> ZipList [3,4,5] [3,8] *Main Control.Monad> getZipList $ (*) `liftM` ZipList [1,2] `ap` ZipList [3,4,5] [3,6] Cheers, -~nwn On Sat, Oct 31, 2009 at 2:06 AM, Tom Davie wrote: > On Fri, Oct 30, 2009 at 5:59 PM, Luke Palmer wrote: >> >> On Fri, Oct 30, 2009 at 10:39 AM, Tom Davie wrote: >> > Of note, there is a sensible monad instance for zip lists which I >> > *think* >> > agrees with the Applicative one, I don't know why they're not monads: >> > instance Monad (ZipList a) where >> > return = Ziplist . return >> > join (ZipList []) = ZipList [] >> > join (ZipList (a:as)) = zlHead a `zlCons` join (map zlTail as) >> >> IIRC, that doesn't satisfy the associativity law, particularly when >> you are joining a list of lists of different lengths. 2 minutes of >> experimenting failed to find me the counterexample though. > > Cool, thanks Luke, that explains why this is available in Stream, but not in > ZipList too. > Bob > ___ > 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] Applicative but not Monad
Hello cafe, Do you know any data-type which is Applicative but not Monad? Cheers, -~nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How do I uninstall GHC on a Mac running Snow Leopard?
Invoke uninstaller with sudo from Terminal as: sudo Library/Frameworks/GHC.framework/Tools/Uninstaller HTH -nwn 2009/10/24 R J : > What's the cleanest way to fully uninstall GHC on a Mac running Snow > Leopard? > Running Library/Frameworks/GHC.framework/Tools/Uninstaller from an account > with admin privileges produced the following, surprising error message: >> GHC.framework installer must be run with admin privileges >> Prefix command by 'sudo' >> logout >> [Process completed] > Thanks as always. > > Windows 7: Simplify your PC. Learn more. > ___ > 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] Real World Haskell Chapter 19 and GHC 6.10
To deal with "amigous type variable 'e'", I often write the codes like: handle (\...@someexception{} -> print e) (5 `div` 0) and IIRC, the base-4.0 initially released with GHC 6.10.1, introduced this exceptions. It enables us to specify which exception should be caught and define types of exceptions what you want. And I hear this is based on the paper http://www.haskell.org/~simonmar/papers/ext-exceptions.pdf On Wed, Oct 21, 2009 at 6:29 PM, Michael Mossey wrote: > The examples in the "error handling" chapter (19) of RWH don't run under GHC > 6.10. > > For instance, an example might be > > main = handle (\_ -> putStrLn "error") (print $ 5 `div` 0) > > Trying to load this results in "amigous type variable 'e' in the constraint: > 'Exception e' arising from a use of 'handle' etc etc. > > I was able to fix this via the ludicrously complex: > > main2 = (handle :: (SomeException -> IO ()) -> IO () -> IO ()) > (\_ -> putStrLn "Error calculating result") > (print $ 5 `div` 0) > > Is there a more concise way to use "handle", or can someone point me to a > tutorial that might explain the changes in 6.10 and in a general way how to > get the RWH examples to run? > > Thanks, > Mike > ___ > 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] QuickCheck Questions
After a few more investigations, I can say QuickCheck does: - make easy to finding couter-cases and refactoring codes - make easy to test some functions if they have good mathematical properties - generate random test cases But QuickCheck does *not*: - help us to find good properties So what I want to know is "how to find good properties." Please let me know how do you find QuickCheck properties. There are so many tutorials or papers for using QuickCheck, but when I try to apply them to my programming, I often miss properties in my codes. Cheers -nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] QuickCheck Questions
On Mon, Sep 28, 2009 at 4:42 AM, Gwern Branwen wrote: > On Sun, Sep 27, 2009 at 3:19 PM, Yusaku Hashimoto > wrote: > ... >> >> Do you think I wasted times? Have you ever tried PDD? And has it >> worked? If you have experience with TDD, how do you think about PDD? >> >> If you have any answers in any questions above, please tell me them. >> Thanks in advance. >> >> Cheers >> -nwn > > Here are some links from my Wikipedia article on QC which tout it: Thanks for pointers. But I feel curious about in many QC examples (especially RWH's in your pointers), if property is falsifiable, they changes definition of the property, not implementation. And it annoys me because it seemed to almost duplicate its implementation. Am I misunderstanding? Cheers -nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] QuickCheck Questions
Hello, I recently worked with QuickCheck for a while, But I still can't handle it well, And a few questions come to my mind. 1. How to find properties In QuickCheck examples on the codes or the papers, they find good properties easily. How did they find these properties? What property can make us believed its implementation is collect? 2. Property driven developments But finally, I could find some patterns of the properties. For example, occasionally, their shape is like unF . f == id or unF . f == Just I thought it could be applied to writing parser, and lead me to "Property Driven Developments." I tried writing S-expression parser in this way. First, I defined my property as parseSexpr . prettySexpr == Just where parseSexpr :: String -> Maybe Sexpr, prettySexpr :: Sexpr -> String. As you see, I should write printer before parser. Fortunately writing such printer for S-expr is easy, but I don't know if what I did is right. Do you think I wasted times? Have you ever tried PDD? And has it worked? If you have experience with TDD, how do you think about PDD? If you have any answers in any questions above, please tell me them. Thanks in advance. Cheers -nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Averting QuickCheck Madness
I think using the runTests hook and the test flag make sense, described at http://www.haskell.org/pipermail/haskell-cafe/2008-September/047223.html. I released some libraries in this way, AFAIK it works well. On Sun, Sep 6, 2009 at 4:57 PM, Christopher Lane Hinson wrote: > > There are some libraries that depend on QuickCheck 2, and others that depend > on QuickCheck 1. This can be a problem. AIUI, the Haskell Platform current > depends on QC1, but intends to move to QC2 soon. I also know that the cabal > mailing list has talked about some kind of private-depends capability to > mitigate this kind of thing in the future. > > However, I don't see how it can possibly be a best practice to depend on > QuickCheck from a shipping library. End users never use this, and for users > of upstream packages who may compile from source and contribute the > occasional (but valuable!) patch, this is nothing but a compile-time problem > waiting to happen. > > (Note that a user who contributes to a library should be encouraged to run > test suites, but I'm talking about users of upstream packages.) > > There are some good ideas discussed on this list last year*, but none of > them seem to have been blessed by the community. > > * http://www.haskell.org/pipermail/haskell-cafe/2008-September/047216.html > > Friendly, > --Lane > ___ > 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] ANN: tkhs-0.1.* Presentation Utility
Hi, I'm pleased to announce the release of tkhs-0.1.*, Simple presentation utility. If you are thinking PowerPoint is overkill for your presentation, Tkhs may fit the purpose. See screenshot of running tkhs in my terminal: http://nonowarn.tumblr.com/post/152324109 When you invoke tkhs with slide file written in simple format text, tkhs displays the first slide. And you can operate a presentation by some keys. Details like slide file format and keybinds for operating slide are available at README[1] file included by the package. Project is hosted at patch-tag[2], Feel free to send me patch. Finally, I'm very grateful Evan Klizke and Brian Lewis. Evan is the author of hspresent[3] which inspired me to write tkhs. Brian sent me patch improving English. [1]: http://patch-tag.com/r/tkhs/snapshot/current/content/pretty/README [2]: http://patch-tag.com/r/tkhs/pullrepo [3]: http://hackage.haskell.org/package/hspresent Thanks, nwn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Convert IO Int to Int
On 2009/06/09, at 19:33, Tobias Olausson wrote: You can not convert an IO Int to Int, or at least, you shouldn't. However, you can do as follows: test :: IO () test = do int <- randomRIO -- or whatever it is called print $ useInt int useInt :: Int -> Int useInt x = x+10 Or, you can lift pure function into IO. the below test' function almost same as above test function. (But I used randomIO instead of randomRIO because it seemed to be a typo :-) test' = print =<< fmap useInt randomIO I think it is more handy than using do notation, when you want to do something simple with monads. And converting IO Int to IO anything is much easier and safer than converting IO Int to Int. ghci> :m +System.Random Data.Char ghci> :t fmap (+1) randomIO fmap (+1) randomIO :: (Num a, Random a) => IO a ghci> :t fmap show randomIO fmap show randomIO :: IO String ghci> :t fmap chr randomIO fmap Data.Char.chr randomIO :: IO Char ghci> :t fmap (+) randomIO fmap (+) randomIO :: (Num a, Random a) => IO (a -> a) Thanks, Hashimoto //Tobias 2009/6/9 ptrash : Hi, I am using the System.Random method randomRIO. How can I convert its output to an Int? Thanks... -- View this message in context: http://www.nabble.com/Convert-IO-Int- to-Int-tp23940249p23940249.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Tobias Olausson tob...@gmail.com ___ 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] No VerboseCheck in QuickCheck 2?
Hi, I don't think I am familiar enough with QuickCheck 2. But there seems to be no verboseCheck like function, and sample and sample' is useful to printing test cases. ghci> sample (arbitrary :: Gen Int) 1 0 1 -2 -2 -5 -16 -9 -57 -115 -94 ghci> sample (arbitrary :: Gen (Int,Int)) (0,1) (-1,-1) (1,2) (3,-4) (-4,5) (11,-9) (-2,8) (18,17) (-63,-32) (-117,-71) (195,-198) ghci> f <- fmap head $ sample' (arbitrary :: Gen (Int -> Maybe Int)) ghci> map f [1..10] [Nothing,Just (-1),Just 1,Nothing,Nothing,Just 1,Nothing,Nothing,Just 1,Just 1] Thanks, Hashimoto On 2009/05/26, at 11:54, Shin-Cheng Mu wrote: Hi, I believe this must have been raised before but I did some searching to no avail, so please allow me.. It seems that verboseCheck is gone in QuickCheck 2. In QuickCheck 2, how do we print the test cases, specify the number of tests, or the sizes of test cases, etc.? Thank you very much.. sincerely, Shin ___ 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 to define a common return and bind?
On 2009/04/10, at 20:32, Bas van Dijk wrote: So I rewrote it using type families which I find much easier to understand: Cool. Type family version is easier to read and understand for me. This seems useful. Does this exists somewhere on hackage? I glanced at Monad category in package list, It seemed that There is no package like this. I hope you to upload your package! Thanks, Hashimoto ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to define a common return and bind?
Now I'm wondering if the derive_* functions can be overloaded using something like this. Note that the following doesn't typecheck: {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE FlexibleInstances #-} class Iso m n | m -> n, n -> m where close :: forall a. m a -> n a open :: forall a. n a -> m a deriveReturn :: (Monad m, Monad n, Iso m n) => a -> n a deriveReturn = close . return deriveBind :: (Monad m, Iso m n) => n a -> (a -> n b) -> n b deriveBind m k = close $ open m >>= open . k newtype T1 m a = T1 { unT1 :: A1 m a } type A1 m a = m a instance Iso m (T1 m) where close = T1 open = unT1 instance Monad m => Monad (T1 m) where return = deriveReturn (>>=) = deriveBind Hi, I changed a line, It type checks. But I can't explain why your version does not type check. --- iso_orig.hs 2009-04-10 17:56:12.0 +0900 +++ iso.hs 2009-04-10 17:56:36.0 +0900 @@ -5,7 +5,7 @@ -class Iso m n | m -> n, n -> m where +class Iso m n | n -> m where close :: forall a. m a -> n a open :: forall a. n a -> m a Thanks, Hashimoto ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Generating arbitrary function in QuickCheck
Thanks for all replies! these gave me many enlightenments. I have been looking in about free theorem for a while, and I want to try to describe what I learned, and please collect me where I am wrong or misunderstood. Generating random functions for sortBy is meaningless, because, for testing sortBy, casual comparison function is enough to it. The reason of this comes from free theorem. By the free theorem, sortBy should satisfy this theorem (copied from ftshell). forall t1,t2 in TYPES, f :: t1 -> t2. forall p :: t1 -> t1 -> Ordering. forall q :: t2 -> t2 -> Ordering. (forall x :: t1. p x = q (f x) . f) ==> (map f . sortBy p = sortBy q . map f) Any linear orderings can be defined by mapping value to Integer. And above theorem says comparing functions in the theorem must be homomorphic to each other. Hence comparing function is meaningful only when it is homomorhpic to Integer's that. (I have not tried to Generate such function yet, and it may be unnecessary, but) taking such function is easy. Furthermore, it should be satisfied by all functions that have same type signitures to sortBy, because it is implied from only its own type. Reference: - [Theorem for free!](http://citeseerx.ist.psu.edu/viewdoc/summary? doi=10.1.1.38.9875) - [ftshell](http://hackage.haskell.org/cgi-bin/hackage-scripts/ package/ftshell-0.3) Thanks, Yusaku Hashimoto On 2009/04/07, at 15:19, Janis Voigtlaender wrote: Jason Dagit wrote: On Mon, Apr 6, 2009 at 10:09 PM, Eugene Kirpichov wrote: Since the argument to sortBy must impose a linear ordering on its arguments, and any linear ordering may as well be generated by assigning an integer to each element of type 'a', and your sorting function is polymorphic, from the free theorem for the sorting function we may deduce that it suffices to test your function on integer lists with a casual comparison function (Data.Ord.compare), and there is no need to generate a random comparison function. Interesting. How is this free theorem stated for the sorting function? Intuitively I understand that if the type is polymorphic, then it seems reasonable to just pick one type and go with it. You can try free theorems here: http://linux.tcs.inf.tu-dresden.de/~voigt/ft/ For example, for sort :: Ord a => [a] -> [a] it generates the following: forall t1,t2 in TYPES(Ord), f :: t1 -> t2, f respects Ord. forall x :: [t1]. map f (sort x) = sort (map f x) where: f respects Ord if f respects Eq and forall x :: t1. forall y :: t1. compare x y = compare (f x) (f y) forall x :: t1. forall y :: t1. (<) x y = (<) (f x) (f y) forall x :: t1. forall y :: t1. (<=) x y = (<=) (f x) (f y) forall x :: t1. forall y :: t1. (>) x y = (>) (f x) (f y) forall x :: t1. forall y :: t1. (>=) x y = (>=) (f x) (f y) f respects Eq if forall x :: t1. forall y :: t1. (==) x y = (==) (f x) (f y) forall x :: t1. forall y :: t1. (/=) x y = (/=) (f x) (f y) Assuming that all the comparison functions relate to each other in the mathematically sensible way, the latter can be reduced to: f respects Ord if forall x :: t1. forall y :: t1. (x <= y) = (f x <= f y) For sortBy you would get a similar free theorem. To see how the free theorem allows you to switch from an arbitrary type to just integers, set t2=Int and simply use f to build a order-preserving bijection between elements in the list x and a prefix of [1,2,3,4,...] Ciao, Janis. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:vo...@tcs.inf.tu-dresden.de ___ 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] Generating arbitrary function in QuickCheck
Hi, today, I want to ask about QuickCheck. I have known recently that arbitrary works on functions as well. I played on this for a while, and met a strange behavior. I tried to write property for sortBy. import Test.QuickCheck import Data.List instance Show (a -> b) where show _ = "<>" instance Arbitrary Ordering where arbitrary = elements [LT,EQ,GT] mySortBy :: (Ord a) => (a -> a -> Ordering) -> [a] -> [a] mySortBy = sortBy prop_mysortby_sorted :: (Int -> Int -> Ordering) -> [Int] -> Property prop_mysortby_sorted cmp ls = null ls `trivial` all eq_or_lt (zipWith cmp sorted (tail sorted)) where sorted = mySortBy cmp ls eq_or_lt ord = ord == EQ || ord == LT I had thought Arbitrary instance for Ord and property for sortBy both fine. But checking fails: ghci> quickCheck prop_mysortby_sorted Falsifiable, after 2 tests: <> [2,3] I guess arbitrary for (Int -> Int -> Ordering) generates a non-sense function like this: -- let (arb_compare :: Int -> Int -> Ordering) generated function. arb_compare 0 1 = GT arb_compare 1 0 = GT arb_compare _ _ = EQ Then, I want to ask two questions. 1. Is my guessing in function generated by arbitrary right? 2. If so, How do I generate right function? Thanks, Yusaku Hashimoto ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANN: io-capture-0.2 capturing std(out|err) in IO action
Hi, I announce the release of io-capture package of version 0.2 io-capture is a Library to capturing stdout and stderr in IO action. It exports a function named `capture', It takes an IO and an String, the action to run and the given whole stdin, and returns whole stdout and stderr in the action as String. For example: ghci> :m +System.IO.Capture ghci> :m +System.IO ghci> capture (getLine >>= putStr >> getLine >>= hPutStr stderr) "foo \nbar\n" ("foo","bar") io-capture is available on hackage, repository is on patch-tag. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/io- capture-0.2 http://patch-tag.com/repo/io-capture/browse It behaves almost fine, but It's still experimental, I know some trouble about when given lazy reading action such as getContents. And there may be unknown bugs. If you find it, feel free to report it and any feedbacks are welcome. Thanks for reading, Yusaku Hashimoto ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] What unsafeInterleaveIO is unsafe
Hi, On 2009/03/16, at 10:04, wren ng thornton wrote: > next r = do n <- readIORef r > writeIORef r (n+1) > return n Now, if I use unsafeInterleaveIO: > main = do r <- newIORef 0 > x <- do a <- unsafeInterleaveIO (next r) >b <- unsafeInterleaveIO (next r) >return (a,b) > ... The values of a and b in x are entirely arbitrary, and are only set at the point when they are first accessed. They're not just arbitrary between which is 0 and which is 1, they could be *any* pair of values (other than equal) since the reference r is still in scope and other code in the ... could affect it before we access a and b, or between the two accesses. OK, the values of a and b depend how to deconstruct x. Moreover, let's have two pure implementations, f and g, of the same mathematical function. Even if f and g are close enough to correctly give the same output for inputs with _|_ in them, we may be able to observe the fact that they arrive at those answers differently by passing in our x. Given that such observations are possible, it is no longer safe to exchange f and g for one another, despite the fact that they are pure and give the same output for all (meaningful) inputs. Hm, Does it means that in optimization, a compiler may replace implementation of a pure function that have different order of evaluation, so order of actions would be different in some environments? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] What unsafeInterleaveIO is unsafe
Hello, I was studying about what unsafeInterleaveIO is.I understood unsafeInterleaveIO takes an IO action, and delays it. But I couldn't find any reason why unsafeInterleaveIO is unsafe. I have already read an example in http://www.haskell.org/pipermail/haskell-cafe/2009-March/057101.html says lazy IO may break purity, but I think real matter in this example are wrong use of seq. did I misread? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: ANNOUNCE: torch-0.1
On Wed, Mar 11, 2009 at 1:06 AM, Yusaku Hashimoto wrote: >> import Test.Torch >> >> main = run $ do >> ok (odd 1) "assertion" >> is 42 (7*6) "equality assertion" >> isBottom (error undefined) "check whether value is bottom" >> ans <- liftIO (putStr "\n5 + 7 = " >> readLn) >> is ans 12 "sanity check :)" Sorry, Above code doesn't work. I fixed this. But this code still don't work as I expected in output order: import Test.Torch import Control.Monad.Trans (liftIO) import System.IO main = run $ do liftIO $ hSetBuffering stdout NoBuffering ok (odd 1) "assertion" is 42 (7*6) "equality assertion" isBottom (undefined::Int) "check whether value is bottom" ans <- liftIO (putStr "\n5 + 7 = " >> readLn) is ans 12 "sanity check :)" ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANNOUNCE: torch-0.1
Hello, I have wrote and uploaded my unit test library (or framework) torch-0.1 on Hackage. With torch, We can write simple unit test and run like this: > import Test.Torch > > main = run $ do > ok (odd 1) "assertion" > is 42 (7*6) "equality assertion" > isBottom (error undefined) "check whether value is bottom" > ans <- liftIO (putStr "\n5 + 7 = " >> readLn) > is ans 12 "sanity check :)" Despite QuickCheck and HUnit, Why I wrote unit test library now? There is two reasons. 1. QuickCheck is not all-round QuickCheck is awesome. But I often can't write Property when I want to test things, like not pure function. Even for pure function, I can't write Property by thinking _laws_ behind that with probability about 0.5. When I can't write Property, I want to write unit test using HUnit. But for me, HUnit does not fit to my hands. Because it is annoying that writing list of test literally in source code and confusing operators (I still can't remember distinction between (@=?) and (@?=)) This means when I read test written in HUnit, I can't read this without Documentation. And I got an idea for expressing structure of tests from a blog post about Monoid and Writer Monad. 2. Try to realize an idea that Writer monad can be treated as Monoid (and list of Test is Monoid) I read sigfpe's great blog post (http://blog.sigfpe.com/2009/01/ haskell-monoids-and-their-uses.html) and hit upon the idea that considering Writer as Monoid, list of tests can be expressed with Monad. And I tried to realize it, and want to sympathize this idea with Haskellers. I think almost complete realizing this idea in the library. Those is why I wrote this library. And It is my first package on Hackage, I apologize for any wrong about packaging. I hope you'll enjoy this! Thanks, nonowarn ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe