[Haskell-cafe] Sequencing Operations in a Monad
If you arrange the types to try to do all the operations inside the IO monad you can't chain together more than 1 binary operation. eg. do S - A + B Z - Q * S vs do S - Q * (A + B) Are there any suggestions for this dilemma? Am I using the wrong monad for this task? I'm not sure if this is what you are asking but isn't liftM2 or some variant what you need? Dominic. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Sequencing Operations in a Monad
Dominic Steinitz wrote: If you arrange the types to try to do all the operations inside the IO monad you can't chain together more than 1 binary operation. eg. do S - A + B Z - Q * S vs do S - Q * (A + B) Are there any suggestions for this dilemma? Am I using the wrong monad for this task? I'm not sure if this is what you are asking but isn't liftM2 or some variant what you need? Dominic. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe OK so check out what really happens with liftM2. Suppose I have an IO containing an involved matrix computation called s. For simplicity we might assume that s :: IO (Int) and the Int is an index into an array containing a bunch of matrices in C land. Assume that s is determined by a succession of many IO operations that have lots of side effects and are fairly computationally intensive. Also assume that s is unevaluated. Now do an operation like q = liftM2 MultMatrix s s What happens is that s is 'evaluated' twice when q is evaluated e.g. do qint - q That becomes evident when we look at liftM2's definition liftM2 f = \a b - do { a' - a; b' - b; return (f a' b') } the statements a' - a and b' - b will cause s to be evaluated twice. Therein lies my problem. -- View this message in context: http://www.nabble.com/Sequencing-Operations-in-a-Monad-tf4446788.html#a12687963 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] Building production stable software in Haskell
David Roundy wrote: I long for a Data.Map.Strict, for instance, because it's so hard to use Data.Map without producing memory leaks... It's at times like this that I really wonder why on earth I bother working hard on libs for the benefit of the community. But I guess I'm not alone in that. You're surely aware of the existance of the Data.Map clone with knobs on that I wrote, since I can distinctly remember pointing it out to you in an earlier thread.. http://www.haskell.org/pipermail/haskell-cafe/2007-April/024730.html Perhaps what you really mean is, you long for a Data.Map.Strict that carries the offically blessed status of being shipped with ghc (reminds me of someone asking for a ghc approved SDL binding a while back :-). Regards -- Adrian Hey ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Sequencing Operations in a Monad
SevenThunders wrote: OK so check out what really happens with liftM2. Suppose I have an IO containing an involved matrix computation called s. For simplicity we might assume that s :: IO (Int) and the Int is an index into an array containing a bunch of matrices in C land. Assume that s is determined by a succession of many IO operations that have lots of side effects and are fairly computationally intensive. Also assume that s is unevaluated. Now do an operation like q = liftM2 MultMatrix s s What happens is that s is 'evaluated' twice when q is evaluated e.g. do qint - q That becomes evident when we look at liftM2's definition liftM2 f = \a b - do { a' - a; b' - b; return (f a' b') } the statements a' - a and b' - b will cause s to be evaluated twice. Therein lies my problem. Here's your solution: do -- Compare this to liftM2 and your definition of q s' - s -- this evaluates s once and for all qint - return $ MultMatrix s' s' -- Ron ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Sequencing Operations in a Monad
SevenThunders wrote: Ryan Ingram wrote: As long as the FFI calls don't make destructive updates to existing matrices, you can do what you want. For example, assuming you have: -- adds the second matrix to the first overwrites the first matrixAddIO :: MatrixIO - MatrixIO - IO () -- creates a new copy of a matrix matrixCopyIO :: MatrixIO - IO MatrixIO ... Well as you point out there is an efficiency issue if we need to copy matrices all of the time in order to insure 'referential transparency'. Moreover I manage my matrices on a stack in C, since it makes it easy to handle memory allocation and deallocation. The stack configuration tends to be highly fluid so there are always side effects going on. Right now my Matrix type wraps the index from the bottom of the Matrix stack into the IO monad. If you need destructive updates, you indeed need a monad. Otherwise, I'd use ForeignPtrs and import the matrix operations as pure functions (~ unsafePerformIO). I was just wondering if there was any obvious way to force an IO action to execute only once, since now each reference to the action IO causes it to execute again. Isn't that simply do x - onlyOnce mult x x with onlyOnce :: IO Int mult :: Int - Int - IO Int ? If you want mult = liftM2 something :: IO Int - IO Int - IO Int you can do x' - onlyOnce let x = return x' mult x x which is do x - return `liftM` onlyOnce mult x x for short. Regards, apfelmus ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Sequencing Operations in a Monad
SevenThunders wrote: Unfortunately if I wrap my matrix references in the IO monad, then at best computations like S = A + B are themselves IO computations and thus whenever they are 'invoked' the computation ends up getting performed repeatedly contrary to my intentions. This sounds like a case for the infamous performUnsafeIO. The reason this is unsafe is that the compiler assumes that the IO computation it wraps has no visible side effects, so it doesn't matter when it is performed or how many times it gets performed. If your IO computation indeed has this nature then you are OK, but its up to you to make sure of this. Paul. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Binary Endianness
On Tuesday 11 September 2007 09:17, Don Stewart wrote: Just in case people didn't see, the `binary' package lives on http://darcs.haskell.org/binary/ However, Lennart Kolmodin, Duncan and I are actively maintaining and reviewing patches, so send them to one (or all) of us for review. Is there any deep reason why binary is not below packages like almost all other packages? The toplevel directory is already a complete mess, so a little bit more structure would be good. So I propose the following: Move binary to packages/binary, update any references to the old URL and use a symlink on darcs.haskell.org for backwards compatibility (this should be nuked in a few months or so). Cheers, S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Type-Marking finite/infinte lists?
Hi, today while mowing the lawn, I thought how to statically prevent some problems with infinte lists. I was wondering if it is possible to somehow mark a list as one of finite/infinite/unknown and to mark list-processing functions as whether they can handle infinte lists. For example, it is known that a list produced by repeat or cycle is always infinte, that foldl can’t handle infinte lists (while foldr might). length should never be called on an infinte list, and map’s output is infinite iff the input list is. So the compiler might use this information to emit errors like “line 42: program will not terminate: length applied to infinite list” or warnings “line 23: termination not guaranteed: foldl applied to possible infinte list”. I guess you can’t know it for all lists (halting problem...), but for those where it is possible, it would be nice to tell. Or am I missing something? Can this be implemented using some guru type system hacking? Greetings, Joachim -- Joachim nomeata Breitner mail: [EMAIL PROTECTED] | ICQ# 74513189 | GPG-Key: 4743206C JID: [EMAIL PROTECTED] | http://www.joachim-breitner.de/ Debian Developer: [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Binary Endianness
On Sat, Sep 15, 2007 at 01:20:57PM +0200, Sven Panne wrote: On Tuesday 11 September 2007 09:17, Don Stewart wrote: Just in case people didn't see, the `binary' package lives on http://darcs.haskell.org/binary/ However, Lennart Kolmodin, Duncan and I are actively maintaining and reviewing patches, so send them to one (or all) of us for review. Is there any deep reason why binary is not below packages like almost all other packages? The toplevel directory is already a complete mess, so a little bit more structure would be good. So I propose the following: Move binary to packages/binary, update any references to the old URL and use a symlink on darcs.haskell.org for backwards compatibility (this should be nuked in a few months or so). packages is only for those libraries that are shipped with GHC. Stefan signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data.Binary Endianness
On Saturday 15 September 2007 20:09, Stefan O'Rear wrote: packages is only for those libraries that are shipped with GHC. First of all, this fact would be new to me, furthermore this would be a highly volatile categorization. Should URLs change when a package suddenly gets into or was thrown out of boot-/extra-packages? I don't think so. The fact what is shipped and what not is explicit in the ghc/libraries/{boot,extra}-libraries, not in the structure of darcs.haskell.org. Packages which are hosted there should be below, well, packages. The toplevel directory is currently a bit crowded and unstructured... Cheers, S. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Type-Marking finite/infinte lists?
On 9/15/07, Joachim Breitner [EMAIL PROTECTED] wrote: today while mowing the lawn, I thought how to statically prevent some problems with infinte lists. I was wondering if it is possible to somehow mark a list as one of finite/infinite/unknown and to mark list-processing functions as whether they can handle infinte lists. One possibility would be to have some phantom markers in the list type. For example, newtype List isEmpty isFinite a = MarkList [a] data Finite data Infinite data Empty data Nonempty data Unknown This is possibly non-optimal, since emptiness and finiteness are semi-related. Specifically: emptyIsFinite :: List Empty f a - List Empty Finite a infiniteIsNonempty :: List e Infinite a - List Nonempty Infinite a You can test whether a list is (non)empty, but not whether it's (in)finite. nonEmpty :: List e f a - Maybe (List Nonempty f a) Aside from unfoldr, most operations that create lists are explicit about whether the result is finite. Then you can mark the properties of the common operations. nil :: List Empty Finite a cons:: a - List e f a - List Nonempty f a repeat :: a - List Nonempty Infinite a tail:: List Nonempty f a - List Unknown f a last:: List Nonempty Finite a - a map :: (a - b) - List e f a - List e f b filter :: (a - Bool) - List e f a - List Unknown f a foldl :: (a - b - b) - b - List e Finite a - b length :: List e Finite a - Int unfoldr :: (a - Maybe (b,a)) - a - List Unknown Unknown b Note the type of infiniteIsNonempty . tail. As a variation, we can use rank-2 types instead of Unknown; e.g. tail :: List Nonempty f a - (forall e. List e f a - w) - w filter :: (a - Bool) - List e f a - (forall e. List e f a - w) - w The most general form of (++) would require either fundeps or type families: (++) :: List e1 f1 a - List e2 f2 a - List (BothEmpty e1 e2) (BothFinite f1 f2) a type instance BothEmpty Empty Empty = Empty type instance BothEmpty Empty Unknown = Unknown type instance BothEmpty Empty Nonempty = Nonempty type instance BothEmpty Nonempty Unknown = Nonempty etc. -- Dave Menendez [EMAIL PROTECTED] http://www.eyrie.org/~zednenem/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: [Haskell] question about a failure to generalize
[replying to haskell-cafe] On 9/15/07, Norman Ramsey [EMAIL PROTECTED] wrote: Dear Haskellers, I've had a Haskell program rejected for reasons I don't understand. Here's the relevant bit of code; the problem is that I expected the type-inference engine to generalize the abbreviation 'fold' to an overloaded function, but it doesn't---to make the code work, I had to expand 'fold' into 'foldRegsUsed' everywhere it appears. I'm baffled because 'fold' isn't mutually recursive with anything, so I would have expected ordinary Hindley-Milner style inference to generalize it to something of the type UserOfLocalRegs a = (b - LocalReg - b) - b - a - b But that's not what happens. This code fails to compile because the compiler is willing to use 'fold' at only one type (CmmExpr as it happens): class UserOfLocalRegs a where foldRegsUsed :: (b - LocalReg - b) - b - a - b instance UserOfLocalRegs Middle where foldRegsUsed f z m = middle m where middle (MidComment {})= z middle (MidAssign _lhs expr) = fold f z expr middle (MidStore addr rval) = fold f (fold f z addr) rval middle (MidUnsafeCall tgt _ress args) = fold f (fold f z tgt) args middle (CopyIn _ _formals _) = z middle (CopyOut _ actuals)= fold f z actuals fold = foldRegsUsed What rule of the language have I overlooked? Monomorphism restriction? Replacing fold with foldRegsUsed would work because there's a type signature for foldRegsUsed. Cheers, Tim -- Tim Chevalier * catamorphism.org * Often in error, never in doubt ...There is no mystery; there is only paradox, the incontrovertible union of contradictory truths. -- Edward Abbey ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: [Haskell] question about a failure to generalize
Hi Monomorphism restriction? Replacing fold with foldRegsUsed would work because there's a type signature for foldRegsUsed. That looks like it. Another solution would be: fold = foldRegsUsed becomes: fold x = foldRegsUsed x Now the monomorphism restriction doesn't kick in because fold has an explicit argument. Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] How can I stop GHCi from calling show for IO actions?
Prelude let inf = repeat 1 Prelude inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I expect this to happen) Prelude let x = inf (no output here!) Prelude :t x x :: [Integer] Prelude return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I also expect this to happen) Prelude y - return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I do not expect this to happen here!) Prelude :t y interactive:1:0: Not in scope: 'y' Is this a bug? Why does y - return exp have different behavior than let y = exp? Is there a way to make GHCi not print the result of an action but still make my variables get bound? -- ryan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How can I stop GHCi from calling show for IO actions?
On Sat, Sep 15, 2007 at 08:35:02PM -0700, Ryan Ingram wrote: Prelude let inf = repeat 1 Prelude inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I expect this to happen) Prelude let x = inf (no output here!) Prelude :t x x :: [Integer] Prelude return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I also expect this to happen) Prelude y - return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I do not expect this to happen here!) Prelude :t y interactive:1:0: Not in scope: 'y' Is this a bug? Why does y - return exp have different behavior than let y = exp? Is there a way to make GHCi not print the result of an action but still make my variables get bound? GHC manual, Flag reference, Interactive-mode options: | -fno-print-bind-result │ Turn off printing of binding results in GHCi │ dynamic│ - │ with a link to: http://haskell.org/ghc/dist/current/docs/users_guide/interactive-evaluation.html#ghci-stmts Note that let bindings do not automatically print the value bound, unlike monadic bindings. The automatic printing of binding results can be supressed with :set -fno-print-bind-result (this does not supress printing the result of non-binding statements). You might want to do this to prevent the result of binding statements from being fully evaluated by the act of printing them, for example. Stefan signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How can I stop GHCi from calling show for IO actions?
Ryan Ingram wrote: Prelude let inf = repeat 1 Prelude inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I expect this to happen) Prelude let x = inf (no output here!) Prelude :t x x :: [Integer] Prelude return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I also expect this to happen) Prelude y - return inf [1,1,(lots of output until I press ctrl-c),Interrupted. (I do not expect this to happen here!) Prelude :t y interactive:1:0: Not in scope: 'y' Is this a bug? Why does y - return exp have different behavior than let y = exp? Is there a way to make GHCi not print the result of an action but still make my variables get bound? That's weird. Prelude (x,y) - return $ (repeat 1, repeat 2) Prelude Just x - return $ Just (repeat 1) [1,1,1,... Prelude (x,_) - return $ (repeat 1, repeat 2) [1,1,1,... Prelude Just (x,y) - return $ Just (repeat 1, repeat 2) Prelude It seems that GHCi outputs the contents of the variable you've created when there's only one of them. - Sam ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] unknown RTS option: -N2
I've built a program with the -threaded option using ghc. This option is supposed to link your program to the threaded runtime with support for multicore CPUS (mine is a dual core). The program pukes with the message in the subject line when I try to use the -N option to tell it to use both CPUs. I'm reasonably sure that I actually have the threaded runtime because initially I got a runtime message from the program about how GTK is single threaded and can only be used with the threaded runtime using runtimeunsafeInitGUIForThreadedRTS instead of the standard initGUI. I made the change in the code and now the program runs but still does not know the -N RTS option. This problem seems to affect all my programs. It is as if the threaded runtime is there but doesn't know any of its special flags. This is the line I'm compiling the program with. It's a life game clone in case you're curious. Compiler is ghc-6.4.2 on 64 bit AMD dual core running Gentoo Linux. - Greg ghc -threaded -package gtk -package glade -o hlife hlgtk.o Hlife.o Check out the hottest 2008 models today at Yahoo! Autos. http://autos.yahoo.com/new_cars.html___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] unknown RTS option: -N2
On Sat, Sep 15, 2007 at 09:49:56PM -0700, Gregory Propf wrote: I've built a program with the -threaded option using ghc. This option is supposed to link your program to the threaded runtime with support for multicore CPUS (mine is a dual core). The program pukes with the message in the subject line when I try to use the -N option to tell it to use both CPUs. I'm reasonably sure that I actually have the threaded runtime because initially I got a runtime message from the program about how GTK is single threaded and can only be used with the threaded runtime using runtimeunsafeInitGUIForThreadedRTS instead of the standard initGUI. I made the change in the code and now the program runs but still does not know the -N RTS option. This problem seems to affect all my programs. It is as if the threaded runtime is there but doesn't know any of its special flags. This is the line I'm compiling the program with. It's a life game clone in case you're curious. Compiler is ghc-6.4.2 on 64 bit AMD dual core running Gentoo Linux. - Greg ghc -threaded -package gtk -package glade -o hlife hlgtk.o Hlife.o -Nx is new in 6.6.0 from http://haskell.org/ghc/docs/6.6/html/users_guide/release-6-6.html: -- 1.4. Release notes for version 6.6 1.4.1. User-visible compiler changes o GHC now supports SMP: when you compile with -threaded, you now get an RTS flag -N that allows you to specify the number of OS threads that GHC should use. Defaults to 1. See [7]Section 4.12, Using SMP parallelism and [8]Section 7.15, Parallel Haskell.a Stefan signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe