From magnus at therning.org Wed Sep 2 01:39:34 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Sep 2 01:18:55 2009 Subject: [Haskell-beginners] Utter Newbie - simple problems, output - GHC vs GHCi In-Reply-To: <4A9AFDEE.30104@wiggly.org> References: <4A9AF8A5.3050500@wiggly.org> <4A9AFDEE.30104@wiggly.org> Message-ID: On Mon, Aug 31, 2009 at 12:32 AM, Nigel Rantor wrote: > Magnus Therning wrote: >> On Sun, Aug 30, 2009 at 11:09 PM, Nigel Rantor wrote: >>> >>> length( filter( Char.isLower "LoweR" ) ) >> >> try this instead >> >> length (filter Char.isLower "LoweR") >> >> `filter` takes two arguments. > > Thank you, I have been flip-flopping between having no parentheses and > lots...I think I get it now. > > I owe you a beer. > > This will, however, probably not be my last post... I'm most often in Cambridge, but do find myself in London occasionally. Adding more beers to the first one is nothing I shy away from :-) /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From kwangyul.seo at gmail.com Wed Sep 2 07:21:43 2009 From: kwangyul.seo at gmail.com (KwangYul Seo) Date: Wed Sep 2 07:01:04 2009 Subject: [Haskell-beginners] Using FiniteMap in ghci Message-ID: <7beb12420909020421v191f7210k35f5eafcccef20d5@mail.gmail.com> Hello, I tried to load FiniteMap module in ghci and encountered an error message like the following: Prelude> import FiniteMap Could not find module `FiniteMap': it is a member of package ghc-6.8.2, which is hidden The error message says that it is hidden. How can I use the FiniteMap? Regards, Kwang Yul Seo -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090902/1823774e/attachment.html From chaddai.fouche at gmail.com Wed Sep 2 08:02:28 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Wed Sep 2 07:41:50 2009 Subject: [Haskell-beginners] Using FiniteMap in ghci In-Reply-To: <7beb12420909020421v191f7210k35f5eafcccef20d5@mail.gmail.com> References: <7beb12420909020421v191f7210k35f5eafcccef20d5@mail.gmail.com> Message-ID: On Wed, Sep 2, 2009 at 1:21 PM, KwangYul Seo wrote: > The error message says that it is hidden. How can I use the FiniteMap? FiniteMap has been superceded by Data.Map, this is probably what you want to use (or Data.IntMap if your keys are Int). -- Jeda? From kwangyul.seo at gmail.com Wed Sep 2 08:34:42 2009 From: kwangyul.seo at gmail.com (KwangYul Seo) Date: Wed Sep 2 08:14:02 2009 Subject: [Haskell-beginners] Using FiniteMap in ghci In-Reply-To: References: <7beb12420909020421v191f7210k35f5eafcccef20d5@mail.gmail.com> Message-ID: <7beb12420909020534m31981cc9wae2da87038282660@mail.gmail.com> Oh, I see. Thanks. I was playing with code from Yet Another Haskell Tutorial, and it uses FiniteMap there. I think the tutorial needs to be updated too. Regards, Kwang Yul Seo On Wed, Sep 2, 2009 at 9:02 PM, Chadda? Fouch? wrote: > > On Wed, Sep 2, 2009 at 1:21 PM, KwangYul Seo wrote: > > The error message says that it is hidden. How can I use the FiniteMap? > > FiniteMap has been superceded by Data.Map, this is probably what you > want to use (or Data.IntMap if your keys are Int). > > -- > Jeda? From daniel.is.fischer at web.de Wed Sep 2 09:18:38 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Sep 2 09:00:23 2009 Subject: [Haskell-beginners] Using FiniteMap in ghci In-Reply-To: <7beb12420909020421v191f7210k35f5eafcccef20d5@mail.gmail.com> References: <7beb12420909020421v191f7210k35f5eafcccef20d5@mail.gmail.com> Message-ID: <200909021518.38642.daniel.is.fischer@web.de> Am Mittwoch 02 September 2009 13:21:43 schrieb KwangYul Seo: > Hello, > > I tried to load FiniteMap module in ghci and encountered an error message > like the following: > > Prelude> import FiniteMap > Could not find module `FiniteMap': > it is a member of package ghc-6.8.2, which is hidden > > The error message says that it is hidden. How can I use the FiniteMap? > > Regards, > Kwang Yul Seo As Chaddai said, it's obsolete and superceded by Data.Map and Data.IntMap. But the names and types of some functions have been changed, if you don't want to change the tutorial code so that it works with the new modules, you can ghc-pkg expose ghc-6.8.2 then work with FiniteMap and afterwards ghc-pkg hide ghc-6.8.2 From giannismantz at gmail.com Wed Sep 2 11:48:27 2009 From: giannismantz at gmail.com (=?ISO-8859-7?B?w+nc7e3n8iDM4e305u/18dz07/I=?=) Date: Wed Sep 2 11:27:48 2009 Subject: [Haskell-beginners] function - argument termination problem Message-ID: Hello, Is it possible to implement in haskell a function f (A) such that if A does not ever terminate then f always terminates, and if A always terminates then f does not ever terminate? I've been thinking it for a while, with no results unfortunately. the actual problem is that i can think of no way to force both ifs: a function that always terminates independently from its argument could be f (a) = 1, and a function that does not terminate even if its argument terminates could be: f (a) = f (a + 1), but i can't figure out a "hybrid" version... Is there any idea on this? Thanks in advance :-), yannis From rk at trie.org Wed Sep 2 11:54:30 2009 From: rk at trie.org (Rahul Kapoor) Date: Wed Sep 2 11:33:51 2009 Subject: [Haskell-beginners] function - argument termination problem In-Reply-To: References: Message-ID: > Is it possible to implement in haskell a function f (A) such that if A > does not ever terminate then f always terminates, and if A always > terminates then f does not ever terminate? Is the argument A itself another function? If so, such a function does not exist. See http://en.wikipedia.org/wiki/Halting_problem for more details. Rahul From magnus at therning.org Wed Sep 2 11:56:41 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Sep 2 11:36:02 2009 Subject: [Haskell-beginners] function - argument termination problem In-Reply-To: References: Message-ID: 2009/9/2 ??????? ???????????? : > Hello, > > Is it possible to implement in haskell a function f (A) such that if A > does not ever terminate then f always terminates, and if A always > terminates then f does not ever terminate? I've been thinking it for a > while, with no results unfortunately. the actual problem is that i can > think of no way to force both ifs: a function that always terminates > independently from its argument could be f (a) = 1, and a function > that does not terminate even if its argument terminates could be: f > (a) = f (a + 1), but i can't figure out a "hybrid" version... Is there > any idea on this? > > Thanks in advance :-), Isn't this the halting problem? /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From chaddai.fouche at gmail.com Wed Sep 2 14:46:35 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Wed Sep 2 14:25:55 2009 Subject: [Haskell-beginners] function - argument termination problem In-Reply-To: References: Message-ID: 2009/9/2 Magnus Therning : >> Is it possible to implement in haskell a function f (A) such that if A >> does not ever terminate then f always terminates, and if A always >> terminates then f does not ever terminate? >> >> Thanks in advance :-), > > Isn't this the halting problem? It is, since to ensure that f terminates and evaluates to a given value for every A that do not terminate it must be able to determine that it don't terminate in finite time, thus solving the halting problem. In other words this can't be written in Haskell and a fortiori can't be written on a computer in any language. -- Jeda? From aditya.siram at gmail.com Wed Sep 2 17:24:46 2009 From: aditya.siram at gmail.com (aditya siram) Date: Wed Sep 2 17:04:05 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference Message-ID: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Hi all, Recently I wrote a function that takes a unique identifier that I called 'id'. I then tried to apply the 'id' function to it and GHC did not like that. But it should. For example in 'test' I have told the compiler that the id argument is an Int. So type inference should be able to determine the first 'id' in 'id id' couldn't possibly be an Int, but it complains. So I explicitly told the compiler the type of 'id' in test1 - this didn't work either. The final function 'test3' works as expected. Is there something I am not understand about the way type inference is supposed to work? test :: Int -> Int test id = id id test1 :: Int -> Int test1 id = idFunc id where idFunc :: a -> a idFunc = id test2 :: Int -> Int test2 myid@id = id myid test3 :: Int -> Int test3 id = Prelude.id id thanks ... -deech -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090902/b6ce09e2/attachment.html From aslatter at gmail.com Wed Sep 2 17:30:55 2009 From: aslatter at gmail.com (Antoine Latter) Date: Wed Sep 2 17:10:15 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference In-Reply-To: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> References: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Message-ID: <694519c50909021430l52b286fep4c3c7184c25035b2@mail.gmail.com> On Wed, Sep 2, 2009 at 4:24 PM, aditya siram wrote: > Hi all, > Recently I wrote a function that takes a unique identifier that I called > 'id'. I then tried to apply the 'id' function to it and GHC did not like > that. But it should. > > For example in 'test' I have told the compiler that the id argument is an > Int. So type inference should be able to determine the first 'id' in 'id id' > couldn't possibly be an Int, but it complains. So I explicitly told the > compiler the type of 'id' in test1 - this didn't work either. The final > function 'test3' works as expected. Is there something I am not understand > about the way type inference is supposed to work? > > test :: Int -> Int > test id = id id > Type inference plays no role in figuring out what your identifiers refer to. In the body of your function, the only identifier named "id" that's in scope is the argument to the function. There's a compiler warning for GHC you can use to be warned we you introduce identifiers which "shadow" existing identifiers, like in your function. Does my explanation make sense? Antoine From tom.davie at gmail.com Wed Sep 2 17:33:08 2009 From: tom.davie at gmail.com (Thomas Davie) Date: Wed Sep 2 17:12:49 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference In-Reply-To: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> References: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Message-ID: Ints can't make up the function part of an application, that must have type (a -> b). In the mean time, the reason it didn't accept test id = id id is because it must fix the argument id to only one type. It infers that id must have type (a -> b), from the fact that it appears in the function position, then sees it in the argument position, and infers that a = (a -> b) which obviously causes a problem. To resolve that problem, you need rank-2 polymorphism. Bob On 2 Sep 2009, at 23:24, aditya siram wrote: > Hi all, > Recently I wrote a function that takes a unique identifier that I > called 'id'. I then tried to apply the 'id' function to it and GHC > did not like that. But it should. > > For example in 'test' I have told the compiler that the id argument > is an Int. So type inference should be able to determine the first > 'id' in 'id id' couldn't possibly be an Int, but it complains. So I > explicitly told the compiler the type of 'id' in test1 - this didn't > work either. The final function 'test3' works as expected. Is there > something I am not understand about the way type inference is > supposed to work? > > test :: Int -> Int > test id = id id > > test1 :: Int -> Int > test1 id = idFunc id > where > idFunc :: a -> a > idFunc = id > > test2 :: Int -> Int > test2 myid@id = id myid > > test3 :: Int -> Int > test3 id = Prelude.id id > > thanks ... > -deech > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From daniel.is.fischer at web.de Wed Sep 2 17:37:58 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Sep 2 17:18:21 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference In-Reply-To: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> References: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Message-ID: <200909022337.59114.daniel.is.fischer@web.de> Am Mittwoch 02 September 2009 23:24:46 schrieb aditya siram: > Hi all, > Recently I wrote a function that takes a unique identifier that I called > 'id'. I then tried to apply the 'id' function to it and GHC did not like > that. But it should. > > For example in 'test' I have told the compiler that the id argument is an > Int. So type inference should be able to determine the first 'id' in 'id > id' couldn't possibly be an Int, but it complains. So I explicitly told the > compiler the type of 'id' in test1 - this didn't work either. The final > function 'test3' works as expected. Is there something I am not understand > about the way type inference is supposed to work? > > test :: Int -> Int > test id = id id > > test1 :: Int -> Int > test1 id = idFunc id > where > idFunc :: a -> a > idFunc = id > > test2 :: Int -> Int > test2 myid@id = id myid > > test3 :: Int -> Int > test3 id = Prelude.id id > > thanks ... > -deech It's not type inference, it's scoping. By calling the function's parameter id, you shadow the name id, so within that scope each (unqualified) mention of the name id refers to the function's parameter. In test3, you use the qualified name Prelude.id, thus tell the compiler that it's not the parameter you want but the identity function. From magnus at therning.org Wed Sep 2 17:42:45 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Sep 2 17:22:07 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference In-Reply-To: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> References: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Message-ID: On Wed, Sep 2, 2009 at 11:24 PM, aditya siram wrote: > Hi all, > Recently I wrote a function that takes a unique identifier that I called > 'id'. I then tried to apply the 'id' function to it and GHC did not like > that. But it should. > > For example in 'test' I have told the compiler that the id argument is an > Int. So type inference should be able to determine the first 'id' in 'id id' > couldn't possibly be an Int, but it complains. So I explicitly told the > compiler the type of 'id' in test1 - this didn't work either. The final > function 'test3' works as expected. Is there something I am not understand > about the way type inference is supposed to work? > > test :: Int -> Int > test id = id id > > test1 :: Int -> Int > test1 id = idFunc id > ??? where > ????? idFunc :: a -> a > ????? idFunc = id > > test2 :: Int -> Int > test2 myid@id = id myid > > test3 :: Int -> Int > test3 id = Prelude.id id How is the compiler supposed to differentiate between the situation where you really mean `id id` (and it type checks properly) and where you've mistakenly written `id id` (but it doesn't type check)? If that doesn't make you realise then think of it as your `id` shadowing the Prelude's, and that happens irrespective of the types. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From hyangfji at gmail.com Wed Sep 2 18:40:38 2009 From: hyangfji at gmail.com (Hong Yang) Date: Wed Sep 2 18:19:57 2009 Subject: [Haskell-beginners] Help with CSV Message-ID: I need to process csv files that have the characteristics as follows: 1) each file has thousands of columns which have String, Int, and Double types 2) the number of columns may change 3) for those columns whose name do not change, their location may change I want to process some columns in 3) using Haskell. In Perl, I can easily have the code like below: use Text::CSV; my $csv = Text::CSV->new( { allow_whitespace => 1 } ); open my $temp, "<", "temp.csv" or die "Cannot open temp.csv! ($!)"; my @fields = @{ $csv->getline($temp) }; $csv->column_names(@fields); while ( my $hr = $csv->getline_hr($temp) ) { my $sn = $hr->{"UNIT:unitSerialNumber"}; # processing goes here ... } close $temp; Can someone please give me an equivalent code in Haskell? Then I can digest and expand it. Thanks, Hong -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090902/0efd9ff1/attachment.html From s.clover at gmail.com Wed Sep 2 19:15:45 2009 From: s.clover at gmail.com (Sterling Clover) Date: Wed Sep 2 18:54:51 2009 Subject: [Haskell-beginners] Figuring out errors In-Reply-To: <1251773361-sup-1494@javelin> References: <1251773361-sup-1494@javelin> Message-ID: <321DD7E6-B987-4965-A4DD-E4A4F5EC21ED@gmail.com> On Aug 31, 2009, at 11:02 PM, Edward Z. Yang wrote: > Hello all, > > I've been looking at [1] and trying to make tops and bottoms > (pardon the pun) of error handling in Haskell. I am still > uncertain of what to do. The problem, I think, is that it really depends on the situation. One also should distinguish between errors and exceptions -- errors being when the program/programmer is faulty, and exceptions being signals of expected conditions. The choice is really about when you want to always continue, what the appropriate level to test for failure is, and what to do on various exceptional conditions. I tend to think that pure functions should always return pure results, and error calls should be reserved for labeling actual error situations (i.e. code paths that should never be taken). Then when you want to assert that the result will never occur, you can write a mayToError (i.e. a labeled fromJust) or eitherToError function. Either String should of course not be used to represent a range of string exceptions, but should be thought of as returning a single exception (i.e. "Unable to Parse") with accompanying explanatory text. If you need to make sure that a pure value in IO doesn't contain either an error or a bottom, I would suggest using evaluate and rnf judiciously, both of which are important to understand, not just for error handling, but also for performance. Cheers, S. > I recognize that there are different areas of code that > may have different requirements for errors: > > * Pure code that is simple enough can probably get away > with returning Maybe a > > * Pure code that has multiple failure modes (the canonical > example is parsing) should return a Either e a. The type > of e is a little difficult: many standard libraries seem > to use String, but coming from Python this seems analogous > to the long deprecated "string exceptions", which are quick > and easy but not long-term maintainable due to lack of an > easy way to match for them. This leads naturally into > Either MyError, which is supported using throwError and > catchError. > > * However, [1] specifically warns against using this technique > in the IO monad, and I need a way to short circuit execution > when a crucial pure operation fails (in Python, this would have just > been an uncaught exception). This suggests using ErrorT on > IO, however, [1] also claims that this is generally not a good > idea, and suggests to use throwDyn (which actually looks like > it's been renamed to throw) > > * Unfortunately, when I've tried to use this technique, I've > run up against the fact that throw is implemented using bottom, > so if I do a throw in a pure function, the exception might > not actually surface up until I'm, say, attempting to print > its return value to IO. Denizens on #haskell have instructed > me to treat bottom as the bane of existence and opt for > stacking ErrorT on IO (it would be nice to know if this was > a good idea or bad idea) > > At which point, I am most thoroughly confused. Pointers, please! > > Cheers, > Edward > > [1] http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways- > to-report-errors > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From keithshep at gmail.com Wed Sep 2 21:23:05 2009 From: keithshep at gmail.com (Keith Sheppard) Date: Wed Sep 2 21:02:25 2009 Subject: [Haskell-beginners] Help with CSV In-Reply-To: References: Message-ID: <92e42b740909021823x6e32cf56o8138e2b8339dac58@mail.gmail.com> Not quite code but... here is an example of parsing CSV http://book.realworldhaskell.org/read/using-parsec.html and here is a library that you can use which is similar http://hackage.haskell.org/package/csv These approaches give you a 2D String list that you can do whatever you want with. if you need to turn a string into a double and you know the string is well formed i think the syntax looks like > let doubleVal = read stringVal :: Double There are better ways to do this if you need to be able to handle formatting errors but I don't know them off the top of my head -Keith On Wed, Sep 2, 2009 at 6:40 PM, Hong Yang wrote: > I need to process csv files that have the characteristics as follows: > 1)??? each file has thousands of columns which have String, Int, and Double > types > 2)??? the number of columns may change > 3)??? for those columns whose name do not change, their location may change > > I want to process some columns in 3) using Haskell. > > In Perl, I can easily have the code like below: > > use Text::CSV; > my $csv = Text::CSV->new( { allow_whitespace => 1 } ); > open my $temp, "<", "temp.csv" or die "Cannot open temp.csv! ($!)"; > my @fields = @{ $csv->getline($temp) }; > $csv->column_names(@fields); > while ( my $hr = $csv->getline_hr($temp) ) { > ??? my $sn = $hr->{"UNIT:unitSerialNumber"}; > ??? # processing goes here ... > } > close $temp; > > Can someone please give me an equivalent code in Haskell? Then I can digest > and expand it. > > Thanks, > > Hong > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- keithsheppard.name From magnus at therning.org Thu Sep 3 02:35:56 2009 From: magnus at therning.org (Magnus Therning) Date: Thu Sep 3 02:15:16 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference In-Reply-To: References: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Message-ID: On Wed, Sep 2, 2009 at 11:33 PM, Thomas Davie wrote: > Ints can't make up the function part of an application, that must have type > (a -> b). Well, I don't think that's strictly true. foo::Int can be argued to be a function that takes no arguments and return an Int. It is however very convenient to call such a function a "constant". However, that such a function actully is constant is far from guaranteed in most programming languages. It happens to be true in Haskell though, due to the strict separation between (proper) functions and "procedures" (functions with side effects, i.e. stuff in the IO monad). > In the mean time, the reason it didn't accept test id = id id is because it > must fix the argument id to only one type. It infers that id must have type > (a -> b), from the fact that it appears in the function position, then sees > it in the argument position, and infers that a = (a -> b) which obviously > causes a problem. ?To resolve that problem, you need rank-2 polymorphism. Is that really correct? I suspect the only thing that causes the OP problems is scoping. If I understand you correctly I wouldn't be able to do the following: Prelude> let f x = x Prelude> :t f f :: t -> t Prelude> :t id id :: a -> a Prelude> :t id f id f :: t -> t Prelude> let g = id f Prelude> :t g g :: t -> t Prelude> id 5 5 Prelude> f 5 5 Prelude> g 5 5 Clearly that's not a problem at all. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From magnus at therning.org Thu Sep 3 02:48:28 2009 From: magnus at therning.org (Magnus Therning) Date: Thu Sep 3 02:27:46 2009 Subject: [Haskell-beginners] Identical function and variable names and type inference In-Reply-To: References: <594f78210909021424j75e5407evb451d3a508d3a872@mail.gmail.com> Message-ID: On Thu, Sep 3, 2009 at 8:35 AM, Magnus Therning wrote: > On Wed, Sep 2, 2009 at 11:33 PM, Thomas Davie wrote: >> Ints can't make up the function part of an application, that must have type >> (a -> b). > > Well, I don't think that's strictly true. foo::Int can be argued to be > a function that takes no arguments and return an Int. ?It is however > very convenient to call such a function a "constant". ?However, that > such a function actully is constant is far from guaranteed in most > programming languages. ?It happens to be true in Haskell though, due > to the strict separation between (proper) functions and "procedures" > (functions with side effects, i.e. stuff in the IO monad). > >> In the mean time, the reason it didn't accept test id = id id is because it >> must fix the argument id to only one type. It infers that id must have type >> (a -> b), from the fact that it appears in the function position, then sees >> it in the argument position, and infers that a = (a -> b) which obviously >> causes a problem. ?To resolve that problem, you need rank-2 polymorphism. > > Is that really correct? ?I suspect the only thing that causes the OP > problems is scoping. ?If I understand you correctly I wouldn't be able > to do the following: > > Prelude> let f x = x > Prelude> :t f > f :: t -> t > Prelude> :t id > id :: a -> a > Prelude> :t id f > id f :: t -> t > Prelude> let g = id f > Prelude> :t g > g :: t -> t > Prelude> id 5 > 5 > Prelude> f 5 > 5 > Prelude> g 5 > 5 > > Clearly that's not a problem at all. Ah, sorry, I just remembered that the 'forall' is implicit (http://en.wikibooks.org/wiki/Haskell/Existentially_quantified_types). /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From sergey at mikhanov.com Thu Sep 3 08:41:46 2009 From: sergey at mikhanov.com (Sergey V. Mikhanov) Date: Thu Sep 3 08:21:03 2009 Subject: [Haskell-beginners] Multiplexing I/O in Haskell Message-ID: Hello community, Is there a good/proper way to do multiplexing I/O in Haskell, similar to what we are getting by using select()/poll() in Unix kernel? It does not seem impossible to implement this on top of existing IO library (though I haven't tried this actually, so I may be wrong). What interest me most here is whether we could use Haskell's implicit multithreading here (select()/poll() combination is all about multithreading)? Thanks in advance, Sergey From magnus at therning.org Thu Sep 3 09:52:15 2009 From: magnus at therning.org (Magnus Therning) Date: Thu Sep 3 09:31:33 2009 Subject: [Haskell-beginners] Multiplexing I/O in Haskell In-Reply-To: References: Message-ID: On Thu, Sep 3, 2009 at 2:41 PM, Sergey V. Mikhanov wrote: > ? Hello community, > > Is there a good/proper way to do multiplexing I/O in Haskell, similar > to what we are getting by using select()/poll() in Unix kernel? It > does not seem impossible to implement this on top of existing IO > library (though I haven't tried this actually, so I may be wrong). > What interest me most here is whether we could use Haskell's implicit > multithreading here (select()/poll() combination is all about > multithreading)? There used to be a select()-like call available in a Posix-related module in older versions of GHC. It seems to have disappeared a while ago though. The current "best-practice" seems to be using forkIO "threads". The old-ish paper on the haskell web server[1] contains some numbers that suggest this is indeed a fairly high-performing solution. /M [1]: http://www.haskell.org/haskellwiki/Haskell_Web_Server -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From anand.prabhakar.patil at gmail.com Fri Sep 4 05:38:49 2009 From: anand.prabhakar.patil at gmail.com (Anand Patil) Date: Fri Sep 4 05:18:24 2009 Subject: [Haskell-beginners] Haskell data structures: cheap immutable manipulation and nested equality? Message-ID: <2bc7a5a50909040238y32325c00ke5cbaae6a448da3a@mail.gmail.com> Hi all, I'm slowly shopping around for a functional language. The first one I seriously considered was Clojure, because of its agents; but I'm now realizing that most of my concurrency needs can be met using 'par' and 'seq', so I'm thinking about giving Haskell a closer look. However, Clojure's collections (maps, lists, vectors, sets) have a couple of features that I would really miss: - Cheap 'manipulation'. In the following: user=> (def m {:a 1 :b 2}) #'user/m1 user=> (assoc m :a 3) {:a 3, :b 2} the 'assoc' function produces a new map which actually shares structure with m, meaning it doesn't need to make a full copy in memory; but it also leaves m unchanged. This works efficiently even for nested, mixed collections. - Cheap equality by value: user=> (= m {:a 1 :b 2 :c {:d 3 :f 4}}) false user=> (= m {:a 1 :b 2}) true If I understand correctly, equality is computed based on some kind of hash rather than by comparing the two maps element by element, so it's efficient even for large and/or nested collections. Do Haskell's data structures have analogous properties? If not, are there libraries providing such data structures? Thanks, Anand From apfelmus at quantentunnel.de Fri Sep 4 06:27:59 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Fri Sep 4 06:07:42 2009 Subject: [Haskell-beginners] Re: Haskell data structures: cheap immutable manipulation and nested equality? In-Reply-To: <2bc7a5a50909040238y32325c00ke5cbaae6a448da3a@mail.gmail.com> References: <2bc7a5a50909040238y32325c00ke5cbaae6a448da3a@mail.gmail.com> Message-ID: Anand Patil wrote: > - Cheap 'manipulation'. In the following: > > user=> (def m {:a 1 :b 2}) > #'user/m1 > user=> (assoc m :a 3) > {:a 3, :b 2} > > the 'assoc' function produces a new map which actually shares > structure with m, meaning it doesn't need to make a full copy in > memory; but it also leaves m unchanged. This works efficiently even > for nested, mixed collections. Since Haskell is pure, this is the default. Every data structure is persistent and shares as much as possible with previous versions. Example in point: an infinite list of ones ones = 1:ones > - Cheap equality by value: > > user=> (= m {:a 1 :b 2 :c {:d 3 :f 4}}) > false > user=> (= m {:a 1 :b 2}) > true > > If I understand correctly, equality is computed based on some kind of > hash rather than by comparing the two maps element by element, so it's > efficient even for large and/or nested collections. > > Do Haskell's data structures have analogous properties? If not, are > there libraries providing such data structures? Not present in Haskell, no existing library either. You can roll your own, of course. (Also note that hash based comparison might yield false positives). Why would you need this feature? I can only think of common subexpression elimination. Generally, the above two points are minor compared to the other things that Haskell has to offer, like pattern matching, Hindler Milner type system, purity. Regards, apfelmus -- http://apfelmus.nfshost.com From cora.nightshade at gmail.com Fri Sep 4 08:01:47 2009 From: cora.nightshade at gmail.com (Colin Campbell-McPherson) Date: Fri Sep 4 07:58:01 2009 Subject: [Haskell-beginners] Generalised data constructor matching Message-ID: <7B251974-F3B6-485C-B2D5-5EC525878C06@logaan.net> Hi Chaps, I've been trying to write a function that would get the name of most data that is an instance of class "Animal". This getName function could then be overwritten for data that doesn't follow the normal pattern for "Animal" data types. So the getName defined in the Animal class would be a generic, or default implementation. I've written a couple of examples that don't use classes, but that I hope expresses what I'm trying to accomplish. In the first example getName needs to be defined for Dogs and Birds, even though they're essentially identical. The tries to define a more general function that works for both Birds and Dogs, and anything else that might come along. The first examples works, the second gives a "Parse error in pattern" error. EXAMPLE 1 data Animal = Person String String | Dog String | Bird String deriving Show getName :: Animal -> String getName (Person firstName lastName) = firstName ++ " " ++ lastName getName (Dog name) = name getName (Bird name) = name logan = Person "Logan" "Campbell" ebony = Dog "Ebony" poly = Bird "Poly" main = do putStrLn $ show $ getName logan putStrLn $ show $ getName ebony putStrLn $ show $ getName poly EXAMPLE 2 data Animal = Person String String | Dog String | Bird String deriving Show getName :: Animal -> String getName (Person firstName lastName) = firstName ++ " " ++ lastName getName (_ name) = name logan = Person "Logan" "Campbell" ebony = Dog "Ebony" poly = Bird "Poly" main = do putStrLn $ show $ getName logan putStrLn $ show $ getName ebony putStrLn $ show $ getName poly I have a background with Ruby and Erlang, so if drawing from concepts in either of those languages would help explain please do so. I'm also quite new to haskell so if i've used the wrong terms, or I'm trying to apply concepts where they don't belong, sorry about that. TLDR: How can I write a generic function that will work on most instances of a class, but be over over-written for instances of that class that deviate from the normal structure (ie. a data constructor with two params, rather than one). Many thanks, Colin From daniel.is.fischer at web.de Fri Sep 4 08:57:18 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Sep 4 08:39:15 2009 Subject: [Haskell-beginners] Generalised data constructor matching In-Reply-To: <7B251974-F3B6-485C-B2D5-5EC525878C06@logaan.net> References: <7B251974-F3B6-485C-B2D5-5EC525878C06@logaan.net> Message-ID: <200909041457.18565.daniel.is.fischer@web.de> Am Freitag 04 September 2009 14:01:47 schrieb Colin Campbell-McPherson: > Hi Chaps, > > I've been trying to write a function that would get the name of most > data that is an instance of class "Animal". This getName function > could then be overwritten for data that doesn't follow the normal > pattern for "Animal" data types. So the getName defined in the Animal > class would be a generic, or default implementation. > > I've written a couple of examples that don't use classes, but that I > hope expresses what I'm trying to accomplish. In the first example > getName needs to be defined for Dogs and Birds, even though they're > essentially identical. The tries to define a more general function > that works for both Birds and Dogs, and anything else that might come > along. > > The first examples works, the second gives a "Parse error in pattern" > error. > > EXAMPLE 1 > > data Animal = Person String String | Dog String | Bird String deriving > Show > getName :: Animal -> String > getName (Person firstName lastName) = firstName ++ " " ++ lastName > getName (Dog name) = name > getName (Bird name) = name > > logan = Person "Logan" "Campbell" > ebony = Dog "Ebony" > poly = Bird "Poly" > > main = do > putStrLn $ show $ getName logan > putStrLn $ show $ getName ebony > putStrLn $ show $ getName poly > For such a datatype, named fields come in handy: data Animal = Person { firstName, lastName :: String } | Dog { name :: String } | Bird { name :: String } getName :: Animal -> String getName (Person f l) = f ++ " " ++ l -- other special cases if the datatype is extended getName other = name other > > EXAMPLE 2 > > data Animal = Person String String | Dog String | Bird String deriving > Show > getName :: Animal -> String > getName (Person firstName lastName) = firstName ++ " " ++ lastName > getName (_ name) = name > > logan = Person "Logan" "Campbell" > ebony = Dog "Ebony" > poly = Bird "Poly" > > main = do > putStrLn $ show $ getName logan > putStrLn $ show $ getName ebony > putStrLn $ show $ getName poly > > I have a background with Ruby and Erlang, so if drawing from concepts > in either of those languages would help explain please do so. I'm also > quite new to haskell so if i've used the wrong terms, or I'm trying to > apply concepts where they don't belong, sorry about that. > > > TLDR: How can I write a generic function that will work on most > instances of a class, but be over over-written for instances of that > class that deviate from the normal structure (ie. a data constructor > with two params, rather than one). In general: not. The problem is that potentially every datatype can be made an instance of the class, so in the default implementations, you can only[*] use functions which work on every datatype. There aren't many interesting functions that do. [*]well, you can also use methods of the class and superclasses, so e.g. class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) class (Eq a) => Ord a where compare :: a -> a -> Ordering (<), (<=), (>), (>=) :: a -> a -> Bool min, max :: a -> a -> a x < y = x <= y && x /= y x <= y = x < y || x == y -- etc. is possible. But you still have to write instances manually for every type (except instances you can let the compiler derive). > > Many thanks, > Colin From chaddai.fouche at gmail.com Sat Sep 5 03:53:34 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Sat Sep 5 03:32:48 2009 Subject: [Haskell-beginners] Generalised data constructor matching In-Reply-To: <200909041457.18565.daniel.is.fischer@web.de> References: <7B251974-F3B6-485C-B2D5-5EC525878C06@logaan.net> <200909041457.18565.daniel.is.fischer@web.de> Message-ID: On Fri, Sep 4, 2009 at 2:57 PM, Daniel Fischer wrote: > In general: not. > The problem is that potentially every datatype can be made an instance of the class, so in > the default implementations, you can only[*] use functions which work on every datatype. > There aren't many interesting functions that do. > > [*]well, you can also use methods of the class and superclasses. And so by using Data and Typeable as superclasses, he could do what he is asking for... And Data and Typeable are automatically derivables by GHC (extension). -- Jeda? From daniel.is.fischer at web.de Sat Sep 5 08:45:25 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Sep 5 08:25:42 2009 Subject: [Haskell-beginners] Generalised data constructor matching In-Reply-To: References: <7B251974-F3B6-485C-B2D5-5EC525878C06@logaan.net> <200909041457.18565.daniel.is.fischer@web.de> Message-ID: <200909051445.25550.daniel.is.fischer@web.de> Am Samstag 05 September 2009 09:53:34 schrieb Chadda? Fouch?: > On Fri, Sep 4, 2009 at 2:57 PM, Daniel Fischer wrote: > > In general: not. > > The problem is that potentially every datatype can be made an instance of > > the class, so in the default implementations, you can only[*] use > > functions which work on every datatype. There aren't many interesting > > functions that do. > > > > [*]well, you can also use methods of the class and superclasses. > > And so by using Data and Typeable as superclasses, he could do what he > is asking for... And Data and Typeable are automatically derivables by > GHC (extension). True. But I don't think the code would become any shorter/better/easier to maintain (*shudder*), so I'd stick to individual instance declarations. From apfelmus at quantentunnel.de Sun Sep 6 11:07:19 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Sun Sep 6 10:46:56 2009 Subject: [Haskell-beginners] Re: Haskell data structures: cheap immutable manipulation and nested equality? In-Reply-To: References: <2bc7a5a50909040238y32325c00ke5cbaae6a448da3a@mail.gmail.com> Message-ID: Anand Patil wrote: Heinrich Apfelmus wrote: >> Anand Patil wrote: >>> - Cheap equality by value: >>> >>> user=> (= m {:a 1 :b 2 :c {:d 3 :f 4}}) >>> false >>> user=> (= m {:a 1 :b 2}) >>> true >>> >>> If I understand correctly, equality is computed based on some kind of >>> hash rather than by comparing the two maps element by element, so it's >>> efficient even for large and/or nested collections. >> >> Why would you need this feature? I can only think of common >> subexpression elimination. > > I have an expensive function that takes complicated data structures as > an argument, and I know that it will often be evaluated twice running > with the same argument, but it will be evaluated with lots of > different arguments over the course of the program. A cheap equality > test would make it easy to cache the last return value. Is there a > better way to optimize this in Haskell? Sounds indeed like a case for memoization to me. (While complicated data structures as keys sounds suspicious, in my opinion.) Cheap equality won't necessarily help, though, you want at least the hash value itself. But since calculating a hash will take O(length of complicated data structure) anyway, you can also use a memoization scheme based on "sums of products" which has the same complexity^1. In particular, have a look at http://hackage.haskell.org/package/data-memocombinators which is used like this: fib = Memo.integral fib' where fib' 0 = 0 fib' 1 = 1 fib' x = fib (x-1) + fib (x-2) You'll have to combine existing memo combinators to make one for your data structure; feel free to ask if you need more info on that. ^1: For common subexpression elimination, intermediate hashes count as well, so this comparison doesn't apply. Regards, apfelmus -- http://apfelmus.nfshost.com From john.moore54 at gmail.com Sun Sep 6 14:37:40 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Sep 6 14:16:48 2009 Subject: [Haskell-beginners] graphics.ui.wxcore Message-ID: <4f7ad1ad0909061137i30ab793bo55830c0478b9098b@mail.gmail.com> Hi, Could someone please explain how to intall this. I have installed ghc and gtk2Hs and tried running small programs to get an idea of the gui system in Haskell. It keeps telling me cannot find graphics.ui.wxcore. Module failed etc. I just very recently started to learn Haskell. ( I need an idea for my final year project which at this moment is on functional programming and want to find advantages/disadvantages of the gui system in Haskell) Regards John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090906/af752a11/attachment.html From daniel.is.fischer at web.de Sun Sep 6 14:53:35 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Sep 6 14:35:07 2009 Subject: [Haskell-beginners] graphics.ui.wxcore In-Reply-To: <4f7ad1ad0909061137i30ab793bo55830c0478b9098b@mail.gmail.com> References: <4f7ad1ad0909061137i30ab793bo55830c0478b9098b@mail.gmail.com> Message-ID: <200909062053.35262.daniel.is.fischer@web.de> Am Sonntag 06 September 2009 20:37:40 schrieb John Moore: > Hi, > Could someone please explain how to intall this. I have installed ghc > and gtk2Hs and tried running small programs to get an idea of the gui > system in Haskell. It keeps telling me cannot find graphics.ui.wxcore. That would be in the wxcore package, as far as I know it has nothing to do with gtk2hs. Try cabal update && cabal install wx (wx is the higher level GUI package on top of wxcore) > Module failed etc. I just very recently started to learn Haskell. ( I need > an idea for my final year project which at this moment is on functional > programming and want to find advantages/disadvantages of the gui system in > Haskell) Regards > John From akshay.v.dave at hotmail.com Mon Sep 7 01:28:58 2009 From: akshay.v.dave at hotmail.com (Akshay Dave) Date: Mon Sep 7 01:08:05 2009 Subject: [Haskell-beginners] Urgent: Defining Momory data Types in Haskell Message-ID: Hi All, I am stuck in converting the transition semantics in Haskell. Please let me know how to define memory data types in Haskell( like we define pointer in C). Prompt help would be greatly appreciated. Sincerely, Akshay _________________________________________________________________ With Windows Live, you can organize, edit, and share your photos. http://www.windowslive.com/Desktop/PhotoGallery -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090907/ac3a8d50/attachment.html From bugfact at gmail.com Mon Sep 7 02:28:51 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Mon Sep 7 02:07:57 2009 Subject: [Haskell-beginners] Urgent: Defining Momory data Types in Haskell In-Reply-To: References: Message-ID: I don't think you provided enough information to give a full answer, at least not to me. Briefly, if you want to use pointers as in C, then use IORef or STRef and the IO resp ST monad. If you refer to the ability to define recursive structures in C using pointers like struct IntNode { int value; IntNode* next; } then you can just use Haskell's algebraic data types (ADTs), like data IntNode = IntNodeTag Int IntNode Of course this particular example is just a list of integers in Haskell, and on lists you have a multitude of premade functions to manipulate them. But I guess this is not the answer you've been looking for? More about this can be found in any standard Haskell text book or the wikis On Mon, Sep 7, 2009 at 7:28 AM, Akshay Dave wrote: > Hi All, > ?? I am stuck in converting the transition semantics in Haskell. Please let > me know how to define memory data types in Haskell( like we define pointer > in C). Prompt help would be greatly appreciated. > > Sincerely, > Akshay > ________________________________ > With Windows Live, you can organize, edit, and share your photos. Click > here. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From akshay.v.dave at hotmail.com Mon Sep 7 02:37:16 2009 From: akshay.v.dave at hotmail.com (Akshay Dave) Date: Mon Sep 7 02:16:22 2009 Subject: [Haskell-beginners] Urgent: Defining Momory data Types in Haskell In-Reply-To: References: Message-ID: Hi, Thanks for your prompt reply. Actually I am trying to convert the following transitive semantics to Haskell: (Memory maps I to Z) lookup m i = ( meaning lookup for I in memory m) evB b m = true/(while b do c od;m) -> (c; while b do c od;m) I have written the boolean expression and statement part but I am not able to write the memory representation in Haskell. Hope above explanation helps. Cheers! Akshay > Date: Mon, 7 Sep 2009 08:28:51 +0200 > Subject: Re: [Haskell-beginners] Urgent: Defining Momory data Types in Haskell > From: bugfact@gmail.com > To: akshay.v.dave@hotmail.com > CC: beginners@haskell.org > > I don't think you provided enough information to give a full answer, > at least not to me. > > Briefly, if you want to use pointers as in C, then use IORef or STRef > and the IO resp ST monad. > > If you refer to the ability to define recursive structures in C using > pointers like > > struct IntNode > { > int value; > IntNode* next; > } > > then you can just use Haskell's algebraic data types (ADTs), like > > data IntNode = IntNodeTag Int IntNode > > Of course this particular example is just a list of integers in > Haskell, and on lists you have a multitude of premade functions to > manipulate them. > > But I guess this is not the answer you've been looking for? More about > this can be found in any standard Haskell text book or the wikis > > > On Mon, Sep 7, 2009 at 7:28 AM, Akshay Dave wrote: > > Hi All, > > I am stuck in converting the transition semantics in Haskell. Please let > > me know how to define memory data types in Haskell( like we define pointer > > in C). Prompt help would be greatly appreciated. > > > > Sincerely, > > Akshay > > ________________________________ > > With Windows Live, you can organize, edit, and share your photos. Click > > here. > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > _________________________________________________________________ Get back to school stuff for them and cashback for you. http://www.bing.com/cashback?form=MSHYCB&publ=WLHMTAG&crea=TEXT_MSHYCB_BackToSchool_Cashback_BTSCashback_1x1 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090907/841a1931/attachment-0001.html From dima at simonchik.net Mon Sep 7 05:05:39 2009 From: dima at simonchik.net (Dmitry Simonchik) Date: Mon Sep 7 04:45:04 2009 Subject: [Haskell-beginners] number formatting with locale Message-ID: <80eb7b2e0909070205t649e6c01y648ae9756a42e751@mail.gmail.com> Hi all haskell lovers I'm trying to print out Double in russian locale (we use "," as decimal separator insetad of "."). I'm trying to set locale with System.Locale.SetLocale like setLocale LC_ALL $ Just "ru_RU.UTF-8" this returns Just "ru_RU.UTF-8", so it seems that function call succeeded, but when I call show 20.2 it just prints 20.2 and not desired 20,2 Can anyone please help? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090907/edd802be/attachment.html From tomdoris at gmail.com Mon Sep 7 13:28:22 2009 From: tomdoris at gmail.com (Tom Doris) Date: Mon Sep 7 13:07:28 2009 Subject: [Haskell-beginners] idiomatic haskell question Message-ID: <19e5d1d00909071028s349a7552tae8036f3b1567d87@mail.gmail.com> Hi all, I've recently started using Haskell and am looking for some feedback on code I've written; I sometimes feel that maybe I'm not doing things the best way possible that Haskell allows, and maybe missing out on obvious improvements in brevity or elegance. So here's a tic-tac-toe solver I wrote that does basic min-max search of the entire tree (not efficient algorithmically, but it's tictactoe so a blank board can be fully solved in a few seconds). Also, if people have suggestions on how to change the program to actually output the moves it would make, please let me know - right now it just responds with 1 for a win for X, 0 for a draw, and -1 for a win for O. And there are probably bugs! Thanks in advance import Data.List data Box = Blank | X | O deriving (Eq, Show) -- usage: call score with the board rows concatenated: score [Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank, Blank] -- or score [Blank, X, O, Blank, X, O, Blank, Blank, Blank] -- score is 1 if X wins, 0 for draw, -1 for lose score :: [Box] -> Int score g | haveline X g = 1 | haveline O g = -1 | gridfull g = 0 | isxmove g = maximum (map score (makeallmoves X g)) | otherwise = minimum (map score (makeallmoves O g)) tolines :: [Box] -> [[Box]] tolines [a1, a2, a3, b1, b2, b3, c1, c2, c3] = [ [a1,a2,a3], [b1,b2,b3], [c1,c2,c3], [a1,b1,c1], [a2,b2,c2], [a3,b3,c3], [a1,b2,c3], [a3,b2,c1] ] haveline :: Box->[Box] -> Bool haveline b g = any ([b,b,b]==) (tolines g) gridfull :: [Box] -> Bool gridfull g = not $ any (Blank==) g isxmove :: [Box] -> Bool isxmove g = let movecount = sum $ map (\b -> if b==Blank then 0 else 1) g in mod movecount 2 == 0 isomove = not . isxmove fl :: Box->[Box]->[Box]->[[Box]] fl b xs (Blank:ys) = [xs ++ b:ys] fl _ _ _ = [] makeallmoves :: Box->[Box]-> [[Box]] makeallmoves b g = concat $ zipWith (fl b) (inits g) (tails g) -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090907/9a7ec5f1/attachment.html From mpm at alumni.caltech.edu Mon Sep 7 17:54:17 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Mon Sep 7 17:33:23 2009 Subject: [Haskell-beginners] qtHaskell, ForeignPtr Message-ID: <64711.208.57.251.240.1252360457.squirrel@mail.alumni.caltech.edu> I'm trying to learn qtHaskell, an interface to the Qt GUI library. I am fairly new to Haskell, but have used Qt for a long time, so I thought I could probably reasonably attempt to grok qtHaskell at this point. My main question is: anyone recommended a good explanation of the foreign function or foreign pointer interface? My immediate question is that I was poking through the Qtc docs, and saw this: Documentation data Object a Constructors QObject !(ForeignPtr a) I am not sure how Haddock works. Is this telling me that an Object constructor takes one argument, which is of type QObject, where QObject is a type constructor that takes !(ForeignPtr a)? Or is this saying it takes two arguments, a QObject and a !(ForeignPtr a). That latter makes more sense I guess. What does the ! mean? Thanks, Mike From mpm at alumni.caltech.edu Mon Sep 7 17:57:20 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Mon Sep 7 17:36:26 2009 Subject: [Haskell-beginners] oops Message-ID: <53031.208.57.251.240.1252360640.squirrel@mail.alumni.caltech.edu> I'm sorry, I just noticed one of my prior questions was pretty silly. The second Object is the name of the type constructor. I realized that now. I was just fooled by the layout of Haddock and got temporarily confused. However, I'm still interested in learning more about what the ! means, and where I can learn about ForeignPtr (maybe a short tutorial to start). Thanks, Mike From bugfact at gmail.com Mon Sep 7 18:14:28 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Mon Sep 7 17:53:32 2009 Subject: [Haskell-beginners] oops In-Reply-To: <53031.208.57.251.240.1252360640.squirrel@mail.alumni.caltech.edu> References: <53031.208.57.251.240.1252360640.squirrel@mail.alumni.caltech.edu> Message-ID: About the ! These might help http://en.wikibooks.org/wiki/Haskell/Laziness http://en.wikibooks.org/wiki/Haskell/Graph_reduction On Mon, Sep 7, 2009 at 11:57 PM, Michael Mossey wrote: > I'm sorry, I just noticed one of my prior questions was pretty silly. The > second Object is the name of the type constructor. I realized that now. I > was just fooled by the layout of Haddock and got temporarily confused. > However, I'm still interested in learning more about what the ! means, and > where I can learn about ForeignPtr (maybe a short tutorial to start). > > Thanks, > Mike > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From daniel.is.fischer at web.de Mon Sep 7 18:36:00 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Sep 7 18:16:09 2009 Subject: [Haskell-beginners] qtHaskell, ForeignPtr In-Reply-To: <64711.208.57.251.240.1252360457.squirrel@mail.alumni.caltech.edu> References: <64711.208.57.251.240.1252360457.squirrel@mail.alumni.caltech.edu> Message-ID: <200909080036.00611.daniel.is.fischer@web.de> Am Montag 07 September 2009 23:54:17 schrieb Michael Mossey: > I'm trying to learn qtHaskell, an interface to the Qt GUI library. I am > fairly new to Haskell, but have used Qt for a long time, so I thought I > could probably reasonably attempt to grok qtHaskell at this point. > > My main question is: anyone recommended a good explanation of the foreign > function or foreign pointer interface? > > My immediate question is that I was poking through the Qtc docs, and saw > this: > > > Documentation > > data Object a > > Constructors > > QObject !(ForeignPtr a) > > I am not sure how Haddock works. Is this telling me that an Object > constructor takes one argument, which is of type QObject, where QObject is > a type constructor that takes !(ForeignPtr a)? Or is this saying it takes > two arguments, a QObject and a !(ForeignPtr a). That latter makes more > sense I guess. What does the ! mean? It tells you that the (Object a) datatype has a data-constructor called QObject (I know you already figured that out) which takes one argument of type (ForeignPtr a). However, the constructor's argument type has a bang (!), which means that the constructor is strict in its argument. For nonstrict constructors (data Ob a = Con a), you can happily create a value Con _|_ which will be a nice and harmless citizen until you try to look at its contents. For example, let x = Con undefined in x `seq` 3 returns 3, only let x = Con undefined in case x of Con y -> y `seq` 3 will raise an exception. If you make your constructor strict (data Thing a = SCon !a), it doesn't accept a bottom, the strict constructor evaluates its argument to weak head normal form, if it encounters a bottom, an exception is raised. So, Con _|_ /= _|_ but SCon _|_ == _|_ > > Thanks, > Mike From wagner.andrew at gmail.com Mon Sep 7 19:44:03 2009 From: wagner.andrew at gmail.com (Andrew Wagner) Date: Mon Sep 7 19:23:08 2009 Subject: [Haskell-beginners] idiomatic haskell question In-Reply-To: <19e5d1d00909071028s349a7552tae8036f3b1567d87@mail.gmail.com> References: <19e5d1d00909071028s349a7552tae8036f3b1567d87@mail.gmail.com> Message-ID: First of all, I highly recommend reading John Hughes' paper "Why Functional Programming Matters" (http://www.cs.chalmers.se/~rjmh/Papers/whyfp.pdf) to get a grasp of how to write alpha-beta and similar algorithms functionally. Secondly, I've been playing around lately with representing tree traversals as operations on a zipper. That is: type TreeTraversal a = State (TreeLoc a) This would allow you to cleanly write a traversal function which would "return" a score, but also have, as its state, a focus on the node which it has selected, from which you could easily get a path. See some of the top results at http://www.google.com/search?q=haskell+zipper for a refresher on zippers if needed. I hope to release a library around this code some day soon. On Mon, Sep 7, 2009 at 1:28 PM, Tom Doris wrote: > Hi all, I've recently started using Haskell and am looking for some > feedback on code I've written; I sometimes feel that maybe I'm not doing > things the best way possible that Haskell allows, and maybe missing out on > obvious improvements in brevity or elegance. So here's a tic-tac-toe solver > I wrote that does basic min-max search of the entire tree (not efficient > algorithmically, but it's tictactoe so a blank board can be fully solved in > a few seconds). Also, if people have suggestions on how to change the > program to actually output the moves it would make, please let me know - > right now it just responds with 1 for a win for X, 0 for a draw, and -1 for > a win for O. And there are probably bugs! > Thanks in advance > > import Data.List > data Box = Blank | X | O deriving (Eq, Show) > -- usage: call score with the board rows concatenated: score [Blank, Blank, > Blank, Blank, Blank, Blank, Blank, Blank, Blank] > -- or score [Blank, X, O, Blank, X, O, Blank, Blank, Blank] > -- score is 1 if X wins, 0 for draw, -1 for lose > > score :: [Box] -> Int > score g | haveline X g = 1 > | haveline O g = -1 > | gridfull g = 0 > | isxmove g = maximum (map score (makeallmoves X g)) > | otherwise = minimum (map score (makeallmoves O g)) > > tolines :: [Box] -> [[Box]] > tolines [a1, a2, a3, b1, b2, b3, c1, c2, c3] = [ [a1,a2,a3], [b1,b2,b3], > [c1,c2,c3], > [a1,b1,c1], [a2,b2,c2], > [a3,b3,c3], > [a1,b2,c3], [a3,b2,c1] ] > > haveline :: Box->[Box] -> Bool > haveline b g = any ([b,b,b]==) (tolines g) > > gridfull :: [Box] -> Bool > gridfull g = not $ any (Blank==) g > > isxmove :: [Box] -> Bool > isxmove g = let movecount = sum $ map (\b -> if b==Blank then 0 else 1) g > in mod movecount 2 == 0 > > isomove = not . isxmove > > fl :: Box->[Box]->[Box]->[[Box]] > fl b xs (Blank:ys) = [xs ++ b:ys] > fl _ _ _ = [] > > makeallmoves :: Box->[Box]-> [[Box]] > makeallmoves b g = concat $ zipWith (fl b) (inits g) (tails g) > > > > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090907/7be5e917/attachment-0001.html From hyangfji at gmail.com Mon Sep 7 22:35:29 2009 From: hyangfji at gmail.com (Hong Yang) Date: Mon Sep 7 22:14:32 2009 Subject: [Haskell-beginners] Help with CSV In-Reply-To: <92e42b740909021823x6e32cf56o8138e2b8339dac58@mail.gmail.com> References: <92e42b740909021823x6e32cf56o8138e2b8339dac58@mail.gmail.com> Message-ID: Thanks for your reply. I have a working program now using Text.CSV module. Do you see any that can be improved? When I print, the screen reads "Just "abc"". How can I get rid of "Just" in the most elegant way? Thanks, Hong -- file: ch22/PodMain.hs module Main where import System.Environment (getArgs) import Text.CSV import qualified Data.Map as M main = do [args] <- getArgs result <- parseCSVFromFile args case result of Left errmsg -> putStrLn "Error when parsing!" Right contents -> map_header_records contents map_header_records :: CSV -> IO () map_header_records [] = return () map_header_records (x:xs) = process x xs process :: [String] -> CSV -> IO () process x [] = return () process x (y:ys) = do let tuple = zip x y let hash = M.fromList tuple putStrLn (show (M.lookup "name" hash)) process x ys On Wed, Sep 2, 2009 at 8:23 PM, Keith Sheppard wrote: > Not quite code but... > > here is an example of parsing CSV > http://book.realworldhaskell.org/read/using-parsec.html and here is a > library that you can use which is similar > http://hackage.haskell.org/package/csv > > These approaches give you a 2D String list that you can do whatever > you want with. > > if you need to turn a string into a double and you know the string is > well formed i think the syntax looks like > > > let doubleVal = read stringVal :: Double > > There are better ways to do this if you need to be able to handle > formatting errors but I don't know them off the top of my head > > -Keith > > On Wed, Sep 2, 2009 at 6:40 PM, Hong Yang wrote: > > I need to process csv files that have the characteristics as follows: > > 1) each file has thousands of columns which have String, Int, and > Double > > types > > 2) the number of columns may change > > 3) for those columns whose name do not change, their location may > change > > > > I want to process some columns in 3) using Haskell. > > > > In Perl, I can easily have the code like below: > > > > use Text::CSV; > > my $csv = Text::CSV->new( { allow_whitespace => 1 } ); > > open my $temp, "<", "temp.csv" or die "Cannot open temp.csv! ($!)"; > > my @fields = @{ $csv->getline($temp) }; > > $csv->column_names(@fields); > > while ( my $hr = $csv->getline_hr($temp) ) { > > my $sn = $hr->{"UNIT:unitSerialNumber"}; > > # processing goes here ... > > } > > close $temp; > > > > Can someone please give me an equivalent code in Haskell? Then I can > digest > > and expand it. > > > > Thanks, > > > > Hong > > > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > > > > > -- > keithsheppard.name > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090907/971883c0/attachment.html From mle+hs at mega-nerd.com Mon Sep 7 22:45:30 2009 From: mle+hs at mega-nerd.com (Erik de Castro Lopo) Date: Mon Sep 7 22:24:38 2009 Subject: [Haskell-beginners] Help with CSV In-Reply-To: References: <92e42b740909021823x6e32cf56o8138e2b8339dac58@mail.gmail.com> Message-ID: <20090908124530.6e280a53.mle+hs@mega-nerd.com> Hong Yang wrote: > Thanks for your reply. I have a working program now using Text.CSV module. > > Do you see any that can be improved? > > When I print, the screen reads "Just "abc"". How can I get rid of "Just" in > the most elegant way? Try Data.Maybe.fromMaybe: http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html#v:fromMaybe Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/ From apfelmus at quantentunnel.de Tue Sep 8 04:42:19 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Tue Sep 8 04:21:45 2009 Subject: [Haskell-beginners] Re: number formatting with locale In-Reply-To: <80eb7b2e0909070205t649e6c01y648ae9756a42e751@mail.gmail.com> References: <80eb7b2e0909070205t649e6c01y648ae9756a42e751@mail.gmail.com> Message-ID: Dmitry Simonchik wrote: > I'm trying to print out Double in russian locale (we use "," as decimal > separator insetad of "."). I'm trying to set locale with > System.Locale.SetLocale like > > setLocale LC_ALL $ Just "ru_RU.UTF-8" > > this returns Just "ru_RU.UTF-8", so it seems that function call succeeded, > but when I call > > show 20.2 > > it just prints 20.2 and not desired 20,2 > > Can anyone please help? By design, show and read don't use the locale. Not to mention that since Haskell is pure, it is *impossible* for the expression show 20.2 :: String to depend on the current locale. I have never tried to use locales in Haskell, maybe someone else can help. Most likely, you'll need to do foreign imports of C functions. Regards, apfelmus -- http://apfelmus.nfshost.com From dima at simonchik.net Tue Sep 8 04:47:43 2009 From: dima at simonchik.net (Dmitry Simonchik) Date: Tue Sep 8 04:27:06 2009 Subject: [Haskell-beginners] Re: number formatting with locale In-Reply-To: References: <80eb7b2e0909070205t649e6c01y648ae9756a42e751@mail.gmail.com> Message-ID: <80eb7b2e0909080147j615d556fw73cc4022a29b4aaa@mail.gmail.com> Not to mention that > since Haskell is pure, it is *impossible* for the expression show 20.2 > :: String to depend on the current locale. > This is a very valuable remark. Thanks! I will think about using some special functions to read and write numbers depending on current locale. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090908/469ee3e1/attachment.html From byorgey at seas.upenn.edu Tue Sep 8 07:52:09 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue Sep 8 07:31:11 2009 Subject: [Haskell-beginners] Urgent: Defining Momory data Types in Haskell In-Reply-To: References: Message-ID: <20090908115209.GA29363@seas.upenn.edu> On Mon, Sep 07, 2009 at 06:37:16AM +0000, Akshay Dave wrote: > > (Memory maps I to Z) > lookup m i = ( meaning lookup for I in memory m) You shouldn't need to use anything so crass as pointers to accomplish this. Try taking a look at the Map data structure in Data.Map: http://hackage.haskell.org/packages/archive/containers/0.2.0.1/doc/html/Data-Map.html If there is some reason Data.Map won't work for you, please explain why so we can better understand what you are trying to do. -Brent From wirtwolff at gmail.com Tue Sep 8 10:31:10 2009 From: wirtwolff at gmail.com (Wirt Wolff) Date: Tue Sep 8 10:10:16 2009 Subject: [Haskell-beginners] Re: number formatting with locale In-Reply-To: <80eb7b2e0909080147j615d556fw73cc4022a29b4aaa@mail.gmail.com> References: <80eb7b2e0909070205t649e6c01y648ae9756a42e751@mail.gmail.com> <80eb7b2e0909080147j615d556fw73cc4022a29b4aaa@mail.gmail.com> Message-ID: <1252420124-sup-1102@chigamba> Excerpts from Dmitry Simonchik's message of Tue Sep 08 02:47:43 -0600 2009: > Not to mention that > > since Haskell is pure, it is *impossible* for the expression show 20.2 > > :: String to depend on the current locale. > > > > This is a very valuable remark. Thanks! I will think about using some > special functions to read and write numbers depending on current locale. The i18n module from hackage is worth a look for this: http://hackage.haskell.org/cgi-bin/hackage-scripts/package/i18n -- wmw From amy at nualeargais.ie Wed Sep 9 16:51:34 2009 From: amy at nualeargais.ie (=?ISO-8859-1?Q?Amy_de_Buitl=E9ir?=) Date: Wed Sep 9 16:30:36 2009 Subject: [Haskell-beginners] Problems defining a type with a multiplication function Message-ID: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> I'm trying to define a Quaternion, which is sort of a four-element vector with special rules for multiplication. ----- Quaternion.hs ----- data Quaternion = Quaternion Double Double Double Double deriving (Show) (*) :: (Quaternion a) => a -> a -> a (*) (Quaternion w1 x1 y1 z1) (Quaternion w2 x2 y2 z2) = Quaternion w x y z where w = w1*w2 - x1*x2 - y1*y2 - z1*z2 x = x1*w2 + w1*x2 + y1*z2 - z1*y2 y = w1*y2 - x1*z2 + y1*w2 + z1*x2 z = w1*z2 + x1*y2 - y1*x2 + z1*w2 ----- end code ----- When I try to load this into ghci, I get: Quaternion.hs:6:13: Ambiguous occurrence `*' It could refer to either `Main.*', defined at Quaternion.hs:5:0 or `Prelude.*', imported from Prelude ... and lots more messages like that. I understand roughly what the message means, but I don't know how to tell it that when I use "*" within the definition, I just want ordinary multiplication. Thanks in advance for any help! Amy -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090909/f2a3ffee/attachment.html From haskell at colquitt.org Wed Sep 9 17:02:47 2009 From: haskell at colquitt.org (John Dorsey) Date: Wed Sep 9 16:41:45 2009 Subject: [Haskell-beginners] Problems defining a type with a multiplication function In-Reply-To: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> References: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> Message-ID: <20090909210247.GI14826@colquitt.org> Amy, > Quaternion.hs:6:13: > Ambiguous occurrence `*' > It could refer to either `Main.*', defined at Quaternion.hs:5:0 > or `Prelude.*', imported from Prelude > > ... and lots more messages like that. I understand roughly what the message > means, but I don't know how to tell it that when I use "*" within the > definition, I just want ordinary multiplication. Thanks in advance for any You're redefining (*) in Main, which creates the ambiguity, which is the problem. One alternative is, instead of creating a new ambiguous (*), is use the existing one, and make it apply to your type. The existing (*) belongs to the typeclass Num. You can make your type an instance of Num, with your definition of (*): instance Num (Quaternion a) where q1 * q2 = ... q1 + q2 = undefined -- or, better, a valid definition q1 - q2 = undefined ... You'll find that this cascades into requiring you to define a handful of other class functions (+, -, negate, abs, signum, fromInteger) and you'll need Eq and Show instances. You can derive the latter, and you can give trivial (undefined or error) definitions for, say, negate if you won't be using it. Hope this helps. John From jfredett at gmail.com Wed Sep 9 17:05:04 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Sep 9 16:44:17 2009 Subject: [Haskell-beginners] Problems defining a type with a multiplication function In-Reply-To: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> References: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> Message-ID: <27841E46-CEBB-4859-A40F-C62F521C32D6@gmail.com> You need to write an instance of the Num class, eg instance Num Quaternion where (Q a b c d) + (Q e f g h) = ... (Q a b c d) * (Q e f g h) = ... etc This allows Haskell to overload things like numbers. You seem to be headed in the right direction w/ your type signature, except (*) has type Num a => a -> a -> a Which means "For any type `a` that is an instance of the type class `Num`, this is a closed binary function on that type" HTH /Joe On Sep 9, 2009, at 4:51 PM, Amy de Buitl?ir wrote: > I'm trying to define a Quaternion, which is sort of a four-element > vector with special rules for multiplication. > > ----- Quaternion.hs ----- > data Quaternion = Quaternion Double Double Double Double > deriving (Show) > > (*) :: (Quaternion a) => a -> a -> a > (*) (Quaternion w1 x1 y1 z1) (Quaternion w2 x2 y2 z2) = Quaternion w > x y z > where w = w1*w2 - x1*x2 - y1*y2 - z1*z2 > x = x1*w2 + w1*x2 + y1*z2 - z1*y2 > y = w1*y2 - x1*z2 + y1*w2 + z1*x2 > z = w1*z2 + x1*y2 - y1*x2 + z1*w2 > > ----- end code ----- > > When I try to load this into ghci, I get: > > Quaternion.hs:6:13: > Ambiguous occurrence `*' > It could refer to either `Main.*', defined at Quaternion.hs:5:0 > or `Prelude.*', imported from Prelude > > ... and lots more messages like that. I understand roughly what the > message means, but I don't know how to tell it that when I use "*" > within the definition, I just want ordinary multiplication. Thanks > in advance for any help! > > Amy > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From haskell at colquitt.org Wed Sep 9 17:10:38 2009 From: haskell at colquitt.org (John Dorsey) Date: Wed Sep 9 16:49:35 2009 Subject: [Haskell-beginners] Problems defining a type with a multiplication function In-Reply-To: <27841E46-CEBB-4859-A40F-C62F521C32D6@gmail.com> References: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> <27841E46-CEBB-4859-A40F-C62F521C32D6@gmail.com> Message-ID: <20090909211038.GJ14826@colquitt.org> Amy, > instance Num Quaternion where Use this (from Joe's email), and not > instance Num (Quaternion a) this (from mine). I had skimmed your code too quickly and wrote the above in error because I'd thought you had make Quaternion parametric. Unless you want the parametricity, as in: data Quaternion a = Quaternion a a a a class (Num a) => Num (Quaternion a) where ... Cheers, John From byorgey at seas.upenn.edu Wed Sep 9 17:50:46 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Wed Sep 9 17:29:44 2009 Subject: [Haskell-beginners] Problems defining a type with a multiplication function In-Reply-To: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> References: <77b28c350909091351o2c5c288bi6851b3cde1dd4c1b@mail.gmail.com> Message-ID: <20090909215046.GA10984@seas.upenn.edu> On Wed, Sep 09, 2009 at 09:51:34PM +0100, Amy de Buitl?ir wrote: > > ----- Quaternion.hs ----- > data Quaternion = Quaternion Double Double Double Double > deriving (Show) > > (*) :: (Quaternion a) => a -> a -> a Also, I should point out that the '(blah) => ...' syntax is only for type *classes*; Quaternion is just a data type so it doesn't make sense to use it in this way. The type signature instead ought to just say (*) :: Quaternion -> Quaternion -> Quaternion although as others have pointed out you will still get the error about (*) being ambiguous. You can either implement Num as others have suggested, or just use a different name for quaternion multiplication, such as, say, (*!). -Brent From rich.talley at gmail.com Wed Sep 9 23:50:13 2009 From: rich.talley at gmail.com (Richard Talley) Date: Wed Sep 9 23:29:11 2009 Subject: [Haskell-beginners] problem running yi on OS X Message-ID: I'm running OS X 10.5.8 on Intel. I installed GHC 6.10.4 and the Haskell Platform 2009.2.0.2 with no problem (thanks to everybody who made this so easy on OS X). The yi editor seems very interesting, so I installed it with 'cabal install yi' (after running 'cabal update'). That went OK (with some warnings about deprecated GHC features) and I was able to run yi which I found in ~/.cabal/bin Pressing h for help gave me a sample configuration file for yi which I edited to use cua keybindings and the (experimental) cocoa gui. Now when I tried to run yi again, I got this: Recompiling custom yi Launching custom yi: "/Users/rbtalley/.yi/yi-i386-darwin" yi: /Users/rbtalley/.yi/yi-i386-darwin: executeFile: does not exist (No such file or directory) Any suggestions? -- Rich -- Dealing with failure is easy: Work hard to improve. Success is also easy to handle: You've solved the wrong problem. Work hard to improve. -- Alan Perlis -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090909/fb248860/attachment.html From hyangfji at gmail.com Thu Sep 10 13:24:26 2009 From: hyangfji at gmail.com (Hong Yang) Date: Thu Sep 10 13:03:22 2009 Subject: [Haskell-beginners] hide Perl code inside Haskell program Message-ID: I was trying to hide Perl code inside Haskell program like the following: > module Main where > import System.Environment (getArgs) > import System.Cmd > main = do > args <- getArgs > rawSystem pl args > where pl = "#!/usr/bin/perl\n" ++ > "print \"testing ...\n\";" The program can run, but generates no output at all. Can someone tell me any other tricks which might be missing? (Do we need to use pipe here?) Thanks, Hong -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090910/0259444b/attachment.html From hiena03 at gmail.com Thu Sep 10 13:45:33 2009 From: hiena03 at gmail.com (=?ISO-8859-1?Q?Jos=E9_Prous?=) Date: Thu Sep 10 13:24:30 2009 Subject: [Haskell-beginners] hide Perl code inside Haskell program In-Reply-To: References: Message-ID: you are putting the perl script as the name of the command you can do something like: rawSystem "perl" ["-e","print \"testing ...\n\";" ] this is equivalent to call in the shell: perl -e "print \"testing ...\";" (sorry Hong I din't put reply to all in the fist mail) On Thu, Sep 10, 2009 at 1:24 PM, Hong Yang wrote: > I was trying to hide Perl code inside Haskell program like the following: > > > module Main where > > > import System.Environment (getArgs) > > import System.Cmd > > > main = do > > args <- getArgs > > rawSystem pl args > > where pl = "#!/usr/bin/perl\n" ++ > > "print \"testing ...\n\";" > > The program can run, but generates no output at all. Can someone tell me > any other tricks which might be missing? (Do we need to use pipe here?) > > Thanks, > > Hong > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090910/2c07e9de/attachment.html From velman at cox.net Thu Sep 10 15:36:54 2009 From: velman at cox.net (John Velman) Date: Thu Sep 10 15:15:51 2009 Subject: [Haskell-beginners] Parsec 2 and Parsec 3 Message-ID: <20090910193654.GA1999@cox.net> I've just started picking up Haskell where I left off a couple years ago when I switched from Linux to OS X (and some other things interfered with continuing my Haskell projects). I was using Parsec at the time -- must have been Parsec 2. When I loaded HOC it seemed to depend on Parsec 3, so I installed Parsec 3. Now -- suppose I want to use Parsec 2 for my own work. Is there a reasonably easy way to install Parsec 2 as, say Parsec2 and have both? Thanks, John Velman From chaddai.fouche at gmail.com Fri Sep 11 02:44:39 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Fri Sep 11 02:23:33 2009 Subject: [Haskell-beginners] Parsec 2 and Parsec 3 In-Reply-To: <20090910193654.GA1999@cox.net> References: <20090910193654.GA1999@cox.net> Message-ID: On Thu, Sep 10, 2009 at 9:36 PM, John Velman wrote: > I've just started picking up Haskell where I left off a couple years ago > when I switched from Linux to OS X (and some other things interfered with > continuing my Haskell projects). > > I was using Parsec at the time -- must have been Parsec 2. > > When I loaded HOC it seemed to depend on Parsec 3, so I installed Parsec 3. > > Now -- suppose I want to use Parsec 2 for my own work. > > Is there a reasonably easy way to install Parsec 2 as, say Parsec2 and have > both? You can install several version of a package, it's not a problem. You then have to say which version to use when compiling your code (by default it uses the latest version). If you want to install parsec3 for the code that may require it and use parsec2 automatically when compiling your own code, use ghc-pkg to hide the version 3. -- Jeda? From velman at cox.net Fri Sep 11 12:01:58 2009 From: velman at cox.net (John Velman) Date: Fri Sep 11 11:40:55 2009 Subject: [Haskell-beginners] Parsec 2 and Parsec 3 In-Reply-To: References: <20090910193654.GA1999@cox.net> Message-ID: <20090911160158.GA290@cox.net> Thanks, Jeda?. Very helpful, John V. On Fri, Sep 11, 2009 at 08:44:39AM +0200, Chadda? Fouch? wrote: > On Thu, Sep 10, 2009 at 9:36 PM, John Velman wrote: > > I've just started picking up Haskell where I left off a couple years ago > > when I switched from Linux to OS X (and some other things interfered with > > continuing my Haskell projects). > > > > I was using Parsec at the time -- must have been Parsec 2. > > > > When I loaded HOC it seemed to depend on Parsec 3, so I installed Parsec 3. > > > > Now -- suppose I want to use Parsec 2 for my own work. > > > > Is there a reasonably easy way to install Parsec 2 as, say Parsec2 and have > > both? > > You can install several version of a package, it's not a problem. You > then have to say which version to use when compiling your code (by > default it uses the latest version). If you want to install parsec3 > for the code that may require it and use parsec2 automatically when > compiling your own code, use ghc-pkg to hide the version 3. > > -- > Jeda? From hyangfji at gmail.com Fri Sep 11 17:18:01 2009 From: hyangfji at gmail.com (Hong Yang) Date: Fri Sep 11 16:56:53 2009 Subject: [Haskell-beginners] hide Perl code inside Haskell program In-Reply-To: References: Message-ID: This way works fine if the program has no arguments. What if I have to pass arguments such as "-i input -o output"? Thanks, Hong On Thu, Sep 10, 2009 at 12:45 PM, Jos? Prous wrote: > you are putting the perl script as the name of the command > you can do something like: rawSystem "perl" ["-e","print \"testing > ...\n\";" ] > this is equivalent to call in the shell: perl -e "print \"testing ...\";" > (sorry Hong I din't put reply to all in the fist mail) > On Thu, Sep 10, 2009 at 1:24 PM, Hong Yang wrote: > >> I was trying to hide Perl code inside Haskell program like the following: >> >> > module Main where >> >> > import System.Environment (getArgs) >> > import System.Cmd >> >> > main = do >> > args <- getArgs >> > rawSystem pl args >> > where pl = "#!/usr/bin/perl\n" ++ >> > "print \"testing ...\n\";" >> >> The program can run, but generates no output at all. Can someone tell me >> any other tricks which might be missing? (Do we need to use pipe here?) >> >> Thanks, >> >> Hong >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090911/48f22a56/attachment.html From alexey.skladnoy at gmail.com Sat Sep 12 04:19:42 2009 From: alexey.skladnoy at gmail.com (Khudyakov Alexey) Date: Sat Sep 12 03:52:37 2009 Subject: [Haskell-beginners] hide Perl code inside Haskell program In-Reply-To: References: Message-ID: <200909121219.42576.alexey.skladnoy@gmail.com> ? ????????? ?? ??????? 12 ???????? 2009 01:18:01 ????? Hong Yang ???????: > This way works fine if the program has no arguments. What if I have to pass > arguments such as "-i input -o output"? > You can just run perl and feed you program to perl's stdin. With this you can add all flags you like Shell $ echo 'print "fff\n"; $i=1; print "$i\n";' | perl fff 1 From mpm at alumni.caltech.edu Sat Sep 12 11:14:28 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Sat Sep 12 10:53:35 2009 Subject: [Haskell-beginners] Maybe, Either Message-ID: <4AABBAD4.4090900@alumni.caltech.edu> I want to use 'lookup' inside an Either String monad. So I want to write something like eitherLookup :: Eq a => String -> a -> [(a,b)] -> Either String b eitherLookup s x ps = case lookup x ps of Just y -> Right y Nothing -> Left s Is there such a function existing? Thanks, Mike From nonowarn at gmail.com Sat Sep 12 11:51:56 2009 From: nonowarn at gmail.com (Yusaku Hashimoto) Date: Sat Sep 12 11:30:45 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <4AABBAD4.4090900@alumni.caltech.edu> References: <4AABBAD4.4090900@alumni.caltech.edu> Message-ID: I think there is no such function in standard library. But you can also do the same by `maybe (Left err) Right (lookup key assoc)` without defining eitherLookup. HTH -nwn On Sun, Sep 13, 2009 at 12:14 AM, Michael Mossey wrote: > I want to use 'lookup' inside an Either String monad. So I want to write > something like > > eitherLookup :: Eq a => String -> a -> [(a,b)] -> Either String b > eitherLookup s x ps = case lookup x ps of > ? ? ? ? ? ? ? ? ? ? ? ? Just y -> Right y > ? ? ? ? ? ? ? ? ? ? ? ? Nothing -> Left s > > Is there such a function existing? > > Thanks, > Mike > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From michael at snoyman.com Sat Sep 12 14:13:58 2009 From: michael at snoyman.com (Michael Snoyman) Date: Sat Sep 12 13:52:49 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: References: <4AABBAD4.4090900@alumni.caltech.edu> Message-ID: <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> I often times have to write a lookup function that returns its value into any monad instead of just Maybe. For example: mLookup :: (Eq k, Monad m) => k -> [(k, v)] -> m v mLookup k pairs = case lookup k pairs of Nothing -> fail "mLookup: nothing found" Just v -> return v Hope that helps. On Sat, Sep 12, 2009 at 6:51 PM, Yusaku Hashimoto wrote: > I think there is no such function in standard library. > > But you can also do the same by `maybe (Left err) Right (lookup key > assoc)` without defining eitherLookup. > > HTH > -nwn > > On Sun, Sep 13, 2009 at 12:14 AM, Michael Mossey > wrote: > > I want to use 'lookup' inside an Either String monad. So I want to write > > something like > > > > eitherLookup :: Eq a => String -> a -> [(a,b)] -> Either String b > > eitherLookup s x ps = case lookup x ps of > > Just y -> Right y > > Nothing -> Left s > > > > Is there such a function existing? > > > > Thanks, > > Mike > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090912/0080fad9/attachment.html From byorgey at seas.upenn.edu Sun Sep 13 08:40:11 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sun Sep 13 08:18:58 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> Message-ID: <20090913124011.GA31381@seas.upenn.edu> On Sat, Sep 12, 2009 at 09:13:58PM +0300, Michael Snoyman wrote: > I often times have to write a lookup function that returns its value into > any monad instead of just Maybe. For example: > > mLookup :: (Eq k, Monad m) => k -> [(k, v)] -> m v > mLookup k pairs = case lookup k pairs of > Nothing -> fail "mLookup: nothing found" > Just v -> return v > This is actually the type that the lookup function USED to have, but it was changed since monads actually have nothing to do with failing (the fail method is just a hack used to handle pattern-match failures in do-notation). Probably a better implementation of this would be mLookup :: (Eq k, MonadPlus m) => k -> [(k,v)] -> m v mLookup k pairs = maybe mzero return (lookup k pairs) -Brent From john.moore54 at gmail.com Sun Sep 13 14:01:36 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Sep 13 13:40:23 2009 Subject: [Haskell-beginners] parse error on input Message-ID: <4f7ad1ad0909131101x4f582b3cq960ea6ab1800e1c8@mail.gmail.com> Hi, Can anyone explain what I'm doing wrong. I'm learning Haskell using the book real world haskell. It tells me to load programs and update cabal using '$' symbol but everytime I enter it tells me parse error on input '$'. The programs are taken from the book so there nothing wrong with them.What amI misssing? Regards John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090913/01151b80/attachment.html From colin at colina.demon.co.uk Sun Sep 13 14:10:24 2009 From: colin at colina.demon.co.uk (Colin Paul Adams) Date: Sun Sep 13 13:49:11 2009 Subject: [Haskell-beginners] parse error on input In-Reply-To: <4f7ad1ad0909131101x4f582b3cq960ea6ab1800e1c8@mail.gmail.com> (John Moore's message of "Sun\, 13 Sep 2009 19\:01\:36 +0100") References: <4f7ad1ad0909131101x4f582b3cq960ea6ab1800e1c8@mail.gmail.com> Message-ID: >>>>> "John" == John Moore writes: John> Hi, Can anyone explain what I'm doing wrong. I'm learning John> Haskell using the book real world haskell. It tells me to John> load programs and update cabal using '$' symbol but John> everytime I enter it tells me parse error on input '$'. The John> programs are taken from the book so there nothing wrong with John> them.What amI misssing? It sounds like you are mistaking the unix command line prompt symbol (that is: $) for something you are supposed to type. E.g. If it says: $ cabal install then it means type: cabal install at the command line prompt. -- Colin Adams Preston Lancashire From michael at snoyman.com Mon Sep 14 14:42:22 2009 From: michael at snoyman.com (Michael Snoyman) Date: Mon Sep 14 14:21:07 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <20090913124011.GA31381@seas.upenn.edu> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> Message-ID: <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> On Sun, Sep 13, 2009 at 3:40 PM, Brent Yorgey wrote: > On Sat, Sep 12, 2009 at 09:13:58PM +0300, Michael Snoyman wrote: > > I often times have to write a lookup function that returns its value into > > any monad instead of just Maybe. For example: > > > > mLookup :: (Eq k, Monad m) => k -> [(k, v)] -> m v > > mLookup k pairs = case lookup k pairs of > > Nothing -> fail "mLookup: nothing found" > > Just v -> return v > > > > This is actually the type that the lookup function USED to have, but > it was changed since monads actually have nothing to do with failing > (the fail method is just a hack used to handle pattern-match failures > in do-notation). Probably a better implementation of this would be > > mLookup :: (Eq k, MonadPlus m) => k -> [(k,v)] -> m v > mLookup k pairs = maybe mzero return (lookup k pairs) > > I understand that fail being in Monad is controversial, but my version of the function works in *all* monads. This is very useful for: 1) Quickly writing up code in the IO monad (ie, for a shell script) 2) Check out the data-objects library; having an mLookup function makes dealing with mappings very convenient. Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090914/437f6726/attachment.html From mpm at alumni.caltech.edu Mon Sep 14 16:39:04 2009 From: mpm at alumni.caltech.edu (Michael P Mossey) Date: Mon Sep 14 16:18:06 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909141143w41b3c52ei3d53aecb3d78575d@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <4AAC027A.3090406@alumni.caltech.edu> <29bf512f0909141143w41b3c52ei3d53aecb3d78575d@mail.gmail.com> Message-ID: <4AAEA9E8.9070004@alumni.caltech.edu> Michael Snoyman wrote: > Can you give me a better idea of what you mean by catching errors? That > could mean a lot of things. > > Michael Hi Michael, I mean that I need my software not to exit the program on an error condition, but have a higher level "catch" of that condition which handles it gracefully. I'm writing a musical score editor for personal use in my spare hobby time. Because it's just a bit of spare time, I can't make a promise my software won't have bugs. When I'm working on a score, I don't want to lose my work when an error occurs, such as internal errors that violate the invariants I've established in my data structures. So any action that results in an error should trigger a message box that says "such-and-such error" and otherwise leave the data unchanged. Then I can save my work and attempt to debug the problem. Thanks, Mike From byorgey at seas.upenn.edu Mon Sep 14 20:08:08 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon Sep 14 19:46:50 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> Message-ID: <20090915000808.GA12753@seas.upenn.edu> On Mon, Sep 14, 2009 at 09:42:22PM +0300, Michael Snoyman wrote: > On Sun, Sep 13, 2009 at 3:40 PM, Brent Yorgey wrote: > > > On Sat, Sep 12, 2009 at 09:13:58PM +0300, Michael Snoyman wrote: > > > I often times have to write a lookup function that returns its value into > > > any monad instead of just Maybe. For example: > > > > > > mLookup :: (Eq k, Monad m) => k -> [(k, v)] -> m v > > > mLookup k pairs = case lookup k pairs of > > > Nothing -> fail "mLookup: nothing found" > > > Just v -> return v > > > > > > > This is actually the type that the lookup function USED to have, but > > it was changed since monads actually have nothing to do with failing > > (the fail method is just a hack used to handle pattern-match failures > > in do-notation). Probably a better implementation of this would be > > > > mLookup :: (Eq k, MonadPlus m) => k -> [(k,v)] -> m v > > mLookup k pairs = maybe mzero return (lookup k pairs) > > > > > I understand that fail being in Monad is controversial, but my version of > the function works in *all* monads. This is very useful for: It doesn't work in *all* monads -- it only works in monads which support a sensible notion of failure. This is exactly what is captured by the MonadPlus constraint on my version of mLookup. And, in fact, any monad in context of which you would want to use mLookup (IO, Maybe, [], ...) are already instances of MonadPlus. Also, fail being in Monad isn't controversial, it's just wrong. =) The only controversial thing is what to DO about it now that it's there... -Brent From allbery at ece.cmu.edu Mon Sep 14 23:21:17 2009 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Mon Sep 14 23:00:19 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> Message-ID: Skipped content of type multipart/alternative-------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 195 bytes Desc: This is a digitally signed message part Url : http://www.haskell.org/pipermail/beginners/attachments/20090914/cfd50e58/PGP.bin From nonowarn at gmail.com Tue Sep 15 02:14:28 2009 From: nonowarn at gmail.com (Yusaku Hashimoto) Date: Tue Sep 15 01:53:10 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> Message-ID: I prefer Alternative to MonadPlus for explaining failure. It has better name and operator for failure and try-another. import Control.Applicative aLookup :: (Alternative f, Eq k) => k -> [(k,v)] -> f v aLookup key pairs = maybe empty pure $ lookup key pairs -nwn On Tue, Sep 15, 2009 at 12:21 PM, Brandon S. Allbery KF8NH wrote: > On Sep 14, 2009, at 14:42 , Michael Snoyman wrote: > > I understand that fail being in Monad is controversial, but my version of > the function works in *all* monads. This is very > > Not really; "fail" in non-MonadPlus-es is a rather poorly defined notion, > and there are no guarantees that the result will be at all sane. ?"mzero" is > well defined. > -- > brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com > system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu > electrical and computer engineering, carnegie mellon university ? ?KF8NH > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From conor at strictlypositive.org Tue Sep 15 04:21:02 2009 From: conor at strictlypositive.org (Conor McBride) Date: Tue Sep 15 03:59:57 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> Message-ID: <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> Hi This topic comes up a lot, and this is what I usually say when it does. It's a thing I learned from James McKinna, many years ago... Might I gently suggest that there is a much better, more natural way to abstract over every type-former which has some sort of return/pure-like thing and some sort of mzero/empty like thing? You could use the type-former which is inductively defined to be the least such thing, and as such has a canonical mapping to all the others, namely Maybe. It's not necessarily a good idea to fix on Monad or MonadPlus as there are other choices. For example, On 15 Sep 2009, at 07:14, Yusaku Hashimoto wrote: > I prefer Alternative to MonadPlus for explaining failure. It has > better name and operator for failure and try-another. > > import Control.Applicative > > aLookup :: (Alternative f, Eq k) => k -> [(k,v)] -> f v > aLookup key pairs = maybe empty pure $ lookup key pairs there are notorious non-monadic instances for the above f (some formulations of parsing, in particular). So, >> I understand that fail being in Monad is controversial, but my >> version of >> the function works in *all* monads. this is a touch presumptuous. On the one hand, Brent is right when he says > It doesn't work in *all* monads -- it only works in monads which > support a sensible notion of failure. but he's perhaps excessive when he says > This is exactly what is captured by the MonadPlus constraint > on my version of mLookup. because it's not exact: it requires mplus as well as a sensible notion of failure. And yes, why should we insist on (>>=) when we just need a return and an mzero? Incidentally, I don't know where the MonadPlus instance > (IO, Maybe, [], ...) are already instances of MonadPlus. of IO is coming from, but I want it caught and locked up now (in STM, for example) before it does any permanent damage. Why not factor out the failure-prone operations from the business of interpreting failure in some failure-supporting context? Work concretely while you can (types stay shorter, error messages make more sense) then apply adapters malt :: Alternative f => Maybe x -> f x malt = maybe empty pure mop :: MonadPlus m => Maybe x -> m x mop = maybe mzero return when you need to? This also reduces the risk of connecting an ambiguous supplier to an ambiguous consumer, (show . read) style. The message clearly bears repeating. Inductive definition is a concrete form of abstraction. Don't be fooled by its appearance: Maybe is the most abstract choice here -- the classier options demand more structure than is needed and thus exclude use-cases. I'll crawl back under my stone now. All the best Conor From apfelmus at quantentunnel.de Tue Sep 15 06:25:43 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Tue Sep 15 06:04:51 2009 Subject: [Haskell-beginners] Re: Maybe, Either In-Reply-To: <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> Message-ID: Conor McBride wrote: > > malt :: Alternative f => Maybe x -> f x > malt = maybe empty pure > > mop :: MonadPlus m => Maybe x -> m x > mop = maybe mzero return > > The message clearly bears repeating. Inductive definition is > a concrete form of abstraction. Don't be fooled by its > appearance: Maybe is the most abstract choice here -- the > classier options demand more structure than is needed and > thus exclude use-cases. Seconded! Maybe is the most general choice. The only issue left here would be that the combinators malt and mop are missing from the standard library. Once again, their purpose is not generality, but the convenience of overloading. Regards, apfelmus -- http://apfelmus.nfshost.com From mpm at alumni.caltech.edu Tue Sep 15 09:45:14 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Tue Sep 15 09:23:57 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> Message-ID: <1227.75.50.163.142.1253022314.squirrel@mail.alumni.caltech.edu> As a beginner, I'm not directly following the usefulness of these alternative implementations. I thought I would give some example code. Here I am trying to handle errors with Either String. You can read it here or in hpaste.org: import Data.Ratio import qualified Data.Map as Map -- An elemental music object such as note, rest, dynamic mark, etc. data MusicObject = MusicObject ... -- A composition has several "streams". A stream could be a continuous -- melody that appears on a single staff, or other types of data that -- are arranged serially in time. data Time = Rational data StreamId = StreamId ... data MusicStream = (StreadId, Map.Map Time MusicObject) data Comp = [MusicStream] -- A cursor is a concept used to "point to" a note or generalized location -- in the composition so that editing can be done at that point. For now, -- all we need is to point to the stream and time. data Cursor = Cursor { getCurId :: StreamId , getCurTime :: Time } -- Utility to make it easier to annotate an Either monad with a function -- that catches an error message, prepends a context message, and rethrows. ce :: String -> Either String a -> Either String a ce c = (flip catchError) (\s -> throwError (c ++ "\n" ++ s)) -- Utility to replace an item in an assoc list, inside the Either String -- monad. replaceAlist :: Eq a => a -> b -> [(a,b)] -> Either String [(a,b)] replaceAlist _ _ [] = throwError "Item not found in alist." replaceAlist iid obj (x:xs) = if fst x == iid then return $ (iid,obj) : xs else do rem <- replaceAlist iid obj xs return $ x : rem ... -- Delete a note from a composition. Deleting the last note in a stream is -- an error condition. -- -- Conditions that will cause an error: -- - cursor stream id doesn't exist in the composition -- - there is no note at the given cursor -- - there is only one note in the stream (so deleting it would delete -- the last note) compDeleteNote :: Cursor -> Comp -> Either String Comp compDeleteNote cur comp = ce "In compeDeleteNote:" $ do let Cursor { getCurId=iid, getCurTime=t } = cur -- First internal error might occur if no stream with the cursor's id -- occurs in the Comp. oldMap <- maybe (Left "no such stream") Right (lookup iid comp) -- Second internal error: no music object is found at the cursor's time. moAtCur <- maybe (Left "no m.o. at cursor") Right (Map.lookup t oldMap) let durAtCur = getDur moAtCur (l,r) = Map.split t oldMap r' = Map.mapKeys (\k -> k - durAtCur) r joined = Map.union l r' -- Third error condition: this action deleted the last note. if Map.null joined then (Left "deleted last note") else Right () replaceAlist iid joined comp From michael at snoyman.com Tue Sep 15 11:56:19 2009 From: michael at snoyman.com (Michael Snoyman) Date: Tue Sep 15 11:35:00 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> Message-ID: <29bf512f0909150856l2d20889cr87b881f1297898e8@mail.gmail.com> On Tue, Sep 15, 2009 at 6:21 AM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote: > On Sep 14, 2009, at 14:42 , Michael Snoyman wrote: > > I understand that fail being in Monad is controversial, but my version of > the function works in *all* monads. This is very > > > Not really; "fail" in non-MonadPlus-es is a rather poorly defined notion, > and there are no guarantees that the result will be at all sane. "mzero" is > well defined. > mzero also does not allow giving error messages. There are times when you want to be able to fail with an explanation of why. fail seems to fit the bill properly for this (fail taking a String argument and all...). Now you point out that fail is not always properly defined. I quite agree with that. Nonetheless, in the simple cases I am trying to address here, it is IMO the best option available. If you end up using the function only with monads that properly define fail, then all the better. Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090915/c1c0e99e/attachment.html From nonowarn at gmail.com Tue Sep 15 13:59:36 2009 From: nonowarn at gmail.com (Yusaku Hashimoto) Date: Tue Sep 15 13:38:20 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> Message-ID: If I understood your post correctly, you said - generalizing lookup to MonadPlus or Alternative or such classes are not necessary - use Maybe as usual, we should use adapters as we need Conor, You have said this many times elsewhere, but unfortunatelly, I heard it for the first time =) so please correct me if I'm wrong. I thought generalizing lookup is good example for usage of the MonadPlus as I read in RWH[1], but you said it's not necesarry. Now, I understood there are two positions for such classes. One is using generalizing for it, another is not. So, I want to know that when such classes should be used from later position. Heinrich suggested that is for overloading. But do any other usages are exist? [1]: http://book.realworldhaskell.org/read/programming-with-monads.html#VCard.hs:lookupM Cheers -nwn On Tue, Sep 15, 2009 at 5:21 PM, Conor McBride wrote: > Hi > > This topic comes up a lot, and this is what I usually say when > it does. It's a thing I learned from James McKinna, many years > ago... > > Might I gently suggest that there is a much better, more > natural way to abstract over every type-former which has > some sort of return/pure-like thing and some sort of mzero/empty > like thing? You could use the type-former which is inductively > defined to be the least such thing, and as such has a canonical > mapping to all the others, namely Maybe. > > It's not necessarily a good idea to fix on Monad or MonadPlus > as there are other choices. For example, > > On 15 Sep 2009, at 07:14, Yusaku Hashimoto wrote: > >> I prefer Alternative to MonadPlus for explaining failure. It has >> better name and operator for failure and try-another. >> >> import Control.Applicative >> >> aLookup :: (Alternative f, Eq k) => k -> [(k,v)] -> f v >> aLookup key pairs = maybe empty pure $ lookup key pairs > > there are notorious non-monadic instances for the above f > (some formulations of parsing, in particular). So, > >>> I understand that fail being in Monad is controversial, but my version of >>> the function works in *all* monads. > > this is a touch presumptuous. On the one hand, Brent is right > when he says > >> It doesn't work in *all* monads -- it only works in monads which >> support a sensible notion of failure. > > but he's perhaps excessive when he says > >> This is exactly what is captured by the MonadPlus constraint >> on my version of mLookup. > > because it's not exact: it requires mplus as well as a sensible > notion of failure. And yes, why should we insist on (>>=) when > we just need a return and an mzero? ?Incidentally, I don't know > where the MonadPlus instance > >> (IO, Maybe, [], ...) are already instances of MonadPlus. > > of IO is coming from, but I want it caught and locked up now (in > STM, for example) before it does any permanent damage. > > Why not factor out the failure-prone operations from the business > of interpreting failure in some failure-supporting context? Work > concretely while you can (types stay shorter, error messages make > more sense) then apply adapters > > malt :: Alternative f => Maybe x -> f x > malt = maybe empty pure > > mop :: MonadPlus m => Maybe x -> m x > mop = maybe mzero return > > when you need to? This also reduces the risk of connecting an > ambiguous supplier to an ambiguous consumer, (show . read) style. > > The message clearly bears repeating. Inductive definition is > a concrete form of abstraction. Don't be fooled by its > appearance: Maybe is the most abstract choice here -- the > classier options demand more structure than is needed and > thus exclude use-cases. > > I'll crawl back under my stone now. > > All the best > > Conor > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From michael at snoyman.com Tue Sep 15 16:21:46 2009 From: michael at snoyman.com (Michael Snoyman) Date: Tue Sep 15 16:00:28 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <20090915000808.GA12753@seas.upenn.edu> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <20090915000808.GA12753@seas.upenn.edu> Message-ID: <29bf512f0909151321u616c63b4p81e89b093b3703cf@mail.gmail.com> On Tue, Sep 15, 2009 at 3:08 AM, Brent Yorgey wrote: > > It doesn't work in *all* monads -- it only works in monads which > support a sensible notion of failure. This is exactly what is > captured by the MonadPlus constraint on my version of mLookup. And, > in fact, any monad in context of which you would want to use mLookup > (IO, Maybe, [], ...) are already instances of MonadPlus. > I'm looking at the Control.Monad documentation ( http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html), and it doesn't seem that IO is an instance of MonadPlus. I get the same results with a simple code check. Can you clarify? Calling lookup from IO is a common use case for me. Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090915/5d6be542/attachment.html From allbery at ece.cmu.edu Tue Sep 15 16:49:21 2009 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Tue Sep 15 16:28:20 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909151321u616c63b4p81e89b093b3703cf@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <20090915000808.GA12753@seas.upenn.edu> <29bf512f0909151321u616c63b4p81e89b093b3703cf@mail.gmail.com> Message-ID: <20D858B9-27F9-4A76-8E73-E5EDFE34EDF5@ece.cmu.edu> Skipped content of type multipart/alternative-------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 195 bytes Desc: This is a digitally signed message part Url : http://www.haskell.org/pipermail/beginners/attachments/20090915/683dab3a/PGP-0001.bin From alexmac at adobe.com Tue Sep 15 17:56:08 2009 From: alexmac at adobe.com (Alexander MacDonald) Date: Tue Sep 15 17:34:53 2009 Subject: [Haskell-beginners] Using state inside quickcheck generators Message-ID: Hi guys, I?ve been using quickcheck for a while and I?m having no problem making generators for my data types, however recently I stumbled upon a situation where I need to maintain State inside the quickcheck generator functions. Here's a simple example, imagine I've got a basic language which only has two features, variable definitions and assignments: type Script = [Statement] data Name = Name String deriving (Show,Eq) data Statement = Def Name Int | Assign Name Name deriving Show Generating random scripts is easy enough, just make each type an instance of Arbitrary with a suitable generator function, however I want to make sure that the generated scripts are syntactically valid. In this case that means no "Name" can be used in an "Assign" statement unless it has previously been defined by a "Def" statement. My knowledge of using monads is somewhat basic so I'm struggling to figure out how to use the State monad with QuickCheck. I've pasted my current effort on the web: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=9412 If anyone could spare a moment to glance at my code and give me a push in the right direction that would be much appreciated! Thanks, Alex Mac From michael at snoyman.com Tue Sep 15 18:19:53 2009 From: michael at snoyman.com (Michael Snoyman) Date: Tue Sep 15 17:58:32 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <20D858B9-27F9-4A76-8E73-E5EDFE34EDF5@ece.cmu.edu> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <20090915000808.GA12753@seas.upenn.edu> <29bf512f0909151321u616c63b4p81e89b093b3703cf@mail.gmail.com> <20D858B9-27F9-4A76-8E73-E5EDFE34EDF5@ece.cmu.edu> Message-ID: <29bf512f0909151519q5e3c3bhd3ce4063ff7f23e0@mail.gmail.com> On Tue, Sep 15, 2009 at 11:49 PM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote: > On Sep 15, 2009, at 16:21 , Michael Snoyman wrote: > > On Tue, Sep 15, 2009 at 3:08 AM, Brent Yorgey wrote: > >> It doesn't work in *all* monads -- it only works in monads which >> support a sensible notion of failure. This is exactly what is >> captured by the MonadPlus constraint on my version of mLookup. And, >> in fact, any monad in context of which you would want to use mLookup >> (IO, Maybe, [], ...) are already instances of MonadPlus. >> > > I'm looking at the Control.Monad documentation ( > http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html), > and it doesn't seem that IO is an instance of MonadPlus. I get the same > results with a simple code check. Can you clarify? Calling lookup from IO is > a common use case for me. > > > That's a bit of stupidity I wish would be fixed; for some bizarre reason, > the instance is in Control.Monad.Error. > And according to the documentation, that instance has a broken version of mzero. So it seems the argument of use MonadPlus because fail is not always defined properly doesn't exactly hold much water here. Can you tell me why you would still recommend representing lookup failure with the mzero function instead of the seemingly more aptly named and more available fail function? Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090915/215e6da9/attachment.html From alexander.dunlap at gmail.com Tue Sep 15 18:33:25 2009 From: alexander.dunlap at gmail.com (Alexander Dunlap) Date: Tue Sep 15 18:12:23 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909150856l2d20889cr87b881f1297898e8@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <29bf512f0909150856l2d20889cr87b881f1297898e8@mail.gmail.com> Message-ID: <57526e770909151533y67f86275u5bd3c5222e93c9@mail.gmail.com> On Tue, Sep 15, 2009 at 8:56 AM, Michael Snoyman wrote: > > > On Tue, Sep 15, 2009 at 6:21 AM, Brandon S. Allbery KF8NH > wrote: >> >> On Sep 14, 2009, at 14:42 , Michael Snoyman wrote: >> >> I understand that fail being in Monad is controversial, but my version of >> the function works in *all* monads. This is very >> >> Not really; "fail" in non-MonadPlus-es is a rather poorly defined notion, >> and there are no guarantees that the result will be at all sane. ?"mzero" is >> well defined. > > mzero also does not allow giving error messages. There are times when you > want to be able to fail with an explanation of why. fail seems to fit the > bill properly for this (fail taking a String argument and all...). In general, I think this is a bad idea. Except for short, throwaway code, Strings should not be used to encode errors simply because if your error message is a String, client code basically has no choice but to show it to the user and probably exit: since it's quite hard for the code to parse the String back in to find out what the error message was, it's hard for client code to recover from the error. If you have multiple types of errors from a single function, it's better to define your own error type with constructors representing different types of errors so client code can figure out what went wrong. (And in many cases, I have found that if a function can fail in multiple ways, I am trying to do too much in one function. There are, of course, numerous exceptions.) Alex From byorgey at seas.upenn.edu Tue Sep 15 18:50:45 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue Sep 15 18:29:25 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909151519q5e3c3bhd3ce4063ff7f23e0@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <20090915000808.GA12753@seas.upenn.edu> <29bf512f0909151321u616c63b4p81e89b093b3703cf@mail.gmail.com> <20D858B9-27F9-4A76-8E73-E5EDFE34EDF5@ece.cmu.edu> <29bf512f0909151519q5e3c3bhd3ce4063ff7f23e0@mail.gmail.com> Message-ID: <20090915225045.GA30118@seas.upenn.edu> On Wed, Sep 16, 2009 at 01:19:53AM +0300, Michael Snoyman wrote: > On Tue, Sep 15, 2009 at 11:49 PM, Brandon S. Allbery KF8NH < > allbery@ece.cmu.edu> wrote: > > > I'm looking at the Control.Monad documentation ( > > http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html), > > and it doesn't seem that IO is an instance of MonadPlus. I get the same > > results with a simple code check. Can you clarify? Calling lookup from IO is > > a common use case for me. > > > > > > That's a bit of stupidity I wish would be fixed; for some bizarre reason, > > the instance is in Control.Monad.Error. > > > > And according to the documentation, that instance has a broken version of > mzero. So it seems the argument of use MonadPlus because fail is not always > defined properly doesn't exactly hold much water here. Can you tell me why > you would still recommend representing lookup failure with the mzero > function instead of the seemingly more aptly named and more available fail > function? The IO instance of mzero is only broken in the sense that it does not satisfy the law m >> mzero === mzero since the effects of m will happen on the left but not on the right. But this is to be expected with the IO monad: if you have to perform some I/O effects in order to find out whether the computation fails or not, you can't expect to roll back the effects once you do fail. In practice I don't think this would be much of an issue. But anyway, the reason I recommend not using fail is because it is an inelegant, unprincipled hack. If you have no problem with inelegant, unprincipled hacks then I guess I won't be able to convince you. =) Also, if you haven't already, you should read the email from Conor McBride in this same thread: after reading his email I'm now of the opinion that you shouldn't even use MonadPlus, but simply use Maybe. -Brent From allbery at ece.cmu.edu Tue Sep 15 23:09:16 2009 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Tue Sep 15 22:48:11 2009 Subject: [Haskell-beginners] Maybe, Either In-Reply-To: <29bf512f0909151519q5e3c3bhd3ce4063ff7f23e0@mail.gmail.com> References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <20090915000808.GA12753@seas.upenn.edu> <29bf512f0909151321u616c63b4p81e89b093b3703cf@mail.gmail.com> <20D858B9-27F9-4A76-8E73-E5EDFE34EDF5@ece.cmu.edu> <29bf512f0909151519q5e3c3bhd3ce4063ff7f23e0@mail.gmail.com> Message-ID: Skipped content of type multipart/alternative-------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 195 bytes Desc: This is a digitally signed message part Url : http://www.haskell.org/pipermail/beginners/attachments/20090915/a43cfbdc/PGP.bin From orclev at gmail.com Thu Sep 17 01:40:31 2009 From: orclev at gmail.com (Kyle Murphy) Date: Thu Sep 17 01:19:08 2009 Subject: [Haskell-beginners] Using state inside quickcheck generators In-Reply-To: References: Message-ID: <2db78cee0909162240o33a9eeb6vdba21a12947b4391@mail.gmail.com> I myself am fairly new to Haskell and I'm trying to understand Monads better myself, but maybe this could help? http://user.cs.tu-berlin.de/~magr/pub/Transformers.en.html It's a little out of date and won't compile cleanly on newer instances of GHC without changing all the instances of Map.lookup ... to return $ fromJust Map.lookup, but once you make that small change it works fine. Also, I'm not familiar with the QuickCheck package, but I browsed through it quickly and Test.QuickCheck.Monadic caught my eye. Perhaps a State or StateT combined with IdM or PropertyM would do what you want? On Tue, Sep 15, 2009 at 5:56 PM, Alexander MacDonald wrote: > Hi guys, I?ve been using quickcheck for a while and I?m having no problem > making generators for my data types, however recently I stumbled upon a > situation where I need to maintain State inside the quickcheck generator > functions. Here's a simple example, imagine I've got a basic language which > only has two features, variable definitions and assignments: > > type Script = [Statement] > > data Name = Name String > deriving (Show,Eq) > > data Statement = Def Name Int | Assign Name Name > deriving Show > > Generating random scripts is easy enough, just make each type an instance > of > Arbitrary with a suitable generator function, however I want to make sure > that the generated scripts are syntactically valid. In this case that means > no "Name" can be used in an "Assign" statement unless it has previously > been > defined by a "Def" statement. > > My knowledge of using monads is somewhat basic so I'm struggling to figure > out how to use the State monad with QuickCheck. I've pasted my current > effort on the web: http://hpaste.org/fastcgi/hpaste.fcgi/view?id=9412 > > If anyone could spare a moment to glance at my code and give me a push in > the right direction that would be much appreciated! > > Thanks, > Alex Mac > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090917/5ae175b9/attachment-0001.html From joostkremers at fastmail.fm Thu Sep 17 07:31:38 2009 From: joostkremers at fastmail.fm (Joost Kremers) Date: Thu Sep 17 07:11:47 2009 Subject: [Haskell-beginners] map question Message-ID: <20090917113138.GC15221@enterprise.localdomain> Hi all, I've just started learning Haskell and while experimenting with map a bit, I ran into something I don't understand. The following commands do what I'd expect: Prelude> map (+ 1) [1,2,3,4] [2,3,4,5] Prelude> map (* 2) [1,2,3,4] [2,4,6,8] Prelude> map (/ 2) [1,2,3,4] [0.5,1.0,1.5,2.0] Prelude> map (2 /) [1,2,3,4] [2.0,1.0,0.6666666666666666,0.5] But I can't seem to find a way to get map to substract 1 from all members of the list. The following form is the only one that works, but it doesn't give the result I'd expect: Prelude> map ((-) 1) [1,2,3,4] [0,-1,-2,-3] I know I can use an anonymous function, but I'm just trying to understand the result here... I'd appreciate any hints to help me graps this. TIA Joost -- Joost Kremers, PhD University of Frankfurt Institute for Cognitive Linguistics Gr?neburgplatz 1 60629 Frankfurt am Main, Germany From daniel.is.fischer at web.de Thu Sep 17 07:50:27 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Sep 17 07:30:11 2009 Subject: [Haskell-beginners] map question In-Reply-To: <20090917113138.GC15221@enterprise.localdomain> References: <20090917113138.GC15221@enterprise.localdomain> Message-ID: <200909171350.27848.daniel.is.fischer@web.de> Am Donnerstag 17 September 2009 13:31:38 schrieb Joost Kremers: > Hi all, > > I've just started learning Haskell and while experimenting with map a bit, > I ran into something I don't understand. The following commands do what I'd > expect: > > Prelude> map (+ 1) [1,2,3,4] > [2,3,4,5] > Prelude> map (* 2) [1,2,3,4] > [2,4,6,8] > Prelude> map (/ 2) [1,2,3,4] > [0.5,1.0,1.5,2.0] > Prelude> map (2 /) [1,2,3,4] > [2.0,1.0,0.6666666666666666,0.5] > > But I can't seem to find a way to get map to substract 1 from all members > of the list. The following form is the only one that works, but it doesn't > give the result I'd expect: > > Prelude> map ((-) 1) [1,2,3,4] > [0,-1,-2,-3] > > I know I can use an anonymous function, but I'm just trying to understand > the result here... I'd appreciate any hints to help me graps this. (-) a b = a - b, so (((-) 1) x) = 1 - x and you've mapped (\x -> 1-x) over the list. You want to map (\x -> x-1), which is map (subtract 1) list or map (flip (-) 1) list (or map (+ (-1)) list) > > TIA > > Joost From byorgey at seas.upenn.edu Thu Sep 17 08:01:50 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Thu Sep 17 07:40:27 2009 Subject: [Haskell-beginners] map question In-Reply-To: <20090917113138.GC15221@enterprise.localdomain> References: <20090917113138.GC15221@enterprise.localdomain> Message-ID: <20090917120150.GA20349@seas.upenn.edu> On Thu, Sep 17, 2009 at 01:31:38PM +0200, Joost Kremers wrote: > > But I can't seem to find a way to get map to substract 1 from all members of the > list. The following form is the only one that works, but it doesn't give the > result I'd expect: > > Prelude> map ((-) 1) [1,2,3,4] > [0,-1,-2,-3] By the way, the reason map (+1) [1,2,3,4] works but map (-1) [1,2,3,4] doesn't is because of an ugly corner of Haskell syntax: -1 here is parsed as negative one, rather than an operator section with subtraction. The 'subtract' function is provided exactly for this purpose, so that you can write map (subtract 1) [1,2,3,4] instead. -Brent From joostkremers at fastmail.fm Thu Sep 17 08:01:47 2009 From: joostkremers at fastmail.fm (Joost Kremers) Date: Thu Sep 17 07:41:51 2009 Subject: [Haskell-beginners] map question In-Reply-To: <200909171350.27848.daniel.is.fischer@web.de> References: <20090917113138.GC15221@enterprise.localdomain> <200909171350.27848.daniel.is.fischer@web.de> Message-ID: <20090917120147.GA29167@enterprise.localdomain> On Thu, Sep 17, 2009 at 01:50:27PM +0200, Daniel Fischer wrote: > > Prelude> map ((-) 1) [1,2,3,4] > > [0,-1,-2,-3] > > > > I know I can use an anonymous function, but I'm just trying to understand > > the result here... I'd appreciate any hints to help me graps this. > > (-) a b = a - b, so (((-) 1) x) = 1 - x and you've mapped (\x -> 1-x) over the list. Ah yes... Of course, I didn't realise that wrapping a binary operator in parens turns it into a normal function. And from , which someone pointed me to off-list, I gather that in (- 1) the minus sign is interpreted as the unary operator, not the binary one. Which means that (- 1) is not a function, which means map will barf. Ok, thanks, all makes sense now! Joost -- Joost Kremers Life has its moments From DekuDekuplex at Yahoo.com Thu Sep 17 08:41:47 2009 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Sep 17 08:21:04 2009 Subject: [Haskell-beginners] How to import the Data.Char library in Hugs? Message-ID: My apologies if this is an extremely elementary question, but I am having difficulties in importing the Data.Char library in Hugs. In GHCi, the command "import Data.Char" works correctly, as follows: --8<---------------cut here---------------start------------->8--- GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Prelude> import Data.Char Prelude Data.Char> --8<---------------cut here---------------end--------------->8--- However, in Hugs, the same command fails with an error, as follows: --8<---------------cut here---------------start------------->8--- __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Bugs: http://hackage.haskell.org/trac/hugs || || Version: 20051031 _________________________________________ Haskell 98 mode: Restart with command line option -98 to enable extensions Type :? for help Hugs> import Data.Char ERROR - Syntax error in expression (unexpected keyword "import") Hugs> --8<---------------cut here---------------end--------------->8--- Does anybody know how to import the Data.Char library in Hugs? -- Benjamin L. Russell -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 80-3603-6725 "Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ From aneumann at inf.fu-berlin.de Thu Sep 17 09:02:21 2009 From: aneumann at inf.fu-berlin.de (Adrian Neumann) Date: Thu Sep 17 08:43:45 2009 Subject: [Haskell-beginners] How to import the Data.Char library in Hugs? In-Reply-To: References: Message-ID: <4AB2335D.2030108@inf.fu-berlin.de> You do > :l Data.Char As far as I know you can't have multiple loaded modules unless you put them in a file and load that. Regards, Adrian Benjamin L.Russell schrieb: > My apologies if this is an extremely elementary question, but I am > having difficulties in importing the Data.Char library in Hugs. > > In GHCi, the command "import Data.Char" works correctly, as follows: > > --8<---------------cut here---------------start------------->8--- > GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help > Loading package ghc-prim ... linking ... done. > Loading package integer ... linking ... done. > Loading package base ... linking ... done. > ___ ___ _ > / _ \ /\ /\/ __(_) > / /_\// /_/ / / | | GHC Interactive, for Haskell 98. > / /_\\/ __ / /___| | http://www.haskell.org/ghc/ > \____/\/ /_/\____/|_| Type :? for help. > > Prelude> import Data.Char > Prelude Data.Char> > --8<---------------cut here---------------end--------------->8--- > > However, in Hugs, the same command fails with an error, as follows: > > --8<---------------cut here---------------start------------->8--- > __ __ __ __ ____ ___ _________________________________________ > || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard > ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 > ||---|| ___|| World Wide Web: http://haskell.org/hugs > || || Bugs: http://hackage.haskell.org/trac/hugs > || || Version: 20051031 _________________________________________ > > Haskell 98 mode: Restart with command line option -98 to enable > extensions > > Type :? for help > Hugs> import Data.Char > ERROR - Syntax error in expression (unexpected keyword "import") > Hugs> > --8<---------------cut here---------------end--------------->8--- > > Does anybody know how to import the Data.Char library in Hugs? > > -- Benjamin L. Russell -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 251 bytes Desc: OpenPGP digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20090917/4aa56846/signature.bin From daniel.is.fischer at web.de Thu Sep 17 09:04:07 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Sep 17 08:43:50 2009 Subject: [Haskell-beginners] How to import the Data.Char library in Hugs? In-Reply-To: References: Message-ID: <200909171504.07566.daniel.is.fischer@web.de> Am Donnerstag 17 September 2009 14:41:47 schrieb Benjamin L.Russell: > My apologies if this is an extremely elementary question, but I am > having difficulties in importing the Data.Char library in Hugs. Hugs> :a Data.Char Data.Char> From tomdoris at gmail.com Thu Sep 17 09:06:23 2009 From: tomdoris at gmail.com (Tom Doris) Date: Thu Sep 17 08:44:58 2009 Subject: [Haskell-beginners] map question In-Reply-To: <20090917113138.GC15221@enterprise.localdomain> References: <20090917113138.GC15221@enterprise.localdomain> Message-ID: <19e5d1d00909170606l18ef6ae6n45cd4c2f69532820@mail.gmail.com> This works: map (+ (-1)) [1,2,3,4] 2009/9/17 Joost Kremers > Hi all, > > I've just started learning Haskell and while experimenting with map a bit, > I ran > into something I don't understand. The following commands do what I'd > expect: > > Prelude> map (+ 1) [1,2,3,4] > [2,3,4,5] > Prelude> map (* 2) [1,2,3,4] > [2,4,6,8] > Prelude> map (/ 2) [1,2,3,4] > [0.5,1.0,1.5,2.0] > Prelude> map (2 /) [1,2,3,4] > [2.0,1.0,0.6666666666666666,0.5] > > But I can't seem to find a way to get map to substract 1 from all members > of the > list. The following form is the only one that works, but it doesn't give > the > result I'd expect: > > Prelude> map ((-) 1) [1,2,3,4] > [0,-1,-2,-3] > > I know I can use an anonymous function, but I'm just trying to understand > the > result here... I'd appreciate any hints to help me graps this. > > TIA > > Joost > > > -- > Joost Kremers, PhD > University of Frankfurt > Institute for Cognitive Linguistics > Gr?neburgplatz 1 > 60629 Frankfurt am Main, Germany > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090917/3d8634f3/attachment.html From magnus at therning.org Thu Sep 17 09:54:19 2009 From: magnus at therning.org (Magnus Therning) Date: Thu Sep 17 09:32:55 2009 Subject: [Haskell-beginners] map question In-Reply-To: <19e5d1d00909170606l18ef6ae6n45cd4c2f69532820@mail.gmail.com> References: <20090917113138.GC15221@enterprise.localdomain> <19e5d1d00909170606l18ef6ae6n45cd4c2f69532820@mail.gmail.com> Message-ID: On Thu, Sep 17, 2009 at 2:06 PM, Tom Doris wrote: > This works: > > map (+ (-1)) [1,2,3,4] map pred [1..4] /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From mcguire at crsr.net Thu Sep 17 11:05:43 2009 From: mcguire at crsr.net (Tommy M. McGuire) Date: Thu Sep 17 10:44:14 2009 Subject: [Haskell-beginners] map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <19e5d1d00909170606l18ef6ae6n45cd4c2f69532820@mail.gmail.com> Message-ID: <4AB25047.4010000@crsr.net> Cue The Evolution of a Haskell Programmer: http://www.willamette.edu/~fruehr/haskell/evolution.html :-) Magnus Therning wrote: > On Thu, Sep 17, 2009 at 2:06 PM, Tom Doris wrote: >> This works: >> >> map (+ (-1)) [1,2,3,4] > > map pred [1..4] > > /M > -- Tommy M. McGuire mcguire@crsr.net From DekuDekuplex at Yahoo.com Thu Sep 17 22:27:31 2009 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Sep 17 22:06:33 2009 Subject: [Haskell-beginners] Re: How to import the Data.Char library in Hugs? References: <200909171504.07566.daniel.is.fischer@web.de> Message-ID: <0sr5b5524g1078cv68dt1vm1vsaagsri2i@4ax.com> On Thu, 17 Sep 2009 15:04:07 +0200, Daniel Fischer wrote: >Am Donnerstag 17 September 2009 14:41:47 schrieb Benjamin L.Russell: >> My apologies if this is an extremely elementary question, but I am >> having difficulties in importing the Data.Char library in Hugs. > >Hugs> :a Data.Char >Data.Char> Thank you; that was exactly the information for which I was looking. Incidentally, what option name does 'a' represent? That one doesn't appear when I type ":?." Shouldn't it appear in that list? -- Benjamin L. Russell -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 80-3603-6725 "Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ From DekuDekuplex at Yahoo.com Thu Sep 17 22:39:50 2009 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Sep 17 22:18:47 2009 Subject: [Haskell-beginners] Re: How to import the Data.Char library in Hugs? References: <4AB2335D.2030108@inf.fu-berlin.de> Message-ID: <8ks5b5t6snt7de6sf1irclis3oftjub7r4@4ax.com> On Thu, 17 Sep 2009 15:02:21 +0200, Adrian Neumann wrote: >You do > >> :l Data.Char That command didn't work; see the following results: --8<---------------cut here---------------start------------->8--- Hugs>:| Data.Char Command not recognised. Type :? for help Hugs> --8<---------------cut here---------------end--------------->8--- -- Benjamin L. Russell > >As far as I know you can't have multiple loaded modules unless you put >them in a file and load that. > >Regards, > >Adrian > >Benjamin L.Russell schrieb: >> My apologies if this is an extremely elementary question, but I am >> having difficulties in importing the Data.Char library in Hugs. >> >> In GHCi, the command "import Data.Char" works correctly, as follows: >> >> --8<---------------cut here---------------start------------->8--- >> GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help >> Loading package ghc-prim ... linking ... done. >> Loading package integer ... linking ... done. >> Loading package base ... linking ... done. >> ___ ___ _ >> / _ \ /\ /\/ __(_) >> / /_\// /_/ / / | | GHC Interactive, for Haskell 98. >> / /_\\/ __ / /___| | http://www.haskell.org/ghc/ >> \____/\/ /_/\____/|_| Type :? for help. >> >> Prelude> import Data.Char >> Prelude Data.Char> >> --8<---------------cut here---------------end--------------->8--- >> >> However, in Hugs, the same command fails with an error, as follows: >> >> --8<---------------cut here---------------start------------->8--- >> __ __ __ __ ____ ___ _________________________________________ >> || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard >> ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 >> ||---|| ___|| World Wide Web: http://haskell.org/hugs >> || || Bugs: http://hackage.haskell.org/trac/hugs >> || || Version: 20051031 _________________________________________ >> >> Haskell 98 mode: Restart with command line option -98 to enable >> extensions >> >> Type :? for help >> Hugs> import Data.Char >> ERROR - Syntax error in expression (unexpected keyword "import") >> Hugs> >> --8<---------------cut here---------------end--------------->8--- >> >> Does anybody know how to import the Data.Char library in Hugs? >> >> -- Benjamin L. Russell > -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 80-3603-6725 "Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ From daniel.is.fischer at web.de Thu Sep 17 22:52:17 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Sep 17 22:31:58 2009 Subject: [Haskell-beginners] Re: How to import the Data.Char library in Hugs? In-Reply-To: <0sr5b5524g1078cv68dt1vm1vsaagsri2i@4ax.com> References: <200909171504.07566.daniel.is.fischer@web.de> <0sr5b5524g1078cv68dt1vm1vsaagsri2i@4ax.com> Message-ID: <200909180452.18071.daniel.is.fischer@web.de> Am Freitag 18 September 2009 04:27:31 schrieb Benjamin L.Russell: > On Thu, 17 Sep 2009 15:04:07 +0200, Daniel Fischer > > wrote: > >Am Donnerstag 17 September 2009 14:41:47 schrieb Benjamin L.Russell: > >> My apologies if this is an extremely elementary question, but I am > >> having difficulties in importing the Data.Char library in Hugs. > > > >Hugs> :a Data.Char > >Data.Char> > > Thank you; that was exactly the information for which I was looking. > > Incidentally, what option name does 'a' represent? That one doesn't > appear when I type ":?." Shouldn't it appear in that list? It should, and it does for me (hugs september 2006): Hugs> :? LIST OF COMMANDS: Any command may be abbreviated to :c where c is the first character in the full name. :load load modules from specified files :load clear all files except prelude :also read additional modules <--- There :reload repeat last load command Unfortunately, Hugs' behaviour is much less convenient than ghci's: Hugs> :also Data.Char SimplTest SimplTest> ord 'a' ERROR - Undefined variable "ord" SimplTest> Data.Char.ord 'a' ERROR - Undefined qualified variable "Data.Char.ord" You can't directly use it, neither qualified nor unqualified, you have to switch contexts with :m(odule): SimplTest> :m Data.Char Data.Char> ord 'a' 97 Data.Char> :m SimplTest SimplTest> filter (test 4) $ digl 3 [[0,0,0],[0,1,4],[0,2,8],[1,4,0],[1,5,4],[1,6,8],[2,8,0],[2,9,4]] > > -- Benjamin L. Russell From DekuDekuplex at Yahoo.com Fri Sep 18 00:25:43 2009 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Fri Sep 18 00:05:27 2009 Subject: [Haskell-beginners] Re: How to import the Data.Char library in Hugs? References: <200909171504.07566.daniel.is.fischer@web.de> <0sr5b5524g1078cv68dt1vm1vsaagsri2i@4ax.com> <200909180452.18071.daniel.is.fischer@web.de> Message-ID: On Fri, 18 Sep 2009 04:52:17 +0200, Daniel Fischer wrote: >Am Freitag 18 September 2009 04:27:31 schrieb Benjamin L.Russell: >> On Thu, 17 Sep 2009 15:04:07 +0200, Daniel Fischer >> >> wrote: >> >Am Donnerstag 17 September 2009 14:41:47 schrieb Benjamin L.Russell: >> >> My apologies if this is an extremely elementary question, but I am >> >> having difficulties in importing the Data.Char library in Hugs. >> > >> >Hugs> :a Data.Char >> >Data.Char> >> >> Thank you; that was exactly the information for which I was looking. >> >> Incidentally, what option name does 'a' represent? That one doesn't >> appear when I type ":?." Shouldn't it appear in that list? > >It should, and it does for me (hugs september 2006): > >Hugs> :? >LIST OF COMMANDS: Any command may be abbreviated to :c where >c is the first character in the full name. > >:load load modules from specified files >:load clear all files except prelude >:also read additional modules <--- There >:reload repeat last load command Oops; you're right: The structure of the two commands above it was the following: >:load load modules from specified files >:load clear all files except prelude Apparently, for some reason, when I tried to scan through the list, I subconsciously grouped the two commands below similarly: >:also read additional modules >:reload repeat last load command Therefore, I somehow only read the second line of what I thought was a structurally similar second group, and therefore noticed the ":reload," but not the ":also," expecting the command above it to be ":reload ." I probably should have either read more slowly (I was rushing out to lunch at the time), or somehow avoided unconsciously assuming patterns that didn't exist. Too much mental pattern-matching ;-). > >Unfortunately, Hugs' behaviour is much less convenient than ghci's: > >Hugs> :also Data.Char SimplTest >SimplTest> ord 'a' >ERROR - Undefined variable "ord" >SimplTest> Data.Char.ord 'a' >ERROR - Undefined qualified variable "Data.Char.ord" > >You can't directly use it, neither qualified nor unqualified, you have to switch contexts >with :m(odule): > >SimplTest> :m Data.Char >Data.Char> ord 'a' >97 >Data.Char> :m SimplTest >SimplTest> filter (test 4) $ digl 3 >[[0,0,0],[0,1,4],[0,2,8],[1,4,0],[1,5,4],[1,6,8],[2,8,0],[2,9,4]] Interesting; what happens if I then need to use a higher-order function composed of other functions, some of which are from different modules, interactively? -- Benjamin L. Russell -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 80-3603-6725 "Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ From DekuDekuplex at Yahoo.com Fri Sep 18 02:05:46 2009 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Fri Sep 18 01:44:44 2009 Subject: [Haskell-beginners] Re: How to import the Data.Char library in Hugs? References: <4AB2335D.2030108@inf.fu-berlin.de> <8ks5b5t6snt7de6sf1irclis3oftjub7r4@4ax.com> Message-ID: My apologies; another respondent in this forum responded in a private e-mail message that I had misrecognized your ":l" (a colon followed by a lowercase 'L') for a vertical bar; I was using a variable-width font; perhaps I should have double-checked using a fixed-width font. I stand (er, sit, rather) corrected, as follows: --8<---------------cut here---------------start------------->8--- Hugs>:l Data.Char Data.Char>:? --8<---------------cut here---------------end--------------->8--- Incidentally, if WinHugs cannot have multiple loaded modules unless they are put in a file and loaded therein, then what is the difference between the ":l" (a colon followed by a lowercase 'L') and ":a" (a colon followed by a lowercase 'A') commands? -- Benjamin L. Russell On Fri, 18 Sep 2009 11:39:50 +0900, Benjamin L.Russell wrote: >On Thu, 17 Sep 2009 15:02:21 +0200, Adrian Neumann > wrote: > >>You do >> >>> :l Data.Char > >That command didn't work; see the following results: > >--8<---------------cut here---------------start------------->8--- >Hugs>:| Data.Char >Command not recognised. Type :? for help >Hugs> >--8<---------------cut here---------------end--------------->8--- > >-- Benjamin L. Russell > >> >>As far as I know you can't have multiple loaded modules unless you put >>them in a file and load that. >> >>Regards, >> >>Adrian >> >>Benjamin L.Russell schrieb: >>> My apologies if this is an extremely elementary question, but I am >>> having difficulties in importing the Data.Char library in Hugs. >>> >>> In GHCi, the command "import Data.Char" works correctly, as follows: >>> >>> --8<---------------cut here---------------start------------->8--- >>> GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help >>> Loading package ghc-prim ... linking ... done. >>> Loading package integer ... linking ... done. >>> Loading package base ... linking ... done. >>> ___ ___ _ >>> / _ \ /\ /\/ __(_) >>> / /_\// /_/ / / | | GHC Interactive, for Haskell 98. >>> / /_\\/ __ / /___| | http://www.haskell.org/ghc/ >>> \____/\/ /_/\____/|_| Type :? for help. >>> >>> Prelude> import Data.Char >>> Prelude Data.Char> >>> --8<---------------cut here---------------end--------------->8--- >>> >>> However, in Hugs, the same command fails with an error, as follows: >>> >>> --8<---------------cut here---------------start------------->8--- >>> __ __ __ __ ____ ___ _________________________________________ >>> || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard >>> ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 >>> ||---|| ___|| World Wide Web: http://haskell.org/hugs >>> || || Bugs: http://hackage.haskell.org/trac/hugs >>> || || Version: 20051031 _________________________________________ >>> >>> Haskell 98 mode: Restart with command line option -98 to enable >>> extensions >>> >>> Type :? for help >>> Hugs> import Data.Char >>> ERROR - Syntax error in expression (unexpected keyword "import") >>> Hugs> >>> --8<---------------cut here---------------end--------------->8--- >>> >>> Does anybody know how to import the Data.Char library in Hugs? >>> >>> -- Benjamin L. Russell >> -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 80-3603-6725 "Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ From apfelmus at quantentunnel.de Fri Sep 18 05:05:02 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Fri Sep 18 04:46:44 2009 Subject: [Haskell-beginners] Re: Maybe, Either In-Reply-To: References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> Message-ID: Yusaku Hashimoto wrote: > If I understood your post correctly, you said > > - generalizing lookup to MonadPlus or Alternative or such classes are > not necessary In particular, it doesn't become more general, it becomes less general (in a sense). > - use Maybe as usual, we should use adapters as we need > > Conor, You have said this many times elsewhere, but unfortunately, I > heard it for the first time =) so please correct me if I'm wrong. > > I thought generalizing lookup is good example for usage of the > MonadPlus as I read in RWH[1], but you said it's not necessary. > > Now, I understood there are two positions for such classes. One is > using generalizing for it, another is not. > > So, I want to know that when such classes should be used from later position. > > Heinrich suggested that is for overloading. To elaborate on generality versus overloading: the function lookupM :: MonadPlus m => k -> Map k a -> m a is not more general than lookup :: k -> Map k a -> Maybe a because you can implement the former with the latter lookupM k = mop . lookup k mop = maybe mzero return In other words, lookupM doesn't introduce new functionality. Rather, it gives you the syntactic convenience of not having to mention mop by overloading the result type. In other words, you can write lookup = lookupM or lookupE :: k -> Map k a -> Either e a lookupE = lookupM > But do any other usages are exist? I'm not quite sure I understand what you mean here? Regards, apfelmus -- http://apfelmus.nfshost.com From daniel.is.fischer at web.de Fri Sep 18 06:25:56 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Sep 18 06:05:37 2009 Subject: [Haskell-beginners] Re: How to import the Data.Char library in Hugs? In-Reply-To: References: <8ks5b5t6snt7de6sf1irclis3oftjub7r4@4ax.com> Message-ID: <200909181225.56378.daniel.is.fischer@web.de> Am Freitag 18 September 2009 08:05:46 schrieb Benjamin L.Russell: > Incidentally, if WinHugs cannot have multiple loaded modules unless > they are put in a file and loaded therein, then what is the difference > between the ":l" (a colon followed by a lowercase 'L') and ":a" (a > colon followed by a lowercase 'A') commands? The difference is that :load loads one module (and its imports) and forgets previously loaded modules, while :also doesn't forget the previously loaded modules. So if you first load a large project (A), evaluate some expressions in that context, then switch to a different project (B), and then switch back, if you use A> :a B B> some expression some result B> :m A A> the last A> prompt should be there pretty immediately, while in A> :l B B> some expression some result B> :l A A> the last A> prompt will take a while, since the entire project has to be lexed, parsed and compiled (to whatever intermediate representation Hugs uses) again. From midfield at gmail.com Fri Sep 18 10:38:19 2009 From: midfield at gmail.com (Ben) Date: Fri Sep 18 10:16:53 2009 Subject: [Haskell-beginners] type class question Message-ID: <9157df230909180738m38797aacta2bc94b3631f3be4@mail.gmail.com> dear haskellers -- this email is an (il)literate haskell file. suppose i have class of computations a -> State s b. for concreteness, let's say i'm writing a library of on-line statistical summary functions, like > {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-} > > module Foo where > > import Control.Monad > import Control.Monad.State > import Control.Monad.State.Class > > data RunningAverageState = S Double Int > > runningAverage :: Double -> State RunningAverageState Double > runningAverage v = do > S sum count <- get > let nsum = sum + v > ncount = count + 1 > put $ S nsum ncount > return $ nsum / (fromIntegral ncount) > > test = take 10 $ evalState (mapM runningAverage [1..]) $ S 0 0 test -> [1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5] here "on-line" means that we may be taking data from an intermittant external source, e.g. a data generator IO [Double], say, and want to be able to feed the summarizer datum one-by-one, and produce intermediate summaries. also we may want to be able to serialize our computation state (with Data.Binary, say) so that we can resume data collection and summarization later. naturally i want to create some common higher order operations on these primitives, like applying them to a stream of data, or combining them in some way. it seems that one would want some kind of type class to define a common interface to them. > class (MonadState s m) => Summarizer s m | m -> s where > initialState :: s > runOne :: Double -> m Double > where initialize puts some intial state into the system, and runOne collects and summarizes the next piece of data. an instance for runningAverage would look like > instance Summarizer RunningAverageState (State RunningAverageState) where > initialState = S 0 0 > runOne = runningAverage but how would i use this, e.g. > --summarizeMany vs = last $ evalState (mapM runOne vs) initialState does not compile. 1) what am i doing wrong? what are the right type class and instance declarations? 2) is there a better way of expressing this kind of "on-line" calculation, perhaps in pure (non-monadic) functions? best, ben From daniel.is.fischer at web.de Fri Sep 18 11:28:06 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Sep 18 11:07:47 2009 Subject: [Haskell-beginners] type class question In-Reply-To: <9157df230909180738m38797aacta2bc94b3631f3be4@mail.gmail.com> References: <9157df230909180738m38797aacta2bc94b3631f3be4@mail.gmail.com> Message-ID: <200909181728.07074.daniel.is.fischer@web.de> Am Freitag 18 September 2009 16:38:19 schrieb Ben: > > instance Summarizer RunningAverageState (State RunningAverageState) where > > ? ?initialState = S 0 0 > > ? ?runOne = runningAverage > > but how would i use this, e.g. > > > --summarizeMany vs = last $ evalState (mapM runOne vs) initialState > > does not compile. No, summarizeMany would have the type (Summarizer m s) => [Double] -> Double which is ambiguous. You would have to make m somehow accessible (dummy argument?). > > 1) what am i doing wrong? ?what are the right type class and instance > declarations? > > 2) is there a better way of expressing this kind of "on-line" > calculation, perhaps in pure (non-monadic) functions? Perhaps scanl ? From invite+ium~5jh_ at facebookmail.com Fri Sep 18 11:49:59 2009 From: invite+ium~5jh_ at facebookmail.com (Renick Bell) Date: Fri Sep 18 11:28:31 2009 Subject: [Haskell-beginners] Check out my photos on Facebook Message-ID: <2034a6f7f276b9d5acbb7f50b7f76da4@localhost.localdomain> Hi beginners@haskell.org, I set up a Facebook profile where I can post my pictures, videos and events and I want to add you as a friend so you can see it. First, you need to join Facebook! Once you join, you can also create your own profile. Thanks, Renick To sign up for Facebook, follow the link below: http://www.facebook.com/p.php?i=100000244637353&k=Z6E3Y5U6QX413GGJPJ53RTSVZP&r beginners@haskell.org was invited to join Facebook by Renick Bell. If you do not wish to receive this type of email from Facebook in the future, please click on the link below to unsubscribe. http://www.facebook.com/o.php?k=7d4325&u=1810302595&mid=11da955G6be70683G0G8 Facebook's offices are located at 1601 S. California Ave., Palo Alto, CA 94304. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090918/a6b42d74/attachment.html From midfield at gmail.com Fri Sep 18 12:04:22 2009 From: midfield at gmail.com (Ben) Date: Fri Sep 18 11:42:53 2009 Subject: [Haskell-beginners] type class question In-Reply-To: <200909181728.07074.daniel.is.fischer@web.de> References: <9157df230909180738m38797aacta2bc94b3631f3be4@mail.gmail.com> <200909181728.07074.daniel.is.fischer@web.de> Message-ID: <9157df230909180904g7109c82v28e8eab6ad1f6b4c@mail.gmail.com> On Fri, Sep 18, 2009 at 8:28 AM, Daniel Fischer wrote: > No, summarizeMany would have the type > > (Summarizer m s) => [Double] -> Double > > which is ambiguous. You would have to make m somehow accessible (dummy argument?). yes, i realize the type is ambiguous, ghc even says so in the error message. but a dummy argument doesn't seem very elegant. >> 2) is there a better way of expressing this kind of "on-line" >> calculation, perhaps in pure (non-monadic) functions? > > Perhaps scanl ? i originally was using mapAccumL (i need the final result of the state) but it seemed like i was missing the point of the State monad. i guess that is a matter of opinion? b From nonowarn at gmail.com Fri Sep 18 21:04:35 2009 From: nonowarn at gmail.com (Yusaku Hashimoto) Date: Fri Sep 18 20:43:05 2009 Subject: [Haskell-beginners] Re: Maybe, Either In-Reply-To: References: <4AABBAD4.4090900@alumni.caltech.edu> <29bf512f0909121113q6f47b3d3s3bee5328237c4851@mail.gmail.com> <20090913124011.GA31381@seas.upenn.edu> <29bf512f0909141142m1d5129f0t6a744d691cc1608a@mail.gmail.com> <3F9A8109-2B66-4791-9201-F635EE837B6B@strictlypositive.org> Message-ID: On Fri, Sep 18, 2009 at 6:05 PM, Heinrich Apfelmus wrote: > Yusaku Hashimoto wrote: >> - use Maybe as usual, we should use adapters as we need >> >> Conor, You have said this many times elsewhere, but unfortunately, I >> heard it for the first time =) so please correct me if I'm wrong. >> >> I thought generalizing lookup is good example for usage of the >> MonadPlus as I read in RWH[1], but you said it's not necessary. >> >> Now, I understood there are two positions for such classes. One is >> using generalizing for it, another is not. >> >> So, I want to know that when such classes should be used from later position. >> >> Heinrich suggested that is for overloading. > > To elaborate on generality versus overloading: the function > > ? ? lookupM :: MonadPlus m => k -> Map k a -> m a > > is not more general than > > ? ? lookup :: k -> Map k a -> Maybe a > > because you can implement the former with the latter > > ? ? lookupM k = mop . lookup k > > ? ? mop = maybe mzero return > > In other words, ?lookupM ?doesn't introduce new functionality. > > Rather, it gives you the syntactic convenience of not having to mention > ?mop ?by overloading the result type. In other words, you can write > > ? ? lookup ?= lookupM > > or > > ? ? lookupE :: k -> Map k a -> Either e a > ? ? lookupE = lookupM I got it. Thank you for explanation. >> But do any other usages are exist? > > I'm not quite sure I understand what you mean here? I had misread the Conor's post like the raison d'etre of type classes was denied, so I asked how type class should be used. Thanks again. Cheers -nwn From luca.padovani at uniurb.it Mon Sep 21 09:12:45 2009 From: luca.padovani at uniurb.it (Luca Padovani) Date: Mon Sep 21 08:51:08 2009 Subject: [Haskell-beginners] interface files Message-ID: I'd like to be able to extract the "interface" of a Haskell module, where the interface should basically contain the list of values defined within the module along with their type, as well as the types defined in the module. It seems to me like the only way of doing this is with GHC and the option --show-iface, which dumps the .hi file into some readable format. However, the dumped interface seems to contain far more information than the one I'm looking for (like annotations to the data types explaining whether they are recursive, etc.), and am not sure whether the grammar of this format is documented anywhere (other than the actual source of GHC, I mean). Is there any other convenient utility to automatically extract this kind of "interfaces"? [*] Thanks in advance. Luca From daniel.is.fischer at web.de Mon Sep 21 09:25:53 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Sep 21 09:07:25 2009 Subject: [Haskell-beginners] interface files In-Reply-To: References: Message-ID: <200909211525.54006.daniel.is.fischer@web.de> Am Montag 21 September 2009 15:12:45 schrieb Luca Padovani: > I'd like to be able to extract the "interface" of a Haskell module, > where the interface should basically contain the list of values > defined within the module along with their type, as well as the types > defined in the module. > > It seems to me like the only way of doing this is with GHC and the > option --show-iface, which dumps the .hi file into some readable > format. However, the dumped interface seems to contain far more > information than the one I'm looking for (like annotations to the data > types explaining whether they are recursive, etc.), and am not sure > whether the grammar of this format is documented anywhere (other than > the actual source of GHC, I mean). > > Is there any other convenient utility to automatically extract this > kind of "interfaces"? [*] haddock ? > > Thanks in advance. > > Luca From tim at timsears.com Mon Sep 21 09:47:23 2009 From: tim at timsears.com (Tim Sears) Date: Mon Sep 21 09:25:49 2009 Subject: [Haskell-beginners] interface files In-Reply-To: References: Message-ID: <44C98E6C-A7B2-4C90-8F4A-9D951FC82ABA@timsears.com> I have been trying out leksah over the past few days and it displays the interface information for all the packages on my system. I have found if *very* helpful for reading code. Thanks leksah devs! The leksah IDE extracts this information from cabal files in order to display it to the developer. Depending on what you are doing, you could either use leksah, or examine the code to see how it extracts the information. -Tim On Sep 21, 2009, at 9:12 AM, Luca Padovani wrote: > I'd like to be able to extract the "interface" of a Haskell module, > where the interface should basically contain the list of values > defined within the module along with their type, as well as the types > defined in the module. > > It seems to me like the only way of doing this is with GHC and the > option --show-iface, which dumps the .hi file into some readable > format. However, the dumped interface seems to contain far more > information than the one I'm looking for (like annotations to the data > types explaining whether they are recursive, etc.), and am not sure > whether the grammar of this format is documented anywhere (other than > the actual source of GHC, I mean). > > Is there any other convenient utility to automatically extract this > kind of "interfaces"? [*] > > Thanks in advance. > > Luca > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From aditya.siram at gmail.com Mon Sep 21 10:21:10 2009 From: aditya.siram at gmail.com (aditya siram) Date: Mon Sep 21 09:59:33 2009 Subject: [Haskell-beginners] Vaccum Cairo Errors Message-ID: <594f78210909210721u4ae0148k41deff7855a6d25@mail.gmail.com> Hi all, I am trying out the awesome Vacuum Cairo package on GHC 6.10.3 but I am getting some interesting errors. The following works in GHCI: > view [1 .. 3] But the following does not work in GHCI: > let x = [1 .. 3] > view x or, > x <- return [1 .. 3] > view x For the last two I get: context: "(:)|0" -> >>> {"1|1", <<< "(:)|2"} ghc: ../../src/xcb_lock.c:77: _XGetXCBBuffer: Assertion `((int) ((xcb_req) - (dpy->request)) >= 0)' failed. Aborted and GHCI quits. Any ideas? thanks ... deech -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090921/3437fbbc/attachment-0001.html From daniel.is.fischer at web.de Mon Sep 21 10:37:04 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Sep 21 10:18:32 2009 Subject: [Haskell-beginners] Vaccum Cairo Errors In-Reply-To: <594f78210909210721u4ae0148k41deff7855a6d25@mail.gmail.com> References: <594f78210909210721u4ae0148k41deff7855a6d25@mail.gmail.com> Message-ID: <200909211637.04184.daniel.is.fischer@web.de> Am Montag 21 September 2009 16:21:10 schrieb aditya siram: > Hi all, > I am trying out the awesome Vacuum Cairo package on GHC 6.10.3 but I am > > getting some interesting errors. The following works in GHCI: > > view [1 .. 3] > > But the following does not work in GHCI: > > let x = [1 .. 3] > > view x > > or, > > > x <- return [1 .. 3] > > view x > > For the last two I get: > context: "(:)|0" -> >>> {"1|1", <<< "(:)|2"} > ghc: ../../src/xcb_lock.c:77: _XGetXCBBuffer: Assertion `((int) ((xcb_req) > - (dpy->request)) >= 0)' failed. > Aborted > > and GHCI quits. > > Any ideas? > thanks ... > deech Probably it's that in the last two, x is given the type [Integer] by ghci (see http://www.haskell.org/haskellwiki/Monomorphism_Restriction ) while view expects an [Int]. Do let x = [1 .. 3] :: [Int] view x and x <- return ([1 .. 3] :: [Int]) view x work? From moc at coders-haven.net Mon Sep 21 15:47:40 2009 From: moc at coders-haven.net (MoC) Date: Mon Sep 21 15:26:16 2009 Subject: [Haskell-beginners] Question regarding infering the type of a function Message-ID: <4AB7D85C.5050706@coders-haven.net> Hi everybody, today somebody on IRC (#haskell on Freenode) jokingly submitted the following to lambdabot: > (.) (.) It turned out that (.) (.) :: (a1 -> b -> c) -> a1 -> (a -> b) -> a -> c. I got interested in why that is so, but unfortunately I have not been able to figure this out by myself yet. I know that partial application is used, but don't know to what it would expand to and why. Could someone please explain? (I asked on the channel, but nobody did answer so I thought I'd try my luck here.) Regards, MoC From jfredett at gmail.com Mon Sep 21 16:18:45 2009 From: jfredett at gmail.com (Joe Fredette) Date: Mon Sep 21 15:57:10 2009 Subject: [Haskell-beginners] Question regarding infering the type of a function In-Reply-To: <4AB7D85C.5050706@coders-haven.net> References: <4AB7D85C.5050706@coders-haven.net> Message-ID: The type of `(.)` is (.) :: (b->c) -> (a->b) -> a -> c it can be defined as: (.) f g x = f (g x) So, let f = (.), then we can plug that in and make it infix in the defn to get: (.) (.) g x q y = g x . q $ y I had to add a few variables there to make it pointed, as opposed to the more succint semipointfree version: (.) (.) g x = (.) g x So we can see that this function, `(.) (.)` takes four arguments, a function on 2 inputs (g, because (.) is going to feed it another one, and it's taking one already, in the form of x). One of the variables of appropriate some arbitrary type `t` (since g is the only thing that will ever see it, you can think of it as a kind of index to an indexed family of functions of type `b->c`.) giving `(.) (.)` just `x` will reduce the type to something you should recognize, that is if: foo x = something in (\ g q y -> (.) (.) g x' q y) then foo has type: foo :: (b -> c) -> (a -> b) -> a -> c eg, foo = (.) So, (.) (.) effectively says, "Given a indexed family of functions from `b -> c`, indexed by some type `t` compose the `t`th function with some function `q` from `a -> b`, and return a function of type `a -> c`. You might use it for something like mapping over a list at a given starting point, or something. HTH. /Joe On Sep 21, 2009, at 3:47 PM, MoC wrote: > Hi everybody, > > today somebody on IRC (#haskell on Freenode) jokingly submitted the > following to lambdabot: > > (.) (.) > It turned out that (.) (.) :: (a1 -> b -> c) -> a1 -> (a -> b) -> a - > > c. I got interested in why that is so, but unfortunately I have > not been able to figure this out by myself yet. I know that partial > application is used, but don't know to what it would expand to and > why. > Could someone please explain? > (I asked on the channel, but nobody did answer so I thought I'd try > my luck here.) > > Regards, > MoC > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From daniel.is.fischer at web.de Mon Sep 21 16:18:33 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Sep 21 16:00:01 2009 Subject: [Haskell-beginners] Question regarding infering the type of a function In-Reply-To: <4AB7D85C.5050706@coders-haven.net> References: <4AB7D85C.5050706@coders-haven.net> Message-ID: <200909212218.33909.daniel.is.fischer@web.de> Am Montag 21 September 2009 21:47:40 schrieb MoC: > Hi everybody, > > today somebody on IRC (#haskell on Freenode) jokingly submitted the > > following to lambdabot: > > (.) (.) > > It turned out that (.) (.) :: (a1 -> b -> c) -> a1 -> (a -> b) -> a -> > c. I got interested in why that is so, but unfortunately I have not been > able to figure this out by myself yet. I know that partial application > is used, but don't know to what it would expand to and why. > Could someone please explain? > (I asked on the channel, but nobody did answer so I thought I'd try my > luck here.) And it's not futile :) The type of (.) is (forall a, b, c.) (b -> c) -> (a -> b) -> a -> c Now, to not get confused, choose different type variables for the type of the first (.) and the second, say the first has type (b -> c) -> (a -> b) -> a -> c and the second has type (v -> w) -> (u -> v) -> u -> w === (v -> w) -> ((u -> v) -> (u -> w)) Feeding it as an argument to the first, we must unify this type with the type of the first (.)'s (first) argument, (b -> c), thus b = (v -> w) c = (u -> v) -> u -> w and the type of (.) (.) becomes (a -> b) -> a -> c, which is (a -> (v -> w)) -> a -> ((u -> v) -> u -> w) by what we got for b and c. Now remove those parentheses which aren't necessary because (->) is right associative, to get (a -> v -> w) -> a -> (u -> v) -> u -> w and rename the type variables to get exactly lambdabot's answer. > > Regards, > MoC From moc at coders-haven.net Mon Sep 21 17:06:37 2009 From: moc at coders-haven.net (MoC) Date: Mon Sep 21 16:45:09 2009 Subject: [Haskell-beginners] Question regarding infering the type of a function In-Reply-To: <200909212218.33909.daniel.is.fischer@web.de> References: <4AB7D85C.5050706@coders-haven.net> <200909212218.33909.daniel.is.fischer@web.de> Message-ID: <4AB7EADD.8030808@coders-haven.net> Daniel Fischer wrote: > > (v -> w) -> (u -> v) -> u -> w === (v -> w) -> ((u -> v) -> (u -> w)) > > Feeding it as an argument to the first, we must unify this type with the type of the first > (.)'s (first) argument, (b -> c), thus > > b = (v -> w) > c = (u -> v) -> u -> w > > and the type of (.) (.) becomes (a -> b) -> a -> c, which is > > (a -> (v -> w)) -> a -> ((u -> v) -> u -> w) > > by what we got for b and c. I started with the same approach but for whatever reason failed to see the "feeding" process so I basically said that b = (v -> w) -> ((u -> v) -> (u -> w)) which, of course, was a dead end. It's interesting that I did not have problems with acknowledging a feeding process with "simple" (for I don't know the proper term) types, e. g. it was clear to me that if I feed f :: (a->b)->c with g :: Integer -> Integer a and b in this case would be Integer in this case. I think I'm not completely used to functions being value, yet. ;) Anyway, thank you very much for helping out (actually to both replies) and in this case also for clarifying. Regards, MoC From tomdoris at gmail.com Mon Sep 21 17:35:24 2009 From: tomdoris at gmail.com (Tom Doris) Date: Mon Sep 21 17:13:47 2009 Subject: [Haskell-beginners] edit-compile-test loop Message-ID: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> Hi I'd like to know what the typical edit-compile-test loop looks like with the Haskell platform; that is, in C++ this would be edit, run make to compile everything in the project into libraries and executables, then run an executable test suite. I'm confused as to how people work on larger projects in Haskell - do you work on a single module and load it into ghci to test as you develop, then compile the entire package and run a test suite? Or do you generally only use ghci for prototyping and not when in the middle of proper development? Or do you compile the package and load that into ghci? I'd like to know as I'm starting to work on patches for some hackage packages which have proper cabal builds etc., and want to follow the correct (and efficient!) convention. Thanks Tom -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090921/2f01d645/attachment.html From jfredett at gmail.com Mon Sep 21 17:52:49 2009 From: jfredett at gmail.com (Joe Fredette) Date: Mon Sep 21 17:31:12 2009 Subject: [Haskell-beginners] edit-compile-test loop In-Reply-To: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> Message-ID: <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> Assuming I'm starting a brand-new project, it starts by creating a file in my code directory. initing darcs there, creating the .cabal file, and creating a basic module hierarchy. After that, I `touch` a couple of files. add the standard license/description/other header info... When I'm working on the code proper, I have a screen session split to a ghci session and a vim session editing the file. The ghci sits in the base directory of the projects (where the _darcs folder is) and has the files I'm working on loaded. I edit, ^a-tab to ghci, reload, flip back, fix errors, repeat till it loads. After that, I run a few tests to make sure the program does what I think it does, if not, I fix it, then back to adding new functionality. I hope this is what you wanted to know. Towards the point where I'm going to release a version, I substitute the ghci-business to a proper test harness/cabal build to make sure it compiles all correctly. /Joe On Sep 21, 2009, at 5:35 PM, Tom Doris wrote: > Hi > I'd like to know what the typical edit-compile-test loop looks like > with the Haskell platform; that is, in C++ this would be edit, run > make to compile everything in the project into libraries and > executables, then run an executable test suite. I'm confused as to > how people work on larger projects in Haskell - do you work on a > single module and load it into ghci to test as you develop, then > compile the entire package and run a test suite? Or do you generally > only use ghci for prototyping and not when in the middle of proper > development? Or do you compile the package and load that into ghci? > I'd like to know as I'm starting to work on patches for some hackage > packages which have proper cabal builds etc., and want to follow the > correct (and efficient!) convention. > Thanks > Tom > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From tomdoris at gmail.com Mon Sep 21 18:22:33 2009 From: tomdoris at gmail.com (Tom Doris) Date: Mon Sep 21 18:01:19 2009 Subject: [Haskell-beginners] edit-compile-test loop In-Reply-To: <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> Message-ID: <19e5d1d00909211522v1a6eac25y862d1164e60c892c@mail.gmail.com> Thanks, this is very helpful; can you give more detail about your process once you've transitioned to a "proper test harness/cabal build", how does this usually hang together? 2009/9/21 Joe Fredette > Assuming I'm starting a brand-new project, it starts by creating a file in > my code directory. initing darcs there, creating the .cabal file, and > creating a basic module hierarchy. After that, I `touch` a couple of files. > add the standard license/description/other header info... > > When I'm working on the code proper, I have a screen session split to a > ghci session and a vim session editing the file. The ghci sits in the base > directory of the projects (where the _darcs folder is) and has the files I'm > working on loaded. I edit, ^a-tab to ghci, reload, flip back, fix errors, > repeat till it loads. After that, I run a few tests to make sure the program > does what I think it does, if not, I fix it, then back to adding new > functionality. > > I hope this is what you wanted to know. Towards the point where I'm going > to release a version, I substitute the ghci-business to a proper test > harness/cabal build to make sure it compiles all correctly. > > /Joe > > > On Sep 21, 2009, at 5:35 PM, Tom Doris wrote: > > Hi >> I'd like to know what the typical edit-compile-test loop looks like with >> the Haskell platform; that is, in C++ this would be edit, run make to >> compile everything in the project into libraries and executables, then run >> an executable test suite. I'm confused as to how people work on larger >> projects in Haskell - do you work on a single module and load it into ghci >> to test as you develop, then compile the entire package and run a test >> suite? Or do you generally only use ghci for prototyping and not when in the >> middle of proper development? Or do you compile the package and load that >> into ghci? I'd like to know as I'm starting to work on patches for some >> hackage packages which have proper cabal builds etc., and want to follow the >> correct (and efficient!) convention. >> Thanks >> Tom >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090921/d8501178/attachment-0001.html From jfredett at gmail.com Mon Sep 21 18:38:48 2009 From: jfredett at gmail.com (Joe Fredette) Date: Mon Sep 21 18:17:12 2009 Subject: [Haskell-beginners] edit-compile-test loop In-Reply-To: <19e5d1d00909211522v1a6eac25y862d1164e60c892c@mail.gmail.com> References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> <19e5d1d00909211522v1a6eac25y862d1164e60c892c@mail.gmail.com> Message-ID: Well, basically I switch from doing manual "testing" (entering functions with test arguments by hand into ghci, checking types w/ the inferrer, etc.) to compiling via cabal build (which requires me to make sure I've got all the appropriate modules listed in the appropriate places, make sure I'm not importing hidden modules, etc) and running a test suite (typically QuickCheck properties with some HUnit thrown in around the IO stuff and for corner cases of functions (eg, making sure things fail gracefully when I have to write a partial function, etc)) and working generally more as if I were writing in a language like Java (where most of my dev cycle is interacting with JUnit, rather than a interpreter). Typically it works out to: 1> edit some stuff 2> cabal build 3> fix any errors; goto 1 else continue 4> run test suite [see 1] 5> if any tests fail, and that wasn't expected, fix the issue; goto 1 else continue 6> goto 1 until ready for release. Towards the release date, this process helps me add functionality slower, and ensure everything is actually doing what it's supposed to. I tend to work on projects solo, so I'll do various naughty things like develop without writing alot of tests or documenting, etc. When I get ready to release, I really want to make sure I've covered everything that needs to be tested with tests, documented everything appropriately. As I code (even before this part of the project) I generally keep track of what I should write tests for, even if I haven't written the test yet. During this phase I build up the test suite to cover all the things I've written down, and the test suite kind of becomes part of the specification at that point. If something in the future causes an early test to fail, then that change "breaks" the program, if it causes a current test to fail, then I might want to see if that test is really ensuring the property I want. I should note, I'm not a CS-guy, I'm a math guy, and this kind of methodology is more like when I write a math paper than what I think most people do when they code. When I have a ECT loop for math papers, it starts with me making wild conjectures and "testing" them (trying to come up with proof sketches), later that turns to this slower process of actually, formally proving things (the compiling part of this cycle) and finally it hits the, "Now make it pretty" phase, where I eliminate superfluous arguments/lemmas, rework proofs to flow better, etc (the "make sure the tests pass" phase). I don't know if this is similar to "normal" dev cycles, but it does me nicely. :) HTH /Joe [1] right now I use my own little hacked up testrunning script (just loads the stuff in ghci, and I have one function that sits in a base "test" directory, underwhich the project module hierarchy is mirrored. So if I have, as I do now, Text/HWN/Parser/HWN.hs, then I also have Test/Text/HWN/Parser/HWN_Test.hs. in the Test directory I have Test_Harness.hs, which has a main function which runs all the tests in the directories below it. Each directory has a similar Test_Harness.hs (though all the names are different) which loads all the tests of the directories below it, and exports a single test group. All of this is manual, though I've been planning a kind of manager thing for it lately. But thats a story for another day. On Sep 21, 2009, at 6:22 PM, Tom Doris wrote: > Thanks, this is very helpful; can you give more detail about your > process once you've transitioned to a "proper test harness/cabal > build", how does this usually hang together? > > > 2009/9/21 Joe Fredette > Assuming I'm starting a brand-new project, it starts by creating a > file in my code directory. initing darcs there, creating the .cabal > file, and creating a basic module hierarchy. After that, I `touch` a > couple of files. add the standard license/description/other header > info... > > When I'm working on the code proper, I have a screen session split > to a ghci session and a vim session editing the file. The ghci sits > in the base directory of the projects (where the _darcs folder is) > and has the files I'm working on loaded. I edit, ^a-tab to ghci, > reload, flip back, fix errors, repeat till it loads. After that, I > run a few tests to make sure the program does what I think it does, > if not, I fix it, then back to adding new functionality. > > I hope this is what you wanted to know. Towards the point where I'm > going to release a version, I substitute the ghci-business to a > proper test harness/cabal build to make sure it compiles all > correctly. > > /Joe > > > On Sep 21, 2009, at 5:35 PM, Tom Doris wrote: > > Hi > I'd like to know what the typical edit-compile-test loop looks like > with the Haskell platform; that is, in C++ this would be edit, run > make to compile everything in the project into libraries and > executables, then run an executable test suite. I'm confused as to > how people work on larger projects in Haskell - do you work on a > single module and load it into ghci to test as you develop, then > compile the entire package and run a test suite? Or do you generally > only use ghci for prototyping and not when in the middle of proper > development? Or do you compile the package and load that into ghci? > I'd like to know as I'm starting to work on patches for some hackage > packages which have proper cabal builds etc., and want to follow the > correct (and efficient!) convention. > Thanks > Tom > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From mpm at alumni.caltech.edu Wed Sep 23 18:03:03 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Sep 23 17:41:25 2009 Subject: [Haskell-beginners] StateT, list monad In-Reply-To: References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> <19e5d1d00909211522v1a6eac25y862d1164e60c892c@mail.gmail.com> Message-ID: <3694.75.50.169.210.1253743383.squirrel@mail.alumni.caltech.edu> Trying to understand Douglas Auclair's article "MonadPlus - What a super monad!" here Defines splits :: Eq a => [a] -> [(a,[a])] splits list = do x <- list return (x, delete x list) choose :: Eq a => StateT [a] [] a choose = StateT (\s -> splits s) I'm trying to understand what "StateT [a] [] a" means: I wrote t1 :: StateT [Int] [] [Int] t1 = do s <- get return s That compiles. Then I tried to write t2 :: StateT [Int] [] [Int] t2 = do x <- [1,2,3] s <- get return (x:s) I thought this would be fine because [1,2,3] is an example of a list monad. But I get "Can't match expected type StateT [Int] [] t against inferred type [a]" From daniel.is.fischer at web.de Wed Sep 23 18:36:09 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Sep 23 18:16:10 2009 Subject: [Haskell-beginners] StateT, list monad In-Reply-To: <3694.75.50.169.210.1253743383.squirrel@mail.alumni.caltech.edu> References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> <3694.75.50.169.210.1253743383.squirrel@mail.alumni.caltech.edu> Message-ID: <200909240036.09934.daniel.is.fischer@web.de> Am Donnerstag 24 September 2009 00:03:03 schrieb Michael Mossey: > Trying to understand Douglas Auclair's article > "MonadPlus - What a super monad!" here > > > Defines > > splits :: Eq a => [a] -> [(a,[a])] > splits list = do x <- list > return (x, delete x list) > > choose :: Eq a => StateT [a] [] a > choose = StateT (\s -> splits s) > > I'm trying to understand what "StateT [a] [] a" means: StateT stateType innerMonad resultType The state is a list of a, the inner monad is the list monad, apart from the newtype wrapper, it's [a] -> [(a,[a])] > > I wrote > > t1 :: StateT [Int] [] [Int] > t1 = do > s <- get > return s > > That compiles. Then I tried to write > > t2 :: StateT [Int] [] [Int] > t2 = do > x <- [1,2,3] > s <- get > return (x:s) > > I thought this would be fine because [1,2,3] is an example of a list monad. Within one do-block, you can use only one monad. do x <- action1 y <- action2 return (f x y) desugars into action1 >>= (\x -> (action2 >>= (\y -> (return (f x y))))) (I used more parentheses than necessary to make the associativity clear, with less clutter, it's action1 >>= \x -> action2 >>= \y -> return (f x y) .) The type of (>>=) is (>>=) :: Monad m => m a -> (a -> m b) -> m b so you can use only one monad in such an expression. With StateT (most monad transformers, I think), you can achieve what you want with t2 :: StateT [Int] [] [Int] t2 = do ? x <- lift [1,2,3] ? s <- get ? return (x:s) 'lift' lifts actions in the inner monad to StateT actions. > > But I get > > "Can't match expected type StateT [Int] [] t > against inferred type [a]" > From hyangfji at gmail.com Thu Sep 24 14:12:01 2009 From: hyangfji at gmail.com (Hong Yang) Date: Thu Sep 24 13:50:16 2009 Subject: [Haskell-beginners] proxy Message-ID: Hi, I was using Finance.Quote.Yahoo, but got an error: user error (openTCPConnection: host lookup failure for " download.finance.yahoo.com"). I think it is due to I being behind a proxy. I then imported Network.Browser, ran setProxy, and tried to fetch data using Finance.Quote.Yahoo.getQuote. I still got the error. So I suggest the module author incorporate proxy setting inside the module. Thanks, Hong -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090924/39755102/attachment.html From joe at fixieconsulting.com Sun Sep 27 00:29:49 2009 From: joe at fixieconsulting.com (Joe Van Dyk) Date: Sun Sep 27 00:08:13 2009 Subject: [Haskell-beginners] Distributing executables Message-ID: How hard is it to distribute a haskell binary that contains a bunch of libraries embedded in the executable? Is it pretty easy to make a large statically-linked executable? -- Joe Van Dyk http://fixieconsulting.com From informationen at gmx.de Sun Sep 27 06:22:50 2009 From: informationen at gmx.de (informationen) Date: Sun Sep 27 06:00:26 2009 Subject: [Haskell-beginners] Haskell type system Message-ID: <20090927102249.GB4216@debian> Hi, i am trying to understand the Haskell type system. I thought i understood it quite well until i encountered the three following exercises. As you can see, i have the answers already. But i don't understand, why they are correct. Could anybody tell me a good place where i could learn how to answers these kind of questions correctly or could give me some explanations, why these answers are correct? Any help is highly appreciated. Kind regards Chris Two functions f and g with the following signatures are given: f :: a -> a g :: b -> c -> b A) Give the type of the following expressions: 1) g [False] True :: 2) g [] True :: 3) g (f True) :: 4) g f :: Answers: 1 [Boolean] 2) [a] 3) c -> Bool 4) c -> (a -> a)1) g f 1 B) Which of the following statements is correct? 2) g (f 1) is type correct 3) g . (f 1) is type correct 4) g . f 1 is type correct 5) (g . f) 1 is type correct 6) none of the expressions is correct Answers: 1,2 and 5 are correct. C) A function h is given as: h p x = p (f x). Which of the following statements is correct. 1) h :: a -> b -> a -> b 2) h :: (a -> a) -> a -> a 3) h :: (a -> b) -> a -> b 4) h is equivalent to h' with h' p = p . f 5) h is equivalent to h' with h' p = p f 5) h is equivalent to h' with h' p x = p f x Answers: (I am not sure, if i remember correctly, but 3) and 4) should be correct.) From bugfact at gmail.com Sun Sep 27 08:50:53 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Sun Sep 27 08:29:00 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: <20090927102249.GB4216@debian> References: <20090927102249.GB4216@debian> Message-ID: Which parts would like explanations for? All of them? On Sun, Sep 27, 2009 at 12:22 PM, informationen wrote: > Hi, > > i am trying to understand the Haskell type system. I thought i understood > it > quite well until i encountered the three following exercises. As you can > see, i > have the answers already. But i don't understand, why they are correct. > > Could anybody tell me a good place where i could learn how to answers these > kind > of questions correctly or could give me some explanations, why these > answers are > correct? > > Any help is highly appreciated. > > Kind regards > > Chris > Two functions f and g with the following signatures are given: > f :: a -> a > g :: b -> c -> b > > A) Give the type of the following expressions: > > 1) g [False] True :: 2) g [] True :: 3) g (f True) :: 4) g f > :: > Answers: > > 1 [Boolean] 2) [a] 3) c -> Bool > 4) c -> (a -> a)1) g f 1 > B) Which of the following statements is correct? > > 2) g (f 1) is type correct > 3) g . (f 1) is type correct > 4) g . f 1 is type correct > 5) (g . f) 1 is type correct > 6) none of the expressions is correct > > Answers: > 1,2 and 5 are correct. > > C) A function h is given as: h p x = p (f x). Which of the following > statements is correct. > > 1) h :: a -> b -> a -> b > 2) h :: (a -> a) -> a -> a > 3) h :: (a -> b) -> a -> b > 4) h is equivalent to h' with h' p = p . f > 5) h is equivalent to h' with h' p = p f > 5) h is equivalent to h' with h' p x = p f x > > Answers: > (I am not sure, if i remember correctly, but 3) and 4) should be > correct.) > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090927/aaa65450/attachment-0001.html From informationen at gmx.de Sun Sep 27 09:32:59 2009 From: informationen at gmx.de (informationen) Date: Sun Sep 27 09:10:36 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: References: <20090927102249.GB4216@debian> Message-ID: <20090927133259.GC4216@debian> For the parts you could answer and only the correct answers. But every hint would be of great value. I searched the web and the haskell wiki, especially http://www.haskell.org/haskellwiki/Category:Theoretical_foundations, for further informations, but found none, so if you could point me to a good tutorial for that kind of question, that would be of help too. >Which parts would like explanations for? All of them? > >On Sun, Sep 27, 2009 at 12:22 PM, informationen wrote: > > Hi, > > i am trying to understand the Haskell type system. I thought i understood > it > quite well until i encountered the three following exercises. As you can > see, i > have the answers already. But i don't understand, why they are correct. > > Could anybody tell me a good place where i could learn how to answers these > kind > of questions correctly or could give me some explanations, why these > answers are > correct? > > Any help is highly appreciated. > > Kind regards > > Chris > Two functions f and g with the following signatures are given: > f :: a -> a > g :: b -> c -> b > > A) Give the type of the following expressions: > > 1) g [False] True ?:: 2) g [] True ? ? ? :: 3) g (f True) ? ? ?:: 4) g f ? > ? ? ? ? ? :: > Answers: > > 1 [Boolean] 2) [a] ? ? ? ? ?3) c -> Bool > 4) c -> (a -> a)1) ?g f 1 ? ? ? > B) Which of the following statements is correct? > > 2) ?g (f 1) ? ? ? ? is type correct > 3) ?g . (f 1) ? ? ? is type correct > 4) ?g . f 1 ? ? ? ? is type correct > 5) ?(g . f) 1 ? ? ? is type correct > 6) ? ? ? ? ? ? ? ? ?none of the expressions is correct > > Answers: > 1,2 and 5 are correct. > > C) A function h is given as: h p x = p (f x). Which of the following > statements is correct. > > 1) h :: a -> b -> a -> b > 2) h :: (a -> a) -> a -> a > 3) h :: (a -> b) -> a -> b > 4) h is equivalent to h' with h' p = p . f > 5) h is equivalent to h' with h' p = p f > 5) h is equivalent to h' with h' p x = p f x > > Answers: > (I am not sure, if i remember correctly, but 3) and 4) should be > correct.) > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From bugfact at gmail.com Sun Sep 27 10:10:24 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Sun Sep 27 09:48:29 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: <20090927133259.GC4216@debian> References: <20090927102249.GB4216@debian> <20090927133259.GC4216@debian> Message-ID: well, depending on what you do and don't understand, the explanation can very from short to very long, so it might help to try to explain what you don't understand for each question/answer. usually when you try to explain what you don't understand, you will understand it, so that's always a good way to start :) On Sun, Sep 27, 2009 at 3:32 PM, informationen wrote: > For the parts you could answer and only the correct answers. But every hint > would be of great value. I searched the web and the > haskell wiki, especially > http://www.haskell.org/haskellwiki/Category:Theoretical_foundations, > for further informations, but found none, so if you could point me > to a good tutorial for that kind of question, that would be of help > too. > >> Which parts would like explanations for? All of them? >> >> On Sun, Sep 27, 2009 at 12:22 PM, informationen >> wrote: >> >> Hi, >> >> i am trying to understand the Haskell type system. I thought i understood >> it >> quite well until i encountered the three following exercises. As you can >> see, i >> have the answers already. But i don't understand, why they are correct. >> >> Could anybody tell me a good place where i could learn how to answers >> these >> kind >> of questions correctly or could give me some explanations, why these >> answers are >> correct? >> >> Any help is highly appreciated. >> >> Kind regards >> >> Chris >> Two functions f and g with the following signatures are given: >> f :: a -> a >> g :: b -> c -> b >> >> A) Give the type of the following expressions: >> >> 1) g [False] True :: 2) g [] True :: 3) g (f True) :: 4) g f >> >> :: >> Answers: >> >> 1 [Boolean] 2) [a] 3) c -> Bool >> 4) c -> (a -> a)1) g f 1 >> B) Which of the following statements is correct? >> >> 2) g (f 1) is type correct >> 3) g . (f 1) is type correct >> 4) g . f 1 is type correct >> 5) (g . f) 1 is type correct >> 6) none of the expressions is correct >> >> Answers: >> 1,2 and 5 are correct. >> >> C) A function h is given as: h p x = p (f x). Which of the following >> statements is correct. >> >> 1) h :: a -> b -> a -> b >> 2) h :: (a -> a) -> a -> a >> 3) h :: (a -> b) -> a -> b >> 4) h is equivalent to h' with h' p = p . f >> 5) h is equivalent to h' with h' p = p f >> 5) h is equivalent to h' with h' p x = p f x >> >> Answers: >> (I am not sure, if i remember correctly, but 3) and 4) should be >> correct.) >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090927/c0bd2849/attachment.html From daniel.is.fischer at web.de Sun Sep 27 10:32:20 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Sep 27 10:11:53 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: <20090927102249.GB4216@debian> References: <20090927102249.GB4216@debian> Message-ID: <200909271632.20499.daniel.is.fischer@web.de> Am Sonntag 27 September 2009 12:22:50 schrieb informationen: > Hi, > > i am trying to understand the Haskell type system. I thought i understood > it quite well until i encountered the three following exercises. As you can > see, i have the answers already. But i don't understand, why they are > correct. > > Could anybody tell me a good place where i could learn how to answers these > kind of questions correctly or could give me some explanations, why these > answers are correct? > > Any help is highly appreciated. > > Kind regards > > Chris > > Two functions f and g with the following signatures are given: > f :: a -> a > g :: b -> c -> b If you have a function of type fun :: x -> y then for all values v of type x, fun x has type y. > > A) Give the type of the following expressions: > > 1) g [False] True :: g :: b -> c -> b [False] :: [Bool] ~> (we must unify b and [Bool]) g [False] :: c -> [Bool] True :: Bool -- but that doesn't matter, could be any type ~> g [False] True :: [Bool] > 2) g [] True :: g :: b -> c -> b [] :: [a] ~> (unify b and [a]) g [] :: c -> [a] ~> g [] True :: [a] > 3) g (f True) :: g :: b -> c -> b f :: a -> a True :: Bool ~> (unify a and Bool) f True :: Bool ~> (unify b and the type of (f True)) g (f True) :: c -> Bool > 4) g f :: g :: b -> c -> b f :: a -> a ~> (unify b and (a -> a)) g f :: c -> (a -> a) > > Answers: > > 1 [Boolean] > 2) [a] > 3) c -> Bool > 4) c -> (a -> a) > > B) Which of the following statements is correct? > > 1) g f 1 is type correct By the above, g f :: c -> (a -> a) , hence (unify c and the type of 1 (forall n. Num n => n)) to get g f 1 :: (a -> a) which is valid, so (g f 1) is type correct > 2) g (f 1) is type correct f 1 has the same type as 1, (forall n. Num n => n), that must be unified with b, since the latter is a type variable, there's no problem, giving g (f 1) :: forall n. Num n => c -> n > 3) g . (f 1) is type correct For (g . (f 1)) to be type correct, (f 1) must be a function (i.e. have type (x -> y) for some x, y). As said above, (f 1) has type (forall n. Num n => n). We must now (try to) unify that with (x -> y). n is a type variable, so the art of unifying disregarding contexts is fine, replace n by (x -> y). No go for the context, the appropriate substitutions there lead to the context forall x, y. Num (x -> y) => (x -> y) If you have such a Num instance around, then g . (f 1) is well typed. But usually you haven't (and you shouldn't), so then it's not well typed because you can't unify (forall n. Num n => n) with (x -> y). > 4) g . f 1 is type correct Due to the precedences of function application and composition (.), this is exactly the same as 3). > 5) (g . f) 1 is type correct (.) :: (y -> z) -> (x -> y) -> (x -> z) g :: b -> (c -> b) f :: a -> a Thus in (g . f) we have b -> (c -> b) === (y -> z), hence y === b, z === c -> b and a -> a === (x -> y), hence x === a, y === a By y === b and y === a, we find a === b, so g . f :: x -> z === a -> (c -> a) === a -> c -> a a can be trivially unified with the type of 1 (forall n. Num n => n), so (g . f) 1 :: forall n. Num n => c -> n is type correct. > 6) none of the expressions is correct > > Answers: > 1,2 and 5 are correct. > > C) A function h is given as: h p x = p (f x). Which of the following > statements is correct. h p x = p (f x) h takes two arguments, so h :: t1 -> t2 -> t3 with as yet unknown types t1, t2, t3. From the right hand side, we see that p takes one argument and it returns a value of the same type as h, so p :: t4 -> t3 On the other hand, p is the first argument of h, so p :: t1, hence t1 === t4 -> t3 p's argument is (f x), where x is the second argument of h, so x :: t2 f :: a -> a, unifying a with t2 is just renaming, so (f x) :: t2 On the other hand, (f x) is the argument of p, so (f x) :: t4, hence t4 === t2 Together, h :: (t2 -> t3) -> t2 -> t3 rename to h :: (a -> b) -> a -> b > > 1) h :: a -> b -> a -> b If h had that type, we'd have a === (t2 -> t3) b === t2 a -> b === t3, so (t2 -> t3) -> t2 === t3 [( a ) -> b a -> b] which leads to an infinite type t3 === (t2 -> t3) -> t2 === (t2 -> ((t2 -> t3) -> t2)) -> t2 === (t2 -> ((t2 -> ((t2 -> t3) -> t2)) -> t2)) -> t2 === (t2 -> ((t2 -> ((t2 -> ((t2 -> t3) -> t2)) -> t2)) -> t2)) -> t2 which is not allowed. > 2) h :: (a -> a) -> a -> a h has a more general type, so h can have this type, too (any function expecting an argument of that type will accept h) > 3) h :: (a -> b) -> a -> b Yup. > 4) h is equivalent to h' with h' p = p . f h p x = p (f x) (h' p) x = (p . f) x = p (f x) Aye. > 5) h is equivalent to h' with h' p = p f Nay. With h' p = p f, we find h' :: s1 -> s2 p :: s1 and from the right hand side, p :: (a -> a) -> b so s2 === b and s1 === ((a -> a) -> b), giving h' :: ((a -> a) -> b) -> b which is not the type of h. > 5) h is equivalent to h' with h' p x = p f x > > Answers: > (I am not sure, if i remember correctly, but 3) and 4) should be > correct.) From psarge at gmail.com Sun Sep 27 10:52:44 2009 From: psarge at gmail.com (Paul Sargent) Date: Sun Sep 27 10:30:51 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: <20090927102249.GB4216@debian> References: <20090927102249.GB4216@debian> Message-ID: I'll try my hand at these, anybody feel free to point out all my deliberate mistakes. On 27 Sep 2009, at 11:22, informationen wrote: > Two functions f and g with the following signatures are given: > f :: a -> a > g :: b -> c -> b > > A) Give the type of the following expressions: > > 1) g [False] True :: 2) g [] True These two are simply that the result of the function g is the same as it's first argument. The first takes a list of Boolean, the second a list of an unknown type. Therefore the results are the same types [Boolean] and [a] respectively. > :: 3) g (f True) :: The result of f is the same type as it's argument, in this case Boolean. g's first and only argument which is the type of the result of f -- Boolean. As we haven't specified a second argument, the returned type is a function which takes an unknown type and returns a boolean. (c -> Bool) > 4) g f :: Similar to the last case except now the only argument supplied is a function of type (a -> a). (c -> (a -> a)) > Answers: > > B) Which of the following statements is correct? > First two are easy, and the same. > 1) g f 1 c -> Int > 2) g (f 1) c -> Int Remember that the type of (.) is (b -> c) -> (a -> b) -> a -> c. That is, it takes two functions where they both take an argument, and the type of the first functions argument is the same as the seconds result. > 3) g . (f 1) Here (f 1) doesn't take an argument, it's already supplied. That makes it's type Int, not (a -> b). That's not right. > 4) g . f 1 The same, the brackets where redundant in the previous expression. > 5) (g . f) 1 Here we're composing before giving the arguments. So, remembering that f :: a -> a and g :: b -> c -> b, we can start filling in the types: (b -> c) -> (a -> b) -> a -> c -- To make g fit, c must become (c -> b) (b -> (c -> b)) -> (a -> b) -> a -> (c -> b) -- f returns same type as input, so b == a (a -> (c -> a)) -> (a -> a) -> a -> (c -> a) -- Now we've supplied f and g we can just worry about the result part a -> (c -> a) -- This is the type of (g . f), but we've got an Int argument "1" Int -> (c -> Int) -- So the type of the expression is (c->Int) Nothing wrong with that. Type safe. It might be easier to just start plugging types in than following that explanation, but it does work. > C) A function h is given as: h p x = p (f x). Which of the following > statements is correct. p is obviously a function of one (or more) arguments. h takes two arguments, and returns the result of p, so h :: (a -> b) -> c -> b because we know f :: a -> a, we can say a and c are the same type h :: (a -> b) -> a -> b That's number 3. (g . f) x is the same as g (f x). Therefore we can re-write h h' p x = (p . f) x -- or h' p = (p . f) -- the argument is implicit That's 4 > 3) h :: (a -> b) -> a -> b > 4) h is equivalent to h' with h' p = p . f From informationen at gmx.de Sun Sep 27 11:18:21 2009 From: informationen at gmx.de (informationen) Date: Sun Sep 27 10:55:59 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: References: <20090927102249.GB4216@debian> Message-ID: <20090927151821.GE4216@debian> A thousand thanks to Peter, Paul and Daniel. That helped a lot. I now understand he Haskell type system better than ever. Thanks you guys. Chris >I'll try my hand at these, anybody feel free to point out all my >deliberate mistakes. > >On 27 Sep 2009, at 11:22, informationen wrote: > >>Two functions f and g with the following signatures are given: >>f :: a -> a >>g :: b -> c -> b >> >>A) Give the type of the following expressions: >> >>1) g [False] True :: 2) g [] True > >These two are simply that the result of the function g is the same as >it's first argument. The first takes a list of Boolean, the second a >list of an unknown type. Therefore the results are the same types >[Boolean] and [a] respectively. > >>:: 3) g (f True) :: > >The result of f is the same type as it's argument, in this case Boolean. >g's first and only argument which is the type of the result of f -- >Boolean. >As we haven't specified a second argument, the returned type is a >function which takes an unknown type and returns a boolean. > >(c -> Bool) > > >>4) g f :: > >Similar to the last case except now the only argument supplied is a >function of type (a -> a). >(c -> (a -> a)) > >>Answers: >> >>B) Which of the following statements is correct? >> >First two are easy, and the same. >>1) g f 1 c -> Int >>2) g (f 1) c -> Int > >Remember that the type of (.) is (b -> c) -> (a -> b) -> a -> c. That >is, it takes two functions where they both take an argument, and the >type of the first functions argument is the same as the seconds >result. > >>3) g . (f 1) > >Here (f 1) doesn't take an argument, it's already supplied. That >makes it's type Int, not (a -> b). That's not right. > >>4) g . f 1 > >The same, the brackets where redundant in the previous expression. > >>5) (g . f) 1 > >Here we're composing before giving the arguments. >So, remembering that f :: a -> a and g :: b -> c -> b, we can start >filling in the types: > >(b -> c) -> (a -> b) -> a -> c -- To make g fit, c must >become (c -> b) >(b -> (c -> b)) -> (a -> b) -> a -> (c -> b) -- f returns same type >as input, so b == a >(a -> (c -> a)) -> (a -> a) -> a -> (c -> a) -- Now we've supplied f >and g we can just worry about the result part >a -> (c -> a) -- This is the type of >(g . f), but we've got an Int argument "1" >Int -> (c -> Int) -- So the type of the >expression is (c->Int) > >Nothing wrong with that. Type safe. > >It might be easier to just start plugging types in than following >that explanation, but it does work. > >>C) A function h is given as: h p x = p (f x). Which of the following >>statements is correct. > >p is obviously a function of one (or more) arguments. h takes two >arguments, and returns the result of p, so > >h :: (a -> b) -> c -> b > >because we know f :: a -> a, we can say a and c are the same type > >h :: (a -> b) -> a -> b > >That's number 3. > >(g . f) x is the same as g (f x). Therefore we can re-write h > >h' p x = (p . f) x -- or >h' p = (p . f) -- the argument is implicit > >That's 4 > >>3) h :: (a -> b) -> a -> b >>4) h is equivalent to h' with h' p = p . f -- Ein Wertsack ist ein Beutel, der aufgrund seiner besonderen Verwendung nicht Wertbeutel, sondern Wertsack genannt wird, weil sein Inhalt aus mehreren Wertbeuteln besteht, die in den Wertsack nicht verbeutelt, sondern versackt werden. Merkblatt der Deutschen Bundespost From rziemba at gmail.com Sun Sep 27 13:40:48 2009 From: rziemba at gmail.com (Robert Ziemba) Date: Sun Sep 27 13:18:51 2009 Subject: [Haskell-beginners] Haskell type system In-Reply-To: <20090927102249.GB4216@debian> References: <20090927102249.GB4216@debian> Message-ID: <65135790909271040m1fa77ad3m70871c5a095882a5@mail.gmail.com> I have found this online book to be fairly easy to understand http://book.realworldhaskell.org/read/types-and-functions.html Take a look at the section called "The type of a function with more than one argument" and also follow the link to "Partial function application and currying" If you decide to use this book, I would recommend reading the entire thing. I tried to skip around at first, but got lost pretty quickly. The author does a good job of adding complexity step by step, so it might be best to start from the beginning. On Sun, Sep 27, 2009 at 3:22 AM, informationen wrote: > Hi, > > i am trying to understand the Haskell type system. I thought i understood > it > quite well until i encountered the three following exercises. As you can > see, i > have the answers already. But i don't understand, why they are correct. > > Could anybody tell me a good place where i could learn how to answers these > kind > of questions correctly or could give me some explanations, why these > answers are > correct? > > Any help is highly appreciated. > > Kind regards > > Chris > Two functions f and g with the following signatures are given: > f :: a -> a > g :: b -> c -> b > > A) Give the type of the following expressions: > > 1) g [False] True :: 2) g [] True :: 3) g (f True) :: 4) g f > :: > Answers: > > 1 [Boolean] 2) [a] 3) c -> Bool > 4) c -> (a -> a)1) g f 1 > B) Which of the following statements is correct? > > 2) g (f 1) is type correct > 3) g . (f 1) is type correct > 4) g . f 1 is type correct > 5) (g . f) 1 is type correct > 6) none of the expressions is correct > > Answers: > 1,2 and 5 are correct. > > C) A function h is given as: h p x = p (f x). Which of the following > statements is correct. > > 1) h :: a -> b -> a -> b > 2) h :: (a -> a) -> a -> a > 3) h :: (a -> b) -> a -> b > 4) h is equivalent to h' with h' p = p . f > 5) h is equivalent to h' with h' p = p f > 5) h is equivalent to h' with h' p x = p f x > > Answers: > (I am not sure, if i remember correctly, but 3) and 4) should be > correct.) > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090927/cdfba994/attachment.html From nathanmholden at gmail.com Sun Sep 27 23:08:11 2009 From: nathanmholden at gmail.com (Nathan Holden) Date: Sun Sep 27 22:46:14 2009 Subject: [Haskell-beginners] $= operator Message-ID: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> I was reading Eleara examples out of Hackage, and I kept noticing lines like this: > t <- get GLFW.time > GLFW.time $= 0 Most of the rest of it made more-or-less sense (I mean, there IS a reason I'm still asking questions to beginners!) but I couldn't seem to find anything about it on my usual reference sources, and it's uses don't make that much sense to me. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090927/83978194/attachment.html From mklklk at hotmail.com Sun Sep 27 23:27:53 2009 From: mklklk at hotmail.com (Kui Ma) Date: Sun Sep 27 23:05:57 2009 Subject: [Haskell-beginners] examples of Crypto In-Reply-To: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> References: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> Message-ID: Hello, I want some examples to learn the package of Crypto, anyone knows where I can find them? Thank you in advance! Regards, kui _________________________________________________________________ Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090927/635e1ec9/attachment.html From alexander.dunlap at gmail.com Mon Sep 28 00:04:41 2009 From: alexander.dunlap at gmail.com (Alexander Dunlap) Date: Sun Sep 27 23:43:03 2009 Subject: [Haskell-beginners] $= operator In-Reply-To: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> References: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> Message-ID: <57526e770909272104v1a12fcbfo687d5201f012c564@mail.gmail.com> On Sun, Sep 27, 2009 at 8:08 PM, Nathan Holden wrote: > I was reading Eleara examples out of Hackage, and I kept noticing lines like > this: > >>?? t <- get GLFW.time >>?? GLFW.time $= 0 > > Most of the rest of it made more-or-less sense (I mean, there IS a reason > I'm still asking questions to beginners!) but I couldn't seem to find > anything about it on my usual reference sources, and it's uses don't make > that much sense to me. > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > That's not an Elerea operator; that's from OpenGL. Roughly speaking, it means "set" - change the value of a setting or variable of some sort. I'm also working on familiarizing myself with FRP and Elerea, so good luck! Alex From roman_naumann at fastmail.fm Mon Sep 28 12:55:00 2009 From: roman_naumann at fastmail.fm (Roman Naumann) Date: Mon Sep 28 12:33:03 2009 Subject: [Haskell-beginners] Better way to retrieve data strucures from sql-db? Message-ID: <4AC0EA64.2040105@fastmail.fm> The following function converts the data read from an sql database to a player data structure. toPlayer :: [SqlValue] -> Player toPlayer sx = Player { plID = fromSql (sx!!0), plAccount = fromSql (sx!!1), plForename = fromSql (sx!!2), plSurename = fromSql (sx!!3), plPos_x = fromSql (sx!!4), plPos_y = fromSql (sx!!5) } It appears to me, that I have to write this boilerplate code every time I introduce a new data structure that has to be read from the sql database. Can you think of a way to automate this? Thanks for your replies, Roman Naumann -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 261 bytes Desc: OpenPGP digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20090928/23ead438/signature.bin From michael at snoyman.com Mon Sep 28 13:36:39 2009 From: michael at snoyman.com (Michael Snoyman) Date: Mon Sep 28 13:14:40 2009 Subject: [Haskell-beginners] examples of Crypto In-Reply-To: References: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> Message-ID: <29bf512f0909281036q554c0b51h48bbae7df2c9a2fd@mail.gmail.com> I use it in clientsession: http://hackage.haskell.org/package/clientsession. Let me know if I can be of some assistance. Michael 2009/9/28 Kui Ma > > Hello, > > I want some examples to learn the package of Crypto, anyone knows where I > can find them? Thank you in advance! > > Regards, > kui > > ------------------------------ > Invite your mail contacts to join your friends list with Windows Live > Spaces. It's easy! Try it! > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090928/f4e3f04b/attachment.html From joe at fixieconsulting.com Mon Sep 28 13:47:12 2009 From: joe at fixieconsulting.com (Joe Van Dyk) Date: Mon Sep 28 13:25:33 2009 Subject: [Haskell-beginners] Anyone use haskell + cassandra? Message-ID: I'm thinking of using Haskell + Cassandra + Rails to build a horizontally-scalable web app. Haskell + Cassandra would power the webservice that Rails communicates with. Has anyone done anything like that? -- Joe Van Dyk http://fixieconsulting.com From hyangfji at gmail.com Mon Sep 28 15:22:40 2009 From: hyangfji at gmail.com (Hong Yang) Date: Mon Sep 28 15:00:42 2009 Subject: [Haskell-beginners] Finance.Quote.Yahoo Message-ID: Hi All, I was using a modified version of Finance.Quote.Yahoo module (I only added "setproxy"), and got unwanted print on the screen. My questions are as follows: 1) How can I get rid of this screen print? (I guess HTTP package called by the Finance.Quote.Yahoo module internally prints those info. Should I temporarily turn off STDOUT then turn it on? How to do this?) 2) "ghc-pkg list" shows HTTP-4000.0.8. How come the output shows "HTTP-4000.0.5"? Thanks, Hong P.S. The following is the screen output: Sending: GET http://download.finance.yahoo.com/d/quotes.csv?s=BAX&f=l1 HTTP/1.1 Content-Length: 0 User-Agent: hs-HTTP-4000.0.5 Host: download.finance.yahoo.com proxy uri host: xxxproxy, port: :74 Creating new connection to xxxproxy:74 Received: HTTP/1.1 200 OK Proxy-Connection: Keep-Alive Connection: Keep-Alive Transfer-Encoding: chunked Via: 1.1 SAUSISA02 Date: Mon, 28 Sep 2009 18:49:43 GMT Content-Type: application/octet-stream P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV" Cache-Control: private Content-Length: 7 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090928/c500bf96/attachment-0001.html From joaopizani at gmail.com Mon Sep 28 19:23:57 2009 From: joaopizani at gmail.com (=?UTF-8?Q?Jo=C3=A3o_Paulo_Pizani_Flor?=) Date: Mon Sep 28 19:02:18 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: Message-ID: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> I'm also prety interested in that point. Last semester I've programmed some coursework on formal language theory in Haskell and had to run the software on my PC. Only if I knew how to easily statically link everything together... Has anyone here experience in doing this? Thanks in advance, Jo?o Paulo Pizani Flor http://pet.inf.ufsc.br/~joaopizani CS Student - Third year @ Brazilian Federal University - UFSC 2009/9/27 Joe Van Dyk > How hard is it to distribute a haskell binary that contains a bunch of > libraries embedded in the executable? Is it pretty easy to make a > large statically-linked executable? > > -- > Joe Van Dyk > http://fixieconsulting.com > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090928/6123baf8/attachment.html From magnus at therning.org Tue Sep 29 01:43:23 2009 From: magnus at therning.org (Magnus Therning) Date: Tue Sep 29 01:21:24 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> Message-ID: <20090929054323.GA28496@tatooine> On Mon, Sep 28, 2009 at 08:23:57PM -0300, Jo?o Paulo Pizani Flor wrote: > I'm also prety interested in that point. Last semester I've programmed some > coursework on formal language theory in Haskell and had to run the software > on my PC. Only if I knew how to easily statically link everything > together... > > Has anyone here experience in doing this? What OS is it for? (I'm inclined to believe it's windows, but I can't be sure.) In either case, why distribute it as a _completely_ statically linked executable, wouldn't putting together an installer just as good? /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20090929/5381498e/attachment.bin From alp at mestan.fr Tue Sep 29 02:36:19 2009 From: alp at mestan.fr (Alp Mestan) Date: Tue Sep 29 02:14:18 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: <20090929054323.GA28496@tatooine> References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 7:43 AM, Magnus Therning wrote: > What OS is it for? (I'm inclined to believe it's windows, but I can't be > sure.) > > In either case, why distribute it as a _completely_ statically linked > executable, wouldn't putting together an installer just as good? > It would. But actually, while going to work, I was wondering if a sort of Haskell application deployer would be a good idea ? It'd package Haskell apps with the necessary dynamic libraries, etc, depending on the target system of course, just like NSIS or so does. Except if nobody can't find a good reason not to use tools like NSIS which are already portable IIRC. -- Alp Mestan http://blog.mestan.fr/ http://alp.developpez.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090929/e0b3c82d/attachment.html From magnus at therning.org Tue Sep 29 04:15:11 2009 From: magnus at therning.org (Magnus Therning) Date: Tue Sep 29 03:53:17 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 7:36 AM, Alp Mestan wrote: > On Tue, Sep 29, 2009 at 7:43 AM, Magnus Therning > wrote: >> >> What OS is it for? ?(I'm inclined to believe it's windows, but I can't be >> sure.) >> >> In either case, why distribute it as a _completely_ statically linked >> executable, wouldn't putting together an installer just as good? > > It would. But actually, while going to work, I was wondering if a sort of > Haskell application deployer would be a good idea ? > It'd package Haskell apps with the necessary dynamic libraries, etc, > depending on the target system of course, just like NSIS or so does. Except > if nobody can't find a good reason not to use tools like NSIS which are > already portable IIRC. It could be a good extension to Cabal (not the tool), currently there's the target 'sdist', maybe add a target 'wininst'. Similar to how Python's distutils have targets for MSI and Windows installers. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From alp at mestan.fr Tue Sep 29 04:53:48 2009 From: alp at mestan.fr (Alp Mestan) Date: Tue Sep 29 04:31:50 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 10:15 AM, Magnus Therning wrote: > It could be a good extension to Cabal (not the tool), currently > there's the target 'sdist', maybe add a target 'wininst'. Similar to > how Python's distutils have targets for MSI and Windows installers. > Actually, I was more thinking about a separate tool. Something you'd just install, from cabal/apt/pacman/whatever, using some external system tools to check out dependencies of the application, then packaging everything and putting some install.sh script around for linux, and a nice installer under Windows, etc. It is just an idea, but the main point here is that it'd just be "simple". (of course, the idea I describe may fail heavily for packaging very complex and heavy apps, IMO, but I think there are workarounds) PS : shouldn't we move this discussion to -cafe ? -- Alp Mestan http://blog.mestan.fr/ http://alp.developpez.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090929/dc06a91d/attachment.html From magnus at therning.org Tue Sep 29 05:13:59 2009 From: magnus at therning.org (Magnus Therning) Date: Tue Sep 29 04:51:58 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 9:53 AM, Alp Mestan wrote: > On Tue, Sep 29, 2009 at 10:15 AM, Magnus Therning > wrote: >> >> It could be a good extension to Cabal (not the tool), currently >> there's the target 'sdist', maybe add a target 'wininst'. ?Similar to >> how Python's distutils have targets for MSI and Windows installers. > > Actually, I was more thinking about a separate tool. Something you'd just > install, from cabal/apt/pacman/whatever, using some external system tools to > check out dependencies of the application, then packaging everything and > putting some install.sh script around for linux, and a nice installer under > Windows, etc. It is just an idea, but the main point here is that it'd just > be "simple". (of course, the idea I describe may fail heavily for packaging > very complex and heavy apps, IMO, but I think there are workarounds) Linux ought to be simple, a script based on output from ldd would most likely do it. Or even better, run prelink on the executable (I suspect it'd work, but I'm not sure). Though for Linux I don't understand why anyone would do this, since it's so simple to put together distro packages :-) IMNSHO Windows is the only OS where it's useful to create binary installers. There are quite a few tools out there for that, I'd expect most of them would be suitable for Haskell binaries. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From maydwell at gmail.com Tue Sep 29 05:19:03 2009 From: maydwell at gmail.com (Lyndon Maydwell) Date: Tue Sep 29 04:57:02 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: No love for OS X? On Tue, Sep 29, 2009 at 5:13 PM, Magnus Therning wrote: > On Tue, Sep 29, 2009 at 9:53 AM, Alp Mestan wrote: >> On Tue, Sep 29, 2009 at 10:15 AM, Magnus Therning >> wrote: >>> >>> It could be a good extension to Cabal (not the tool), currently >>> there's the target 'sdist', maybe add a target 'wininst'. ?Similar to >>> how Python's distutils have targets for MSI and Windows installers. >> >> Actually, I was more thinking about a separate tool. Something you'd just >> install, from cabal/apt/pacman/whatever, using some external system tools to >> check out dependencies of the application, then packaging everything and >> putting some install.sh script around for linux, and a nice installer under >> Windows, etc. It is just an idea, but the main point here is that it'd just >> be "simple". (of course, the idea I describe may fail heavily for packaging >> very complex and heavy apps, IMO, but I think there are workarounds) > > Linux ought to be simple, a script based on output from ldd would most > likely do it. ?Or even better, run prelink on the executable (I > suspect it'd work, but I'm not sure). ?Though for Linux I don't > understand why anyone would do this, since it's so simple to put > together distro packages :-) > > IMNSHO Windows is the only OS where it's useful to create binary > installers. ?There are quite a few tools out there for that, I'd > expect most of them would be suitable for Haskell binaries. > > /M > > -- > Magnus Therning ? ? ? ? ? ? ? ? ? ? ? ?(OpenPGP: 0xAB4DFBA4) > magnus?therning?org ? ? ? ? ?Jabber: magnus?therning?org > http://therning.org/magnus ? ? ? ? identi.ca|twitter: magthe > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From alp at mestan.fr Tue Sep 29 05:29:45 2009 From: alp at mestan.fr (Alp Mestan) Date: Tue Sep 29 05:07:44 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 11:19 AM, Lyndon Maydwell wrote: > No love for OS X? > No knowledge of OS X binary/library interactions, actually :-) Magnus : I agree about Linux. At least, a cabal package is enough, since there are a lot of cabal-to- tools around. For Windows, which tool do you know that'd make it for Haskell apps ? -- Alp Mestan http://alpmestan.wordpress.com/ http://alp.developpez.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090929/9c10a701/attachment-0001.html From magnus at therning.org Tue Sep 29 05:30:44 2009 From: magnus at therning.org (Magnus Therning) Date: Tue Sep 29 05:08:42 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 10:19 AM, Lyndon Maydwell wrote: > No love for OS X? No, not from me personally ;-) Seriously, I simply don't know anything about OSX. I've never owned a McComputer. Though I suspect OSX isn't quite as broken as Windows in relation to installers. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From magnus at therning.org Tue Sep 29 05:32:52 2009 From: magnus at therning.org (Magnus Therning) Date: Tue Sep 29 05:10:51 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: On Tue, Sep 29, 2009 at 10:29 AM, Alp Mestan wrote: > On Tue, Sep 29, 2009 at 11:19 AM, Lyndon Maydwell > wrote: >> >> No love for OS X? > > No knowledge of OS X binary/library interactions, actually :-) > > Magnus : I agree about Linux. At least, a cabal package is enough, since > there are a lot of cabal-to- tools > around. For Windows, which tool do you know that'd make it for Haskell apps > ? I don't know of any Haskell-specific ones, but I'd be surprised if you can't make NSIS or WiX create an installer for a Haskell binary. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From maydwell at gmail.com Tue Sep 29 05:35:28 2009 From: maydwell at gmail.com (Lyndon Maydwell) Date: Tue Sep 29 05:13:27 2009 Subject: [Haskell-beginners] Distributing executables In-Reply-To: References: <325156ff0909281623k1a38ac84rfa1f0109219bfc57@mail.gmail.com> <20090929054323.GA28496@tatooine> Message-ID: I was thinking more along the lines of how reasonable it would be to ask a non-technical user to build a program you wish to distribute from source. On linux / bsd /etc it would probably be unsurprising, but it may be asking too much of windows and mac users. On Tue, Sep 29, 2009 at 5:30 PM, Magnus Therning wrote: > On Tue, Sep 29, 2009 at 10:19 AM, Lyndon Maydwell wrote: >> No love for OS X? > > No, not from me personally ;-) > > Seriously, I simply don't know anything about OSX. ?I've never owned a > McComputer. ?Though I suspect OSX isn't quite as broken as Windows in > relation to installers. > > /M > > -- > Magnus Therning ? ? ? ? ? ? ? ? ? ? ? ?(OpenPGP: 0xAB4DFBA4) > magnus?therning?org ? ? ? ? ?Jabber: magnus?therning?org > http://therning.org/magnus ? ? ? ? identi.ca|twitter: magthe > From colin at colina.demon.co.uk Tue Sep 29 13:02:40 2009 From: colin at colina.demon.co.uk (Colin Paul Adams) Date: Tue Sep 29 12:40:45 2009 Subject: [Haskell-beginners] Can you identify this recursion pattern please? Message-ID: This looks like some kind of unfold to me. Can you help? I have the following code that works for the first two levels: -- get the list of all (recursive) child gallery names -- first, just the immediate children childNames <- childGalleries db (name gallery) -- now let's try the next level grandchildren <- mapM (childGalleries db) childNames mapM putStrLn (childNames ++ concat grandchildren) The database query childGalleries gives me a list of all the immediate children. So what I want is recursively, the names of all the descendants, as well as the original name, concatenated to a flat list. I'm struggling to identify the higher-order recursion function I need. -- Colin Adams Preston Lancashire From colin at colina.demon.co.uk Tue Sep 29 14:51:23 2009 From: colin at colina.demon.co.uk (Colin Paul Adams) Date: Tue Sep 29 14:29:22 2009 Subject: [Haskell-beginners] Can you identify this recursion pattern please? In-Reply-To: (Colin Paul Adams's message of "Tue\, 29 Sep 2009 18\:02\:40 +0100") References: Message-ID: >>>>> "Colin" == Colin Paul Adams writes: Colin> The database query childGalleries gives me a list of all Colin> the immediate children. So what I want is recursively, the Colin> names of all the descendants, as well as the original name, Colin> concatenated to a flat list. I'm struggling to identify the Colin> higher-order recursion function I need. I worked out that Data.Tree.unfoldM_BF was what I needed. -- Colin Adams Preston Lancashire From rziemba at gmail.com Tue Sep 29 15:25:07 2009 From: rziemba at gmail.com (Robert Ziemba) Date: Tue Sep 29 15:03:28 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix Message-ID: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> I have been working with the regular expression package (Text.Regex.Posix). My hope was to find a simple way to remove a pair of XML tags from a short string. I have something like this "Data" and would like to extract 'Data'. There is only one tag pair, no nesting, and I know exactly what the tag is. My first attempt was this: "123" =~ "[^].+[^]"::String result: "123" Upon further experimenting I realized that it only works with more than 2 digits in 'Data'. I occured to me that my thinking on how this regular expression works was not correct - but I don't understand why it works at all for 3 or more digits. Can anyone help me understand this result and perhaps suggest another strategy? Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090929/77d2a447/attachment.html From colin at colina.demon.co.uk Tue Sep 29 15:33:51 2009 From: colin at colina.demon.co.uk (Colin Paul Adams) Date: Tue Sep 29 15:11:54 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> (Robert Ziemba's message of "Tue\, 29 Sep 2009 12\:25\:07 -0700") References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> Message-ID: >>>>> "Robert" == Robert Ziemba writes: Robert> Can anyone help me understand this result and perhaps Robert> suggest another strategy? I can manage the latter - use an XML parser to extract the text. -- Colin Adams Preston Lancashire From patrick.leboutillier at gmail.com Tue Sep 29 16:29:52 2009 From: patrick.leboutillier at gmail.com (Patrick LeBoutillier) Date: Tue Sep 29 16:07:52 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> Message-ID: Robert, On Tue, Sep 29, 2009 at 3:25 PM, Robert Ziemba wrote: > I have been working with the regular expression package (Text.Regex.Posix). > My hope was to find a simple way to remove a pair of XML tags from a short > string. > > I have something like this "Data" and would like to extract > 'Data'. There is only one tag pair, no nesting, and I know exactly what the > tag is. > > My first attempt was this: > > "123" =~ "[^].+[^]"::String > > result: "123" > > Upon further experimenting I realized that it only works with more than 2 > digits in 'Data'. I occured to me that my thinking on how this regular > expression works was not correct - but I don't understand why it works at > all for 3 or more digits. > > Can anyone help me understand this result and perhaps suggest another > strategy? Thank you. > The regex you are using here can be described as such: "Match a character not in the set '<,t,a,g,>', followed by 1 or more of anything, followed by a character not in the set '<,/,t,a,g,>'." Effectively, it will not match if your data has less than 3 characters and is probably not the correct regex for this job, i.e. it would also match "x123x". What you need is regex capturing, but I don't know if that is available in that regex library (I'm not an expert Haskeller). If you really need a regex to locate the tag, you could use a function like this to extract it: getTagData tag s = let match = s =~ ("<" ++ tag ++ ">.*")::String dropTag = drop (length tag + 2) s getData = take (length match - (2 * length tag + 5)) dropTag in if length match > 0 then Just getData else Nothing *Main> getTagData "tag" "123" Just "123" Patrick > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- ===================== Patrick LeBoutillier Rosem?re, Qu?bec, Canada -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090929/46415b74/attachment.html From magnus at therning.org Wed Sep 30 01:58:35 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Sep 30 01:36:35 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> Message-ID: <20090930055835.GB3478@tatooine> On Tue, Sep 29, 2009 at 12:25:07PM -0700, Robert Ziemba wrote: > I have been working with the regular expression package (Text.Regex.Posix). > My hope was to find a simple way to remove a pair of XML tags from a short > string. > > I have something like this "Data" and would like to extract > 'Data'. There is only one tag pair, no nesting, and I know exactly what the > tag is. > > My first attempt was this: > > "123" =~ "[^].+[^]"::String > > result: "123" > > Upon further experimenting I realized that it only works with more than 2 > digits in 'Data'. I occured to me that my thinking on how this regular > expression works was not correct - but I don't understand why it works at > all for 3 or more digits. > > Can anyone help me understand this result and perhaps suggest another > strategy? Thank you. Personally I would have used tagsoup for this sort of thing. Keep in mind the eternal words Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems. -- Jamie Zawinski As you so nicely demonstrated yourself ;-) /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20090930/bfc363ea/attachment-0001.bin From maydwell at gmail.com Wed Sep 30 02:27:08 2009 From: maydwell at gmail.com (Lyndon Maydwell) Date: Wed Sep 30 02:05:04 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <20090930055835.GB3478@tatooine> References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> <20090930055835.GB3478@tatooine> Message-ID: HXT should be able to do what you're after quite easily from what I've seen. On Wed, Sep 30, 2009 at 1:58 PM, Magnus Therning wrote: > On Tue, Sep 29, 2009 at 12:25:07PM -0700, Robert Ziemba wrote: >> I have been working with the regular expression package (Text.Regex.Posix). >> ?My hope was to find a simple way to remove a pair of XML tags from a short >> string. >> >> I have something like this "Data" and would like to extract >> 'Data'. ?There is only one tag pair, no nesting, and I know exactly what the >> tag is. >> >> My first attempt was this: >> >> ? "123" =~ "[^].+[^]"::String >> >> result: ?"123" >> >> Upon further experimenting I realized that it only works with more than 2 >> digits in 'Data'. ?I occured to me that my thinking on how this regular >> expression works was not correct - but I don't understand why it works at >> all for 3 or more digits. >> >> Can anyone help me understand this result and perhaps suggest another >> strategy? ?Thank you. > > Personally I would have used tagsoup for this sort of thing. ?Keep in mind the > eternal words > > ?Some people, when confronted with a problem, think 'I know, I'll use > ?regular expressions.' Now they have two problems. > ? ? ? -- Jamie Zawinski > > As you so nicely demonstrated yourself ;-) > > /M > > -- > Magnus Therning ? ? ? ? ? ? ? ? ? ? ? ?(OpenPGP: 0xAB4DFBA4) > magnus?therning?org ? ? ? ? ?Jabber: magnus?therning?org > http://therning.org/magnus ? ? ? ? identi.ca|twitter: magthe > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From aditya.siram at gmail.com Wed Sep 30 03:06:05 2009 From: aditya.siram at gmail.com (aditya siram) Date: Wed Sep 30 02:44:06 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> Message-ID: <594f78210909300006l7da49422lf8098bd5f9be66a8@mail.gmail.com> This is how I did it using the HXT library : Prelude Text.XML.HXT.Parser.XmlParsec Text.XML.HXT.Arrow.XmlIOStateArrow Text.XML.HXT.Arrow> runX (readString [] "123" >>> getXPathTrees "tag" >>> getChildren >>> getText) ["123"] Everything after "Prelude" upto the first ">" is what you have to import to make this work. -"readString" converts the input string into a internal representation of an XML tree -"getXPathTrees" sets the path to all 's, -"getChildren" narrows it down to the data between and , -"getText" extracts all the data between those tags, -"runX" fires up the whole process and returns the results as a list in the IO Monad. hth, deech On Tue, Sep 29, 2009 at 2:25 PM, Robert Ziemba wrote: > I have been working with the regular expression package (Text.Regex.Posix). > My hope was to find a simple way to remove a pair of XML tags from a short > string. > > I have something like this "Data" and would like to extract > 'Data'. There is only one tag pair, no nesting, and I know exactly what the > tag is. > > My first attempt was this: > > "123" =~ "[^].+[^]"::String > > result: "123" > > Upon further experimenting I realized that it only works with more than 2 > digits in 'Data'. I occured to me that my thinking on how this regular > expression works was not correct - but I don't understand why it works at > all for 3 or more digits. > > Can anyone help me understand this result and perhaps suggest another > strategy? Thank you. > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090930/43297f2e/attachment.html From magnus at therning.org Wed Sep 30 03:59:35 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Sep 30 03:37:32 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <20090930055835.GB3478@tatooine> References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> <20090930055835.GB3478@tatooine> Message-ID: On Wed, Sep 30, 2009 at 6:58 AM, Magnus Therning wrote: [..] > Personally I would have used tagsoup for this sort of thing. ?Keep in mind the > eternal words > > ?Some people, when confronted with a problem, think 'I know, I'll use > ?regular expressions.' Now they have two problems. > ? ? ? -- Jamie Zawinski > > As you so nicely demonstrated yourself ;-) Here's a quick and dirty solution using tagsoup: % cat file.xml 123 456 789 Text.HTML.Download Text.HTML.TagSoup> tags <- openItem "file.xml" Text.HTML.Download Text.HTML.TagSoup> map (fromTagText . head . tail) $ partitions (TagOpen "tag" [] ~==) (parseTags tags) ["123","456","789"] /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From Christian.Maeder at dfki.de Wed Sep 30 08:48:58 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Wed Sep 30 08:26:57 2009 Subject: [Haskell-beginners] Re: remove XML tags using Text.Regex.Posix In-Reply-To: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> References: <65135790909291225ge73308emb0891250638c5145@mail.gmail.com> Message-ID: <4AC353BA.8050905@dfki.de> I think regexs are a pain und would suggest the xml-light package for your purpose, which is the smallest xml library. (Or use take, drop, isPrefixOf and isSuffixOf to chop of your tags manually.) http://hackage.haskell.org/package/xml Cheers Christian Prelude Text.XML.Light> concatMap strContent . onlyElems $ parseXML "123" "123" Robert Ziemba wrote: > I have been working with the regular expression package > (Text.Regex.Posix). My hope was to find a simple way to remove a pair > of XML tags from a short string. > > I have something like this "Data" and would like to extract > 'Data'. There is only one tag pair, no nesting, and I know exactly what > the tag is. > > My first attempt was this: > > "123" =~ "[^].+[^]"::String > > result: "123" > > Upon further experimenting I realized that it only works with more than > 2 digits in 'Data'. I occured to me that my thinking on how this > regular expression works was not correct - but I don't understand why it > works at all for 3 or more digits. > > Can anyone help me understand this result and perhaps suggest another > strategy? Thank you. > > > ------------------------------------------------------------------------ > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From jakubuv at gmail.com Wed Sep 30 12:11:46 2009 From: jakubuv at gmail.com (Jan Jakubuv) Date: Wed Sep 30 11:49:57 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <20090930144437.GC2040@lxultra2.macs.hw.ac.uk> References: <20090930144437.GC2040@lxultra2.macs.hw.ac.uk> Message-ID: <20090930161146.GD2040@lxultra2.macs.hw.ac.uk> Hi Robert, On Tue, Sep 29, 2009 at 12:25:07PM -0700, Robert Ziemba wrote: > I have been working with the regular expression package (Text.Regex.Posix). > My hope was to find a simple way to remove a pair of XML tags from a short > string. > > I have something like this "Data" and would like to extract > 'Data'. There is only one tag pair, no nesting, and I know exactly what the > tag is. > This is so simple that I would not recommend anything other than regular expressions. Use the following pattern: pat = "(.*)" It creates a group withing the matched string containing the data (it is done using parenthesis). Use `[[String]]` as a result type and you receive a list of matches where each match is described by a list of strings whose first member is the whole matched string (including and ) and it is followed by values of groups (in our case we have just one group). Thus: *Main> "textdatatext" =~ pat :: [[String]] [["data","data"]] It is easy extract the data using `(!!)` and `head`: *Main> (!! 1) . head $ ("text7text" =~ pat :: [[String]]) "7" > My first attempt was this: > > "123" =~ "[^].+[^]"::String > > result: "123" > The problem with your pattern is that `[^]` doesn't mean what you think it does. Its meaning is ?one character which is not `<`, `t`, `a`, or `>`? as Patrick already described in his mail. > Upon further experimenting I realized that it only works with more than 2 > digits in 'Data'. I occured to me that my thinking on how this regular > expression works was not correct - but I don't understand why it works at > all for 3 or more digits. > It doesn't work for all 3 or more digits: *Main> "tag" =~ "[^].+[^]" :: String "" Briefly, it doesn't work when the data contains one of characters `<`, `t`, `a`, `g`, `>`. Finally, consider using pat = "([^<]*)" which works with more tags in the same line as well. Sincerely, jan. -- Heriot-Watt University is a Scottish charity registered under charity number SC000278. From korpios at korpios.com Wed Sep 30 13:30:31 2009 From: korpios at korpios.com (Tom Tobin) Date: Wed Sep 30 13:08:25 2009 Subject: [Haskell-beginners] remove XML tags using Text.Regex.Posix In-Reply-To: <20090930161146.GD2040@lxultra2.macs.hw.ac.uk> References: <20090930144437.GC2040@lxultra2.macs.hw.ac.uk> <20090930161146.GD2040@lxultra2.macs.hw.ac.uk> Message-ID: On Wed, Sep 30, 2009 at 11:11 AM, Jan Jakubuv wrote: > This is so simple that I would not recommend anything other than regular > expressions. Use the following pattern: > > ? ?pat = "(.*)" Don't use this; the * operator is greedy by default, meaning that will match stuff like "foobarbaz", and your data will end up being "foobarbaz". In other words, a greedy operator tries to consume as much of the string as it possibly can while still matching. If that regex module supports non-greedy operators, you want something like this: pat = "(.*?)" A "?" after a greedy operator makes it non-greedy, meaning it will try to match while consuming as little of the string as it can. If the posix regex module doesn't support this, the PCRE-based one should. From hyangfji at gmail.com Wed Sep 30 19:41:35 2009 From: hyangfji at gmail.com (Hong Yang) Date: Wed Sep 30 19:25:44 2009 Subject: [Haskell-beginners] subtle dissimilarity between fromInteger and fromIntegral Message-ID: Hi, Can someone explain the subtle dissimilarity between fromInteger and fromIntegral? Thanks, Hong -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090930/b776f6bb/attachment.html From jfredett at gmail.com Wed Sep 30 19:57:00 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Sep 30 19:34:57 2009 Subject: [Haskell-beginners] subtle dissimilarity between fromInteger and fromIntegral In-Reply-To: References: Message-ID: <910D6CE0-9E0A-4BF7-B21C-23CFE7145D15@gmail.com> fromInteger takes an integer to any instance of the Num class. The num class provides this function, because (at least in theory) any Integer "is" (in some sense) an element of a Num instancing class. fromIntegral has type `(Integral a, Num b) => a -> b` which means it takes any _integral) type (on that supports div and mod, and a function "toInteger" which takes an integral to an integer) to a num type. I imagine that "fromIntegral" is probably implemented fromIntegral = fromInteger . toInteger HTH /Joe On Sep 30, 2009, at 7:41 PM, Hong Yang wrote: > Hi, > > Can someone explain the subtle dissimilarity between fromInteger and > fromIntegral? > > Thanks, > > Hong > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners