[Haskell-cafe] What are free Monads?
Hello, I see the term free monad quite a lot, but don't really see an explanation what a free monad is. What sets a monad free and why in this day and age are there unfree monads? Günther ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] What are free Monads?
Given any functor you can get a monad for free! data Free f a = Either a (f (Free f a)) Not sure about unfree, but there are cofree comonads that are pretty closely related, and give you a comonad given a functor: data Cofree f a = (a, f (Cofree f a)) I'm sure the more categorically minded can tell you way more. Hope this helps, Dan 2010/2/27 Günther Schmidt gue.schm...@web.de Hello, I see the term free monad quite a lot, but don't really see an explanation what a free monad is. What sets a monad free and why in this day and age are there unfree monads? Günther ___ 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] How to understand `|` in this code snippet ?
xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? Sincerely! - fac n = let { f = foldr (*) 1 [1..n] } in f -- View this message in context: http://old.nabble.com/How-to-understand-%60%7C%60-in-this-code-snippet---tp27726581p27726581.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
Re: [Haskell-cafe] What are free Monads?
Hello Daniel, that looks lovely, but it doesn't help me much :) Günther Am 27.02.10 10:06, schrieb Daniel Peebles: Given any functor you can get a monad for free! data Free f a = Either a (f (Free f a)) Not sure about unfree, but there are cofree comonads that are pretty closely related, and give you a comonad given a functor: data Cofree f a = (a, f (Cofree f a)) I'm sure the more categorically minded can tell you way more. Hope this helps, Dan 2010/2/27 Günther Schmidt gue.schm...@web.de mailto:gue.schm...@web.de Hello, I see the term free monad quite a lot, but don't really see an explanation what a free monad is. What sets a monad free and why in this day and age are there unfree monads? Günther ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org mailto: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 understand `|` in this code snippet ?
On Sat, Feb 27, 2010 at 5:07 PM, zaxis z_a...@163.com wrote: xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? It's part of a case expression, see http://www.haskell.org/onlinereport/exps.html#sect3.13 lee ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to understand `|` in this code snippet ?
On Feb 27, 2010, at 04:07 , zaxis wrote: xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? It's a guard. Same as with function definitions (in fact, function definitions of that form are converted to case expressions). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon universityKF8NH PGP.sig Description: This is a digitally signed message part ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] What are free Monads?
On Saturday 27 February 2010 3:54:25 am Günther Schmidt wrote: I see the term free monad quite a lot, but don't really see an explanation what a free monad is. What sets a monad free and why in this day and age are there unfree monads? Free structures originate (I think) in algebra. There you'll find talk of free groups, free rings, free monoids, etc. The plain English explanation is that you want to take some 'underlying' structure, and promote it into the structure in question, but to do so in the simplest way possible in a sense. In the algebra case, the underlying structure is typically a set, so you'll have the free monoid over a set, the free group over a set, etc. Monoids are pretty simple, so they're probably easiest to explain. So, monoids are: A set M A binary operation m : M x M - M An identity element e : M and follow the laws: a `m` (b `m` c) = (a `m` b) `m` c e `m` a = a = a `m` e So, for a free monoid over a set S, we want to produce a structure satisfying the above, with an additional constraint that we need to have an: i : S - M to inject elements of S into the monoid. And by simplest above, we mean something like: 1) All the elements of M should be required to exist either by i, or by the operations for the structure. 2) The only equational laws that should hold for the structure should be those that are required to hold for structures of that type. These may be kind of vague, and I apologize. They can be made more precise in category theory (at least). So, for the above rules, we get that the free monoid over a set is the monoid of lists of elements of that set with concatenation and nil. M = [S] e = [] m = (++) i = \x - [x] -- \x - x : [] [] ++ xs = xs = xs ++ [] xs ++ (ys ++ zs) = (xs ++ ys) ++ zs In category theory, this is usually presented in terms of adjoint functors. What you do is specify a forgetful or underlying functor, from, say, the category of monoids to the category of sets. Then, a left adjoint to that functor is called (I believe) the free monoid functor, and it takes sets to the free monoid thereover. I don't really have the wherewithal to give an explanation of adjoint functors, but adjunctions are what capture the two rules for free things above. So, now we want free monads over a (endo)functor F. As it turns out, monads are monoid objects in the category of endofunctors over a category, which is a generalization of the above monoids to categories other than sets. So, we can hopefully use the above intuitive idea of a free monoid to figure out what might be going on with free monads. So, first we have an endofunctor F which is going to be analogous to the underlying set. And we'll need some type of injection i : F - M M being the functor part of the free monad over F. But arrows in the category in question are natural transformations, so in Haskell, this would be more like: i :: forall a. F a - M a Monads are also required to have a couple operations: return :: forall a. a - M a join :: forall a. M (M a) - M a And they should satisfy some monad laws. I can't explain to you how to figure out what M, return and join should be, because I don't know how myself; I'm kind of winging it at this point. But Daniel Peebles has given the right answer. It's datatype: data M a = Return a | Roll (F (M a)) with: return = Return join (Return m) = m join (Roll fmm) = Roll (fmap join fmm) i f = Roll (fmap Return f) -- this should look vaguely similar to to \x - x : [] which, you might notice, is similar in character to the free monoid above. In the free monoid, you can inject elements of the set, and you can multiply together lists to get arbitrarily long strings of elements of the underlying set. In the free monad, there's a way to 'inject' the functor into the monad, and you can 'multiply' in the monad to get arbitrarily deep composition 'strings' of F (by repeatedly Rolling). There are also cofree structures, as was mentioned. The difference is that while free functors are left adjoints to underlying functors, cofree functors are right adjoints. I don't have much to say beyond that, though. Anyhow, hope that gave some insight. -- Dan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Generalizing nested list comprehensions
Daniel Fischer wrote: Ishaaq Chandy wrote: If this question sounds a bit noob-ish, that would be because I am one - so apologies in advance! I have functions that look something like these: f1 :: [a] - [b] f1 xs = [foo [x1, x2] | x1 - xs, x2 - bar x1, baz x1 /= baz x2] f2 :: [a] - [b] f2 xs = [foo [x1, x2, x3] | x1 - xs, x2 - bar x1, x3 - bar x2, baz x1 /= baz x2, baz x1 /= baz x3, baz x2 /= baz x3] [...] Now, there is probably a frighteningly elegant way to do it with foldM or somesuch, but I don't see it at the moment :( That would be a variant of iterate for monads. iterateM :: Int - (a - m a) - a - m [a] iterateM n f a | n 0 = return [] | otherwise = (a:) `liftM` (iterateM (n-1) f = f a) f n xs = filter p `liftM` (iterateM n bar = xs) where p xs = let ys = map baz xs in nub ys == ys Regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: What are free Monads?
Günther Schmidt wrote: Daniel Peebles wrote: Given any functor you can get a monad for free! data Free f a = Either a (f (Free f a)) that looks lovely, but it doesn't help me much :) Alas, that you get one for free is not the reason why it's called the free monad. Rather, for algebraic structures like groups or rings, freeness refers to the fact that there are no other relations than the imposed ones. The simplest example are probably the natural numbers. As you may know, there are two fundamental operations 0 :: Nat -- nullary operation s :: Nat - Nat-- successor function, unary and the natural numbers are exactly the free algebra over these operations, which intends to rule out any superfluous relations like s (s (s (s 0))) = s 0 or similar. This is a distinguishing property, for there are other sets that support these two operations, for instance the clock numbers where you have 12 = s (s (s ... (s 0)..)) = 0 ^^^ 12 times the successor which corresponds to the time on a clock. In Haskell: newtype Clock = Clock Int 0 = Clock 0 s = \(Clock k) - Clock $ (k+1) `mod` 12 Another example that is not free either: newtype Foo13 = Foo13 Int 0 = Foo13 0 s = \(Foo13 k) - Foo13 $ if k == 13 then 13 else k+1 Regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Testing and module export lists
On 27 February 2010 04:44, Brandon S. Allbery KF8NH allb...@ece.cmu.edu wrote: As I read that, the leading underscore doesn't export anything; it just suppresses any unused warning for the symbol (which is consistent with the other uses of leading underscore in warnings; it's also consistent with binding to _ to throw a result away). OK, I _must_ have misunderstood what Curt was saying :s -- 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] Cabal pre-compiled packages
Hi, currently when one install a cabal package it compiles it and then install generated binaries. I wonder whether or not it would be useful to have pre-compiled binaries as many package managers usually do (e.g. apt). I often think that would save some time on the expense of a busier hackage server capable of generating packages for many different platforms. I'm particularly thinking on the following scenario: suppose that you have code that is ready for production. If cabal supported pre-compiled binaries, there is no need to install ghc or eventually any other compiler, just runtime environment and eventually cabal. I must say that I have no experience in doing this in Haskell (just personal/small projects), so I suppose one have to generate binaries and use other sort of package manager to deploy code to production (which sounds reasonable as well). Thus, if the assumption is correct, cabal is a development tool, not something one could to only deploy runtime-only packages. I also would appreciate if others could share how usually this is managed. Best Regards, ~dsouza ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Cabal pre-compiled packages
Diego Souza wrote: Hi, currently when one install a cabal package it compiles it and then install generated binaries. I wonder whether or not it would be useful to have pre-compiled binaries as many package managers usually do (e.g. apt). I often think that would save some time on the expense of a busier hackage server capable of generating packages for many different platforms. I'm particularly thinking on the following scenario: suppose that you have code that is ready for production. If cabal supported pre-compiled binaries, there is no need to install ghc or eventually any other compiler, just runtime environment and eventually cabal. I must say that I have no experience in doing this in Haskell (just personal/small projects), so I suppose one have to generate binaries and use other sort of package manager to deploy code to production (which sounds reasonable as well). Thus, if the assumption is correct, cabal is a development tool, not something one could to only deploy runtime-only packages. I also would appreciate if others could share how usually this is managed. As far as I know, Cabal is mainly used for deploying Haskell libraries. If you want to deploy a finished Haskell program, just compile it into an executable program and make it downloadable from somewhere. (Much like a C program or any other kind of program.) For example, if you hunt around, you can find Darcs available as a binary download (even for Windows). It might be nice if certain Haskell libraries were available in binary form. The trouble is, Haskell libraries have to be recompiled for each version of the compiler. This is why it's usually released in source form; otherwise you have to make a bazillion different binaries, one for every version of GHC on every platform that GHC runs on. Much easier to just compile from source, Unix-style. (And I've only come across one Haskell package that takes more than 11 seconds to compile anyway.) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Cabal pre-compiled packages
Am Samstag 27 Februar 2010 16:39:27 schrieb Andrew Coppin: Diego Souza wrote: Hi, currently when one install a cabal package it compiles it and then install generated binaries. I wonder whether or not it would be useful to have pre-compiled binaries as many package managers usually do (e.g. apt). I often think that would save some time on the expense of a busier hackage server capable of generating packages for many different platforms. I'm particularly thinking on the following scenario: suppose that you have code that is ready for production. If cabal supported pre-compiled binaries, there is no need to install ghc or eventually any other compiler, just runtime environment and eventually cabal. I must say that I have no experience in doing this in Haskell (just personal/small projects), so I suppose one have to generate binaries and use other sort of package manager to deploy code to production (which sounds reasonable as well). Thus, if the assumption is correct, cabal is a development tool, not something one could to only deploy runtime-only packages. I also would appreciate if others could share how usually this is managed. As far as I know, Cabal is mainly used for deploying Haskell libraries. Yes, Cabal: Common Architecture for Building Applications and Libraries If you want to deploy a finished Haskell program, just compile it into an executable program and make it downloadable from somewhere. (Much And since the binary doesn't need to be built anymore, you don't need Cabal or cabal for that. Of course, a central repo for binaries might be a good thing. like a C program or any other kind of program.) For example, if you hunt around, you can find Darcs available as a binary download (even for Windows). Actually, I think you'll find more binaries for Windows than for *n*x, since commi ( ./configure make make install) is so simple. It might be nice if certain Haskell libraries were available in binary form. The trouble is, Haskell libraries have to be recompiled for each version of the compiler. This is why it's usually released in source form; otherwise you have to make a bazillion different binaries, one for every version of GHC on every platform that GHC runs on. That, and it's so much better to have the source around. Need a small modification of/addition to a library? Unpack, edit, bump version, build, install, offer patch to maintainer. How would you go about it if you only had the binary? Much easier to just compile from source, Unix-style. (And I've only come across one Haskell package that takes more than 11 seconds to compile anyway.) So you've tried at most one of the GUI libraries, HPDF or highlighting- kate? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] listing mountpoints and getting their properties in Haskell
Hello! I need to list all currently mounted filesystems and get some stats like total space, free space, mount point and physical device. Is there any library capable of obtaining such information from OS itself? Parsing output of 'df' is locale-dependent and error-prone (because of locale settings, output settings etc). Thank you in advance. -- Eugene N Dzhurinsky pgp87im5z8ywi.pgp Description: PGP signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to understand `|` in this code snippet ?
On Sat, Feb 27, 2010 at 9:29 AM, Brandon S. Allbery KF8NH allb...@ece.cmu.edu wrote: On Feb 27, 2010, at 04:07 , zaxis wrote: xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? It's a guard. Same as with function definitions (in fact, function definitions of that form are converted to case expressions). In fact it seems to be a pattern guard, which (until recently) are (were) a non-standard extension: http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#pattern-guards ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] save/restore of STRef state
Job Vranish wrote: I was using STRefs the other day and I ran across a case where I really wanted the ability to save the state of all my references, execute some action, and then restore the state later if needed. I didn't find anything that does this on hackage so I implemented a small wrapper around STRefs which does what I want. If you use something like the State or Reader monad, it becomes trivial to temporarily modify the carried state. But maybe something like this is occasionally useful. (In particular, it seems to allow you to restore to a point not necessarily matching the most recent save.) What do you think? Any suggestions? Deriving the Eq instance for ContextRef means that it will compare the key *and* the IORef. Which gives the right answer, but seems rather redundant. Comparing the key alone should be sufficient. Any glaring purity issues that I overlooked? Why an IORef? Why not an STRef? Then you won't need unsafeIOToST. (And since the type system forces a ContextRef to exist in only one state thread, worrying about thread isolation with atomicModifyIORef seems unecessary.) Using a state monad with a mutable structure as the state looks highly dubious. (The whole point of a state monad is, after all, to avoid needing to mutate stuff.) I can see 2 calls to get, but none to put. I would suggest you either use a reader monad with mutable state, or a state monad with immutable state. One or the other. (Personally, I'd go for the latter.) I'm also not 100% sure how the saving and restoring part works. Map Int (IO (IO ())) sounds fruity though. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] listing mountpoints and getting their properties in Haskell
Eugene Dzhurinsky wrote: Hello! I need to list all currently mounted filesystems and get some stats like total space, free space, mount point and physical device. Is there any library capable of obtaining such information from OS itself? Parsing output of 'df' is locale-dependent and error-prone (because of locale settings, output settings etc). Stuff like mount points is Unix-specific, so have a dig around inside the POSIX library. (System.Posix.Files looks promising...) Finding the total free space on a filesystem is something that ought to be possible in a portable way though, and I don't see anything in the standard libraries for doing this. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] save/restore of STRef state
On Sat, Feb 27, 2010 at 11:53 AM, Andrew Coppin andrewcop...@btinternet.com wrote: If you use something like the State or Reader monad, it becomes trivial to temporarily modify the carried state. But maybe something like this is occasionally useful. (In particular, it seems to allow you to restore to a point not necessarily matching the most recent save.) Yeah, in cases where you only need references to values of the same type, then a Map in a state or Reader works really well. But in my case I need to reference values of several different types, which would make things messy in a state monad, and saving/restoring even messier. I'm also using MonadFix quite a bit and a Map in a State monad was a lot harder to make lazy (in my case, sometimes it's not to bad). Deriving the Eq instance for ContextRef means that it will compare the key *and* the IORef. Which gives the right answer, but seems rather redundant. Comparing the key alone should be sufficient. Agree, will fix. Why an IORef? Why not an STRef? Then you won't need unsafeIOToST. (And since the type system forces a ContextRef to exist in only one state thread, worrying about thread isolation with atomicModifyIORef seems unecessary.) I use the IORefs because I wanted to use mkWeakIORef (maybe mkWeak would work just as well?) and atomicModifyIORef. The thread isolation is needed because of the the finalizers that clean out the map when the references get GC'd. Although, it _is_ kinda ugly. I'm thinking I might switch back STRefs and just use unsafeCoerce *flinch* when I want to use atomicModifyIORef. (IORef is just a newtype around STRef) Using a state monad with a mutable structure as the state looks highly dubious. (The whole point of a state monad is, after all, to avoid needing to mutate stuff.) I can see 2 calls to get, but none to put. I would suggest you either use a reader monad with mutable state, or a state monad with immutable state. One or the other. (Personally, I'd go for the latter.) Yeah, I'll switch to Reader, but the state needs to be mutable so that the finalizers can get to it. I'm also not 100% sure how the saving and restoring part works. Map Int (IO (IO ())) sounds fruity though. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe Thanks for the feedback :) - Job ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: listing mountpoints and getting their properties in Haskell
Eugene Dzhurinsky b...@redwerk.com wrote: Hello! I need to list all currently mounted filesystems and get some stats like total space, free space, mount point and physical device. Is there any library capable of obtaining such information from OS itself? Parsing output of 'df' is locale-dependent and error-prone (because of locale settings, output settings etc). Thank you in advance. There's mount, which I think has a portable output format, and my df comes with the -P option to switch to POSIX output. -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] save/restore of STRef state
Job Vranish wrote: On Sat, Feb 27, 2010 at 11:53 AM, Andrew Coppin andrewcop...@btinternet.com mailto:andrewcop...@btinternet.com wrote: Why an IORef? Why not an STRef? Then you won't need unsafeIOToST. (And since the type system forces a ContextRef to exist in only one state thread, worrying about thread isolation with atomicModifyIORef seems unecessary.) I use the IORefs because I wanted to use mkWeakIORef (maybe mkWeak would work just as well?) and atomicModifyIORef. The thread isolation is needed because of the the finalizers that clean out the map when the references get GC'd. Hmm, I see. (I can't actually figure out what the map does, so I can't really comment further about that. You'd think there's a way around this though...) Using a state monad with a mutable structure as the state looks highly dubious. (The whole point of a state monad is, after all, to avoid needing to mutate stuff.) I can see 2 calls to get, but none to put. I would suggest you either use a reader monad with mutable state, or a state monad with immutable state. One or the other. (Personally, I'd go for the latter.) Yeah, I'll switch to Reader, but the state needs to be mutable so that the finalizers can get to it. Ah, I see. That at least makes sense... ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] View patterns
One somewhat neat thing about Haskell is that you can say case list of [[x], [y,_], [z,_,_]] - x + y + z _ - 0 In Java, you'd have to write something like if (list.length() == 3) { List t1 = list.at(0); if (t1.length() == 1) { int x = t1.at(0); List t2 = list.at(1); if (t2.length() == 2) ... I can't even be bothered to finish typing all that lot! However, as somebody pointed out, the Java version is polymorphic. Assuming that length() is defined for multiple types of container, the Java version works with lists, arrays, sets, etc. If you try to do this in Haskell, you end up with case size c of 3 - case (c ! 0, c ! 1, c ! 2) of (xs, ys, zs) | size x == 1 size y == 2 size z == 3 - (xs ! 0) + (ys ! 0) + (zs ! 0) _ - 0 _ - 0 or similar. Which is shorter than Java, but nowhere near as nice as the original list-only version. Now I was under the impression that view patterns fix this problem, but it seems they don't: case c of (size - 3) - case (c ! 0, c ! 1, c ! 2) of (size - 1, size - 2, size - 3) - (c ! 0 ! 0) + (c ! 1 ! 0) + (c ! 2 ! 0) Any suggestions? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns
A humble suggestion: Have a *lazy* to list method for your *lists, arrays, sets, etc.* and use the nice list-only version. On 27 February 2010 18:11, Andrew Coppin andrewcop...@btinternet.comwrote: One somewhat neat thing about Haskell is that you can say case list of [[x], [y,_], [z,_,_]] - x + y + z _ - 0 In Java, you'd have to write something like if (list.length() == 3) { List t1 = list.at(0); if (t1.length() == 1) { int x = t1.at(0); List t2 = list.at(1); if (t2.length() == 2) ... I can't even be bothered to finish typing all that lot! However, as somebody pointed out, the Java version is polymorphic. Assuming that length() is defined for multiple types of container, the Java version works with lists, arrays, sets, etc. If you try to do this in Haskell, you end up with case size c of 3 - case (c ! 0, c ! 1, c ! 2) of (xs, ys, zs) | size x == 1 size y == 2 size z == 3 - (xs ! 0) + (ys ! 0) + (zs ! 0) _ - 0 _ - 0 or similar. Which is shorter than Java, but nowhere near as nice as the original list-only version. Now I was under the impression that view patterns fix this problem, but it seems they don't: case c of (size - 3) - case (c ! 0, c ! 1, c ! 2) of (size - 1, size - 2, size - 3) - (c ! 0 ! 0) + (c ! 1 ! 0) + (c ! 2 ! 0) Any suggestions? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Ozgur Akgun ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns
Ozgur Akgun wrote: A humble suggestion: Have a *lazy* to list method for your /lists, arrays, sets, etc./ and use the nice list-only version. Yeah, that works quite nicely. It won't work for arbitrarily complex structures, however. My main point was that if you make the constructors abstract and provide functions to query the structure, now you can't pattern match against it. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] passing cpp options to c2hs from cabal
Hi all, I have a question about cpp, c2hs and cabal. The short version: What can I put in my cabal file to get -cppopts=-U__BLOCKS__ passed as an argument in calls to c2hs? Longer story: I need to set up my cabal file so that c2hs gets this extra option to make things build smoothly on some macs. If I run cabal from the command line, I can pass it: cabal install --c2hs-options='--cppopts=-U__BLOCKS__' And this works great. I need to make my .cabal file do this, but c2hs-options doesn't seem to be accepted there. I tried: cpp-options: -U__BLOCKS__ But if I run cabal install -v I can see this isn't being passed on to c2hs: ... /home/ccasin/.cabal/bin/c2hs --include=dist/build --cppopts=-D__GLASGOW_HASKELL__=610 --cppopts=-Icontrib/libpuz/include --output-dir=dist/build --output=Codec/Game/Puz/Internal.hs ./Codec/Game/Puz/Internal.chs ... Thanks! --Chris Casinghino ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] passing cpp options to c2hs from cabal
Am Samstag 27 Februar 2010 21:27:27 schrieb Chris Casinghino: Hi all, I have a question about cpp, c2hs and cabal. The short version: What can I put in my cabal file to get -cppopts=-U__BLOCKS__ passed as an argument in calls to c2hs? Maybe cc-options: -U__BLOCKS__ is worth a try. Longer story: I need to set up my cabal file so that c2hs gets this extra option to make things build smoothly on some macs. If I run cabal from the command line, I can pass it: cabal install --c2hs-options='--cppopts=-U__BLOCKS__' And this works great. I need to make my .cabal file do this, but c2hs-options doesn't seem to be accepted there. I tried: cpp-options: -U__BLOCKS__ But if I run cabal install -v I can see this isn't being passed on to c2hs: ... /home/ccasin/.cabal/bin/c2hs --include=dist/build --cppopts=-D__GLASGOW_HASKELL__=610 --cppopts=-Icontrib/libpuz/include --output-dir=dist/build --output=Codec/Game/Puz/Internal.hs ./Codec/Game/Puz/Internal.chs ... Thanks! --Chris Casinghino ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: View patterns
Andrew Coppin andrewcop...@btinternet.com wrote: Ozgur Akgun wrote: A humble suggestion: Have a *lazy* to list method for your /lists, arrays, sets, etc./ and use the nice list-only version. Yeah, that works quite nicely. It won't work for arbitrarily complex structures, however. My main point was that if you make the constructors abstract and provide functions to query the structure, now you can't pattern match against it. http://hackage.haskell.org/trac/ghc/ticket/3583 ...I think the issue just has to be nailed down precisely, and then someone has to volunteer implementing it. -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] redirecting ghc (as a library) output
hi, i'm writing a Haskell View Server for CouchDB. it communicates with couchdb over stdin and stdout. it gets JSON encoded haskell code, compiles it (like on http://www.haskell.org/haskellwiki/GHC/As_a_library), gets values, runs the given code over the given values and writes the results back (also json encoded). when there is an error in the given haskell code it should reply with the error json encoded and exit. but f.e. when the given code imports a module, which cannot be found (here Reaction) ghc just spits out a panic directly to stdin and exits like this: ViewServer: panic! (the 'impossible' happened) (GHC version 6.12.1 for x86_64-unknown-linux): Could not find module `Reaction': Use -v to see a list of the files searched for. Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug it puts other errors directly to stdout too, like: Assembler messages: Fatal error: can't create /home/*/AlkylRadicalDecomposition.o: Permission denied this confuses couchdb, because it expects some JSON and i cant see whats going on between them. now, how can i prevent ghc from using stdout and wrap the output in some JSON? is this even possible? with ghc 6.10 this usage of 'handle' worked for me: main = handle (\e - do let e' = show (e::SomeException) case fromException e of Just UserInterrupt - exitSuccess _ - do let err = error2json the impossible happened... e' putStrLn err logToFile err return []) main_loop did i got the Exception handling wrong? thanks in advance. have fun martin ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] listing mountpoints and getting their properties in Haskell
Eugene, The only Linux function that can do this is `statvfs`. But binding to it weren't implemented in the current libraries. Year ago I implemented function that queries free size of the mounted filesystem. The file is attached. To get other parameters you should only extend `peek` function of the `Storable` class. Hope it helps. Vasyl 2010/2/27 Eugene Dzhurinsky b...@redwerk.com: Hello! I need to list all currently mounted filesystems and get some stats like total space, free space, mount point and physical device. Is there any library capable of obtaining such information from OS itself? Parsing output of 'df' is locale-dependent and error-prone (because of locale settings, output settings etc). Thank you in advance. -- Eugene N Dzhurinsky ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe statvfs.hsc Description: Binary data ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] listing mountpoints and getting their properties in Haskell
On Feb 27, 2010, at 11:47 , Eugene Dzhurinsky wrote: Is there any library capable of obtaining such information from OS itself? Parsing output of 'df' is locale-dependent and error-prone (because of locale settings, output settings etc). I don't know of any Haskell bindings offhand, but getmntent() and friends are the standard library interface for identifying mountpoints and statfs()/statvfs() are the interface for getting information about them. Be aware that the latter can be fairly system-dependent. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon universityKF8NH PGP.sig Description: This is a digitally signed message part ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to understand `|` in this code snippet ?
Then can i change it to : case timeout of Just str - do [(t, _)] - reads str addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () Sincerely! Brandon S. Allbery KF8NH wrote: On Feb 27, 2010, at 04:07 , zaxis wrote: xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? It's a guard. Same as with function definitions (in fact, function definitions of that form are converted to case expressions). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon universityKF8NH ___ 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/How-to-understand-%60%7C%60-in-this-code-snippet---tp27726581p27732364.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
Re: [Haskell-cafe] How to understand `|` in this code snippet ?
Am Sonntag 28 Februar 2010 02:08:18 schrieb zaxis: Then can i change it to : case timeout of Just str - do [(t, _)] - reads str addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () Sincerely! No. The | [(t,_)] - reads str in case timeout of Just str | [(t,_)] - reads str - ... is a pattern guard, not a monadic bind (and where p - reads str is a monadic bind, it's in the list monad). You can change it to case timeout of Just str - case reads str of [(t,_)] - addtimeout (hPutStrLn stderr *** TIMEOUT _exit 1) _ - return () _ - return () but why would you? Brandon S. Allbery KF8NH wrote: On Feb 27, 2010, at 04:07 , zaxis wrote: xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? It's a guard. Same as with function definitions (in fact, function definitions of that form are converted to case expressions). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon universityKF8NH ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to understand `|` in this code snippet ?
On Sat, Feb 27, 2010 at 05:08:18PM -0800, zaxis wrote: Then can i change it to : case timeout of Just str - do [(t, _)] - reads str addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () No, that's different. You could change it to: case timeout of Just str - case reads str of [(t, _)] - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _- other -- (1) _- other -- (2) where other = return () The cases (1) and (2) are the same and simulate the fact that when the pattern guard fails, then execution falls to the next case. Of course you could just write case fmap (reads str) timeout of Just [(t, _)] - ... _ - ... HTH, -- Felipe. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] native threads vs. -threaded
I've sketched out a Haskell interface to a C++ toolkit that uses threads. It's the Haiku (BeOS) platform API (BeOS is an obscure OS from the '90s, Haiku is a pretty faithful recent open source re-implementation.) The API is fairly obvious and I imagine much like others that support windowed graphic interfaces. You have your Window class that spawns a thread, and various virtual methods of that class support program interface with the events involving that window. The curious thing is that I can get this to work with my example program only if it is NOT linked with -threaded. If linked with -threaded, when the application comes up I have 4 threads: the main thread and window thread, and two threads apparently created by the Haskell runtime. Shortly (but not immediately) after the first callback from the window thread, the window thread quietly dies. Callbacks from the main thread work fine. Linked without -threaded, I have only the 2 natural threads, and the window thread can call back apparently without problems. I imagine I'm at fault somewhere in this, since I am also responsible for the GHC port to Haiku, but just wondering if this suggests an obvious course of inquiry to anyone. I assume it's not working as intended, as from the documentation I would rather have guessed that -thread would be required in this situation. thanks! Donn Cave, d...@avvanta.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to understand `|` in this code snippet ?
thanks! case timeout of Just str - case reads str of [(t,_)] - addtimeout (hPutStrLn stderr *** TIMEOUT _exit 1) _ - return () _ - return () is VERY clear! Daniel Fischer-4 wrote: Am Sonntag 28 Februar 2010 02:08:18 schrieb zaxis: Then can i change it to : case timeout of Just str - do [(t, _)] - reads str addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () Sincerely! No. The | [(t,_)] - reads str in case timeout of Just str | [(t,_)] - reads str - ... is a pattern guard, not a monadic bind (and where p - reads str is a monadic bind, it's in the list monad). You can change it to case timeout of Just str - case reads str of [(t,_)] - addtimeout (hPutStrLn stderr *** TIMEOUT _exit 1) _ - return () _ - return () but why would you? Brandon S. Allbery KF8NH wrote: On Feb 27, 2010, at 04:07 , zaxis wrote: xxxMain = do timeout - getEnv xxx_TIMEOUT case timeout of Just str | [(t, _)] - reads str - do addTimeout t (hPutStrLn stderr *** TIMEOUT _exit 1) return () _ - return () ... What does the `|` mean in Just str | [(t, _)] - reads str ? Is it a logical `or` ? It's a guard. Same as with function definitions (in fact, function definitions of that form are converted to case expressions). -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allb...@kf8nh.com system administrator [openafs,heimdal,too many hats] allb...@ece.cmu.edu electrical and computer engineering, carnegie mellon universityKF8NH ___ 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/How-to-understand-%60%7C%60-in-this-code-snippet---tp27726581p27732673.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
Re: [Haskell-cafe] EDSL's using Filet-O-Fish of Barrelfish project
Thanks, I'll follow your suggestion and get back with any doubts that I might have. Regards, Kashyap On Sat, Feb 27, 2010 at 1:37 AM, Pierre-Evariste Dagand pedag...@gmail.comwrote: Hi Kashyap, What would be your recommendation on how to get a grasp of FoF Well, you can read the PLOS paper first. Also, FoF is a literate code, so hopefully the learning curve will be less steeper. You should also take a look at the Fugu compiler literate code and, in less extend, the Hamlet compiler. They are sitting in ./tools/. would the tic-tac-toe sample help - could you share that please? Tic-tac-toe in the caps system was really nothing but a hack (for a demo). There is no pedagogical value in staring at it: it is just the file ./capabilities/caps.hl extended with approx. 4800 capabilities! Reading the official caps.hl is much more interesting, and, if you really want to, you will quickly see how you can implement your favorite board game in there. But don't! :-) Regards, -- Pierre-Evariste DAGAND -- Regards, Kashyap ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Real-time garbage collection for Haskell
I have seen some proposals around here for SoC projects and other things to try to improve the latency of GHC's garbage collector. I'm currently developing a game in Haskell, and even 100ms pauses are unacceptable for a real-time game. I'm calling out to people who have seen or made such proposals, because I would be willing to contribute funding and/or mentor a project that would contribute to this goal. Also any ideas for reducing this latency in other ways would be very appreciated. Thanks, Luke ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe