From Matthew_Williams at xyratex.com Thu Oct 2 00:45:01 2008 From: Matthew_Williams at xyratex.com (Matthew Williams) Date: Thu Oct 2 00:41:45 2008 Subject: [Haskell-beginners] Perfect numbers Message-ID: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> Hi Guys, I'm new to Haskell and I was wondering if you can help me: One of the first program's I tend to write when I'm looking at a new language is a program to generate a list of perfect numbers: --My First Perfect Number Generator factors :: Integer -> [Integer] factors x = [z | z <- [1..x-1], x `mod` z == 0] is_perfect :: Integer -> Bool is_perfect x = if sum(factors x) == x then True else False do_perfect :: [Integer] -> [Integer] do_perfect x = [z |z <- x, is_perfect z ] Then to run it: > do_perfect [1..9000] I'm using GHC to run it. My problem / question is this: It's running quite a lot slower than equivalent programs in erlang and python. I suspect it's down to the way I've written it. Any thoughts (or comments in general) Many thanks Matt ______________________________________________________________________ This email may contain privileged or confidential information, which should only be used for the purpose for which it was sent by Xyratex. No further rights or licenses are granted to use such information. If you are not the intended recipient of this message, please notify the sender by return and delete it. You may not use, copy, disclose or rely on the information contained in it. Internet email is susceptible to data corruption, interception and unauthorised amendment for which Xyratex does not accept liability. While we have taken reasonable precautions to ensure that this email is free of viruses, Xyratex does not accept liability for the presence of any computer viruses in this email, nor for any losses caused as a result of viruses. Xyratex Technology Limited (03134912), Registered in England & Wales, Registered Office, Langstone Road, Havant, Hampshire, PO9 1SA. The Xyratex group of companies also includes, Xyratex Ltd, registered in Bermuda, Xyratex International Inc, registered in California, Xyratex (Malaysia) Sdn Bhd registered in Malaysia, Xyratex Technology (Wuxi) Co Ltd registered in The People's Republic of China and Xyratex Japan Limited registered in Japan. ______________________________________________________________________ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081002/fb1929bb/attachment.htm From 666wman at gmail.com Thu Oct 2 01:25:17 2008 From: 666wman at gmail.com (wman) Date: Thu Oct 2 01:21:58 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> References: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> Message-ID: First step would probably be using Ints instead of Integers. On Thu, Oct 2, 2008 at 6:45 AM, Matthew Williams < Matthew_Williams@xyratex.com> wrote: > Hi Guys, > > I'm new to Haskell and I was wondering if you can help me: > > One of the first program's I tend to write when I'm looking at a new > language is a program to generate a list of perfect numbers: > > --My First Perfect Number Generator > factors :: Integer -> [Integer] > factors x = [z | z <- [1..x-1], x `mod` z == 0] > > is_perfect :: Integer -> Bool > is_perfect x = if sum(factors x) == x then True else False > > do_perfect :: [Integer] -> [Integer] > do_perfect x = [z |z <- x, is_perfect z ] > > Then to run it: > > do_perfect [1..9000] > > I'm using GHC to run it. My problem / question is this: It's running quite > a lot slower than equivalent programs in erlang and python. I suspect it's > down to the way I've written it. Any thoughts (or comments in general) > > Many thanks > > Matt > > ______________________________________________________________________ > This email may contain privileged or confidential information, which should > only be used for the purpose for which it was sent by Xyratex. No further > rights or licenses are granted to use such information. If you are not the > intended recipient of this message, please notify the sender by return and > delete it. You may not use, copy, disclose or rely on the information > contained in it. > > Internet email is susceptible to data corruption, interception and > unauthorised amendment for which Xyratex does not accept liability. While we > have taken reasonable precautions to ensure that this email is free of > viruses, Xyratex does not accept liability for the presence of any computer > viruses in this email, nor for any losses caused as a result of viruses. > > Xyratex Technology Limited (03134912), Registered in England & Wales, > Registered Office, Langstone Road, Havant, Hampshire, PO9 1SA. > > The Xyratex group of companies also includes, Xyratex Ltd, registered in > Bermuda, Xyratex International Inc, registered in California, Xyratex > (Malaysia) Sdn Bhd registered in Malaysia, Xyratex Technology (Wuxi) Co Ltd > registered in The People's Republic of China and Xyratex Japan Limited > registered in Japan. > ______________________________________________________________________ > > _______________________________________________ > 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/20081002/0734ecc1/attachment.htm From jason.dusek at gmail.com Thu Oct 2 01:54:00 2008 From: jason.dusek at gmail.com (Jason Dusek) Date: Thu Oct 2 01:50:39 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: References: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> Message-ID: <42784f260810012254o3f820f11s404bf76bbda2081e@mail.gmail.com> On Wed, Oct 1, 2008 at 22:25, wman <666wman@gmail.com> wrote: > First step would probably be using Ints instead of Integers. Doesn't seem to make a real difference in GHCi. -- _jsn Prelude> do_perfect [1..2000] :: [Integer] [6,28,496] it :: [Integer] (4.22 secs, 169352736 bytes) Prelude> do_perfect [1..2000] :: [Int] [6,28,496] it :: [Int] (4.23 secs, 153699692 bytes) From 666wman at gmail.com Thu Oct 2 01:54:47 2008 From: 666wman at gmail.com (wman) Date: Thu Oct 2 01:51:26 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> References: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> Message-ID: factors :: Int -> [Int] factors x = [z | z <- [1..(x `div` 2)], x `mod` z == 0] is_perfect :: Int -> Bool is_perfect x = sum(factors x) == x do_perfect :: [Int] -> [Int] do_perfect x = [z |z <- x, is_perfect z ] main = print $ do_perfect [1..9000] -- compile with ghc -O2, it's more than ten times faster for me -- althought using 1..(x `div 2) instead of 1..x-1 is a bit of cheating ;-)) On Thu, Oct 2, 2008 at 6:45 AM, Matthew Williams < Matthew_Williams@xyratex.com> wrote: > Hi Guys, > > I'm new to Haskell and I was wondering if you can help me: > > One of the first program's I tend to write when I'm looking at a new > language is a program to generate a list of perfect numbers: > > --My First Perfect Number Generator > factors :: Integer -> [Integer] > factors x = [z | z <- [1..x-1], x `mod` z == 0] > > is_perfect :: Integer -> Bool > is_perfect x = if sum(factors x) == x then True else False > > do_perfect :: [Integer] -> [Integer] > do_perfect x = [z |z <- x, is_perfect z ] > > Then to run it: > > do_perfect [1..9000] > > I'm using GHC to run it. My problem / question is this: It's running quite > a lot slower than equivalent programs in erlang and python. I suspect it's > down to the way I've written it. Any thoughts (or comments in general) > > Many thanks > > Matt > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081002/4b56b37f/attachment-0001.htm From haskell at elbeno.com Thu Oct 2 03:14:23 2008 From: haskell at elbeno.com (Ben Deane) Date: Thu Oct 2 03:11:21 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> References: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> Message-ID: <1222931663.7287.25.camel@epsilon> On Thu, 2008-10-02 at 05:45 +0100, Matthew Williams wrote: > Hi Guys, > > I'm new to Haskell and I was wondering if you can help me: > > One of the first program's I tend to write when I'm looking at a new > language is a program to generate a list of perfect numbers: > > --My First Perfect Number Generator > factors :: Integer -> [Integer] > factors x = [z | z <- [1..x-1], x `mod` z == 0] Hi Matthew, A big optimization for larger numbers is that you only need to go up to the square root of x here and add both z and x/z to the list. (Where x is a perfect square you need to avoid adding the root twice.) It's late and there is probably a better way to do this, but: import List semi_factors :: Int -> [Int] semi_factors x = takeWhile (\n -> n * n <= x) [z | z <- [2..x-1], x `mod` z == 0] factors n = let xs = semi_factors n in nub (1 : (xs ++ (map (n `div`) xs))) > > is_perfect :: Integer -> Bool > is_perfect x = if sum(factors x) == x then True else False "if then True else False" should ring alarm bells! Immediately replace with simply "": is_perfect x = sum (factors x) == x you could also use is_perfect x = foldl' (+) 0 (factors x) == x (strict foldl from Data.List) > > do_perfect :: [Integer] -> [Integer] > do_perfect x = [z |z <- x, is_perfect z ] > > Then to run it: > > do_perfect [1..9000] I think more idiomatic would be: do_perfect x = filter is_perfect [2..x] All this speeds it up a bit. But I can't think any more - time to sleep. thanks Ben > > > I'm using GHC to run it. My problem / question is this: It's running quite a lot slower than equivalent programs in erlang and python. I suspect it's down to the way I've written it. Any thoughts (or comments in general) > > Many thanks > > Matt > From kelanslists at gmail.com Thu Oct 2 08:57:54 2008 From: kelanslists at gmail.com (Kurt Hutchinson) Date: Thu Oct 2 08:54:32 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> References: <7DB4D15C3409994FAADAB909FA63B6BDDD2A2E@XY01EX22.xy01.xyratex.com> Message-ID: On Thu, Oct 2, 2008 at 12:45 AM, Matthew Williams wrote: > I'm using GHC to run it. My problem / question is this: It's running quite a > lot slower than equivalent programs in erlang and python. I suspect it's > down to the way I've written it. Any thoughts (or comments in general) Although Ben's algorithm suggestions are wise (just test up to the square root and add the matching divisor), even without those improvements, I get huge speed-up just using Int instead of Integer and compiling with -O2 instead of using ghci. On my machine, just compiling it takes the time down from about 30 seconds to 4.5. Then switching to Int takes it down to 1.2 seconds. I guess maybe the Python interpreter is a bit more deft at this type of problem than the GHC interpreter. In case you're wondering, since you said you're new to Haskell, the Int type uses machine storage (like 'int' in C, with the same bound restrictions), whereas the Integer type is a bit more abstract and can increase without limit. That's why Integer is a bit more costly to calculate with. Another style tip: if your list comprehension is only used to filter out elements (like in your 'do_perfect' function), it's clearer to use the Prelude function 'filter': do_perfect = filter is_perfect Kurt From dpfrey at shaw.ca Fri Oct 3 13:20:35 2008 From: dpfrey at shaw.ca (David Frey) Date: Fri Oct 3 13:17:05 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: <1222931663.7287.25.camel@epsilon> Message-ID: <3nESBYJG.1223054435.9085580.dfrey@localhost> On 10/2/2008, "Ben Deane" wrote: >On Thu, 2008-10-02 at 05:45 +0100, Matthew Williams wrote: >> Hi Guys, >> >> I'm new to Haskell and I was wondering if you can help me: >> >> One of the first program's I tend to write when I'm looking at a new >> language is a program to generate a list of perfect numbers: >> >> --My First Perfect Number Generator >> factors :: Integer -> [Integer] >> factors x = [z | z <- [1..x-1], x `mod` z == 0] > >Hi Matthew, > >A big optimization for larger numbers is that you only need to go up to >the square root of x here and add both z and x/z to the list. (Where x >is a perfect square you need to avoid adding the root twice.) It's late >and there is probably a better way to do this, but: > >import List > >semi_factors :: Int -> [Int] >semi_factors x = takeWhile (\n -> n * n <= x) [z | z <- [2..x-1], x >`mod` z == 0] > >factors n = > let xs = semi_factors n > in nub (1 : (xs ++ (map (n `div`) xs))) > >> >> is_perfect :: Integer -> Bool >> is_perfect x = if sum(factors x) == x then True else False > >"if then True else False" should ring alarm bells! >Immediately replace with simply "": > >is_perfect x = sum (factors x) == x > >you could also use > >is_perfect x = foldl' (+) 0 (factors x) == x > >(strict foldl from Data.List) > >> >> do_perfect :: [Integer] -> [Integer] >> do_perfect x = [z |z <- x, is_perfect z ] >> >> Then to run it: >> > do_perfect [1..9000] > >I think more idiomatic would be: > >do_perfect x = filter is_perfect [2..x] > >All this speeds it up a bit. But I can't think any more - time to sleep. > >thanks >Ben > >> >> >> I'm using GHC to run it. My problem / question is this: It's running >quite a lot slower than equivalent programs in erlang and python. I >suspect it's down to the way I've written it. Any thoughts (or comments >in general) >> >> Many thanks >> >> Matt >> This isn't really important for this application, because the numbers being factored are relatively small, but there are some inefficiencies in the factors function that Ben presented that are fairly easy to overcome. Here's my version: factors :: (Integral a) => a -> [a] factors n = let p1 = [x | x <- [1 .. floor $ sqrt $ fromIntegral n], n `mod` x == 0] p2 = map (div n) (tail p1) in p1 `concatNoDupe` (reverse p2) where concatNoDupe :: (Eq a) => [a] -> [a] -> [a] concatNoDupe [] ys = ys concatNoDupe [x] (y:ys) = if x == y then (y : ys) else (x : y : ys) concatNoDupe (x:xs) ys = x : (concatNoDupe xs ys) Since my two lists of factors are ordered, I can exploit this to avoid calling nub and instead call 'concatNoDupe' which is more efficient. The other thing I optimized was the creation of the list of potential factors. Ben's solution required the evaluation of a predicate for each element in the list. My solution simply computes the maximum value of the list up front. To demonstrate how these changes affect performance, when I run this on my computer: main = print $ sum $ concat $ map factors [1..80000] Matthew's version: 194 seconds Ben's version: 32 seconds My version: 1 second I don't mean to suggest that my version is optimal. I just want to point out that seemingly small changes to an algorithm can have large consequences. David From haskell at elbeno.com Fri Oct 3 13:35:19 2008 From: haskell at elbeno.com (Ben Deane) Date: Fri Oct 3 13:32:08 2008 Subject: [Haskell-beginners] Perfect numbers In-Reply-To: <3nESBYJG.1223054435.9085580.dfrey@localhost> References: <3nESBYJG.1223054435.9085580.dfrey@localhost> Message-ID: <1223055319.7277.4.camel@epsilon> On Fri, 2008-10-03 at 09:20 -0800, David Frey wrote: > (a nice improvement to the perfect number code) Nice followup David. I knew that there had to be a better way than nub. On one of my better days I might have spotted that removing duplicates from sorted lists is O(n) - this is one of the interview questions I used to use! Good spot on the predicate too. Ben From jeffd at techsociety.ca Wed Oct 8 03:14:08 2008 From: jeffd at techsociety.ca (Jeffrey Drake) Date: Wed Oct 8 03:10:53 2008 Subject: [Haskell-beginners] Cryptography Message-ID: <1223450048.8398.15.camel@darky> I have an application where I would like to use AES to have a public/private key pair and digitally sign/verify documents. I have found documentation for a library that does the encryption: http://www.haskell.org/crypto/doc/html/Codec-Encryption-AES.html However, it seems to have only two methods: encrypt :: AESKey a => a -> Word128 -> Word128 decrypt :: AESKey a => a -> Word128 -> Word128 A problem for me, I don't know where AESKey is supposed to come from, or how to use this to sign things. My ignorance of this topic does not help. Would it be correct to say that signing a document is similar to an MD5 hash on a document? In addition, this project requires a matching python component (that works together with the haskell). I am in a similar situation, I have found this: http://bitconjurer.org/rijndael.py that provides a similar functionality. But it seems to provide insights (and thus portability to haskell) on how to generate the public/private keys by providing a single key and performing a lot of math on it to generate the two pairs. But it still does not help me sign anything. Any help whatsoever on this topic is welcome. With regards, Jeffrey. From jjimenez at salle.url.edu Wed Oct 8 03:22:38 2008 From: jjimenez at salle.url.edu (=?ISO-8859-1?Q?Joan_Josep_Jim=E9nez_Puig?=) Date: Wed Oct 8 03:18:59 2008 Subject: [Haskell-beginners] Cryptography In-Reply-To: <1223450048.8398.15.camel@darky> References: <1223450048.8398.15.camel@darky> Message-ID: Hi, in principle AES is a symmetric encryption algorithm so it does not have public/private key pairs. It only has one key used to both encrypt and decrypt. What you want is RSA (http://www.haskell.org/crypto/doc/html/Codec-Encryption-RSA.html). On Wed, Oct 8, 2008 at 9:14 AM, Jeffrey Drake wrote: > > I have an application where I would like to use AES to have a > public/private key pair and digitally sign/verify documents. > > I have found documentation for a library that does the encryption: > http://www.haskell.org/crypto/doc/html/Codec-Encryption-AES.html > > However, it seems to have only two methods: > > encrypt :: AESKey a => a -> Word128 -> Word128 > decrypt :: AESKey a => a -> Word128 -> Word128 > > A problem for me, I don't know where AESKey is supposed to come from, or > how to use this to sign things. My ignorance of this topic does not > help. Would it be correct to say that signing a document is similar to > an MD5 hash on a document? > > In addition, this project requires a matching python component (that > works together with the haskell). I am in a similar situation, I have > found this: http://bitconjurer.org/rijndael.py that provides a similar > functionality. But it seems to provide insights (and thus portability to > haskell) on how to generate the public/private keys by providing a > single key and performing a lot of math on it to generate the two pairs. > But it still does not help me sign anything. > > Any help whatsoever on this topic is welcome. > With regards, > Jeffrey. > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081008/34debcf2/attachment.htm From magnus at therning.org Wed Oct 8 05:07:19 2008 From: magnus at therning.org (Magnus Therning) Date: Wed Oct 8 05:03:38 2008 Subject: [Haskell-beginners] Cryptography In-Reply-To: <1223450048.8398.15.camel@darky> References: <1223450048.8398.15.camel@darky> Message-ID: On Wed, Oct 8, 2008 at 8:14 AM, Jeffrey Drake wrote: > > I have an application where I would like to use AES to have a > public/private key pair and digitally sign/verify documents. As another responder pointed out, AES is symmetric, i.e. the same key is used for both encryption and decryption. What you most likely want is a layered approach, where you use AES to encrypt the payload and then an asymmetric crypto like RSA to encrypt the AES key. I'd strongly suggest you locate a copy of Bruce Schneier's Applied Cryptography to make sure you have a better understanding of what you are doing. Crypto is complicated, even renowned experts have been known to get things wrong :-) > I have found documentation for a library that does the encryption: > http://www.haskell.org/crypto/doc/html/Codec-Encryption-AES.html > > However, it seems to have only two methods: > > encrypt :: AESKey a => a -> Word128 -> Word128 > decrypt :: AESKey a => a -> Word128 -> Word128 > > A problem for me, I don't know where AESKey is supposed to come from, or > how to use this to sign things. My ignorance of this topic does not > help. Would it be correct to say that signing a document is similar to > an MD5 hash on a document? The key should come from a good random source. That means you need cryptographic randomness. All major OSs come with reasonable sources of randomness. However, again, here be dragons and you need to know what you're doing. Weak randomness leads to weak keys. MD5 is an example of a cryptographic hash, also known as a one-way function. That is it's easy to get the hash from a text, but _hard_ to go in the other way. The MD5 algorithm produces a hash that is 128 bits. It can be used for detecting tampering, but it isn't a signature. OTOH many implementations of signatures use a hash in order to avoid having to sign the entire document. I strongly urge you to read up on crypto before adding it to any code you are writing. It's simply too easy to get it wrong. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From voigt at tcs.inf.tu-dresden.de Wed Oct 8 08:22:12 2008 From: voigt at tcs.inf.tu-dresden.de (Janis Voigtlaender) Date: Thu Oct 9 06:23:16 2008 Subject: [Haskell-beginners] Call for Contributions - Haskell Communities and Activities Report, November 2008 edition Message-ID: <48ECA5F4.3050801@tcs.inf.tu-dresden.de> Dear Haskellers, so much has happened in the Haskell world in the past months. Therefore, I would very much like to collect contributions for the 15th edition of the ================================================================ Haskell Communities & Activities Report http://www.haskell.org/communities/ Submission deadline: 31 October 2008 (please send your contributions to hcar@haskell.org, in plain text or LaTeX format) ================================================================ This is the short story: * If you are working on any project that is in some way related to Haskell, please write a short entry and submit it. Even if the project is very small or unfinished or you think it is not important enough -- please reconsider and submit an entry anyway! * If you are interested in any project related to Haskell that has not previously been mentioned in the HC&A Report, please tell me, so that I can contact the project leaders and ask them to submit an entry. * Feel free to pass on this call for contributions to others that might be interested. More detailed information: The Haskell Communities & Activities Report is a bi-annual overview of the state of Haskell as well as Haskell-related projects over the last, and possibly the upcoming six months. If you have only recently been exposed to Haskell, it might be a good idea to browse the May 2008 edition -- you will find interesting topics described as well as several starting points and links that may provide answers to many questions. Contributions will be collected until the submission deadline. They will then be compiled into a coherent report that is published online as soon as it is ready. As always, this is a great opportunity to update your webpages, make new releases, announce or even start new projects, or to talk about developments you want every Haskeller to know about! Looking forward to your contributions, Janis (current editor) FAQ: Q: What format should I write in? A: The best format is a LaTeX source file, adhering to the template that is available at: http://haskell.org/communities/11-2008/template.tex There is also a LaTeX style file at http://haskell.org/communities/11-2008/hcar.sty that you can use to preview your entry. If you do not know LaTeX, then use plain text. If you modify an old entry that you have written for an earlier edition of the report, you should have received your old entry as a template already (provided I have your valid email address). Please modify that template, rather than using your own version of the old entry as a template. Do not worry about writing correct LaTeX, I will be able to handle your file. Please do not use HTML or even DOC. Q: Can I include images? A: Yes, you are even encouraged to do so. Please use .jpg format, then. Q: How much should I write? A: Authors are asked to limit entries to about one column of text. This corresponds to approximately one page, or 40 lines of text, with the above style and template. A general introduction is helpful. Apart from that, you should focus on recent or upcoming developments. Pointers to online content can be given for more comprehensive or ``historic'' overviews of a project. Images do not count towards the length limit, so you may want to use this opportunity to pep entries up. There is no minimum length of an entry! The report aims at being as complete as possible, so please consider writing an entry, even if it is only a few lines long. Q: Which topics are relevant? A: All topics which are related to Haskell in some way are relevant. We usually had reports from users of Haskell (private, academic, or commercial), from authors or contributors to projects related to Haskell, from people working on the Haskell language, libraries, on language extensions or variants. We also like reports over distributions of Haskell software, Haskell infrastructure, books and tutorials on Haskell. Reports on past and upcoming events related to Haskell are also relevant. Finally, there might be new topics we do not even think about. As a rule of thumb: if in doubt, then it probably is relevant and has a place in the HCAR. You can also ask the editor. Q: Is unfinished work relevant? Are ideas for projects relevant? A: Yes! You can use the HCAR to talk about projects you are currently working on. You can use it to look for other developers that might help you. You can use it to write ``wishlist'' items for libraries and language features you would like to see implemented. Q: If I do not update my entry, but want to keep it in the report, what should I do? A: Tell the editor that there are no changes. The old entry will be reused in this case, but it might be dropped if it is older than a year, to give more room and more attention to projects that change a lot. Do not resend complete entries if you have not changed them. -- Dr. Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de From analytic at gmail.com Thu Oct 9 19:27:07 2008 From: analytic at gmail.com (Stephen) Date: Thu Oct 9 19:23:21 2008 Subject: [Haskell-beginners] Re: [Haskell-cafe] Call for Contributions - Haskell Communities and Activities Report, November 2008 edition In-Reply-To: <48ECA5F4.3050801@tcs.inf.tu-dresden.de> References: <48ECA5F4.3050801@tcs.inf.tu-dresden.de> Message-ID: I've worked on maybe four projects over the past couple of months. Some of them were pretty small, most of them are unfinished. I should still toss them in the direction of the report though if I have time to write them up though? Cheers, Stephen On Wed, Oct 8, 2008 at 1:22 PM, Janis Voigtlaender < voigt@tcs.inf.tu-dresden.de> wrote: > Dear Haskellers, > > so much has happened in the Haskell world in the past months. > Therefore, I would very much like to collect contributions for > the 15th edition of the > > ================================================================ > Haskell Communities & Activities Report > http://www.haskell.org/communities/ > > Submission deadline: 31 October 2008 > > (please send your contributions to hcar@haskell.org, in > plain text or LaTeX format) > ================================================================ > > This is the short story: > > * If you are working on any project that is in some way related > to Haskell, please write a short entry and submit it. Even if > the project is very small or unfinished or you think it is not > important enough -- please reconsider and submit an entry anyway! > > * If you are interested in any project related to Haskell that has not > previously been mentioned in the HC&A Report, please tell me, so that > I can contact the project leaders and ask them to submit an entry. > > * Feel free to pass on this call for contributions to others that > might be interested. > > More detailed information: > > The Haskell Communities & Activities Report is a bi-annual overview of > the state of Haskell as well as Haskell-related projects over the > last, and possibly the upcoming six months. If you have only recently > been exposed to Haskell, it might be a good idea to browse the > May 2008 edition -- you will find interesting topics described as > well as several starting points and links that may provide answers to > many questions. > > Contributions will be collected until the submission deadline. They > will then be compiled into a coherent report that is published online > as soon as it is ready. As always, this is a great opportunity to > update your webpages, make new releases, announce or even start new > projects, or to talk about developments you want every Haskeller to > know about! > > Looking forward to your contributions, > > Janis (current editor) > > > FAQ: > > Q: What format should I write in? > > A: The best format is a LaTeX source file, adhering to the template > that is available at: > > http://haskell.org/communities/11-2008/template.tex > > There is also a LaTeX style file at > > http://haskell.org/communities/11-2008/hcar.sty > > that you can use to preview your entry. If you do not know LaTeX, then > use plain text. If you modify an old entry that you have written for an > earlier edition of the report, you should have received your old entry > as a template already (provided I have your valid email address). > Please modify that template, rather than using your own version of the > old entry as a template. Do not worry about writing correct LaTeX, I > will be able to handle your file. Please do not use HTML or even DOC. > > Q: Can I include images? > > A: Yes, you are even encouraged to do so. Please use .jpg format, then. > > Q: How much should I write? > > A: Authors are asked to limit entries to about one column of text. This > corresponds to approximately one page, or 40 lines of text, with the > above style and template. > > A general introduction is helpful. Apart from that, you should focus on > recent or upcoming developments. Pointers to online content can be given > for more comprehensive or ``historic'' overviews of a project. Images do > not count towards the length limit, so you may want to use this > opportunity to pep entries up. There is no minimum length of an entry! > The report aims at being as complete as possible, so please consider > writing an entry, even if it is only a few lines long. > > Q: Which topics are relevant? > > A: All topics which are related to Haskell in some way are relevant. We > usually had reports from users of Haskell (private, academic, or > commercial), from authors or contributors to projects related to > Haskell, from people working on the Haskell language, libraries, on > language extensions or variants. We also like reports over distributions > of Haskell software, Haskell infrastructure, books and tutorials on > Haskell. Reports on past and upcoming events related to Haskell are also > relevant. Finally, there might be new topics we do not even think about. > As a rule of thumb: if in doubt, then it probably is relevant and has a > place in the HCAR. You can also ask the editor. > > Q: Is unfinished work relevant? Are ideas for projects relevant? > > A: Yes! You can use the HCAR to talk about projects you are currently > working on. You can use it to look for other developers that might help > you. You can use it to write ``wishlist'' items for libraries and > language features you would like to see implemented. > > Q: If I do not update my entry, but want to keep it in the report, what > should I do? > > A: Tell the editor that there are no changes. The old entry will be > reused in this case, but it might be dropped if it is older than a year, > to give more room and more attention to projects that change a lot. > Do not resend complete entries if you have not changed them. > > -- > Dr. Janis Voigtlaender > http://wwwtcs.inf.tu-dresden.de/~voigt/ > mailto:voigt@tcs.inf.tu-dresden.de > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081010/425a9fec/attachment.htm From DekuDekuplex at Yahoo.com Thu Oct 9 23:01:49 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 9 22:58:22 2008 Subject: [Haskell-beginners] Re: [Haskell-cafe] Call for Contributions - Haskell Communities and Activities Report, November 2008 edition References: <48ECA5F4.3050801@tcs.inf.tu-dresden.de> Message-ID: On Fri, 10 Oct 2008 00:27:07 +0100, Stephen wrote: >I've worked on maybe four projects over the past couple of months. Some of >them were pretty small, most of them are unfinished. I should still toss >them in the direction of the report though if I have time to write them up >though? On Wed, 08 Oct 2008 14:22:12 +0200, Janis Voigtlaender wrote: >Dear Haskellers, > >so much has happened in the Haskell world in the past months. >Therefore, I would very much like to collect contributions for >the 15th edition of the > > ================================================================ > Haskell Communities & Activities Report > http://www.haskell.org/communities/ > > Submission deadline: 31 October 2008 > > (please send your contributions to hcar@haskell.org, in > plain text or LaTeX format) > ================================================================ > >This is the short story: > >* If you are working on any project that is in some way related > to Haskell, please write a short entry and submit it. Even if > the project is very small or unfinished or you think it is not > important enough -- please reconsider and submit an entry anyway! Apparently, yes. It seems that the idea is just to gather information on Haskell-related communities & activities (hence the report title). I would venture that the report collects news on not just finished projects, but also on the direction that Haskell is heading as well. -- Benjamin L. Russell From sylvain.nahas at googlemail.com Fri Oct 10 16:55:42 2008 From: sylvain.nahas at googlemail.com (sylvain) Date: Fri Oct 10 17:06:29 2008 Subject: [Haskell-beginners] How to explain this behaviour of ghc ? Message-ID: <1223672142.21779.4.camel@dell.linuxdev.us.dell.com> Hi, when loaded in ghci (version 6.8.2) the following code snippet data Test = A Int instance Show Test main = print (A 1) leads to: *Main> main *** Exception: stack overflow How to explain that ? Thanks, Sylvain From rendel at daimi.au.dk Fri Oct 10 17:46:42 2008 From: rendel at daimi.au.dk (Tillmann Rendel) Date: Fri Oct 10 17:42:56 2008 Subject: [Haskell-beginners] How to explain this behaviour of ghc ? In-Reply-To: <1223672142.21779.4.camel@dell.linuxdev.us.dell.com> References: <1223672142.21779.4.camel@dell.linuxdev.us.dell.com> Message-ID: <1223675202.48efcd4284333@webmail.daimi.au.dk> Hi Sylvain, sylvain wrote: > the following code snippet > > data Test = A Int > instance Show Test > main = print (A 1) > > leads to: > > *Main> main > *** Exception: stack overflow Since "instance Show Test" does not specify how method "show" should be implemented for datatype Test, "show = undefined" is assumed. Now, print calls show to create a string represention of (A 1), which produces the error message. Interestingly, undefined gives a better error message, when called directly: Prelude> undefined *** Exception: Prelude.undefined I don't know why the behaviour of undefined differs from the behaviour of unbound methods. If you want to correct the above program, you could either bind show to something, as in instance Show Test where show (A n) = "(A " ++ show n ++ ")" or you could use ask the compiler to derive an instance for you, as in data Test = Show Int deriving Show Tillmann From micah at cowan.name Fri Oct 10 18:00:14 2008 From: micah at cowan.name (Micah Cowan) Date: Fri Oct 10 17:56:28 2008 Subject: [Haskell-beginners] How to explain this behaviour of ghc ? In-Reply-To: <1223675202.48efcd4284333@webmail.daimi.au.dk> References: <1223672142.21779.4.camel@dell.linuxdev.us.dell.com> <1223675202.48efcd4284333@webmail.daimi.au.dk> Message-ID: <48EFD06E.5080505@cowan.name> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Tillmann Rendel wrote: > Hi Sylvain, > > sylvain wrote: >> the following code snippet >> >> data Test = A Int >> instance Show Test >> main = print (A 1) >> >> leads to: >> >> *Main> main >> *** Exception: stack overflow > > Since "instance Show Test" does not specify how method "show" should be > implemented for datatype Test, "show = undefined" is assumed. Now, print calls > show to create a string represention of (A 1), which produces the error > message. Actually, "show = undefined" isn't assumed. The problem is that the default definitions for show and showsPrec are: showsPrec _ x s = show x ++ s show x = showsPrec 0 x "" This allows you to define either one of showsPrec or show, and have a reasonable default definition for the other one. However, if you leave off the definition of _both_ of them, what you get is an infinite recursion (hence the stack overflow). - -- Micah J. Cowan Programmer, musician, typesetting enthusiast, gamer. GNU Maintainer: wget, screen, teseq http://micah.cowan.name/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFI79Bu7M8hyUobTrERAnTLAJ4i3jfeS8QQQBSXqBNVW0Ph77GYKQCeKzXn BPo7VBKIn/7t4Z8FoU+FbYc= =7ovS -----END PGP SIGNATURE----- From matthewjwilliams1 at googlemail.com Mon Oct 13 02:58:49 2008 From: matthewjwilliams1 at googlemail.com (Matthew J. Williams) Date: Mon Oct 13 22:17:28 2008 Subject: [Haskell-beginners] variables in haskell Message-ID: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> Hello listers, would one be correct in thinking that 'bound variables' such as those used in haskell were in fact constants? Sincerely Matthew Williams From allbery at ece.cmu.edu Mon Oct 13 22:32:29 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Mon Oct 13 22:28:34 2008 Subject: [Haskell-beginners] variables in haskell In-Reply-To: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> References: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> Message-ID: <9022559B-D704-4ECD-BA45-021F3FA5E89D@ece.cmu.edu> On 2008 Oct 13, at 2:58, Matthew J. Williams wrote: > Hello listers, would one be correct in thinking that 'bound > variables' such as those used in haskell were in fact constants? Only in certain limited circumstances. Consider the following: > let x = f x in x This finds the least defined fixed point of f. See < http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion >. Top level constant applicative forms (that is, bindings without arguments) with monomorphic types can generally be considered to be constants, and the compiler may assume this and inline it. But it's not required to do so. -- 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 From amitkumardhar+haskell at gmail.com Tue Oct 14 01:42:17 2008 From: amitkumardhar+haskell at gmail.com (Amit Kumar Dhar) Date: Tue Oct 14 01:38:20 2008 Subject: [Haskell-beginners] Can we specify the module of the function being referred In-Reply-To: <3639d0630810132236m197de7ebl3aa6f9846b992dad@mail.gmail.com> References: <3639d0630810132236m197de7ebl3aa6f9846b992dad@mail.gmail.com> Message-ID: <3639d0630810132242m19ea50a8yd4c22047b44cea5f@mail.gmail.com> Dear listers, I was writing a program which defines a function named "show". But at each of the recursive calls to itself "ghci" is showing the following message: ' Ambiguous occurrence `show' It could refer to either `show', defined at temp.hs:5:0 or `show', imported from Prelude ' I don't want to import the 'show' function from prelude. Is there any way out or I have to declare instance of the 'Show'? Can we specify the module of a function from which it will be referred? -- Amit kumar Dhar -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081014/13c01ec4/attachment.htm From rendel at daimi.au.dk Tue Oct 14 03:46:26 2008 From: rendel at daimi.au.dk (Tillmann Rendel) Date: Tue Oct 14 03:42:36 2008 Subject: [Haskell-beginners] Can we specify the module of the function being referred In-Reply-To: <3639d0630810132242m19ea50a8yd4c22047b44cea5f@mail.gmail.com> References: <3639d0630810132236m197de7ebl3aa6f9846b992dad@mail.gmail.com> <3639d0630810132242m19ea50a8yd4c22047b44cea5f@mail.gmail.com> Message-ID: <48F44E52.8000700@daimi.au.dk> Amit Kumar Dhar wrote: > I don't want to import the 'show' function from prelude. Is there any way > out or I have to declare instance of the 'Show'? The prelude is only implicitly imported, if it is not explicitly imported. That means that you can explicitly say import Prelude hiding (show) to get rid of Prelude's show. > Can we specify the module of a function from which it will be referred? You can give a module a name while importing it, and then use this name with a dot. For your situation, maybe the following two import statements make sense: -- bring everything except show into scope import Prelude hiding (show) -- make Prelude's show available as P.show import qualified Prelude as P Now you can even use Prelude's show in the definition of your own show. Here is an example, showing lists with set notation: show :: [Int] -> String show xs = "{" ++ content ++ "}" where content = concat (intersperse ", " (map P.show xs)) (use "import Data.List" for intersperse). > I was writing a program which defines a function named "show". While it is not wrong to use a name from the Prelude for something else, it is often confusing, so I would consider using some other name (display, toString, prettyPrint, ...). Tillmann From rendel at daimi.au.dk Tue Oct 14 04:03:43 2008 From: rendel at daimi.au.dk (Tillmann Rendel) Date: Tue Oct 14 03:59:52 2008 Subject: [Haskell-beginners] variables in haskell In-Reply-To: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> References: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> Message-ID: <48F4525F.9080502@daimi.au.dk> Matthew J. Williams wrote: > Hello listers, would one be correct in thinking that 'bound variables' > such as those used in haskell were in fact constants? I think I see what you mean, but it is not entirely correct. A constant is something which has the same value at all times. For example, top-level declarations in most languages, including Haskell, can be seen as constants: foo = "hello world" -- this foo will always mean "hello world" If the value associated with a name changes from time to time, then we call that name a variable. bar foo = ... -- foo will mean something else everytime bar is called The behaviour of Haskell variables is similiar to "constant variables" or "final variables" in other languages, e.g. in Java public int bar(final int foo) { } Similiar to the Haskell version, in this Java code, foo will be something different for each call of bar, but it will not change during one execution of bar. Your wouldn't call foo a constant here, would you? Tillmann From matthewjwilliams1 at googlemail.com Wed Oct 15 01:52:29 2008 From: matthewjwilliams1 at googlemail.com (Matthew J. Williams) Date: Wed Oct 15 05:10:17 2008 Subject: [Haskell-beginners] variables in haskell In-Reply-To: <48F4525F.9080502@daimi.au.dk> References: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> <48F4525F.9080502@daimi.au.dk> Message-ID: <48f58518.06a0100a.5c8e.02e1@mx.google.com> >>Hello listers, would one be correct in thinking that 'bound >>variables' such as those used in haskell were in fact constants? > >I think I see what you mean, but it is not entirely correct. A >constant is something which has the same value at all times. For >example, top-level declarations in most languages, including >Haskell, can be seen as constants: > > foo = "hello world" -- this foo will always mean "hello world" > >If the value associated with a name changes from time to time, then >we call that name a variable. > > bar foo = ... -- foo will mean something else everytime bar is called > >The behaviour of Haskell variables is similiar to "constant >variables" or "final variables" in other languages, e.g. in Java > > public int bar(final int foo) { > } > >Similiar to the Haskell version, in this Java code, foo will be >something different for each call of bar, but it will not change >during one execution of bar. Your wouldn't call foo a constant here, would you? My recollection of 'final' in 'Java' is a little vague, nevertheless, in 'c/c++' the const keyword indicates that the associated function argument must be 'treated' as a constant. I am not convinced that the idea of a constant value is not being honored in this situation. The function definition is after all a 'pattern' (if I may be permitted to use the term in its most general sense): int f (const int x) { return (x+1); } would be equivalent to the following: let f x = x+1 In 'c/c++' a global constant would have very similar properties, in fact, exactly similar in so far as the 'immutability' of the value is concerned. Sincerely Matthew J. Williams From flippa at flippac.org Wed Oct 15 05:18:16 2008 From: flippa at flippac.org (Philippa Cowderoy) Date: Wed Oct 15 05:13:56 2008 Subject: [Haskell-beginners] variables in haskell In-Reply-To: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> References: <48f2f1a7.0856100a.7ad9.ffffb458@mx.google.com> Message-ID: On Mon, 13 Oct 2008, Matthew J. Williams wrote: > Hello listers, would one be correct in thinking that 'bound variables' such as > those used in haskell were in fact constants? > Possibly the simplest explanation is that they're variables in the same sense variables in algebra are? -- flippa@flippac.org The task of the academic is not to scale great intellectual mountains, but to flatten them. From Paul.A.Johnston at manchester.ac.uk Wed Oct 15 06:54:50 2008 From: Paul.A.Johnston at manchester.ac.uk (Paul Johnston) Date: Wed Oct 15 06:52:42 2008 Subject: [Haskell-beginners] Problems with ghc Message-ID: <48F5CBFA.1020709@manchester.ac.uk> Apologies if this is not exactly what this list is for but is anyone running ghc on Solaris, specifically Open Solaris 5.11? It seemed to work paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.8.3 paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghci GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> And ghci works as expected but !!! Trying to compile I get: paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -o Haq Haq.hs compilation IS NOT required Undefined first referenced symbol in file __gmpz_tdiv_q /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_tdiv_r /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpn_cmp /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_add /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_and /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_gcd /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_ior /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_com /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_mul /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_xor /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_sub /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpn_gcd_1 /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_init /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_fdiv_qr /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmp_set_memory_functions /usr/local/lib/ghc-6.8.3/libHSrts.a(Storage.o) __gmpz_divexact /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_tdiv_qr /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) ld: fatal: Symbol referencing errors. No output written to Haq collect2: ld returned 1 exit status I get this trying to build hmake with the new configure file supplied by Malcolm Wallace. Once again sorry if its off topic but wondered if it's only me :-) Cheers Paul From daniel.is.fischer at web.de Wed Oct 15 07:04:41 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Oct 15 06:58:21 2008 Subject: [Haskell-beginners] Problems with ghc In-Reply-To: <48F5CBFA.1020709@manchester.ac.uk> References: <48F5CBFA.1020709@manchester.ac.uk> Message-ID: <200810151304.41445.daniel.is.fischer@web.de> Am Mittwoch, 15. Oktober 2008 12:54 schrieb Paul Johnston: > Apologies if this is not exactly what this list is for but is anyone > running ghc on Solaris, specifically Open Solaris 5.11? > > It seemed to work > > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc --version > The Glorious Glasgow Haskell Compilation System, version 6.8.3 > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghci > GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help > Loading package base ... linking ... done. > Prelude> > > And ghci works as expected but !!! > > Trying to compile I get: > > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -o Haq Haq.hs Try ghc -o Haq --make Haq.hs or give the needed packages with the flag -package on the commandline. From Paul.A.Johnston at manchester.ac.uk Wed Oct 15 08:51:03 2008 From: Paul.A.Johnston at manchester.ac.uk (Paul Johnston) Date: Wed Oct 15 08:48:55 2008 Subject: [Haskell-beginners] Problems with ghc In-Reply-To: <200810151304.41445.daniel.is.fischer@web.de> References: <48F5CBFA.1020709@manchester.ac.uk> <200810151304.41445.daniel.is.fischer@web.de> Message-ID: <48F5E737.3060500@manchester.ac.uk> Daniel Fischer wrote: > Am Mittwoch, 15. Oktober 2008 12:54 schrieb Paul Johnston: > >> Apologies if this is not exactly what this list is for but is anyone >> running ghc on Solaris, specifically Open Solaris 5.11? >> >> It seemed to work >> >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc --version >> The Glorious Glasgow Haskell Compilation System, version 6.8.3 >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghci >> GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help >> Loading package base ... linking ... done. >> Prelude> >> >> And ghci works as expected but !!! >> >> Trying to compile I get: >> >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -o Haq Haq.hs >> > > Try > > ghc -o Haq --make Haq.hs > > or give the needed packages with the flag -package on the commandline. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Many thanks but no joy see below Think it could be gmp which I had to also compile but it passed all its checks. paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -o Haq --make Haq.hs Linking Haq ... Undefined first referenced symbol in file __gmpz_tdiv_q /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_tdiv_r /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpn_cmp /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_add /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_and /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_gcd /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_ior /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_com /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_mul /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_xor /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_sub /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpn_gcd_1 /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_init /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_fdiv_qr /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmp_set_memory_functions /usr/local/lib/ghc-6.8.3/libHSrts.a(Storage.o) __gmpz_divexact /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_tdiv_qr /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) ld: fatal: Symbol referencing errors. No output written to Haq collect2: ld returned 1 exit status Regards Paul From daniel.is.fischer at web.de Wed Oct 15 09:32:26 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Oct 15 09:26:10 2008 Subject: [Haskell-beginners] Problems with ghc In-Reply-To: <48F5E737.3060500@manchester.ac.uk> References: <48F5CBFA.1020709@manchester.ac.uk> <200810151304.41445.daniel.is.fischer@web.de> <48F5E737.3060500@manchester.ac.uk> Message-ID: <200810151532.26706.daniel.is.fischer@web.de> Am Mittwoch, 15. Oktober 2008 14:51 schrieb Paul Johnston: > Many thanks but no joy see below > Think it could be gmp which I had to also compile but it passed all its > checks. Seems the linker can't find gmp. Try compiling with the -L/path/to/gmp option (if that works, we at least have identified the problem, of course, for a real fix you would have to include that directory to the linker's search path). > Regards Paul Cheers, Daniel From Paul.A.Johnston at manchester.ac.uk Wed Oct 15 09:36:51 2008 From: Paul.A.Johnston at manchester.ac.uk (Paul Johnston) Date: Wed Oct 15 09:34:41 2008 Subject: [Haskell-beginners] Problems with ghc In-Reply-To: <200810151532.26706.daniel.is.fischer@web.de> References: <48F5CBFA.1020709@manchester.ac.uk> <200810151304.41445.daniel.is.fischer@web.de> <48F5E737.3060500@manchester.ac.uk> <200810151532.26706.daniel.is.fischer@web.de> Message-ID: <48F5F1F3.6050400@manchester.ac.uk> Daniel Fischer wrote: > Am Mittwoch, 15. Oktober 2008 14:51 schrieb Paul Johnston: > >> Many thanks but no joy see below >> Think it could be gmp which I had to also compile but it passed all its >> checks. >> > > Seems the linker can't find gmp. > Try compiling with the -L/path/to/gmp option (if that works, we at least have > identified the problem, of course, for a real fix you would have to include > that directory to the linker's search path). > > >> Regards Paul >> > > Cheers, > Daniel > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > Sorted :-) paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -L=/usr/local/lib/ -o Haq --make Haq.hs [1 of 1] Compiling Main ( Haq.hs, Haq.o ) Linking Haq ... paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ./Haq "Bother" "Haq! BotherOr not!" paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ Tried putting /usr/local/lib in $LD_LIBRARY_PATH but that didn't seem to work, pity. Anyway many, many thanks Paul From Paul.A.Johnston at manchester.ac.uk Wed Oct 15 10:35:03 2008 From: Paul.A.Johnston at manchester.ac.uk (Paul Johnston) Date: Wed Oct 15 10:32:56 2008 Subject: [Haskell-beginners] Re: Problems with ghc In-Reply-To: <48F5F8E9.1050502@dfki.de> References: <48F5CBFA.1020709@manchester.ac.uk> <200810151304.41445.daniel.is.fischer@web.de> <48F5E737.3060500@manchester.ac.uk> <200810151532.26706.daniel.is.fischer@web.de> <48F5F1F3.6050400@manchester.ac.uk> <48F5F8E9.1050502@dfki.de> Message-ID: <48F5FF97.3010800@manchester.ac.uk> Christian Maeder wrote: > Paul Johnston wrote: > >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc >> -L=/usr/local/lib/ -o Haq --make Haq.hs >> [1 of 1] Compiling Main ( Haq.hs, Haq.o ) >> Linking Haq ... >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ./Haq "Bother" >> "Haq! BotherOr not!" >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ >> >> Tried putting /usr/local/lib in $LD_LIBRARY_PATH but that didn't seem to >> work, pity. >> Anyway many, many thanks >> > > I think setting LIBRARY_PATH would work. > > Cheers Christian > Tried setting both LD_LIBRARY_PATH and LIBRARY_PATH paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ env | grep LIBRARY LIBRARY_PATH=/usr/lib:/opt/csw/lib:/opt/csw/lib/engines:/usr/local/lib LD_LIBRARY_PATH=/usr/lib:/opt/csw/lib:/opt/csw/lib/engines:/usr/local/lib paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ Still same issue paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ touch Haq.hs paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -o Haq Haq.hs Undefined first referenced symbol in file __gmpz_tdiv_q /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_tdiv_r /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpn_cmp /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_add /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_and /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_gcd /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_ior /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_com /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_mul /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_xor /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_sub /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpn_gcd_1 /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_init /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_fdiv_qr /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmp_set_memory_functions /usr/local/lib/ghc-6.8.3/libHSrts.a(Storage.o) __gmpz_divexact /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) __gmpz_tdiv_qr /usr/local/lib/ghc-6.8.3/libHSrts.a(PrimOps.o) ld: fatal: Symbol referencing errors. No output written to Haq collect2: ld returned 1 exit status paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ Cheers Paul From daniel.is.fischer at web.de Wed Oct 15 11:04:13 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Oct 15 10:57:51 2008 Subject: [Haskell-beginners] Re: Problems with ghc In-Reply-To: <48F5FF97.3010800@manchester.ac.uk> References: <48F5CBFA.1020709@manchester.ac.uk> <48F5F8E9.1050502@dfki.de> <48F5FF97.3010800@manchester.ac.uk> Message-ID: <200810151704.13283.daniel.is.fischer@web.de> Am Mittwoch, 15. Oktober 2008 16:35 schrieb Paul Johnston: > Christian Maeder wrote: > > Paul Johnston wrote: > >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc > >> -L=/usr/local/lib/ -o Haq --make Haq.hs > >> [1 of 1] Compiling Main ( Haq.hs, Haq.o ) > >> Linking Haq ... > >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ./Haq "Bother" > >> "Haq! BotherOr not!" > >> paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ > >> > >> Tried putting /usr/local/lib in $LD_LIBRARY_PATH but that didn't seem to > >> work, pity. > >> Anyway many, many thanks > > > > I think setting LIBRARY_PATH would work. > > > > Cheers Christian > > Tried setting both LD_LIBRARY_PATH and LIBRARY_PATH > > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ env | grep LIBRARY > LIBRARY_PATH=/usr/lib:/opt/csw/lib:/opt/csw/lib/engines:/usr/local/lib > LD_LIBRARY_PATH=/usr/lib:/opt/csw/lib:/opt/csw/lib/engines:/usr/local/lib > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ > > > Still same issue > > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ touch Haq.hs > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc -o Haq Haq.hs What happens with --make now? > > Cheers Paul From cppljevans at suddenlink.net Wed Oct 15 17:05:30 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Wed Oct 15 16:57:48 2008 Subject: [Haskell-beginners] better way to create Array defined on all indices Message-ID: <48F65B1A.6070203@suddenlink.net> Skipped content of type multipart/alternative-------------- next part -------------- A non-text attachment was scrubbed... Name: array_complete.proto.hs Type: text/x-haskell Size: 1125 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20081015/004cbffb/array_complete.proto.bin From matthewjwilliams1 at googlemail.com Wed Oct 15 18:51:45 2008 From: matthewjwilliams1 at googlemail.com (Matthew J. Williams) Date: Wed Oct 15 18:47:32 2008 Subject: [Haskell-beginners] fix Message-ID: <48f673f7.0d84100a.39a7.77f1@mx.google.com> hello listers, a few days ago A fellow lister sent me the following link: http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion The 'fix' function is interesting to say the least. There is one example that I've had difficulty expanding: fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 120 My interpretation: fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 ((\rec n -> if n == 0 then 1 else n * rec (n-1)) (fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) )) 5 . . . Yet, it does not quite explain how 'fix' does not result in infinite recursion. Sincerely Matthew J. Williams From allbery at ece.cmu.edu Wed Oct 15 19:04:03 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Wed Oct 15 19:00:02 2008 Subject: [Haskell-beginners] fix In-Reply-To: <48f673f7.0d84100a.39a7.77f1@mx.google.com> References: <48f673f7.0d84100a.39a7.77f1@mx.google.com> Message-ID: On 2008 Oct 15, at 18:51, Matthew J. Williams wrote: > fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 > 120 > > My interpretation: > fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 > ((\rec n -> if n == 0 then 1 else n * rec (n-1)) (fix (\rec n -> if > n == 0 then 1 else n * rec (n-1)) )) 5 > . . . > > Yet, it does not quite explain how 'fix' does not result in > infinite recursion. Remember, Haskell is non-strict. When the computation reaches 0, the "then" branch of the conditional is evaluated and the "else" is unneeded and therefore ignored, so its re-invocation isn't seen. -- 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 From allbery at ece.cmu.edu Wed Oct 15 19:09:24 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Wed Oct 15 19:05:23 2008 Subject: [Haskell-beginners] fix In-Reply-To: References: <48f673f7.0d84100a.39a7.77f1@mx.google.com> Message-ID: On 2008 Oct 15, at 19:04, Brandon S. Allbery KF8NH wrote: > On 2008 Oct 15, at 18:51, Matthew J. Williams wrote: >> Yet, it does not quite explain how 'fix' does not result in >> infinite recursion. > > Remember, Haskell is non-strict. When the computation reaches 0, > the "then" branch of the conditional is evaluated and the "else" is > unneeded and therefore ignored, so its re-invocation isn't seen. Think about this, btw: you can create new control operations as ordinary functions In strict languages you need to rely on special syntax (such as Perl's IO syntax + \& prototype, or Ruby's special block/proc syntax; in LISP/Scheme, you have to use a macro and/or quoting); non-strict evaluation gives it to you for free. -- 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 From cppljevans at suddenlink.net Thu Oct 16 00:50:53 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Thu Oct 16 00:43:10 2008 Subject: [Haskell-beginners] better way to create Array defined on all indices In-Reply-To: <48F65B1A.6070203@suddenlink.net> References: <48F65B1A.6070203@suddenlink.net> Message-ID: <48F6C82D.1000208@suddenlink.net> On 10/15/08 16:05, Larry Evans wrote: > I'd like to have an Array indices values > (see http://haskell.org/onlinereport/array.html) > defined on [minBound::indices ... maxBound::indices]. > The attached does this; however, it also allows > duplicate definitions (e.g. in the attached, the > value for Term index is defined twice). > > Is there a better way to do this? Maybe a > way that doesn't use all the contexts? > Also, is there a way to do it so that the value > associated with an index is not redefined? The aforementioned array.html has: _ -> error "Array.!: \ \multiply defined array element") suggesting it should detect redefinitions; however, the library on my system apparently doesn't do that. It will detect missing defintions with the same error found in array.html: [] -> error "Array.!: \ \undefined array element" Is this a bug in my ghc library? I'n on ubuntu and synaptic shows the ghc6 version as: 6.8.2-2ubuntu1 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081015/c14acf5e/attachment-0001.htm From Christian.Maeder at dfki.de Wed Oct 15 10:06:33 2008 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Thu Oct 16 00:55:30 2008 Subject: [Haskell-beginners] Re: Problems with ghc In-Reply-To: <48F5F1F3.6050400@manchester.ac.uk> References: <48F5CBFA.1020709@manchester.ac.uk> <200810151304.41445.daniel.is.fischer@web.de> <48F5E737.3060500@manchester.ac.uk> <200810151532.26706.daniel.is.fischer@web.de> <48F5F1F3.6050400@manchester.ac.uk> Message-ID: <48F5F8E9.1050502@dfki.de> Paul Johnston wrote: > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ghc > -L=/usr/local/lib/ -o Haq --make Haq.hs > [1 of 1] Compiling Main ( Haq.hs, Haq.o ) > Linking Haq ... > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ ./Haq "Bother" > "Haq! BotherOr not!" > paulj@hb-0021209.humanities.manchester:~/haskell/aht/3$ > > Tried putting /usr/local/lib in $LD_LIBRARY_PATH but that didn't seem to > work, pity. > Anyway many, many thanks I think setting LIBRARY_PATH would work. Cheers Christian From jgbailey at gmail.com Wed Oct 15 19:02:50 2008 From: jgbailey at gmail.com (Justin Bailey) Date: Thu Oct 16 00:55:52 2008 Subject: [Haskell-beginners] fix In-Reply-To: <48f673f7.0d84100a.39a7.77f1@mx.google.com> References: <48f673f7.0d84100a.39a7.77f1@mx.google.com> Message-ID: n decreases on each step of the recursion, which will allow it to terminate. You need to expand AND substitute arguments: fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 > fix (\rec 5 -> if 5 == 0 then 1 else n * rec (5 -1)) > fix (\rec 5 -> if 5 == 0 then 1 else n * (fix (\rec 4 -> if 4 == 0 then 1 else 4 * rec (3-1)))) And so on. On Wed, Oct 15, 2008 at 3:51 PM, Matthew J. Williams wrote: > hello listers, a few days ago A fellow lister sent me the following link: > > http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion > > The 'fix' function is interesting to say the least. There is one > example that I've had difficulty expanding: > > fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 > 120 > > My interpretation: > fix (\rec n -> if n == 0 then 1 else n * rec (n-1)) 5 > ((\rec n -> if n == 0 then 1 else n * rec (n-1)) (fix (\rec n -> if n > == 0 then 1 else n * rec (n-1)) )) 5 > . . . > > Yet, it does not quite explain how 'fix' does not result in infinite > recursion. > > Sincerely > Matthew J. Williams > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From jeffd at techsociety.ca Thu Oct 16 04:46:47 2008 From: jeffd at techsociety.ca (Jeffrey Drake) Date: Thu Oct 16 04:43:12 2008 Subject: [Haskell-beginners] Mathematical Blundering Message-ID: <1224146807.6391.2.camel@darky> I have defined myself a set of functions to test: fact 1 = 1 fact n = n * (fact $ n - 1) sine x = x - (x^3/(fact 3)) + (x^5/(fact 5)) - (x^7/(fact 7)) Where my code is 'sine' and the prelude's is sin: *Main> sine 1 0.841468253968254 *Main> sin 1 0.8414709848078965 *Main> sine 2 0.9079365079365079 *Main> sin 2 0.9092974268256817 *Main> sine 3 9.107142857142847e-2 *Main> sin 3 0.1411200080598672 *Main> sine 4 -1.3841269841269837 *Main> sin 4 -0.7568024953079282 After 2 they seem to diverge rather rapidly, and I am not sure why. Any ideas? I would have thought that 4 terms would have been enough. - Jeff. From paul.a.johnston at manchester.ac.uk Thu Oct 16 04:57:39 2008 From: paul.a.johnston at manchester.ac.uk (Paul Johnston) Date: Thu Oct 16 04:55:44 2008 Subject: [Haskell-beginners] Mathematical Blundering In-Reply-To: <1224146807.6391.2.camel@darky> References: <1224146807.6391.2.camel@darky> Message-ID: Bit of basic maths. You are using a power series to approximate sine This works by taking an expansion about a fixed point, usually zero. It only works well around that point. If you get far away it works badly. You need to exploit the cyclic nature of the trignometrical functions i.e. Sin x = sin ((2 * pi) + x) = sin ((4 * pi) + x) Essentially consider the shift in multiples of 2 * pi and calculate the value of x nearest to zero. See http://en.wikipedia.org/wiki/Taylor_series The diagram on the top right is very instructive. Paul -----Original Message----- From: beginners-bounces@haskell.org [mailto:beginners-bounces@haskell.org] On Behalf Of Jeffrey Drake Sent: Thursday, October 16, 2008 9:47 AM To: Haskell Beginners Subject: [Haskell-beginners] Mathematical Blundering I have defined myself a set of functions to test: fact 1 = 1 fact n = n * (fact $ n - 1) sine x = x - (x^3/(fact 3)) + (x^5/(fact 5)) - (x^7/(fact 7)) Where my code is 'sine' and the prelude's is sin: *Main> sine 1 0.841468253968254 *Main> sin 1 0.8414709848078965 *Main> sine 2 0.9079365079365079 *Main> sin 2 0.9092974268256817 *Main> sine 3 9.107142857142847e-2 *Main> sin 3 0.1411200080598672 *Main> sine 4 -1.3841269841269837 *Main> sin 4 -0.7568024953079282 After 2 they seem to diverge rather rapidly, and I am not sure why. Any ideas? I would have thought that 4 terms would have been enough. - Jeff. _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners From cppljevans at suddenlink.net Thu Oct 16 10:17:40 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Thu Oct 16 10:09:57 2008 Subject: solved: implementation dependent (was Re: [Haskell-beginners] better way to create Array defined on all indices In-Reply-To: <48F6C82D.1000208@suddenlink.net> References: <48F65B1A.6070203@suddenlink.net> <48F6C82D.1000208@suddenlink.net> Message-ID: <48F74D04.2010302@suddenlink.net> On 10/15/08 23:50, Larry Evans wrote: > On 10/15/08 16:05, Larry Evans wrote: >> I'd like to have an Array indices values >> (see http://haskell.org/onlinereport/array.html) >> defined on [minBound::indices ... maxBound::indices]. [snip] > The aforementioned array.html has: > > _ -> error "Array.!: \ > \multiply defined array element") > > suggesting it should detect redefinitions; > Is this a bug in my ghc library? According to: http://www.haskell.org/ghc/docs/6.6-latest/html/libraries/base/Data-Array.html it's not. It's just implemented differently by libraries: > The Haskell 98 Report further specifies that if any two associations > in the list have the same index, the value at that index is undefined > (i.e. bottom). However in GHC's implementation, the value at such an > index is the value part of the last association with that index in the > list. So, if I want multiple definitions diagnosed as an error, I'll have to do it myself :( -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081016/69ff3cfa/attachment.htm From daniel.is.fischer at web.de Thu Oct 16 10:26:30 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 16 10:20:14 2008 Subject: [Haskell-beginners] better way to create Array defined on all indices In-Reply-To: <48F65B1A.6070203@suddenlink.net> References: <48F65B1A.6070203@suddenlink.net> Message-ID: <200810161626.31549.daniel.is.fischer@web.de> Am Mittwoch, 15. Oktober 2008 23:05 schrieb Larry Evans: > I'd like to have an Array indices values > (see http://haskell.org/onlinereport/array.html) > defined on [minBound::indices ... maxBound::indices]. > The attached does this; however, it also allows > duplicate definitions (e.g. in the attached, the > value for Term index is defined twice). > > Is there a better way to do this? Maybe a > way that doesn't use all the contexts? I don't know of a better way to do it, your way is fine. What do you mean by "contexts" here? Of course, Bounded and Ix are necessary to define array_complete. If you want to perhaps leave some array entries undefined, there's no problem with that, as long as you don't try to access an undefined entry later. > Also, is there a way to do it so that the value > associated with an index is not redefined? From GHC's user's guide, section 13.1.1.6: GHC's implementation of array takes the value of an array slot from the last (index,value) pair in the list, and does no checking for duplicates. The reason for this is efficiency, pure and simple. If you want to throw an error for duplicate definitions, you have to scan the eqs list manually before passing it to array. > > In effect, I'm making a map, but using an Array > as the implementation because, I guess, it would be > a bit faster. > > TIA. Cheers, Daniel From dpfrey at shaw.ca Thu Oct 16 19:47:56 2008 From: dpfrey at shaw.ca (David Frey) Date: Thu Oct 16 19:43:48 2008 Subject: [Haskell-beginners] HaXml.SAX successfully parses a malformed XML document Message-ID: <2AbNkZkF.1224200876.8228440.dfrey@localhost> It seems that saxParse from Text.XML.HaXml.SAX will successfully parse a malformed XML document. Notice that the SubElem opening and closing tag are not matched in the XML document below. --- input.xml --- element data --- end input.xml --- The Haskell code below runs without error. It prints out the type of elements found during the parse. --- Haskell Code --- module Main where import qualified Text.XML.HaXml.SAX as SAX import Data.Maybe main = let inputFilename = "input.xml" in do content <- readFile inputFilename let (elements, error) = SAX.saxParse inputFilename content if isNothing error then mapM_ putStrLn (summarizeElements elements) else putStrLn $ "ERROR:" ++ (fromJust error) summarizeElements :: [SAX.SaxElement] -> [String] summarizeElements elements = map summarizeElement elements summarizeElement :: SAX.SaxElement -> String summarizeElement element = case element of (SAX.SaxDocTypeDecl d) -> "DocType" (SAX.SaxProcessingInstruction p) -> "Processing Instruction" (SAX.SaxComment s) -> "Comment" (SAX.SaxElementOpen name attrs) -> "Element Open" (SAX.SaxElementClose name) -> "Element Close" (SAX.SaxElementTag name attrs) -> "No Content Element" (SAX.SaxCharData charData) -> "Character Data" (SAX.SaxReference reference) -> "Reference" --- End Haskell Code --- The Python code below throws an exception when parsing the same input document. --- Python Code --- from xml.sax import make_parser from xml.sax.handler import ContentHandler def main(): c = ContentHandler() p = make_parser() p.setContentHandler(c) p.parse(open('input.xml')) if __name__ == '__main__': main() -- End Python Code --- Is this a bug in the HaXml SAX parser? Thanks, David From crimbo1994 at gmail.com Fri Oct 17 02:58:45 2008 From: crimbo1994 at gmail.com (Jamie McCloskey) Date: Fri Oct 17 08:32:10 2008 Subject: [Haskell-beginners] IO Problem Message-ID: <1224226725.7232.23.camel@jamie> Hi everyone, I have just started learning Haskell, it is a very interesting language. As a first project, I am building a brainfuck interpreter, but have hit a stumbling block: IO. My program source is located at http://hpaste.org/11219 My program handles the state of a brainfuck program by passing State(lists of ints) objects around. When I added my getIn fuction, however, it needed to return an IO State object. This broke the run function, as it returned a State. No problem, I thought, and changed run to return IO State. This broke my exec function, which calls run on each element in a list. As exec passes on the state returned by the run function to itself, it needs to take an IO State. This causes problems in the loop function, which calls exec on a subprogram. Because exec now takes IO State, loop also needs to. However, loop is called by run, so now run needs to take IO State. Now, all functions called by run need to take IO State. All this creates a whole load of functions with IO, even though it should not be necessary. What I would like to know, is how I can avoid this, possibly by modifying exec to just take a State. Whew! What a long-winded explanation! Any ideas? --Jamie P.S. Please ignore my long-winded list iteration functions -- I'm working on a cleaner version. From daniel.is.fischer at web.de Fri Oct 17 09:40:46 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 17 09:34:24 2008 Subject: [Haskell-beginners] IO Problem In-Reply-To: <1224226725.7232.23.camel@jamie> References: <1224226725.7232.23.camel@jamie> Message-ID: <200810171540.46879.daniel.is.fischer@web.de> Am Freitag, 17. Oktober 2008 08:58 schrieb Jamie McCloskey: > Hi everyone, > I have just started learning Haskell, it is a very interesting language. > As a first project, I am building a brainfuck interpreter, but have hit > a stumbling block: IO. My program source is located at > http://hpaste.org/11219 > > My program handles the state of a brainfuck program by passing > State(lists of ints) objects around. When I added my getIn fuction, > however, it needed to return an IO State object. This broke the run > function, as it returned a State. No problem, I thought, and changed run > to return IO State. This broke my exec function, which calls run on each > element in a list. > > As exec passes on the state returned by the run function to itself, it > needs to take an IO State. This causes problems in the loop function, > which calls exec on a subprogram. Because exec now takes IO State, loop > also needs to. However, loop is called by run, so now run needs to take > IO State. Now, all functions called by run need to take IO State. > > All this creates a whole load of functions with IO, even though it > should not be necessary. What I would like to know, is how I can avoid > this, possibly by modifying exec to just take a State. exec [] st = return st exec (x:xs) st = do newSt <- run x st exec xs newSt or exec (x:xs) st = run x st >>= exec xs Once you're doing input or output, you're in IO, so run and exec must have type a -> b -> IO c, getIn and putOut must also live in IO, everything else needn't. > > Whew! What a long-winded explanation! > > Any ideas? > > --Jamie > > P.S. Please ignore my long-winded list iteration functions -- I'm > working on a cleaner version. > Probably the tidiest (as if that was a criterion for a brainfuck interpreter) would be to rename your State to ProgramState (or whatever) and write the interpreter in StateT ProgramState IO , you'd have run :: Operator -> StateT ProgramState IO () run Add = modify myAdd run Minus = ... run Input = do n <- lift $ readLn modify (inHelper n) and exec = mapM_ run HTH, Daniel From byorgey at seas.upenn.edu Fri Oct 17 14:11:55 2008 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri Oct 17 14:07:45 2008 Subject: [Haskell-beginners] IO Problem In-Reply-To: <1224226725.7232.23.camel@jamie> References: <1224226725.7232.23.camel@jamie> Message-ID: <20081017181155.GA19349@GRW057-25.cis.upenn.edu> On Fri, Oct 17, 2008 at 07:58:45PM +1300, Jamie McCloskey wrote: > > All this creates a whole load of functions with IO, even though it > should not be necessary. What I would like to know, is how I can avoid > this, possibly by modifying exec to just take a State. The short answer is: it IS necessary. Think about what exec does, for instance. It takes a Program and an initial State and does... what? Executes the program, which might result in some things getting printed to the screen or some input being read from the keyboard. That is, exec can have some I/O effects. Therefore, exec's return type must be an IO value. The same goes for all the other functions you mentioned. This is a feature, not a bug --- the type of a function tells you precisely whether it can possibly have any I/O effects. If the return type is (IO something), then it can; if the return type does not involve IO, then it is *guaranteed*[1] that it cannot have any I/O effects. -Brent [1] Except for that pesky unsafePerformIO. But you should never use that, unless you really absolutely know what you are doing and why. From andrew at swclan.homelinux.org Fri Oct 17 19:52:28 2008 From: andrew at swclan.homelinux.org (Andrew Sackville-West) Date: Fri Oct 17 19:48:22 2008 Subject: [Haskell-beginners] Mathematical Blundering In-Reply-To: References: <1224146807.6391.2.camel@darky> Message-ID: <20081017235228.GB23290@localhost.localdomain> On Thu, Oct 16, 2008 at 09:57:39AM +0100, Paul Johnston wrote: > Bit of basic maths. > You are using a power series to approximate sine > This works by taking an expansion about a fixed point, usually zero. > It only works well around that point. > If you get far away it works badly. > You need to exploit the cyclic nature of the trignometrical functions i.e. > Sin x = sin ((2 * pi) + x) = sin ((4 * pi) + x) > Essentially consider the shift in multiples of 2 * pi and calculate the value of x nearest to zero. > > See > http://en.wikipedia.org/wiki/Taylor_series > The diagram on the top right is very instructive. how appropriate. We're doing this in math right now ;) ISTM that OP should take x modulo pi and (per the wiki diagram) a seven term series to get a nice usable sine function. (and don't forget to watch for the sign (nopun) of the result) fact 1 = 1 fact x = x * (fact $ x - 1) term n x = (-1)**n * (theta**(2*n + 1) / fact(2*n + 1)) where theta = pi * ((x / pi) - xTrunc) * sign xTrunc = fromIntegral $ truncate (x/pi) sign = (-1)**xTrunc sine x = sum $ take 7 [term n x | n<-[0..]] Wow, that took me a lot longer to figure out because of all the mixing of numeric types. The critical part was fromIntegral in the poorly name xTrunc function. but it looks pretty accurate. *Main> maximum [sine x - sin x | x<-[1..100]] 1.2652798913048713e-5 and it's easy to boost the accuracy by taking more terms in the series. taking 9: *Main> maximum [sine x - sin x | x<-[1..100]] 1.1683279538265978e-8 taking 11: *Main> maximum [sine x - sin x | x<-[1..100]] 4.694897248747054e-12 I'm sure there must be a better way to get x modulo pi, but I don't know enough... I'd love some feedback on my solution both from a math and a haskell perspective. A > > Paul > > -----Original Message----- > From: beginners-bounces@haskell.org [mailto:beginners-bounces@haskell.org] On Behalf Of Jeffrey Drake > Sent: Thursday, October 16, 2008 9:47 AM > To: Haskell Beginners > Subject: [Haskell-beginners] Mathematical Blundering > > > I have defined myself a set of functions to test: > > fact 1 = 1 > fact n = n * (fact $ n - 1) > > sine x = x - (x^3/(fact 3)) + (x^5/(fact 5)) - (x^7/(fact 7)) > > Where my code is 'sine' and the prelude's is sin: > *Main> sine 1 > 0.841468253968254 > *Main> sin 1 > 0.8414709848078965 > > *Main> sine 2 > 0.9079365079365079 > *Main> sin 2 > 0.9092974268256817 > > *Main> sine 3 > 9.107142857142847e-2 > *Main> sin 3 > 0.1411200080598672 > > *Main> sine 4 > -1.3841269841269837 > *Main> sin 4 > -0.7568024953079282 > > After 2 they seem to diverge rather rapidly, and I am not sure why. Any ideas? > > I would have thought that 4 terms would have been enough. > > - Jeff. > > _______________________________________________ > 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 -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20081017/5147ce12/attachment.bin From cppljevans at suddenlink.net Sun Oct 19 17:59:39 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Sun Oct 19 17:51:31 2008 Subject: [Haskell-beginners] requested module name differs from name found in interface file Message-ID: <48FBADCB.4060508@suddenlink.net> With a file containing: > module Main where > > import Array > import Control.Functor.Fix I get: > make > ghc -i/root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 -c > catamorphism.example.hs > > catamorphism.example.hs:19:0: > Bad interface file: > /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2/Control/Functor/Fix.hi > Something is amiss; requested module main:Control.Functor.Fix > differs from name found in the interface file > category-extras-0.53.5:Control.Functor.Fix > make: *** [all] Error 1 I used cabal to install category-extras: > /home/evansl/download/haskell/libs # cabal install category-extras > Resolving dependencies... > Downloading category-extras-0.53.5... > Configuring category-extras-0.53.5... > Preprocessing library category-extras-0.53.5... ... > /usr/bin/ar: creating dist/build/libHScategory-extras-0.53.5.a > Installing library in /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 > Registering category-extras-0.53.5... > Reading package info from "dist/installed-pkg-config" ... done. > Saving old package config file... done. > Writing new package config file... done. > /home/evansl/download/haskell/libs # What should I do to import the Functor.Fix as shown here: http://comonad.com/reader/2008/recursion-schemes/ TIA. -Larry -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081019/08b64919/attachment.htm From spranesh at gmail.com Mon Oct 20 08:09:50 2008 From: spranesh at gmail.com (Pranesh Srinivasan) Date: Mon Oct 20 08:05:32 2008 Subject: [Haskell-beginners] A Pascal-like Language Compiler Message-ID: Hi all, I have been learning haskell for the last month or so. I have just started becoming familiar with monads and looking at Parsec. As part of the Language Translators Course this semester, in the university I study, we are to implement a Pascal-like toy language to C translator[1]. The aim is to take a simple subset of Pascal and translate it to an intermediate 3 operand format, before translating it to simple C. The time I have for this project is about a month. I am contemplating doing this in Haskell, since I have read in several places that Haskell (and other functional languages) is very good for writing compilers. In fact, I have started writing code for lexing the basic tokens. I do have doubts about my ability to finish it. The curiosity to do something of a moderate size in Haskell, and the extremely friendly folks on #haskell, tipped me on this decision. I looked around for similar things on the internet, in vain. I have several doubts at this stage some of which include, * how the parser will determine the scope of the variables, i.e, how can a symbol table be implemented (in the state of the parser ?) * how easy or difficult will basic code optimisation (like unused variables, and loops with no side effects) and type checking be? * how would one go about implementing basic error recovery in the parser (make it skip the current line, or block) ? I would love it if I get a few pointers from people who have been there, done that. Or am I being too ambitious too early? :) [1] One of the suggested programming exercises at the end of the "Dragon" Compilers book, by Aho, et al. -- Pranesh S From joel.bjornson at gmail.com Mon Oct 20 09:47:44 2008 From: joel.bjornson at gmail.com (=?ISO-8859-1?Q?Joel_Bj=F6rnson?=) Date: Mon Oct 20 09:43:25 2008 Subject: [Haskell-beginners] A Pascal-like Language Compiler In-Reply-To: References: Message-ID: <658ffcc20810200647pf1f7235gedeaec988d4aa389@mail.gmail.com> Oops, should have hit the replay-to-all button, so sorry Pranesh for double posting. Hi, have you considered using BNCF[1]? With BNFC you can define a grammar for your language then let the BNFC converter generate a Haskell parser (along with an ADT representing syntax trees). On Mon, Oct 20, 2008 at 2:09 PM, Pranesh Srinivasan wrote: > I looked around for similar things on the internet, in vain. I have > several doubts at this stage some of which include, > > * how the parser will determine the scope of the variables, > i.e, how can a symbol table be implemented (in the state of the > parser ?) I think it is probably better to exclude the variable look up, type checking etc from the parsing phase. Here's an example of a general scheme: 1. Parse input file into an abstract syntax tree representation. 2. Perform type checking on your syntax tree. 3. Transform the syntax tree using re-write rules and optimisations. 4. Pretty print the syntax tree in order to output code of your target language. > > * how easy or difficult will basic code optimisation (like unused > variables, and loops with no side effects) and type checking be? I would say probably easier then in many other languages since Haskell has such a great support for recursive data types, pattern matching etc. > * how would one go about implementing basic error recovery in the > parser (make it skip the current line, or block) ? > Not really sure what you mean by error recovery but when using BNFC all of the tedious work of implementing the parsers is automated. Regards, Joel [1] http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/ From cppljevans at suddenlink.net Mon Oct 20 13:15:39 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Mon Oct 20 13:07:38 2008 Subject: [Haskell-beginners] requested module name differs from name found in interface file In-Reply-To: References: <48FBADCB.4060508@suddenlink.net> <48FCB112.9060404@suddenlink.net> Message-ID: <48FCBCBB.4040305@suddenlink.net> On 10/20/08 11:38, Justin Bailey wrote: > On Mon, Oct 20, 2008 at 9:25 AM, Larry Evans wrote: > [snip] > >> Do you mean install it manually as shown in the aforementioned >> How_to_install_a_Cabal_package? >> > > No, I mean go to http://hackage.haskell.org and download the .tar.gz > file directly (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/category-extras). > Unzip/untar that package. I haven't looked at it, but there is > probably a "src" directory. Put your example file in there and load it > in ghci. Your source file should then use the source available in that > directory tree rather than the category-extras install which seems > broken. This works because the GHC will look at the import > "Control.Functor.Fix" and use that to look for > "Control/Functor/Fix.hs" from the current directory. > Tried that but: <--- cut here --- GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :load "/home/evansl/download/haskell/libs/category-extras-0.53.5/src/catamorphism.example.hs" Control/Category/Dual.hs:17:7: Could not find module `Control.Category': Use -v to see a list of the files searched for. Failed, modules loaded: none. Prelude> <--- cut here --- If it's any help, the directory list for src/Control/Category is: <--- cut here --- /home/evansl/download/haskell/libs/category-extras-0.53.5/src/Control/Category: total used in directory 52 available 148798604 drwxr-xr-x 3 evansl evansl 4096 Jun 30 08:08 . drwxr-xr-x 9 evansl evansl 4096 Jun 30 08:08 .. -rw-r--r-- 1 evansl evansl 2118 Jun 30 08:08 Associative.hs -rw-r--r-- 1 evansl evansl 1668 Jun 30 08:08 Braided.hs drwxr-xr-x 2 evansl evansl 4096 Jun 30 08:08 Cartesian -rw-r--r-- 1 evansl evansl 4973 Jun 30 08:08 Cartesian.hs -rw-r--r-- 1 evansl evansl 1020 Jun 30 08:08 Discrete.hs -rw-r--r-- 1 evansl evansl 1340 Jun 30 08:08 Distributive.hs -rw-r--r-- 1 evansl evansl 668 Jun 30 08:08 Dual.hs -rw-r--r-- 1 evansl evansl 578 Jun 30 08:08 Hask.hs -rw-r--r-- 1 evansl evansl 3254 Jun 30 08:08 Monoidal.hs -rw-r--r-- 1 evansl evansl 1746 Jun 30 08:08 Object.hs >--- cut here --- > You might have more luck finding out what's going on by emailing > Haskell-Cafe directly, too. > Yes, thanks. > Justin > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081020/108edec2/attachment-0001.htm From mmexanosh at gmail.com Mon Oct 20 14:17:56 2008 From: mmexanosh at gmail.com (Michael Mekhanoshin) Date: Mon Oct 20 14:13:36 2008 Subject: [Haskell-beginners] do not understand Semantics of Case Expressions in report Message-ID: <239ca350810201117y5652c09k63a5d0154edb641@mail.gmail.com> at http://www.haskell.org/onlinereport/exps.html on figure Figure 4 (Semantics of Case Expressions, Part 2) one can see: (m) case v of { K { f1 = p1 , f2 = p2 , ... } -> e ; _ - > e' } = case e' of { y -> case v of { K { f1 = p1 } -> case v of { K { f2 = p2 , ... } -> e ; _ -> y }; _ -> y }} where f1, f2, ... are fields of constructor K; y is a new variable (In Figures 3.1--3.2: e, e' and ei are expressions; g and gi are boolean-valued expressions; p and pi are patterns; v, x, and xi are variables; K and K' are algebraic datatype (data) constructors (including tuple constructors); and N is a newtype constructor.) I'm totally confused with this identity. Especialy with " = case e'". Can anyone explain a little what does this rule stand, please? It is better to see some example. Respect, Michael From iand675 at gmail.com Mon Oct 20 23:36:03 2008 From: iand675 at gmail.com (Ian Duncan) Date: Mon Oct 20 23:31:42 2008 Subject: [Haskell-beginners] Weird compilation error while doing Euler problems? Message-ID: 'm trying to compile my file that has my Euler problems in it to output the solution to problem four, but I'm getting a compile error. Here's my .hs file: ------------------------- module Euler1 where import Data.List import Data.Ord main = mapM_ putStrLn problem4 problem1 = foldl1' (+) $ nub $ (takeWhile (< 1000) [3,6..] ++ takeWhile (< 1000) [5,10..]) problem2 = sum $ takeWhile (<= 4000000) [x | x <- fibs, even x] where fibs = unfoldr (\(a,b) -> Just (a,(b,a+b))) (0,1) problem3 z = maximumBy compare (filter (\x -> z `mod` x == 0) (takeWhile (<= ceiling (sqrt (fromIntegral z))) primes)) problem4 = nub [ show $ y * z | y <- [100..999], z <- [100..999], show (y*z) == reverse (show $ y*z)] prime p = p `elem` primes primes = small ++ large where 1:p:candidates = roll $ mkWheel small small = [2,3,5,7] large = p : filter isPrime candidates isPrime n = all (not . divides n) $ takeWhile (\p -> p*p <= n) large divides n p = n `mod` p == 0 mkWheel ds = foldl nextSize w0 ds nextSize (Wheel n rs) p = Wheel (p*n) [r' | k <- [0..(p-1)], r <- rs, let r' = n*k+r, r' `mod` p /= 0] w0 = Wheel 1 [1] roll (Wheel n rs) = [n*k+r | k <- [0..], r <- rs] data Wheel = Wheel Integer [Integer] ------------------------ Here's my output when I try to compile: ian$ ghc ~/Documents/eulerProblem1.hs -o test Undefined symbols: "___stginit_ZCMain", referenced from: ___stginit_ZCMain$non_lazy_ptr in libHSrts.a(Main.o) "_ZCMain_main_closure", referenced from: _ZCMain_main_closure$non_lazy_ptr in libHSrts.a(Main.o) ld: symbol(s) not found collect2: ld returned 1 exit status What's going on? I'm running GHC 6.8.3, if that helps. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081020/b8085c7d/attachment.htm From allbery at ece.cmu.edu Mon Oct 20 23:46:50 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Mon Oct 20 23:42:34 2008 Subject: [Haskell-beginners] Weird compilation error while doing Euler problems? In-Reply-To: References: Message-ID: <3C897863-8829-4916-89BD-CB04AA04E99F@ece.cmu.edu> On 2008 Oct 20, at 23:36, Ian Duncan wrote: > 'm trying to compile my file that has my Euler problems in it to > output the solution to problem four, but I'm getting a compile > error. Here's my .hs file: > ------------------------- > module Euler1 where If you're not using the standard module Main, you need to tell ghc which module you put your main function in: ghc --main-is Euler1 ... -- 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081020/d5ea5eb7/attachment.htm From daniel.is.fischer at web.de Mon Oct 20 23:55:41 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Oct 20 23:49:04 2008 Subject: [Haskell-beginners] Weird compilation error while doing Euler problems? In-Reply-To: References: Message-ID: <200810210555.41632.daniel.is.fischer@web.de> Am Dienstag, 21. Oktober 2008 05:36 schrieb Ian Duncan: > 'm trying to compile my file that has my Euler problems in it to output the > solution to problem four, but I'm getting a compile error. Here's my .hs > file: > ------------------------- > module Euler1 where > import Data.List > import Data.Ord > > main = mapM_ putStrLn problem4 > > problem1 = foldl1' (+) $ nub $ (takeWhile (< 1000) [3,6..] ++ takeWhile (< > 1000) [5,10..]) > problem2 = sum $ takeWhile (<= 4000000) [x | x <- fibs, even x] > where fibs = unfoldr (\(a,b) -> Just (a,(b,a+b))) (0,1) > > problem3 z = maximumBy compare (filter (\x -> z `mod` x == 0) (takeWhile > (<= ceiling (sqrt (fromIntegral z))) primes)) > > problem4 = nub [ show $ y * z | y <- [100..999], z <- [100..999], show > (y*z) == reverse (show $ y*z)] > > prime p = p `elem` primes > > primes = small ++ large > where > 1:p:candidates = roll $ mkWheel small > small = [2,3,5,7] > large = p : filter isPrime candidates > isPrime n = all (not . divides n) $ takeWhile (\p -> p*p <= n) large > divides n p = n `mod` p == 0 > mkWheel ds = foldl nextSize w0 ds > nextSize (Wheel n rs) p = > Wheel (p*n) [r' | k <- [0..(p-1)], r <- rs, let r' = n*k+r, r' `mod` p /= > 0] > w0 = Wheel 1 [1] > roll (Wheel n rs) = [n*k+r | k <- [0..], r <- rs] > data Wheel = Wheel Integer [Integer] > ------------------------ > > Here's my output when I try to compile: > > ian$ ghc ~/Documents/eulerProblem1.hs -o test > Undefined symbols: > "___stginit_ZCMain", referenced from: > ___stginit_ZCMain$non_lazy_ptr in libHSrts.a(Main.o) > "_ZCMain_main_closure", referenced from: > _ZCMain_main_closure$non_lazy_ptr in libHSrts.a(Main.o) > ld: symbol(s) not found > collect2: ld returned 1 exit status > > What's going on? I'm running GHC 6.8.3, if that helps. Your module isn't named Main, so to produce an executable you must pass the flag -main-is ModuleName on the command line. Another thing, it is recommendable to develop the habit of passing the --make flag to ghc. From haskell at colquitt.org Tue Oct 21 12:30:47 2008 From: haskell at colquitt.org (John Dorsey) Date: Tue Oct 21 12:26:25 2008 Subject: [Haskell-beginners] do not understand Semantics of Case Expressions in report Message-ID: <20081021163046.GE27422@colquitt.org> Michael, (Sorry for duplicates; I sent this from the wrong address initially.) > (m) case v of { K { f1 = p1 , f2 = p2 , ... } -> e ; _ -> e' } > = case e' of { > y -> > case v of { > K { f1 = p1 } -> > case v of { K { f2 = p2 , ... } -> e ; _ -> y }; > _ -> y }} > where f1, f2, ... are fields of constructor K; y is a new variable [...] > I'm totally confused with this identity. Especialy with " = case e'". > Can anyone explain a little what does this rule stand, please? > It is better to see some example. You got me curious, so I looked over this. The table you reference lists identities that should hold for all implementations of case expressions. (m) is about how case matches against constructors with named fields, and it shows that a case expression that matches against multiple fields (f1, f2, etc.) can be broken into nested case expressions which each match a single field (eg. f1). It looks like "case e' of { y -> ..." was introduced solely to give e' a name that can be used in two places, since the nested form may fail to match at each level. It seems to me that without this, you could get code explosion from duplicating the expression e' below; with this you get sharing. Here it is with a interim step added: -- Match v against a constructor with multiple fields case v of { K { f1 = p1 , f2 = p2 , ... } -> e ; _ -> e' } -- Same, but introduce y to represent e' without loss of sharing case e' of { y -> case v of { K { f1 = p1 , f2 = p2 , ... } -> e ; _ -> y }} -- Same, but match first field of K independent of the rest case e' of { y -> case v of { K { f1 = p1 } -> case v of { K { f2 = p2 , ... } -> e ; _ -> y }; _ -> y }} I hope this helps. Regards, John From mmexanosh at gmail.com Tue Oct 21 13:35:46 2008 From: mmexanosh at gmail.com (Michael Mekhanoshin) Date: Tue Oct 21 13:31:25 2008 Subject: [Haskell-beginners] do not understand Semantics of Case Expressions in report In-Reply-To: <20081021163046.GE27422@colquitt.org> References: <20081021163046.GE27422@colquitt.org> Message-ID: <239ca350810211035u3995b644oac58a50196dd8247@mail.gmail.com> 2008/10/21 John Dorsey : > It looks like > > "case e' of { y -> ..." > > was introduced solely to give e' a name that can be used in two places, > since the nested form may fail to match at each level. It seems to me > that without this, you could get code explosion from duplicating the > expression e' below; with this you get sharing. ... > > -- Match v against a constructor with multiple fields > case v of { K { f1 = p1 , f2 = p2 , ... } -> e ; _ -> e' } > > -- Same, but introduce y to represent e' without loss of sharing > case e' of { y -> > case v of { K { f1 = p1 , f2 = p2 , ... } -> e ; _ -> y }} > > I hope this helps. > Surely it does. Most tricky part here (for me) is that e' goes in outer scope. It works here like in rule (c) . I got it. Thank you for your help. Regards, Michael From cppljevans at suddenlink.net Tue Oct 21 14:25:53 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Tue Oct 21 14:17:32 2008 Subject: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? Message-ID: <48FE1EB1.2050907@suddenlink.net> Skipped content of type multipart/alternative-------------- next part -------------- A non-text attachment was scrubbed... Name: uniplate.try.hs Type: text/x-haskell Size: 956 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20081021/1669ad30/uniplate.try-0001.bin From jason.dusek at gmail.com Wed Oct 22 17:10:05 2008 From: jason.dusek at gmail.com (Jason Dusek) Date: Wed Oct 22 17:05:38 2008 Subject: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? In-Reply-To: <48FE1EB1.2050907@suddenlink.net> References: <48FE1EB1.2050907@suddenlink.net> Message-ID: <42784f260810221410r2e1b230bsec9a5282c3757ca@mail.gmail.com> Can you explain why you think you need that annotation? I can't see an ambiguous interpretation of your code. -- _jsn From aslatter at gmail.com Wed Oct 22 17:44:07 2008 From: aslatter at gmail.com (Antoine Latter) Date: Wed Oct 22 17:39:41 2008 Subject: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? In-Reply-To: <42784f260810221410r2e1b230bsec9a5282c3757ca@mail.gmail.com> References: <48FE1EB1.2050907@suddenlink.net> <42784f260810221410r2e1b230bsec9a5282c3757ca@mail.gmail.com> Message-ID: <694519c50810221444k27e8071cj3a7189287bd8255b@mail.gmail.com> On Wed, Oct 22, 2008 at 4:10 PM, Jason Dusek wrote: > Can you explain why you think you need that annotation? I > can't see an ambiguous interpretation of your code. > The confusion is that the 'Val' constructor is for the 'Expr' type, which has a phantom type parameter in its type constructor. Can you load that up into GHCi and type: > :t val_9 which should cause GHCi to print out what it thinks the type of that expression is. -Antoine From spranesh at gmail.com Wed Oct 22 18:18:24 2008 From: spranesh at gmail.com (Pranesh Srinivasan) Date: Wed Oct 22 18:13:57 2008 Subject: [Haskell-beginners] A Pascal-like Language Compiler In-Reply-To: <658ffcc20810200647pf1f7235gedeaec988d4aa389@mail.gmail.com> References: <658ffcc20810200647pf1f7235gedeaec988d4aa389@mail.gmail.com> Message-ID: Hey all, Thanks to everyone for replying. I was severly caught up with work for the last two days. The link Larry gave for the lookahead states seems very nice :). But I am not sure if Ill be looking to calculate the lookahead states myself, or let BNFC do the job? In either case, I think merely printing the line number where the error occured should do in the worse case. It will be exciting to have nice error messages though. I am starting to reliase the advantage of pattern matching being present, like Chris had said. I mean it definitely beats maintaining and checking a flag, the way you would do it in C :) > Definitely not, just go for it. In the IPL course @ UU we implemented Thanks, Chris :). > 1. Parse input file into an abstract syntax tree representation. > 2. Perform type checking on your syntax tree. > 3. Transform the syntax tree using re-write rules and optimisations. > 4. Pretty print the syntax tree in order to output code of your target language. That seems like a very nice scheme to follow. I had a similar method in mind. Step 3 is what I am really worried about. How easy/difficult will it be in a pure func language, to "transform" the sytnax tree. I have to take a deeper look at BNFC. But from an initial look, it seems way too powerful for me to use? At least as powerful as yacc. And that with Haskell, should give me a very good toolset-advantage? @Chris : The ipl course website, seems very helpful, especially some of the lectures on abstract syntax. -- Pranesh Srinivasan, Third Year Student, Computer Science & Engineering, Indian Institute of Technology - Madras. http://spranesh.googlepages.com http://www.cse.iitm.ac.in/~spranesh/ From daniel.is.fischer at web.de Wed Oct 22 20:52:39 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Oct 22 20:45:58 2008 Subject: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? In-Reply-To: <694519c50810221444k27e8071cj3a7189287bd8255b@mail.gmail.com> References: <48FE1EB1.2050907@suddenlink.net> <42784f260810221410r2e1b230bsec9a5282c3757ca@mail.gmail.com> <694519c50810221444k27e8071cj3a7189287bd8255b@mail.gmail.com> Message-ID: <200810230252.40062.daniel.is.fischer@web.de> Am Mittwoch, 22. Oktober 2008 23:44 schrieb Antoine Latter: > On Wed, Oct 22, 2008 at 4:10 PM, Jason Dusek wrote: > > Can you explain why you think you need that annotation? I > > can't see an ambiguous interpretation of your code. > > The confusion is that the 'Val' constructor is for the 'Expr' type, > which has a phantom type parameter in its type constructor. > > Can you load that up into GHCi and type: > > :t val_9 > > which should cause GHCi to print out what it thinks the type of that > expression is. > > -Antoine ghci correctly thinks that has the type Expr e. Much like *Main> :t [] [] :: [a] I think what goes on here is defaulting (deviating from Section 4.3.4 of the report, but it's the same deviation that allows [] to be printed). To print it, ghci picks some default type for e, probably Integer, as the defaut default is (Integer, Double), doesn't influence the result of show. If you muck around with the Show instance, you can easily get compilation errors like "Ambiguous type variable..." (e.g. if you add a (Show e) constraint to the Show instance for (Expr e), but not if you add a (Num e) or an (Integral e) constraint). From spoon.reloaded at gmail.com Wed Oct 22 22:26:15 2008 From: spoon.reloaded at gmail.com (Xuan Luo) Date: Wed Oct 22 22:21:48 2008 Subject: [Haskell-beginners] Type problems with IOArray Message-ID: <185ee9e60810221926n7562bbd6lb0bd2c22aafd8583@mail.gmail.com> I am having lots of trouble using polymorphic mutable IOArrays. Here is an example program: import Data.Array.MArray import Data.Array.IO foo x = do a <- newArray (0, 4) x readArray a 2 main = foo 42 >>= print So there is a function "foo" which makes an array of polymorphic type initialized with a value, then returns one of the elements of the array. So here "foo x" essentially does the same as "return x"; but it demonstrates problems I am having. So the above program fails with: testarray.hs:7:7: No instance for (MArray a t IO) arising from a use of `foo' at testarray.hs:7:7-12 Possible fix: add an instance declaration for (MArray a t IO) In the first argument of `(>>=)', namely `foo 42' In the expression: foo 42 >>= print In the definition of `main': main = foo 42 >>= print Okay; so the problem is that "newArray" is a function that creates MArrays in general; but there is no function to specifically create IOArrays in particular, and it doesn't know I want to use IOArrays. In fact, all the other functions that are used to operate on mutable arrays are generic MArray functions too, and there is basically nothing that is specific to IOArrays, so it is not as if the compiler can "figure it out from context". This is bad design. I wish it could just decide to default to IOArrays or something, since IOArrays of any type already are instances of MArray, so it would be natural. Okay so the general way to resolve this is to add signatures to tell it that it is an IOArray. But when I give the type for IOArray I also have to tell it what type its contents are, and I want it to work polymorphically over any type, so the array has to be of a polymorphic type: foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) readArray a 2 But of course it doesn't know what "a" is, so maybe I am forced to also add a signature for the function "foo" itself, which the compiler should really be able to figure out for me: import Data.Array.MArray import Data.Array.IO foo :: a -> IO a foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) readArray a 2 main = foo 42 >>= print Okay, now the real problems begin: testarray.hs:5:32: Couldn't match expected type `a1' against inferred type `a' `a1' is a rigid type variable bound by the polymorphic type `forall a1. IO (IOArray Int a1)' at testarray.hs:5:16 `a' is a rigid type variable bound by the type signature for `foo' at testarray.hs:4:7 In the second argument of `newArray', namely `x' In a 'do' expression: a <- newArray (0, 4) x :: IO (IOArray Int a) In the expression: do a <- newArray (0, 4) x :: IO (IOArray Int a) readArray a 2 What the heck is this? I looked through a lot of stuff online and eventually found that this works: {-# LANGUAGE ScopedTypeVariables #-} import Data.Array.MArray import Data.Array.IO foo :: forall a. a -> IO a foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) readArray a 2 main = foo 42 >>= print So I had to add some weird "forall" stuff to my function signature and enable some language extension flag(?). This seems way too complicated. I just want to be able to make and use a simple polymorphic array in the IO monad with regular Haskell, without changing compiler flags or anything like that. I have been doing well in other stuff involving the IO monad, but these mutable arrays really got me stuck. Thanks, From alexander.dunlap at gmail.com Wed Oct 22 23:04:10 2008 From: alexander.dunlap at gmail.com (Alexander Dunlap) Date: Wed Oct 22 22:59:43 2008 Subject: [Haskell-beginners] Type problems with IOArray In-Reply-To: <185ee9e60810221926n7562bbd6lb0bd2c22aafd8583@mail.gmail.com> References: <185ee9e60810221926n7562bbd6lb0bd2c22aafd8583@mail.gmail.com> Message-ID: <57526e770810222004q63bc43d5refd42db5a4440990@mail.gmail.com> On Wed, Oct 22, 2008 at 7:26 PM, Xuan Luo wrote: > I am having lots of trouble using polymorphic mutable IOArrays. Here > is an example program: > > import Data.Array.MArray > import Data.Array.IO > > foo x = do a <- newArray (0, 4) x > readArray a 2 > > main = foo 42 >>= print > > So there is a function "foo" which makes an array of polymorphic type > initialized with a value, then returns one of the elements of the > array. So here "foo x" essentially does the same as "return x"; but it > demonstrates problems I am having. > > So the above program fails with: > testarray.hs:7:7: > No instance for (MArray a t IO) > arising from a use of `foo' at testarray.hs:7:7-12 > Possible fix: add an instance declaration for (MArray a t IO) > In the first argument of `(>>=)', namely `foo 42' > In the expression: foo 42 >>= print > In the definition of `main': main = foo 42 >>= print > > Okay; so the problem is that "newArray" is a function that creates > MArrays in general; but there is no function to specifically create > IOArrays in particular, and it doesn't know I want to use IOArrays. In > fact, all the other functions that are used to operate on mutable > arrays are generic MArray functions too, and there is basically > nothing that is specific to IOArrays, so it is not as if the compiler > can "figure it out from context". This is bad design. I wish it could > just decide to default to IOArrays or something, since IOArrays of any > type already are instances of MArray, so it would be natural. > > Okay so the general way to resolve this is to add signatures to tell > it that it is an IOArray. But when I give the type for IOArray I also > have to tell it what type its contents are, and I want it to work > polymorphically over any type, so the array has to be of a polymorphic > type: > > foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) > readArray a 2 > > But of course it doesn't know what "a" is, so maybe I am forced to > also add a signature for the function "foo" itself, which the compiler > should really be able to figure out for me: > > import Data.Array.MArray > import Data.Array.IO > > foo :: a -> IO a > foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) > readArray a 2 > > main = foo 42 >>= print > > Okay, now the real problems begin: > testarray.hs:5:32: > Couldn't match expected type `a1' against inferred type `a' > `a1' is a rigid type variable bound by > the polymorphic type `forall a1. IO (IOArray Int a1)' > at testarray.hs:5:16 > `a' is a rigid type variable bound by > the type signature for `foo' at testarray.hs:4:7 > In the second argument of `newArray', namely `x' > In a 'do' expression: a <- newArray (0, 4) x :: IO (IOArray Int a) > In the expression: > do a <- newArray (0, 4) x :: IO (IOArray Int a) > readArray a 2 > > What the heck is this? I looked through a lot of stuff online and > eventually found that this works: > > {-# LANGUAGE ScopedTypeVariables #-} > import Data.Array.MArray > import Data.Array.IO > > foo :: forall a. a -> IO a > foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) > readArray a 2 > > main = foo 42 >>= print > > So I had to add some weird "forall" stuff to my function signature and > enable some language extension flag(?). This seems way too > complicated. > > I just want to be able to make and use a simple polymorphic array in > the IO monad with regular Haskell, without changing compiler flags or > anything like that. I have been doing well in other stuff involving > the IO monad, but these mutable arrays really got me stuck. > > Thanks, > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > One way is to move the type signature one level lower: import Data.Array.MArray import Data.Array.IO foo :: a -> IO a foo x = do a <- (newArray (0, 4) :: a -> IO (IOArray Int a)) x readArray a 2 main = foo 42 >>= print Essentially, the problem you are running into is this: You need to tell the compiler what kind of array you want newArray (0,4) x to create. You'd like to be able to say newArray (0,4) x :: IO (IOArray Int *SOMETHING*), but Haskell doesn't let you do that; you have to assign it a type variable. Normally, you'd say IO (IOArray Int a), as you did, but in this case, there's a problem: the type of "newArray (0,4) x" in this context is not as general as "a". You can't make it an instance of *any* type, only the type of x. Unfortunately, there's no way to specify the type of x without using scoped type variables. (In fact, that's pretty much the whole point of scoped type variables.) The solution, then, is to not specify the type of newArray (0,4) x but only the type of newArray (0,4). newArray (0,4) actually can have the type (a -> IO (IOArray Int a)) for *any* a, because you haven't put any constraints on "a" by applying it to "x". I hope that's somewhat clear; I apologize for the convolutedness. Alex From cppljevans at suddenlink.net Wed Oct 22 23:36:09 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Wed Oct 22 23:27:41 2008 Subject: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? In-Reply-To: <200810230252.40062.daniel.is.fischer@web.de> References: <48FE1EB1.2050907@suddenlink.net> <42784f260810221410r2e1b230bsec9a5282c3757ca@mail.gmail.com> <694519c50810221444k27e8071cj3a7189287bd8255b@mail.gmail.com> <200810230252.40062.daniel.is.fischer@web.de> Message-ID: <48FFF129.6040608@suddenlink.net> On 10/22/08 19:52, Daniel Fischer wrote: > Am Mittwoch, 22. Oktober 2008 23:44 schrieb Antoine Latter: > >> On Wed, Oct 22, 2008 at 4:10 PM, Jason Dusek wrote: >> >>> Can you explain why you think you need that annotation? I >>> can't see an ambiguous interpretation of your code. >>> >> The confusion is that the 'Val' constructor is for the 'Expr' type, >> which has a phantom type parameter in its type constructor. >> >> Can you load that up into GHCi and type: >> >>> :t val_9 >>> >> which should cause GHCi to print out what it thinks the type of that >> expression is. >> >> -Antoine >> > > ghci correctly thinks that has the type Expr e. Much like > *Main> :t [] > [] :: [a] > > I think what goes on here is defaulting (deviating from Section 4.3.4 of the > report, but it's the same deviation that allows [] to be printed). To print > it, ghci picks some default type for e, probably Integer, as the defaut > default is (Integer, Double), doesn't influence the result of show. > If you muck around with the Show instance, you can easily get compilation > errors like "Ambiguous type variable..." (e.g. if you add a (Show e) > constraint to the Show instance for (Expr e), but not if you add a (Num e) or > an (Integral e) constraint). > Thanks Deaniel. The fog in my head begins to clear. I took Antoine's suggestion and got: <---cut here --- GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> :load "/home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs" [1 of 1] Compiling Main ( /home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs, interpreted ) Ok, modules loaded: Main. *Main> let val_9 = Val 9 Loading package mtl-1.1.0.0 ... linking ... done. Loading package array-0.1.0.0 ... linking ... done. Loading package containers-0.1.0.1 ... linking ... done. Loading package uniplate-1.2.0.1 ... linking ... done. *Main> :t val_9 val_9 :: Expr e *Main> print val_9 Val 9 *Main> >---cut here--- I guess the phantom type mentioned in Antoine's post is the e in: val_9::Expr e ? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081022/23562c71/attachment-0001.htm From daniel.is.fischer at web.de Thu Oct 23 00:10:21 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 23 00:03:39 2008 Subject: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? In-Reply-To: <48FFF129.6040608@suddenlink.net> References: <48FE1EB1.2050907@suddenlink.net> <200810230252.40062.daniel.is.fischer@web.de> <48FFF129.6040608@suddenlink.net> Message-ID: <200810230610.21530.daniel.is.fischer@web.de> Am Donnerstag, 23. Oktober 2008 05:36 schrieb Larry Evans: > Thanks Deaniel. The fog in my head begins to clear. > I took Antoine's suggestion and got: > <---cut here --- > GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help > Loading package base ... linking ... done. > Prelude> :load > "/home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs" > [1 of 1] Compiling Main ( > /home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs, interpreted > ) Ok, modules loaded: Main. > *Main> let val_9 = Val 9 > Loading package mtl-1.1.0.0 ... linking ... done. > Loading package array-0.1.0.0 ... linking ... done. > Loading package containers-0.1.0.1 ... linking ... done. > Loading package uniplate-1.2.0.1 ... linking ... done. > *Main> :t val_9 > val_9 :: Expr e > *Main> print val_9 > Val 9 > *Main> > > >---cut here--- > > I guess the phantom type mentioned in Antoine's post is the e in: > > val_9::Expr e > > ? Exactly. That type is never used in any value of type (Expr e), therefore it is called a 'phantom' type. It only serves as a tag to prevent mixing Expr's with different type parameters. Cheers, Daniel From DekuDekuplex at Yahoo.com Thu Oct 23 02:07:09 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 02:03:00 2008 Subject: [Haskell-beginners] how to define a user datatype consisting of instances of String? Message-ID: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> How is it possible to define a user datatype consisting of instances of String? Suppose I am preparing a party for wine connoisseurs, and want to define the following data types: data Wine = Red | White data Red = "Merlot" data White = "Sauvignon Blanc" data Entree = "pork" | "chicken" | "tuna" data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" Then, I want to write a Haskell function that takes two lists; e.g., ["pork", "chicken", "tuna"] and ["garlic bread", "mozzarella sticks", "caviar"] and that constructs a three-tuple of the following type: (Entree, SideDish, Wine) such that which Wine is chosen depends on the particular combination of Entree and SideDish, with respect to the following rules: ("pork", "garlic bread") -> ("pork", "garlic bread", "Merlot") ("pork", "mozzarella sticks") -> ("pork", "mozzarella sticks", "Merlot") ("pork", "caviar") -> ("pork", "Beluga", "Sauvignon Blanc") ("chicken", "garlic bread") -> ("chicken", "garlic bread", "Merlot") ("chicken", "mozzarella sticks") -> ("chicken", "mozzarella sticks", "Sauvignon Blanc") ("chicken", "caviar") -> ("chicken", "caviar", "Merlot") ("tuna", "garlic bread") -> ("tuna", "garlic bread", "Sauvignon Blanc") ("tuna", "mozzarella sticks") -> ("tuna", "mozzarella sticks", "Merlot") ("tuna", "caviar") -> ("tuna", "caviar", "Merlot") So far, I have written the following Haskell code: module Wine where data Wine = Red | White data Red = "Merlot" data White = "Sauvignon Blanc" data Entree = "pork" | "chicken" | "tuna" data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] wine entree sidedish | entree == "pork" = | sidedish == "garlic bread" = ("pork", "garlic bread", "Merlot") | sidedish == "mozzarella sticks" = ("pork", "mozzarella sticks", "Merlot") | sidedish == "caviar" = ("pork", "caviar", "Sauvignon Blanc") | entree == "chicken" = | sidedish == "garlic bread" = ("chicken", "garlic bread", "Merlot") | sidedish == "mozzarella sticks" = ("chicken", "mozzarella sticks", "Sauvignon Blanc") | sidedish == "caviar"= ("chicken", "caviar", "Merlot") | entree == "tuna" = | sidedish == "garlic bread" = ("tuna", "garlic bread", "Sauvignon Blanc") | sidedish == "mozzarella sticks" = ("tuna", "mozzarella sticks", "Merlot") | sidedish == "caviar"= ("tuna", "caviar", "Merlot") However, when I load this code into GHCi, I get the following error message: [1 of 1] Compiling Wine ( wine.hs, interpreted ) wine.hs:18:11: parse error on input `"' Failed, modules loaded: none. Prelude> I discovered the following seemingly relevant HaskellWiki page, but am not sure about how to apply the advice in it to this problem, because the advice does not seem sufficiently specific or detailed: List instance - HaskellWiki http://www.haskell.org/haskellwiki/List_instance What would be a good way to overcome this error? -- Benjamin L. Russell From DekuDekuplex at Yahoo.com Thu Oct 23 02:22:15 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 02:17:57 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> Message-ID: <6e50g45rej6k6n4srag6rfh23i1hk9f4gs@4ax.com> On Thu, 23 Oct 2008 15:07:09 +0900, Benjamin L.Russell wrote: >module Wine where > >data Wine = Red | White >data Red = "Merlot" >data White = "Sauvignon Blanc" >data Entree = "pork" | "chicken" | "tuna" >data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" > >wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] >wine entree sidedish > | entree == "pork" = > | sidedish == "garlic bread" = ("pork", "garlic bread", >"Merlot") > | sidedish == "mozzarella sticks" = ("pork", "mozzarella >sticks", "Merlot") > | sidedish == "caviar" = ("pork", "caviar", "Sauvignon Blanc") > | entree == "chicken" = > | sidedish == "garlic bread" = ("chicken", "garlic bread", >"Merlot") > | sidedish == "mozzarella sticks" = ("chicken", "mozzarella >sticks", "Sauvignon Blanc") > | sidedish == "caviar"= ("chicken", "caviar", "Merlot") > | entree == "tuna" = > | sidedish == "garlic bread" = ("tuna", "garlic bread", >"Sauvignon Blanc") > | sidedish == "mozzarella sticks" = ("tuna", "mozzarella >sticks", "Merlot") > | sidedish == "caviar"= ("tuna", "caviar", "Merlot") Sorry, I forgot to take care of list comprehension. I'm not quite sure how to extract elements of both lists at the same time in a nested guard. Here's a slight revision; would this work? module Wine where data Wine = Red | White data Red = "Merlot" data White = "Sauvignon Blanc" data Entree = "pork" | "chicken" | "tuna" data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] wine entrees sidedishes | entree <- entrees == "pork" = | sidedish <- sidedieshes == "garlic bread" = ("pork", "garlic bread", "Merlot") | sidedish <- sidedishes == "mozzarella sticks" = ("pork", "mozzarella sticks", "Merlot") | sidedish <- sidedishes == "caviar" = ("pork", "caviar", "Sauvignon Blanc") | entree <- entrees == "chicken" = | sidedish <- sidedieshes == "garlic bread" = ("chicken", "garlic bread", "Merlot") | sidedish <- sidedieshes == "mozzarella sticks" = ("chicken", "mozzarella sticks", "Sauvignon Blanc") | sidedish <- sidedieshes == "caviar"= ("chicken", "caviar", "Merlot") | entree <- entrees == "tuna" = | sidedish <- sidedieshes == "garlic bread" = ("tuna", "garlic bread", "Sauvignon Blanc") | sidedish <- sidedieshes == "mozzarella sticks" = ("tuna", "mozzarella sticks", "Merlot") | sidedish <- sidedieshes == "caviar"= ("tuna", "caviar", "Merlot") -- Benjamin L. Russell From DekuDekuplex at Yahoo.com Thu Oct 23 02:27:34 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 02:23:17 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <6e50g45rej6k6n4srag6rfh23i1hk9f4gs@4ax.com> Message-ID: On Thu, 23 Oct 2008 15:22:17 +0900, Benjamin L. Russell wrote: >Here's a slight revision; would this work? > >module Wine where > >data Wine = Red | White >data Red = "Merlot" >data White = "Sauvignon Blanc" >data Entree = "pork" | "chicken" | "tuna" >data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" > >wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] >wine entrees sidedishes > | entree <- entrees == "pork" = > | sidedish <- sidedieshes == "garlic bread" = ("pork", "garlic >bread", >"Merlot") > | sidedish <- sidedishes == "mozzarella sticks" = ("pork", >"mozzarella >sticks", "Merlot") > | sidedish <- sidedishes == "caviar" = ("pork", "caviar", >"Sauvignon Blanc") > | entree <- entrees == "chicken" = > | sidedish <- sidedieshes == "garlic bread" = ("chicken", >"garlic bread", >"Merlot") > | sidedish <- sidedieshes == "mozzarella sticks" = ("chicken", >"mozzarella >sticks", "Sauvignon Blanc") > | sidedish <- sidedieshes == "caviar"= ("chicken", "caviar", >"Merlot") > | entree <- entrees == "tuna" = > | sidedish <- sidedieshes == "garlic bread" = ("tuna", "garlic >bread", >"Sauvignon Blanc") > | sidedish <- sidedieshes == "mozzarella sticks" = ("tuna", >"mozzarella >sticks", "Merlot") > | sidedish <- sidedieshes == "caviar"= ("tuna", "caviar", >"Merlot") Sorry; my earlier revision contained some typos where "sidedishes" was misspelled as "sidedieshes." Here is a corrected version: module Wine where data Wine = Red | White data Red = "Merlot" data White = "Sauvignon Blanc" data Entree = "pork" | "chicken" | "tuna" data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] wine entrees sidedishes | entree <- entrees == "pork" = | sidedish <- sidedishes == "garlic bread" = ("pork", "garlic bread", "Merlot") | sidedish <- sidedishes == "mozzarella sticks" = ("pork", "mozzarella sticks", "Merlot") | sidedish <- sidedishes == "caviar" = ("pork", "caviar", "Sauvignon Blanc") | entree <- entrees == "chicken" = | sidedish <- sidedishes == "garlic bread" = ("chicken", "garlic bread", "Merlot") | sidedish <- sidedishes == "mozzarella sticks" = ("chicken", "mozzarella sticks", "Sauvignon Blanc") | sidedish <- sidedishes == "caviar"= ("chicken", "caviar", "Merlot") | entree <- entrees == "tuna" = | sidedish <- sidedishes == "garlic bread" = ("tuna", "garlic bread", "Sauvignon Blanc") | sidedish <- sidedishes == "mozzarella sticks" = ("tuna", "mozzarella sticks", "Merlot") | sidedish <- sidedishes == "caviar"= ("tuna", "caviar", "Merlot") -- Benjamin L. Russell From daniel.is.fischer at web.de Thu Oct 23 02:47:24 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 23 02:40:39 2008 Subject: [Haskell-beginners] how to define a user datatype consisting of instances of String? In-Reply-To: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> Message-ID: <200810230847.24152.daniel.is.fischer@web.de> Am Donnerstag, 23. Oktober 2008 08:07 schrieb Benjamin L.Russell: > How is it possible to define a user datatype consisting of instances > of String? > > Suppose I am preparing a party for wine connoisseurs, and want to > define the following data types: > > data Wine = Red | White > data Red = "Merlot" > data White = "Sauvignon Blanc" > data Entree = "pork" | "chicken" | "tuna" > data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" The syntax for data declarations does not allow such, you must have constructors applied to types. You could make it data Wine = Red Red | White White data Red = Merlot | Syrah data White = SauvignonBlanc | Riesling | PinotNoir data Entree = Pork | Chicken | Tuna deriving (Eq, Enum, Bounded) data SideDish = GarlicBread | MozzarellaSticks | Caviar deriving (Eq, Enum, Bounded) instance Show Wine where ... instance Show Red where ... ... > > Then, I want to write a Haskell function that takes two lists; e.g., > > ["pork", "chicken", "tuna"] > > and > > ["garlic bread", "mozzarella sticks", "caviar"] > > and that constructs a three-tuple of the following type: > > (Entree, SideDish, Wine) > > such that which Wine is chosen depends on the particular combination > of Entree and SideDish, with respect to the following rules: > > ("pork", "garlic bread") -> ("pork", "garlic bread", "Merlot") > ("pork", "mozzarella sticks") -> ("pork", "mozzarella sticks", > "Merlot") > ("pork", "caviar") -> ("pork", "Beluga", "Sauvignon Blanc") > ("chicken", "garlic bread") -> ("chicken", "garlic bread", "Merlot") > ("chicken", "mozzarella sticks") -> ("chicken", "mozzarella sticks", > "Sauvignon Blanc") > ("chicken", "caviar") -> ("chicken", "caviar", "Merlot") > ("tuna", "garlic bread") -> ("tuna", "garlic bread", "Sauvignon > Blanc") > ("tuna", "mozzarella sticks") -> ("tuna", "mozzarella sticks", > "Merlot") > ("tuna", "caviar") -> ("tuna", "caviar", "Merlot") selectWine :: Entree -> SideDish -> Wine selectWine Pork sd = case sd of Caviar -> White SauvignonBlanc _ -> Red Merlot selectWine Chicken sd = case sd of MozzarellaSticks -> White SauvignonBlanc _ -> Red Merlot selectWine Tuna sd = case sd of GarlicBread -> White SauvignonBlanc _ -> Red Merlot options :: [(Entree,SideDish,Wine)] options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- [minBound .. maxBound]] > > So far, I have written the following Haskell code: > > module Wine where > > data Wine = Red | White > data Red = "Merlot" > data White = "Sauvignon Blanc" > data Entree = "pork" | "chicken" | "tuna" > data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" > > wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] > wine entree sidedish > > | entree == "pork" = > | > | sidedish == "garlic bread" = ("pork", "garlic bread", > > "Merlot") > > | sidedish == "mozzarella sticks" = ("pork", "mozzarella > > sticks", "Merlot") > > | sidedish == "caviar" = ("pork", "caviar", "Sauvignon Blanc") > | > | entree == "chicken" = > | > | sidedish == "garlic bread" = ("chicken", "garlic bread", > > "Merlot") > > | sidedish == "mozzarella sticks" = ("chicken", "mozzarella > > sticks", "Sauvignon Blanc") > > | sidedish == "caviar"= ("chicken", "caviar", "Merlot") > | > | entree == "tuna" = > | > | sidedish == "garlic bread" = ("tuna", "garlic bread", > > "Sauvignon Blanc") > > | sidedish == "mozzarella sticks" = ("tuna", "mozzarella > > sticks", "Merlot") > > | sidedish == "caviar"= ("tuna", "caviar", "Merlot") > > However, when I load this code into GHCi, I get the following error > message: > > [1 of 1] Compiling Wine ( wine.hs, interpreted ) > > wine.hs:18:11: parse error on input `"' > Failed, modules loaded: none. > Prelude> > > I discovered the following seemingly relevant HaskellWiki page, but am > not sure about how to apply the advice in it to this problem, because > the advice does not seem sufficiently specific or detailed: > > List instance - HaskellWiki > http://www.haskell.org/haskellwiki/List_instance This isn't relevant here, you're not trying to create an instance of some typeclass for a list-type. > > What would be a good way to overcome this error? > > -- Benjamin L. Russell > See above, but I suggest rethinking your menu :) From DekuDekuplex at Yahoo.com Thu Oct 23 03:02:49 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 02:58:32 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <6e50g45rej6k6n4srag6rfh23i1hk9f4gs@4ax.com> Message-ID: On Thu, 23 Oct 2008 15:27:34 +0900, Benjamin L.Russell wrote: >module Wine where > >data Wine = Red | White >data Red = "Merlot" >data White = "Sauvignon Blanc" >data Entree = "pork" | "chicken" | "tuna" >data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" > >wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] >wine entrees sidedishes > | entree <- entrees == "pork" = > | sidedish <- sidedishes == "garlic bread" = ("pork", "garlic >bread", >"Merlot") > | sidedish <- sidedishes == "mozzarella sticks" = ("pork", >"mozzarella >sticks", "Merlot") > | sidedish <- sidedishes == "caviar" = ("pork", "caviar", >"Sauvignon Blanc") > | entree <- entrees == "chicken" = > | sidedish <- sidedishes == "garlic bread" = ("chicken", >"garlic bread", >"Merlot") > | sidedish <- sidedishes == "mozzarella sticks" = ("chicken", >"mozzarella >sticks", "Sauvignon Blanc") > | sidedish <- sidedishes == "caviar"= ("chicken", "caviar", >"Merlot") > | entree <- entrees == "tuna" = > | sidedish <- sidedishes == "garlic bread" = ("tuna", "garlic >bread", >"Sauvignon Blanc") > | sidedish <- sidedishes == "mozzarella sticks" = ("tuna", >"mozzarella >sticks", "Merlot") > | sidedish <- sidedishes == "caviar"= ("tuna", "caviar", >"Merlot") I just thought of a revision using the guards within an instance of unlines and map. This approach eliminates the need for the Wine, Red, and White datatypes. However, since two elements of type String need to be extracted at each step, I am not sure of the syntax. Would this approach a solution? module Wine where data Entree = "pork" | "chicken" | "tuna" data SideDish = "garlic bread" | "mozzarella sticks" | "caviar" wine :: Show a => [Entree] -> [SideDish] -> String wine entrees sidedishes = unlines (map entree entrees) (map sidedish sidedishes) where (entree Entree) (sidedish SideDish) = | entree == "pork" = | sidedish == "garlic bread" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Merlot)" | sidedish == "mozzarella sticks" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Merlot)" | sidedish == "caviar" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Sauvignon Blanc)" | entree == "chicken" = | sidedish == "garlic bread" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Merlot)" | sidedish == "mozzarella sticks" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Sauvignon Blanc)" | sidedish == "caviar"= "(" ++ show entree ++ ", " ++ show sidedish ++ ", Merlot)" | entree == "tuna" = | sidedish == "garlic bread" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Sauvignon Blanc)" | sidedish == "mozzarella sticks" = "(" ++ show entree ++ ", " ++ show sidedish ++ ", Merlot)" | sidedish == "caviar"= "(" ++ show entree ++ ", " ++ show sidedish ++ ", Merlot)" -- Benjamin L. Russell From joel.bjornson at gmail.com Thu Oct 23 04:23:29 2008 From: joel.bjornson at gmail.com (=?ISO-8859-1?Q?Joel_Bj=F6rnson?=) Date: Thu Oct 23 04:19:02 2008 Subject: [Haskell-beginners] A Pascal-like Language Compiler In-Reply-To: References: <658ffcc20810200647pf1f7235gedeaec988d4aa389@mail.gmail.com> Message-ID: <658ffcc20810230123k3a4d9957gb3ae0de7bdb31a5a@mail.gmail.com> On Thu, Oct 23, 2008 at 12:18 AM, Pranesh Srinivasan wrote: > > That seems like a very nice scheme to follow. I had a similar method in > mind. Step 3 is what I am really worried about. How easy/difficult will > it be in a pure func language, to "transform" the sytnax tree. I think the pureness is actually an advantage here, since the transformation is a function from one syntax tree to another. If however, you feel the need of keeping a state and you don't wanna pass around this explicitly, you may consider using a state monad. > I have to take a deeper look at BNFC. But from an initial look, it seems > way too powerful for me to use? At least as powerful as yacc. And that > with Haskell, should give me a very good toolset-advantage? If the project is about writing parsers then using BNFC would probably be considered cheating. But for practical purposes I think it's a great tool that would save you a lot of work. - Joel From DekuDekuplex at Yahoo.com Thu Oct 23 04:51:59 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 04:47:43 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <200810230847.24152.daniel.is.fischer@web.de> Message-ID: On Thu, 23 Oct 2008 08:47:24 +0200, Daniel Fischer wrote: >[...] > >The syntax for data declarations does not allow such, you must have >constructors applied to types. You could make it > >data Wine = Red Red | White White >data Red = Merlot | Syrah >data White = SauvignonBlanc | Riesling | PinotNoir >data Entree = Pork | Chicken | Tuna deriving (Eq, Enum, Bounded) >data SideDish = GarlicBread | MozzarellaSticks | Caviar deriving (Eq, Enum, >Bounded) > >instance Show Wine where ... >instance Show Red where ... >... > >[...] > >selectWine :: Entree -> SideDish -> Wine >selectWine Pork sd > = case sd of > Caviar -> White SauvignonBlanc > _ -> Red Merlot >selectWine Chicken sd > = case sd of > MozzarellaSticks -> White SauvignonBlanc > _ -> Red Merlot >selectWine Tuna sd > = case sd of > GarlicBread -> White SauvignonBlanc > _ -> Red Merlot > >options :: [(Entree,SideDish,Wine)] >options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- [minBound >.. maxBound]] > >[...] > >This isn't relevant here, you're not trying to create an instance of some >typeclass for a list-type. > >> >> What would be a good way to overcome this error? >> >> -- Benjamin L. Russell >> > >See above, but I suggest rethinking your menu :) After taking your advice into account, I have rewritten the wine.hs program as follows, but am unable to figure out what to write for the type constructor for Wine. In particular, I do not understand why the user type definition for Wine is "Red Red | White White" instead of just "Red | White." Should I fill out "instance Show Wine where ..." as something similar to instance Show Wine where show Red = ... show White = ... and then supply a conditional after "show Red = ..." and "show White = ...," or do I need to supply something else there? Here's my revised code so far (the "instance Show Wine where ..." part is unfinished): module Wine where data Wine = Red Red | White White data Red = Merlot | Syrah | Port data White = SauvignonBlanc | Riesling | PinotNoir data Entree = KobeBeef | LemonChicken | SteamedSalmon deriving (Eq, Enum, Bounded) data SideDish = ButteredPotatoes | BrieCheese | GreekSalad deriving (Eq, Enum, Bounded) instance Show Wine where ... instance Show Red where show Merlot = "Merlot" show Syrah = "Syrah" show Port = "Port" instance Show White where show SauvignonBlanc = "Sauvignon Blanc" show Riesling = "Riesling" show PinotNoir = "Pinot Noir" instance Show Entree where show KobeBeef = "Kobe Beef" show LemonChicken = "Lemon Chicken" show SteamedSalmon = "Steamed Salmon" instance Show SideDish where show ButteredPotatoes = "Buttered Potatoes" show BrieCheese = "Brie Cheese" show GreekSalad = "Greek Salad" selectWine :: Entree -> SideDish -> Wine selectWine KobeBeef sd = case sd of GreekSalad -> White SauvignonBlanc _ -> Red Merlot selectWine LemonChicken sd = case sd of BrieCheese -> White Riesling _ -> Red Syrah selectWine SteamedSalmon sd = case sd of ButteredPotatoes -> White PinotNoir _ -> Red Port options :: [(Entree,SideDish,Wine)] options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- [minBound .. maxBound]] -- Benjamin L. Russell From DekuDekuplex at Yahoo.com Thu Oct 23 05:39:50 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 05:35:36 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <200810230847.24152.daniel.is.fischer@web.de> Message-ID: On Thu, 23 Oct 2008 17:51:59 +0900, Benjamin L.Russell wrote: >[...] > >After taking your advice into account, I have rewritten the wine.hs >program as follows, but am unable to figure out what to write for the >type constructor for Wine. In particular, I do not understand why the >user type definition for Wine is "Red Red | White White" instead of >just "Red | White." > >Should I fill out "instance Show Wine where ..." as something similar >to > >instance Show Wine where > show Red = ... > show White = ... > >and then supply a conditional after "show Red = ..." and "show White = >...," or do I need to supply something else there? > >[...] After researching type classes somewhat, I discovered that in "data Wine = Red Red | White White," the second terms "Red" and "White" were arguments to their respective first terms of the same name. With this information, I then rewrote the type definition and type constructor for Wine as follows: data Wine = Red Red | White White instance Show Wine where show (Red Merlot) = "Merlot" show (Red Syrah) = "Syrah" show (Red Port) = "Port" show (White SauvignonBlanc) = "Sauvignon Blanc" show (White Riesling) = "Riesling" show (White PinotNoir) = "Pinot Noir" Here is my revised program, which seems to work fine this time: module Wine where data Wine = Red Red | White White data Red = Merlot | Syrah | Port data White = SauvignonBlanc | Riesling | PinotNoir data Entree = KobeBeef | LemonChicken | SteamedSalmon deriving (Eq, Enum, Bounded) data SideDish = ButteredPotatoes | BrieCheese | GreekSalad deriving (Eq, Enum, Bounded) instance Show Wine where show (Red Merlot) = "Merlot" show (Red Syrah) = "Syrah" show (Red Port) = "Port" show (White SauvignonBlanc) = "Sauvignon Blanc" show (White Riesling) = "Riesling" show (White PinotNoir) = "Pinot Noir" instance Show Red where show Merlot = "Merlot" show Syrah = "Syrah" show Port = "Port" instance Show White where show SauvignonBlanc = "Sauvignon Blanc" show Riesling = "Riesling" show PinotNoir = "Pinot Noir" instance Show Entree where show KobeBeef = "Kobe Beef" show LemonChicken = "Lemon Chicken" show SteamedSalmon = "Steamed Salmon" instance Show SideDish where show ButteredPotatoes = "Buttered Potatoes" show BrieCheese = "Brie Cheese" show GreekSalad = "Greek Salad" selectWine :: Entree -> SideDish -> Wine selectWine KobeBeef sd = case sd of GreekSalad -> White SauvignonBlanc _ -> Red Merlot selectWine LemonChicken sd = case sd of BrieCheese -> White Riesling _ -> Red Syrah selectWine SteamedSalmon sd = case sd of ButteredPotatoes -> White PinotNoir _ -> Red Port options :: [(Entree,SideDish,Wine)] options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- [minBound .. maxBound]] When I run it in GHCi (with my Haskell program file renamed as wine2.hs), these are the results: Prelude> :l wine2.hs [1 of 1] Compiling Wine ( wine2.hs, interpreted ) Ok, modules loaded: Wine. *Wine> options [(Kobe Beef,Buttered Potatoes,Merlot),(Kobe Beef,Brie Cheese,Merlot),(Kobe Beef, Greek Salad,Sauvignon Blanc),(Lemon Chicken,Buttered Potatoes,Syrah),(Lemon Chic ken,Brie Cheese,Riesling),(Lemon Chicken,Greek Salad,Syrah),(Steamed Salmon,Butt ered Potatoes,Pinot Noir),(Steamed Salmon,Brie Cheese,Port),(Steamed Salmon,Gree k Salad,Port)] *Wine> These results seem correct. Thank you for your help! -- Benjamin L. Russell From daniel.is.fischer at web.de Thu Oct 23 05:48:46 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 23 05:42:02 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? In-Reply-To: References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <200810230847.24152.daniel.is.fischer@web.de> Message-ID: <200810231148.46613.daniel.is.fischer@web.de> Am Donnerstag, 23. Oktober 2008 10:51 schrieb Benjamin L.Russell: > After taking your advice into account, I have rewritten the wine.hs > program as follows, but am unable to figure out what to write for the > type constructor for Wine. In particular, I do not understand why the > user type definition for Wine is "Red Red | White White" instead of > just "Red | White." Okay, that was a bit mean, but not intentionally. In GADT syntax: data Wine where Red :: Red -> Wine White :: White -> Wine So there is a dataconstructor called Red and also a datatype (I was tempted to write typeconstructor) called Red and the dataconstructor Red takes an argument of type Red, similarly for White. The type Wine is isomorphic to Either Red White, but since that wouldn't give a good Show instance, we roll our own type. If we had just data Wine = Red | White , the type Wine would have just the two members Red and White (and _|_) and not have any connection with the types Red and White which give the details (grape/origin). We need a means to transform a value of type Red into a value of type Wine, that is a function of type Red -> Wine, the application demands that that function doesn't lose information, that is best achieved by a dataconstructor of type Red -> Wine. Would probably have been less confusing if I called the dataconstructors RedWine resp. WhiteWine. > > Should I fill out "instance Show Wine where ..." as something similar > to > > instance Show Wine where > show Red = ... > show White = ... > > and then supply a conditional after "show Red = ..." and "show White = > ...," or do I need to supply something else there? You have basically two choices, you can say everybody knows which colour the wines have and use instance Show Wine where show (Red w) = show w show (White w) = show w or include the colour in the rendered String, for example via deriving Show, or by declaring your own instance (if you want White PinotNoir rendered as "White (Pinot Noir)", you would have to do that or write a more complicated Show instance for White). If you want to parse values of type Wine in the future, it will be slightly easier with the dataconstructors included. > > Here's my revised code so far (the "instance Show Wine where ..." part > is unfinished): > > module Wine where > > data Wine = Red Red | White White > data Red = Merlot | Syrah | Port > data White = SauvignonBlanc | Riesling | PinotNoir > data Entree = KobeBeef | LemonChicken | SteamedSalmon deriving (Eq, > Enum, Bounded) > data SideDish = ButteredPotatoes | BrieCheese | GreekSalad deriving > (Eq, Enum, Bounded) > > instance Show Wine where ... > > instance Show Red where > show Merlot = "Merlot" > show Syrah = "Syrah" > show Port = "Port" > > instance Show White where > show SauvignonBlanc = "Sauvignon Blanc" > show Riesling = "Riesling" > show PinotNoir = "Pinot Noir" > > instance Show Entree where > show KobeBeef = "Kobe Beef" > show LemonChicken = "Lemon Chicken" > show SteamedSalmon = "Steamed Salmon" > > instance Show SideDish where > show ButteredPotatoes = "Buttered Potatoes" > show BrieCheese = "Brie Cheese" > show GreekSalad = "Greek Salad" > > selectWine :: Entree -> SideDish -> Wine > selectWine KobeBeef sd > = case sd of > GreekSalad -> White SauvignonBlanc > _ -> Red Merlot > selectWine LemonChicken sd > = case sd of > BrieCheese -> White Riesling > _ -> Red Syrah > selectWine SteamedSalmon sd > = case sd of > ButteredPotatoes -> White PinotNoir > _ -> Red Port > > options :: [(Entree,SideDish,Wine)] > options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- > [minBound .. maxBound]] > > -- Benjamin L. Russell > Bon Appetit, Daniel P.S.: Could I have a Syrah with my beef and potatoes? From DekuDekuplex at Yahoo.com Thu Oct 23 05:48:58 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 05:44:42 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <200810230847.24152.daniel.is.fischer@web.de> Message-ID: On Thu, 23 Oct 2008 18:39:52 +0900, Benjamin L. Russell wrote: >instance Show Wine where > show (Red Merlot) = "Merlot" > show (Red Syrah) = "Syrah" > show (Red Port) = "Port" > show (White SauvignonBlanc) = "Sauvignon Blanc" > show (White Riesling) = "Riesling" > show (White PinotNoir) = "Pinot Noir" > >instance Show Red where > show Merlot = "Merlot" > show Syrah = "Syrah" > show Port = "Port" > >instance Show White where > show SauvignonBlanc = "Sauvignon Blanc" > show Riesling = "Riesling" > show PinotNoir = "Pinot Noir" The only remaining issue is whether there is a way to define the type constructor for Wine without pre-defining parts that are later defined in the type constructors for Red and White. In the above code, for example, >instance Show Wine where > show (Red Merlot) = "Merlot" essentially pre-defines the following definition later given in the type constructor for Red: >instance Show Red where > show Merlot = "Merlot" This seems redundant. Since the type definition for Wine is data Wine = Red Red | White White the type constructors for Red and White both require an argument, so show (Red Merlot) = ... seems reasonable. This would seem to imply that if I need to reduce redundancy, I should probably rewrite the RHS of the above line. Is there a way to refer to the type constructors for Red and White in the type constructor for Wine? -- Benjamin L. Russell From DekuDekuplex at Yahoo.com Thu Oct 23 06:19:32 2008 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Thu Oct 23 06:15:23 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <200810230847.24152.daniel.is.fischer@web.de> <200810231148.46613.daniel.is.fischer@web.de> Message-ID: On Thu, 23 Oct 2008 11:48:46 +0200, Daniel Fischer wrote: >Am Donnerstag, 23. Oktober 2008 10:51 schrieb Benjamin L.Russell: >> After taking your advice into account, I have rewritten the wine.hs >> program as follows, but am unable to figure out what to write for the >> type constructor for Wine. In particular, I do not understand why the >> user type definition for Wine is "Red Red | White White" instead of >> just "Red | White." > >Okay, that was a bit mean, but not intentionally. > >In GADT syntax: >data Wine where > Red :: Red -> Wine > White :: White -> Wine > >So there is a dataconstructor called Red and also a datatype (I was tempted to >write typeconstructor) called Red and the dataconstructor Red takes an >argument of type Red, similarly for White. >The type Wine is isomorphic to Either Red White, but since that wouldn't give >a good Show instance, we roll our own type. > >If we had just >data Wine = Red | White >, the type Wine would have just the two members Red and White (and _|_) and >not have any connection with the types Red and White which give the details >(grape/origin). We need a means to transform a value of type Red into a value >of type Wine, that is a function of type Red -> Wine, the application demands >that that function doesn't lose information, that is best achieved by a >dataconstructor of type Red -> Wine. >Would probably have been less confusing if I called the dataconstructors >RedWine resp. WhiteWine. Thank you. That makes the Wine much clearer ;-) . >>[...] > >You have basically two choices, you can say everybody knows which colour the >wines have and use > >instance Show Wine where > show (Red w) = show w > show (White w) = show w > >or include the colour in the rendered String, for example via deriving Show, >or by declaring your own instance (if you want White PinotNoir rendered as >"White (Pinot Noir)", you would have to do that or write a more complicated >Show instance for White). >If you want to parse values of type Wine in the future, it will be slightly >easier with the dataconstructors included. So perhaps I should rewrite the type definitions for Red and White as follows? data Red = Syrah | Merlot | Port deriving (Eq, Show) data White = SauvignonBlanc | Riesling | PinotNoir deriving (Eq, Show) Alternatively (adapted from an example provided by Dirk Thierbach in the thread "seeking helpful links," dated "Tue, 21 Oct 2008 16:33:41 -0700 (PDT)," on comp.lang.haskell), perhaps the following? type Entree = String type SideDish = String type Wine = String wine :: Entree -> SideDish -> Wine wine "Kobe Beef" "Buttered Potatoes" = "Syrah" wine "Kobe Beef" "Brie Cheese" = "Syrah" wine "Kobe Beef" "Greek Salad" = "Sauvignon Blanc" wine "Lemon Chicken" "Buttered Potatoes" = "Merlot" wine "Lemon Chicken" "Brie Cheese" = "Sauvignon Blanc" wine "Lemon Chicken" "Greek Salad" = "Merlot" wine "Steamed Salmon" "Buttered Potatoes" = "Sauvignon Blanc" wine "Steamed Salmon" "Brie Cheese" = "Port" wine "Steamed Salmon" "Greek Salad" = "Port" wines :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] wines entrees sideDishes = [(e, s, wine e s) | e <- entrees, s <- sideDishes] >>[...] > >Bon Appetit, >Daniel > >P.S.: Could I have a Syrah with my beef and potatoes? Certainly; here is your Syrah with your beef and potatoes. Bon appetit ;-) : module Wine where data Wine = Red Red | White White data Red = Syrah | Merlot | Port data White = SauvignonBlanc | Riesling | PinotNoir data Entree = KobeBeef | LemonChicken | SteamedSalmon deriving (Eq, Enum, Bounded) data SideDish = ButteredPotatoes | BrieCheese | GreekSalad deriving (Eq, Enum, Bounded) instance Show Wine where show (Red w) = show w show (White w) = show w instance Show Red where show Syrah = "Syrah" show Merlot = "Merlot" show Port = "Port" instance Show White where show SauvignonBlanc = "Sauvignon Blanc" show Riesling = "Riesling" show PinotNoir = "Pinot Noir" instance Show Entree where show KobeBeef = "Kobe Beef" show LemonChicken = "Lemon Chicken" show SteamedSalmon = "Steamed Salmon" instance Show SideDish where show ButteredPotatoes = "Buttered Potatoes" show BrieCheese = "Brie Cheese" show GreekSalad = "Greek Salad" selectWine :: Entree -> SideDish -> Wine selectWine KobeBeef sd = case sd of GreekSalad -> White SauvignonBlanc _ -> Red Syrah selectWine LemonChicken sd = case sd of BrieCheese -> White Riesling _ -> Red Merlot selectWine SteamedSalmon sd = case sd of ButteredPotatoes -> White PinotNoir _ -> Red Port options :: [(Entree,SideDish,Wine)] options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- [minBound .. maxBound]] Here's your new menu: *Wine> :l wine2.hs [1 of 1] Compiling Wine ( wine2.hs, interpreted ) Ok, modules loaded: Wine. *Wine> options [(Kobe Beef,Buttered Potatoes,Syrah),(Kobe Beef,Brie Cheese,Syrah),(Kobe Beef,Gr eek Salad,Sauvignon Blanc),(Lemon Chicken,Buttered Potatoes,Merlot),(Lemon Chick en,Brie Cheese,Riesling),(Lemon Chicken,Greek Salad,Merlot),(Steamed Salmon,Butt ered Potatoes,Pinot Noir),(Steamed Salmon,Brie Cheese,Port),(Steamed Salmon,Gree k Salad,Port)] *Wine> -- Benjamin L. Russell From daniel.is.fischer at web.de Thu Oct 23 06:24:00 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 23 06:17:14 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? In-Reply-To: References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> Message-ID: <200810231224.00328.daniel.is.fischer@web.de> Am Donnerstag, 23. Oktober 2008 11:48 schrieb Benjamin L.Russell: > On Thu, 23 Oct 2008 18:39:52 +0900, Benjamin L. Russell > > wrote: > >instance Show Wine where > > show (Red Merlot) = "Merlot" > > show (Red Syrah) = "Syrah" > > show (Red Port) = "Port" > > show (White SauvignonBlanc) = "Sauvignon Blanc" > > show (White Riesling) = "Riesling" > > show (White PinotNoir) = "Pinot Noir" > > > >instance Show Red where > > show Merlot = "Merlot" > > show Syrah = "Syrah" > > show Port = "Port" > > > >instance Show White where > > show SauvignonBlanc = "Sauvignon Blanc" > > show Riesling = "Riesling" > > show PinotNoir = "Pinot Noir" > > The only remaining issue is whether there is a way to define the type > constructor for Wine without pre-defining parts that are later defined > in the type constructors for Red and White. You could make it type Grape = String type Colour = String data Wine = Wine { grape :: Grape, colour :: Colour } but you'd face the possibility of (Wine "" "petillant") - bad. To have only real wines, you have to do it more or less as it is or leave out the Red and White types and make Wine an enumeration without colour tag. > > In the above code, for example, > > >instance Show Wine where > > show (Red Merlot) = "Merlot" > > essentially pre-defines the following definition later given in the > > type constructor for Red: > >instance Show Red where > > show Merlot = "Merlot" with the below Show instance of Wine, instance Show Red where show = show . Red instance Show White where show = show . White would save a bit of typing. But the other way round, instance Show Wine where show (Red w) = show w show (White w) = show w seems cleaner. > > This seems redundant. > > Since the type definition for Wine is > > data Wine = Red Red | White White > > the type constructors for Red and White both require an argument, so > > show (Red Merlot) = ... > > seems reasonable. > > This would seem to imply that if I need to reduce redundancy, I should > probably rewrite the RHS of the above line. > > Is there a way to refer to the type constructors for Red and White in > the type constructor for Wine? I don't understand what you mean, certainly not data Wine a = W a and W Merlot :: Wine Red ? > > -- Benjamin L. Russell > Cheers, Daniel From daniel.is.fischer at web.de Thu Oct 23 06:40:29 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 23 06:33:58 2008 Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? In-Reply-To: References: <8p40g497op7q1rebib3ki2oft42is1e2h8@4ax.com> <200810231148.46613.daniel.is.fischer@web.de> Message-ID: <200810231240.29261.daniel.is.fischer@web.de> Am Donnerstag, 23. Oktober 2008 12:19 schrieb Benjamin L.Russell: > > > >You have basically two choices, you can say everybody knows which colour > > the wines have and use > > > >instance Show Wine where > > show (Red w) = show w > > show (White w) = show w > > > >or include the colour in the rendered String, for example via deriving > > Show, or by declaring your own instance (if you want White PinotNoir > > rendered as "White (Pinot Noir)", you would have to do that or write a > > more complicated Show instance for White). > >If you want to parse values of type Wine in the future, it will be > > slightly easier with the dataconstructors included. > > So perhaps I should rewrite the type definitions for Red and White as > follows? > > data Red = Syrah | Merlot | Port deriving (Eq, Show) > data White = SauvignonBlanc | Riesling | PinotNoir deriving (Eq, Show) That would be the easiest, but of course having the two Whites displayed with a space is aesthetically superior. > > Alternatively (adapted from an example provided by Dirk Thierbach in > the thread "seeking helpful links," dated "Tue, 21 Oct 2008 16:33:41 > -0700 (PDT)," on comp.lang.haskell), perhaps the following? > > type Entree = String > type SideDish = String > type Wine = String > > wine :: Entree -> SideDish -> Wine > wine "Kobe Beef" "Buttered Potatoes" = "Syrah" > wine "Kobe Beef" "Brie Cheese" = "Syrah" > wine "Kobe Beef" "Greek Salad" = "Sauvignon Blanc" > wine "Lemon Chicken" "Buttered Potatoes" = "Merlot" > wine "Lemon Chicken" "Brie Cheese" = "Sauvignon Blanc" > wine "Lemon Chicken" "Greek Salad" = "Merlot" > wine "Steamed Salmon" "Buttered Potatoes" = "Sauvignon > Blanc" > wine "Steamed Salmon" "Brie Cheese" = "Port" > wine "Steamed Salmon" "Greek Salad" = "Port" > > wines :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)] > wines entrees sideDishes = [(e, s, wine e s) | e <- entrees, s <- > sideDishes] The downside of that is that a typo in the entrees or sideDishes will explode with a pattern match failure. Otherwise it's concise and clean. > > >>[...] > > > >Bon Appetit, > >Daniel > > > >P.S.: Could I have a Syrah with my beef and potatoes? > > Certainly; here is your Syrah with your beef and potatoes. Bon > appetit ;-) : > Merci, c'?tait d?licieux. > > -- Benjamin L. Russell > From cppljevans at suddenlink.net Thu Oct 23 09:37:51 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Thu Oct 23 09:29:17 2008 Subject: [Haskell-beginners] A Pascal-like Language Compiler In-Reply-To: <658ffcc20810230123k3a4d9957gb3ae0de7bdb31a5a@mail.gmail.com> References: <658ffcc20810200647pf1f7235gedeaec988d4aa389@mail.gmail.com> <658ffcc20810230123k3a4d9957gb3ae0de7bdb31a5a@mail.gmail.com> Message-ID: <49007E2F.4030900@suddenlink.net> On 10/23/08 03:23, Joel Bj?rnson wrote: > On Thu, Oct 23, 2008 at 12:18 AM, Pranesh Srinivasan wrote: > [snip] >> That seems like a very nice scheme to follow. I had a similar method in >> mind. Step 3 is what I am really worried about. How easy/difficult will >> it be in a pure func language, to "transform" the sytnax tree. >> > > I think the pureness is actually an advantage here, since the > transformation is a function from one syntax tree to another. If > however, you feel the need of keeping a state and you don't wanna pass > around this explicitly, you may consider using a state monad. > > >> I have to take a deeper look at BNFC. But from an initial look, it seems >> way too powerful for me to use? At least as powerful as yacc. And that >> with Haskell, should give me a very good toolset-advantage? >> > > If the project is about writing parsers then using BNFC would probably > be considered cheating. But for practical purposes I think it's a > great tool that would save you a lot of work. > > - Joel > HI Joel. Does BNFC calculate the lookahead sets for a grammar? I'm attempting to do this with haskell, but I would hate to "reinvent the wheel". I would think if haskell can tranform a syntax tree, then haskell could transform a grammar tree to calculate the lookahead sets. My purpose it not so much create a compiler as illustrate how it's done. I found the Dragon's book description hard to follow and thought redoing it using functors (to transform the tree into another, with different operators) then simplify the transformed trees (one tree for each lookahead set) then find the fixpoint of those trees (really forest: 1 tree for each production) would be a good way to demonstrate the method. -Larry -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081023/88ec8c43/attachment.htm From andreas.bernstein at medien.uni-weimar.de Thu Oct 23 09:56:03 2008 From: andreas.bernstein at medien.uni-weimar.de (Andreas-Christoph Bernstein) Date: Thu Oct 23 09:51:34 2008 Subject: [Haskell-beginners] pattern for tree traversel with a state Message-ID: <49008273.6050804@medien.uni-weimar.de> Hi, Is there a pattern for tree traversal with a state ? I am developing a small scenegraph represented by a tree. To draw a scenegraph one traverses over the graph starting with a global state. Inner Nodes can overwrite the inherited state for their subtree (e.g. Transformations are accumulated). The accumulated state is then either used immediately to draw the geometry in the leaf nodes, or a secondary data structure is build. This secondary data structure (a list or a tree) can then be sorted for optimal drawing performance. So i want to do the second and create a list of all leaves with the accumulated global state. To illustrate my problem i appended some code. The idea similar applies to a scenegraph. So my Question is: Is there allready a pattern for traversal with a state ? > module Main > where produces: Fork (0,"a") (Fork (1,"a") (Leaf (2,"a")) (Leaf (1,"a"))) (Leaf (0,"a")) > newTree :: BTree State > newTree = traverse modState globalState sampleTree produces: [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] > stateList = flattenTree newTree > flattenTree (Leaf x) = [x] > flattenTree (Fork x l r) = [x] ++ flattenTree l ++ flattenTree r > type State = (Int, String) > > globalState :: State > globalState = (0, "a") State modifiers > data StateMod > = ModInt > | ModString > | ModNop > deriving Show > modState :: StateMod -> State -> State > modState ModInt (x,w) = (x+1,w) > modState ModNop s = s > modState ModString (x,w) = (x,'b':w) > data BTree a = Fork a (BTree a) (BTree a) > | Leaf a > deriving Show traverses the tree and executes a function which modifies the current state depending on the statemodifier > traverse :: (a -> b -> b) -> b -> BTree a -> BTree b > traverse f state (Leaf x) = Leaf (f x state) > traverse f state (Fork x l r) = > Fork (f x state) newLeft newRight > where newLeft = traverse f (f x state) l > newRight = traverse f (f x state) r an example tree > sampleTree :: BTree StateMod > sampleTree = Fork ModNop > (Fork ModInt (Leaf ModInt) (Leaf ModNop)) > (Leaf ModNop) creates a list from a tree > flattenTree (Leaf x) = [x] > flattenTree (Fork x l r) = [x] ++ flattenTree l ++ flattenTree r Thanks for any help and ideas Andreas From cmb21 at kent.ac.uk Thu Oct 23 10:02:58 2008 From: cmb21 at kent.ac.uk (C.M.Brown) Date: Thu Oct 23 09:58:30 2008 Subject: [Haskell-beginners] pattern for tree traversel with a state In-Reply-To: <49008273.6050804@medien.uni-weimar.de> References: <49008273.6050804@medien.uni-weimar.de> Message-ID: Andreas-Christoph, I'm afraid I can't help answering your question, but I was wondering what you were using to create your scene graph? I'm currently having to use OpenSceneGraph in C++, and would be grateful if you knew of some kind of Haskell wrapper for this? Kind regards, Chris. On Thu, 23 Oct 2008, Andreas-Christoph Bernstein wrote: > Hi, > > Is there a pattern for tree traversal with a state ? > > I am developing a small scenegraph represented by a tree. To draw a > scenegraph one traverses over the graph starting with a global state. > Inner Nodes can overwrite the inherited state for their subtree (e.g. > Transformations are accumulated). The accumulated state is then either > used immediately to draw the geometry in the leaf nodes, or a secondary > data structure is build. This secondary data structure (a list or a > tree) can then be sorted for optimal drawing performance. > > So i want to do the second and create a list of all leaves with the > accumulated global state. To illustrate my problem i appended some code. > The idea similar applies to a scenegraph. > > So my Question is: Is there allready a pattern for traversal with a state ? > > > module Main > > where > > produces: Fork (0,"a") (Fork (1,"a") (Leaf (2,"a")) (Leaf (1,"a"))) > (Leaf (0,"a")) > > > newTree :: BTree State > > newTree = traverse modState globalState sampleTree > > produces: [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] > > > stateList = flattenTree newTree > > > flattenTree (Leaf x) = [x] > > flattenTree (Fork x l r) = [x] ++ flattenTree l ++ flattenTree r > > > type State = (Int, String) > > > > globalState :: State > > globalState = (0, "a") > > State modifiers > > > data StateMod > > = ModInt > > | ModString > > | ModNop > > deriving Show > > > modState :: StateMod -> State -> State > > modState ModInt (x,w) = (x+1,w) > > modState ModNop s = s > > modState ModString (x,w) = (x,'b':w) > > > data BTree a = Fork a (BTree a) (BTree a) > > | Leaf a > > deriving Show > > traverses the tree and executes a function which modifies the current > state depending on the statemodifier > > > traverse :: (a -> b -> b) -> b -> BTree a -> BTree b > > traverse f state (Leaf x) = Leaf (f x state) > > traverse f state (Fork x l r) = > > Fork (f x state) newLeft newRight > > where newLeft = traverse f (f x state) l > > newRight = traverse f (f x state) r > > an example tree > > > sampleTree :: BTree StateMod > > sampleTree = Fork ModNop > > (Fork ModInt (Leaf ModInt) (Leaf ModNop)) > > (Leaf ModNop) > > creates a list from a tree > > > flattenTree (Leaf x) = [x] > > flattenTree (Fork x l r) = [x] ++ flattenTree l ++ flattenTree r > > Thanks for any help and ideas > > Andreas > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From joel.bjornson at gmail.com Thu Oct 23 11:35:46 2008 From: joel.bjornson at gmail.com (=?ISO-8859-1?Q?Joel_Bj=F6rnson?=) Date: Thu Oct 23 11:31:17 2008 Subject: [Haskell-beginners] A Pascal-like Language Compiler In-Reply-To: <49007E2F.4030900@suddenlink.net> References: <658ffcc20810200647pf1f7235gedeaec988d4aa389@mail.gmail.com> <658ffcc20810230123k3a4d9957gb3ae0de7bdb31a5a@mail.gmail.com> <49007E2F.4030900@suddenlink.net> Message-ID: <658ffcc20810230835r16de6e96uf41ae636ed2db09b@mail.gmail.com> Hi, On Thu, Oct 23, 2008 at 3:37 PM, Larry Evans wrote: > Does BNFC calculate the lookahead sets for a grammar? > I'm attempting to do this with haskell, but I would hate > to "reinvent the wheel". I would think if haskell can tranform > a syntax tree, then haskell could transform a grammar tree > to calculate the lookahead sets. My purpose it not so much > create a compiler as illustrate how it's done. I found the > Dragon's book description hard to follow and thought > redoing it using functors (to transform the tree into > another, with different operators) then simplify the > transformed trees (one tree for each lookahead set) > then find the fixpoint of those trees (really forest: > 1 tree for each production) would be a good way > to demonstrate the method. As stated at the BNFC web page, "BNFC is a compiler compiler compiler", i.e. it generates specifications for other lexers and parsers based on the given grammar (expressed in Backus Nour Form). In Haskell mode it outputs specifications for Alex[1] and Happy[2] which in turn can be used to generate haskell code for lexing and parsing. So if I understood you correctly, I think the answer to your question is no. But maybe you wanna have a look the haskell parser code generated by Happy? Unfortunately I really don't know anything about the implementations of either BNFC or Happy, so I'm sure others may provide you with more insightful explanations. - Joel [1] http://www.haskell.org/alex/ [2] http://www.haskell.org/happy/ From apfelmus at quantentunnel.de Thu Oct 23 12:39:02 2008 From: apfelmus at quantentunnel.de (apfelmus) Date: Thu Oct 23 12:34:42 2008 Subject: [Haskell-beginners] Re: pattern for tree traversel with a state In-Reply-To: <49008273.6050804@medien.uni-weimar.de> References: <49008273.6050804@medien.uni-weimar.de> Message-ID: Andreas-Christoph Bernstein wrote: > > Is there a pattern for tree traversal with a state ? > > I am developing a small scenegraph represented by a tree. To draw a > scenegraph one traverses over the graph starting with a global state. > Inner Nodes can overwrite the inherited state for their subtree (e.g. > Transformations are accumulated). The accumulated state is then either > used immediately to draw the geometry in the leaf nodes, or a secondary > data structure is build. This secondary data structure (a list or a > tree) can then be sorted for optimal drawing performance. > > So i want to do the second and create a list of all leaves with the > accumulated global state. To illustrate my problem i appended some code. > The idea similar applies to a scenegraph. > > So my Question is: Is there already a pattern for traversal with a state ? Yes. I'm not sure whether state is really necessary for your problem, i.e. whether there is a more elegant formulation, but your algorithm fits a well-known pattern, namely the one in Data.Traversable import Data.Traversable import Data.Foldable import qualified Control.Monad.State data BTree a = Fork a (BTree a) (BTree a) | Leaf a deriving Show -- main functionality instance Traversable BTree where traverse f (Leaf x) = Leaf <$> f x traverse f (Fork x l r) = Fork <$> f x <*> traverse f l <*> traverse f r -- derived examples instance Foldable BTree where foldMap = foldMapDefault instance Functor BTree where fmap = fmapDefault flattenTree = toList -- state example data StateMod = ModInt | ModString | ModNop deriving Show type State = (Int, String) modState :: StateMod -> State -> State modState ModInt (x,w) = (x+1,w) modState ModNop s = s modState ModString (x,w) = (x,'b':w) startState = (0,"a") newTree :: BTree StateMod -> BTree State newTree = flip evalState startState . Data.Traversable.mapM (modify' . modState) where modify' f = Control.Monad.State.modify f >> Control.Monad.State.get Regards, apfelmus From andreas.bernstein at medien.uni-weimar.de Thu Oct 23 13:35:01 2008 From: andreas.bernstein at medien.uni-weimar.de (Andreas-Christoph Bernstein) Date: Thu Oct 23 13:30:33 2008 Subject: [Haskell-beginners] pattern for tree traversel with a state In-Reply-To: References: <49008273.6050804@medien.uni-weimar.de> Message-ID: <4900B5C5.3050508@medien.uni-weimar.de> hi, well i dont know about any haskell wrapper for openscenegraph. There are some python wrappers. (pyosg, avango). What i want to do is to use haskell for some simple graphics hacking, demo effects and to test ideas. Kind regards, Andreas C.M.Brown wrote: > Andreas-Christoph, > > I'm afraid I can't help answering your question, but I was wondering what > you were using to create your scene graph? I'm currently having to use > OpenSceneGraph in C++, and would be grateful if you knew of some kind of > Haskell wrapper for this? > > Kind regards, > Chris. > > > On Thu, 23 Oct 2008, Andreas-Christoph Bernstein wrote: > > >> Hi, >> >> Is there a pattern for tree traversal with a state ? >> >> I am developing a small scenegraph represented by a tree. To draw a >> scenegraph one traverses over the graph starting with a global state. >> Inner Nodes can overwrite the inherited state for their subtree (e.g. >> Transformations are accumulated). The accumulated state is then either >> used immediately to draw the geometry in the leaf nodes, or a secondary >> data structure is build. This secondary data structure (a list or a >> tree) can then be sorted for optimal drawing performance. >> >> So i want to do the second and create a list of all leaves with the >> accumulated global state. To illustrate my problem i appended some code. >> The idea similar applies to a scenegraph. >> >> So my Question is: Is there allready a pattern for traversal with a state ? >> >> > module Main >> > where >> >> produces: Fork (0,"a") (Fork (1,"a") (Leaf (2,"a")) (Leaf (1,"a"))) >> (Leaf (0,"a")) >> >> > newTree :: BTree State >> > newTree = traverse modState globalState sampleTree >> >> produces: [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] >> >> > stateList = flattenTree newTree >> >> > flattenTree (Leaf x) = [x] >> > flattenTree (Fork x l r) = [x] ++ flattenTree l ++ flattenTree r >> >> > type State = (Int, String) >> > >> > globalState :: State >> > globalState = (0, "a") >> >> State modifiers >> >> > data StateMod >> > = ModInt >> > | ModString >> > | ModNop >> > deriving Show >> >> > modState :: StateMod -> State -> State >> > modState ModInt (x,w) = (x+1,w) >> > modState ModNop s = s >> > modState ModString (x,w) = (x,'b':w) >> >> > data BTree a = Fork a (BTree a) (BTree a) >> > | Leaf a >> > deriving Show >> >> traverses the tree and executes a function which modifies the current >> state depending on the statemodifier >> >> > traverse :: (a -> b -> b) -> b -> BTree a -> BTree b >> > traverse f state (Leaf x) = Leaf (f x state) >> > traverse f state (Fork x l r) = >> > Fork (f x state) newLeft newRight >> > where newLeft = traverse f (f x state) l >> > newRight = traverse f (f x state) r >> >> an example tree >> >> > sampleTree :: BTree StateMod >> > sampleTree = Fork ModNop >> > (Fork ModInt (Leaf ModInt) (Leaf ModNop)) >> > (Leaf ModNop) >> >> creates a list from a tree >> >> > flattenTree (Leaf x) = [x] >> > flattenTree (Fork x l r) = [x] ++ flattenTree l ++ flattenTree r >> >> Thanks for any help and ideas >> >> Andreas >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> From andreas.bernstein at medien.uni-weimar.de Thu Oct 23 13:38:10 2008 From: andreas.bernstein at medien.uni-weimar.de (Andreas-Christoph Bernstein) Date: Thu Oct 23 13:33:41 2008 Subject: [Haskell-beginners] Re: pattern for tree traversel with a state In-Reply-To: References: <49008273.6050804@medien.uni-weimar.de> Message-ID: <4900B682.8020409@medien.uni-weimar.de> apfelmus wrote: > Andreas-Christoph Bernstein wrote: > >> Is there a pattern for tree traversal with a state ? >> >> I am developing a small scenegraph represented by a tree. To draw a >> scenegraph one traverses over the graph starting with a global state. >> Inner Nodes can overwrite the inherited state for their subtree (e.g. >> Transformations are accumulated). The accumulated state is then either >> used immediately to draw the geometry in the leaf nodes, or a secondary >> data structure is build. This secondary data structure (a list or a >> tree) can then be sorted for optimal drawing performance. >> >> So i want to do the second and create a list of all leaves with the >> accumulated global state. To illustrate my problem i appended some code. >> The idea similar applies to a scenegraph. >> >> So my Question is: Is there already a pattern for traversal with a >> state ? >> > > Yes. I'm not sure whether state is really necessary for your problem, > i.e. whether there is a more elegant formulation, but your algorithm > fits a well-known pattern, namely the one in Data.Traversable > > import Data.Traversable > import Data.Foldable > > import qualified Control.Monad.State > > > data BTree a = Fork a (BTree a) (BTree a) | Leaf a deriving Show > > -- main functionality > instance Traversable BTree where > traverse f (Leaf x) = Leaf <$> f x > traverse f (Fork x l r) = Fork <$> > f x <*> traverse f l <*> traverse f r > > -- derived examples > instance Foldable BTree where > foldMap = foldMapDefault > instance Functor BTree where > fmap = fmapDefault > > flattenTree = toList > > -- state example > data StateMod = ModInt | ModString | ModNop deriving Show > type State = (Int, String) > > modState :: StateMod -> State -> State > modState ModInt (x,w) = (x+1,w) > modState ModNop s = s > modState ModString (x,w) = (x,'b':w) > > startState = (0,"a") > > newTree :: BTree StateMod -> BTree State > newTree = flip evalState startState > . Data.Traversable.mapM (modify' . modState) > where > modify' f = Control.Monad.State.modify f >> Control.Monad.State.get > > > Hi, Thanks for the quick reply. But it is not quite what i expect. If i apply your solution to an exampletree i get the following result: tree :: BTree StateMod tree = Fork ModNop (Fork ModInt (Leaf ModInt) (Leaf ModNop)) (Leaf ModNop) flattenTree (newTree tree ) which produces: [(0,"a"),(1,"a"),(2,"a"),(2,"a"),(2,"a")] But what i need is [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] So state changes should affect only their subtree, not the rest of the tree to the right. Kind regards, Andreas From jakubuv at gmail.com Thu Oct 23 18:47:43 2008 From: jakubuv at gmail.com (Jan Jakubuv) Date: Thu Oct 23 18:43:15 2008 Subject: [Haskell-beginners] Re: pattern for tree traversel with a state In-Reply-To: <4900B682.8020409@medien.uni-weimar.de> References: <49008273.6050804@medien.uni-weimar.de> <4900B682.8020409@medien.uni-weimar.de> Message-ID: 2008/10/23 Andreas-Christoph Bernstein : > apfelmus wrote: >> > > But what i need is > [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] > > So state changes should affect only their subtree, not the rest of the tree > to the right. > It seems to me that you are looking for the Reader monad. Try the following: import Control.Monad.Reader t :: (a -> b -> b) -> BTree a -> Reader b (BTree b) t f (Leaf x) = do s <- ask return (Leaf (f x s)) t f (Fork x l r) = do s <- ask l' <- local (f x) (t f l) r' <- local (f x) (t f r) return (Fork (f x s) l' r') new = runReader (t modState sampleTree) globalState Then, flattenTree new gives you [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] I think that the Reader monad is a standard way to this. When you want the state to affect also the rest of the tree then use the State monad. Sincerely, jan. From apfelmus at quantentunnel.de Fri Oct 24 12:25:23 2008 From: apfelmus at quantentunnel.de (apfelmus) Date: Fri Oct 24 12:20:59 2008 Subject: [Haskell-beginners] Re: Type problems with IOArray In-Reply-To: <185ee9e60810221926n7562bbd6lb0bd2c22aafd8583@mail.gmail.com> References: <185ee9e60810221926n7562bbd6lb0bd2c22aafd8583@mail.gmail.com> Message-ID: Xuan Luo wrote: > I am having lots of trouble using polymorphic mutable IOArrays. Here > is an example program: > > import Data.Array.MArray > import Data.Array.IO > > foo x = do a <- newArray (0, 4) x > readArray a 2 > > main = foo 42 >>= print > > So there is a function "foo" which makes an array of polymorphic type > initialized with a value, then returns one of the elements of the > array. Note that there are no arrays with "polymorphic element type", it's rather that your foo can be used to create an array with elements the same type as x . So, if x is an integer, foo creates an array with integer elements etc. > What the heck is this? I looked through a lot of stuff online and > eventually found that this works: > > {-# LANGUAGE ScopedTypeVariables #-} > import Data.Array.MArray > import Data.Array.IO > > foo :: forall a. a -> IO a > foo x = do a <- newArray (0, 4) x :: IO (IOArray Int a) > readArray a 2 > > main = foo 42 >>= print > > So I had to add some weird "forall" stuff to my function signature and > enable some language extension flag(?). This seems way too > complicated. The forall is not weird :). In fact, the type signature foo :: a -> IO a should be seen as an abbreviation for foo :: forall a. a -> IO a See also http://en.wikibooks.org/wiki/Haskell/Polymorphism Here, the forall a introduces a as type variable so that IOArray Int a refers to the same type a . You need an extension for that because for some odd reason, Haskell98 offers no way to do that; it will likely be included in the next version of the Haskell language. Regards, apfelmus From byorgey at seas.upenn.edu Fri Oct 24 14:18:40 2008 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri Oct 24 14:14:08 2008 Subject: [Haskell-beginners] Re: pattern for tree traversel with a state In-Reply-To: References: <49008273.6050804@medien.uni-weimar.de> <4900B682.8020409@medien.uni-weimar.de> Message-ID: <20081024181840.GA15837@GRW057-25.cis.upenn.edu> On Thu, Oct 23, 2008 at 11:47:43PM +0100, Jan Jakubuv wrote: > 2008/10/23 Andreas-Christoph Bernstein : > > apfelmus wrote: > >> > > > > But what i need is > > [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] > > > > So state changes should affect only their subtree, not the rest of the tree > > to the right. > > > > It seems to me that you are looking for the Reader monad. Try the following: > > import Control.Monad.Reader > > t :: (a -> b -> b) -> BTree a -> Reader b (BTree b) > t f (Leaf x) = do > s <- ask > return (Leaf (f x s)) > t f (Fork x l r) = do > s <- ask > l' <- local (f x) (t f l) > r' <- local (f x) (t f r) > return (Fork (f x s) l' r') > > new = runReader (t modState sampleTree) globalState > > Then, > > flattenTree new > > gives you > > [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] > > I think that the Reader monad is a standard way to this. When you want > the state to affect also the rest of the tree then use the State > monad. Just to elaborate on Jan's code, the Reader monad represents an *immutable* state---that is, a read-only "environment" that gets threaded through your computation which you can access at any time (using "ask"). However, using the "local" function, you can run subcomputations within a different environment, obtained by applying some function to the current environment. So this does exactly what you want---after the subcomputation is finished, its locally defined environment goes out of scope and you are back to the original environment. Using the Reader monad in this way is a common idiom for representing recursive algorithms with state that can change on the way down the call stack, but "unwinds" as you come back up, so recursive calls can only affect recursive calls below them, not ones that come afterwards. -Brent From streborg at hotmail.com Sat Oct 25 03:00:08 2008 From: streborg at hotmail.com (Glurk) Date: Sat Oct 25 03:00:33 2008 Subject: [Haskell-beginners] Function composition with more than 1 parameter Message-ID: Hi, Is it possible to use function composition with a function that takes more than 1 parameter ? For example, I have a function that finds all the matches of an element within a list :- matches x xs = [ m | m <- xs, x == m ] So, matches 'e' "qwertyee" will return "eee" I now want a function that will work out how many times the element occurs in the list, easily written as :- howMany x xs = length $ matches x xs So, howMany 5 [1,2,3,4,5,5,5,5,5,6,6,6,7] will return 5 However, I'm thinking I want to try to use function composition to write something like :- howMany = length . matches ...which would be nice and clean :) I thought this would work, as the output of matches is a list, and the input required by length is a list. This doesn't work though, failng with the error :- Type error in application *** Expression : length . matches *** Term : length *** Type : [b] -> Int *** Does not match : ([a] -> [a]) -> Int Which I can't quite make out what it means. Ok, it's telling me lengt has a type [b] -> Int which makes sense, but it says it expected a different type, ([a] -> [a]) -> Int What I can't understand is why it wants something of that type ? Is it possibly something to do with the higher precedence of function application over function composition ? I've tried various things, like putting brackets around (length . matches) and changing my function to take in a tuple of (element, list), instead of 2 parameters (which I thought I had working at some stage ! but which now I can't get to work) Any advice appreciated ! thanks :) From jason.dusek at gmail.com Sat Oct 25 03:48:38 2008 From: jason.dusek at gmail.com (Jason Dusek) Date: Sat Oct 25 03:44:04 2008 Subject: [Haskell-beginners] Function composition with more than 1 parameter In-Reply-To: References: Message-ID: <42784f260810250048h6d3264ffr8430d1035a221a12@mail.gmail.com> You can easily have howMany x = length . matches x I poked around in 'Control.Applicative' (briefly) and didn't find anything that really stood out as answer. -- _jsn From apfelmus at quantentunnel.de Sat Oct 25 05:10:03 2008 From: apfelmus at quantentunnel.de (apfelmus) Date: Sat Oct 25 05:05:42 2008 Subject: [Haskell-beginners] Re: Function composition with more than 1 parameter In-Reply-To: References: Message-ID: Glurk wrote: > Is it possible to use function composition with a function that takes more > than 1 parameter ? > > For example, I have a function that finds all the matches of an element within > a list :- > > matches x xs = [ m | m <- xs, x == m ] > > So, matches 'e' "qwertyee" will return "eee" > > I now want a function that will work out how many times the element occurs in > the list, easily written as :- > > howMany x xs = length $ matches x xs The lambdabot on #haskell has a plugin named "pointless" that can transform this into a definition that doesn't mention x and xs anymore. I think it will propose howMany = (length .) . matches And with matches x xs = filter (x==) xs matches x = filter (x==) matches = filter . (==) we have howMany = (length .) . filter . (==) > I've tried various things, like putting brackets around (length . matches) > and changing my function to take in a tuple of (element, list), instead of 2 > parameters (which I thought I had working at some stage ! but which now I > can't get to work) howMany = curry (length . uncurry matches) Regards, apfelmus From streborg at hotmail.com Sat Oct 25 06:34:23 2008 From: streborg at hotmail.com (Glurk) Date: Sat Oct 25 06:29:57 2008 Subject: [Haskell-beginners] Re: Function composition with more than 1 parameter References: Message-ID: > The lambdabot on #haskell has a plugin named "pointless" that can > transform this into a definition that doesn't mention x and xs > anymore. I think it will propose > > howMany = (length .) . matches > > And with > > matches x xs = filter (x==) xs > > matches x = filter (x==) > > matches = filter . (==) > > we have > > howMany = (length .) . filter . (==) > > howMany = curry (length . uncurry matches) Thanks apfelmus, unfortunately, none of those suggested changes seem to work ! I get errors like :- Unresolved top-level overloading *** Binding : howMany2 *** Outstanding context : Eq b I'm using WinHugs - do you get them to work in some other environment ? Thanks ! :) From daniel.is.fischer at web.de Sat Oct 25 06:48:53 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 25 06:42:06 2008 Subject: [Haskell-beginners] Re: Function composition with more than =?iso-8859-1?q?1 parameter?= In-Reply-To: References: Message-ID: <200810251248.53863.daniel.is.fischer@web.de> Am Samstag, 25. Oktober 2008 12:34 schrieb Glurk: > > The lambdabot on #haskell has a plugin named "pointless" that can > > transform this into a definition that doesn't mention x and xs > > anymore. I think it will propose > > > > howMany = (length .) . matches > > > > And with > > > > matches x xs = filter (x==) xs > > > > matches x = filter (x==) > > > > matches = filter . (==) > > > > we have > > > > howMany = (length .) . filter . (==) > > > > > > howMany = curry (length . uncurry matches) > > Thanks apfelmus, > > unfortunately, none of those suggested changes seem to work ! > I get errors like :- > > Unresolved top-level overloading > *** Binding : howMany2 > *** Outstanding context : Eq b > > I'm using WinHugs - do you get them to work in some other environment ? Did you give a type signature for these? Then you would of course have to include the context Eq b, since (==) is used. If you don't give any type signatures, all above versions should work in all environments, otherwise it would be a serious bug. > > Thanks ! :) > From chrycheng at gmail.com Sat Oct 25 08:57:50 2008 From: chrycheng at gmail.com (Chry Cheng) Date: Sat Oct 25 08:53:28 2008 Subject: [Haskell-beginners] Function composition with more than 1 parameter In-Reply-To: (Glurk's message of "Sat, 25 Oct 2008 07:00:08 +0000 (UTC)") References: Message-ID: Glurk writes: > Type error in application > *** Expression : length . matches > *** Term : length > *** Type : [b] -> Int > *** Does not match : ([a] -> [a]) -> Int > > Which I can't quite make out what it means. > Ok, it's telling me lengt has a type [b] -> Int which makes sense, > but it says it expected a different type, ([a] -> [a]) -> Int > What I can't understand is why it wants something of that type ? It's not saying that it wants ([a] -> [a]) -> Int. Rather, it's saying that the type it inferred (([a] -> [a]) -> Int) is not what it wants ([b] -> Int). It expected that a list would be passed to length ([b]). Instead, you pass a function that eats and spews strings ([a] -> [a]) which is the type that matches would have after being partially applied (e.g., matches 'e'). > Is it possibly something to do with the higher precedence of function > application over function composition ? I don't think so. You might be confusing the instances of length and matches in your new definition of howMany as function applications. In it, the only function that is getting applied is . which is passed length and matches as arguments. From haskell at colquitt.org Sat Oct 25 09:20:58 2008 From: haskell at colquitt.org (John Dorsey) Date: Sat Oct 25 09:16:23 2008 Subject: [Haskell-beginners] Function composition with more than 1 parameter In-Reply-To: References: Message-ID: <20081025132058.GG27422@colquitt.org> Glurk, "(.) . (.)" seems to do it: dorsey@elwood:~$ ghci GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> let (...) = (.) . (.) Prelude> let matches x xs = [ m | m <- xs, x == m ] Prelude> :t length ... matches length ... matches :: (Eq a) => a -> [a] -> Int Prelude> Regards, John From streborg at hotmail.com Sat Oct 25 09:57:38 2008 From: streborg at hotmail.com (Glurk) Date: Sat Oct 25 09:53:10 2008 Subject: [Haskell-beginners] Re: Function composition with more than =?iso-8859-1?q?1 parameter?= References: <200810251248.53863.daniel.is.fischer@web.de> Message-ID: > > I'm using WinHugs - do you get them to work in some other environment ? > > Did you give a type signature for these? > Then you would of course have to include the context Eq b, since (==) is used. > If you don't give any type signatures, all above versions should work in all > environments, otherwise it would be a serious bug. OK, now I'm getting somwhere ! :) I tried it with GHCi, and it works ! ...but only after disabling the monomorphism restriction ! It offered another alternative, which was to give a type definition. So, I tried :- matches :: Eq a => a -> [a] -> [a] matches = filter . (==) hm :: Eq a => a -> [a] -> Int hm = (length .) . matches in Hugs, and that also worked :) I'm still struggling to come to grips with how it is actually working... I was thinking that this pointfree style was to make things simpler, more readable and understandable...but now I'm not so sure ! If it's not a simple case (f.g.h etc) I might just stick with putting the parameter names in. At this point, brackets and multiple dots like this don't really make things clearer to me (Not to mention John's post below, with 3 consecutive dots ! I'm really having trouble with that one !). Of course, a lot of this is due to my lack of understanding - I'll keep experimenting and trying to learn :) In general though, do people find this style clearer ? I'd be interested to know what other people prefer. Thanks for all the help :) From chrycheng at gmail.com Sat Oct 25 10:35:48 2008 From: chrycheng at gmail.com (Chry Cheng) Date: Sat Oct 25 10:32:18 2008 Subject: [Haskell-beginners] Re: Function composition with more than 1 parameter In-Reply-To: (Glurk's message of "Sat, 25 Oct 2008 13:57:38 +0000 (UTC)") References: <200810251248.53863.daniel.is.fischer@web.de> Message-ID: Glurk writes: > If it's not a simple case (f.g.h etc) I might just stick with putting the > parameter names in. At this point, brackets and multiple dots like this don't > really make things clearer to me (Not to mention John's post below, with 3 > consecutive dots ! I'm really having trouble with that one !). Of course, a > lot of this is due to my lack of understanding - I'll keep experimenting and > trying to learn :) > > In general though, do people find this style clearer ? > I'd be interested to know what other people prefer. This is also my personal rule. Beyond such simple cases, insisting on point-free programming IMHO does more bad than good by obfuscating the code. See http://haskell.org/haskellwiki/Pointfree#Combinator_discoveries for some clever, if arcane, examples :P From daniel.is.fischer at web.de Sat Oct 25 10:39:23 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 25 10:32:35 2008 Subject: [Haskell-beginners] Re: Function composition with =?iso-8859-1?q?more than 1 parameter?= In-Reply-To: References: <200810251248.53863.daniel.is.fischer@web.de> Message-ID: <200810251639.23116.daniel.is.fischer@web.de> Am Samstag, 25. Oktober 2008 15:57 schrieb Glurk: > > > I'm using WinHugs - do you get them to work in some other environment ? > > > > Did you give a type signature for these? > > Then you would of course have to include the context Eq b, since (==) is > > used. > > > If you don't give any type signatures, all above versions should work in > > all environments, otherwise it would be a serious bug. > > OK, now I'm getting somwhere ! :) > > I tried it with GHCi, and it works ! > ...but only after disabling the monomorphism restriction ! Oh yes, the dreaded MR I forgot. > It offered another alternative, which was to give a type definition. > > So, I tried :- > > matches :: Eq a => a -> [a] -> [a] > matches = filter . (==) > > hm :: Eq a => a -> [a] -> Int > hm = (length .) . matches > > in Hugs, and that also worked :) > > I'm still struggling to come to grips with how it is actually working... > I was thinking that this pointfree style was to make things simpler, more > readable and understandable...but now I'm not so sure ! Sometimes pointfree style is clearer and more readable, sometimes it makes things obscure. Get some experience writing both, pointfree and pointful versions, to develop a feeling when to write pointfree and when not (and when to use mixed style, e.g. "hm x = length . matches x" might be clearer). > > If it's not a simple case (f.g.h etc) I might just stick with putting the > parameter names in. At this point, brackets and multiple dots like this > don't really make things clearer to me (Not to mention John's post below, > with 3 consecutive dots ! I'm really having trouble with that one !). Of > course, a lot of this is due to my lack of understanding - I'll keep > experimenting and trying to learn :) > > In general though, do people find this style clearer ? > I'd be interested to know what other people prefer. > > Thanks for all the help :) If you compose single-argument functions, the pointfree composition = f . g . h seems clearer than composition x = f . g . h $ x , a case like howMany, howMany = (length .) . matches is sufficiently clear either way (less if written ((.) . (.)) length matches), but beyond that supplying arguments is desirable. Would you prefer to read composition = ((.) . (.) . (.) . (.) . (.)) func thing composition = ((((func .) .) .) .) . thing composition a b c d = func . thing a b c d or composition a b c d e = func $ thing a b c d e ? IMO, the last two are okay, the first is baaad, the second bad unless in special circumstances. From andreas.bernstein at medien.uni-weimar.de Sat Oct 25 13:35:32 2008 From: andreas.bernstein at medien.uni-weimar.de (Andreas-Christoph Bernstein) Date: Sat Oct 25 13:31:17 2008 Subject: [Haskell-beginners] Re: pattern for tree traversel with a state In-Reply-To: <20081024181840.GA15837@GRW057-25.cis.upenn.edu> References: <49008273.6050804@medien.uni-weimar.de> <4900B682.8020409@medien.uni-weimar.de> <20081024181840.GA15837@GRW057-25.cis.upenn.edu> Message-ID: <490358E4.7030508@medien.uni-weimar.de> Brent Yorgey wrote: > On Thu, Oct 23, 2008 at 11:47:43PM +0100, Jan Jakubuv wrote: > >> 2008/10/23 Andreas-Christoph Bernstein : >> >>> apfelmus wrote: >>> >>> But what i need is >>> [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] >>> >>> So state changes should affect only their subtree, not the rest of the tree >>> to the right. >>> >>> >> It seems to me that you are looking for the Reader monad. Try the following: >> >> import Control.Monad.Reader >> >> t :: (a -> b -> b) -> BTree a -> Reader b (BTree b) >> t f (Leaf x) = do >> s <- ask >> return (Leaf (f x s)) >> t f (Fork x l r) = do >> s <- ask >> l' <- local (f x) (t f l) >> r' <- local (f x) (t f r) >> return (Fork (f x s) l' r') >> >> new = runReader (t modState sampleTree) globalState >> >> Then, >> >> flattenTree new >> >> gives you >> >> [(0,"a"),(1,"a"),(2,"a"),(1,"a"),(0,"a")] >> >> I think that the Reader monad is a standard way to this. When you want >> the state to affect also the rest of the tree then use the State >> monad. >> > > Just to elaborate on Jan's code, the Reader monad represents an > *immutable* state---that is, a read-only "environment" that gets > threaded through your computation which you can access at any time > (using "ask"). However, using the "local" function, you can run > subcomputations within a different environment, obtained by applying > some function to the current environment. So this does exactly what > you want---after the subcomputation is finished, its locally defined > environment goes out of scope and you are back to the original > environment. Using the Reader monad in this way is a common idiom for > representing recursive algorithms with state that can change on the > way down the call stack, but "unwinds" as you come back up, so > recursive calls can only affect recursive calls below them, not ones > that come afterwards. > > -Brent > That is what i was looking for. Thank you all very much for your help. Kind regards, Andreas From streborg at hotmail.com Sat Oct 25 18:44:28 2008 From: streborg at hotmail.com (Glurk) Date: Sat Oct 25 18:40:04 2008 Subject: [Haskell-beginners] Re: Function composition with =?iso-8859-1?q?more than 1 parameter?= References: <200810251248.53863.daniel.is.fischer@web.de> <200810251639.23116.daniel.is.fischer@web.de> Message-ID: > Sometimes pointfree style is clearer and more readable, sometimes it makes > things obscure. > Get some experience writing both, pointfree and pointful versions, to develop > a feeling when to write pointfree and when not (and when to use mixed style, > e.g. "hm x = length . matches x" might be clearer). I think that's good advice :) Having a better understanding of how it works is never a bad thing ! :) I still have trouble understanding a simple one like :- matches = filter . (==) which actually is kind of nice and clean, but when I try to understand it I get confused. In particular I wonder how it "knows" where the parameters go ? (==) is of type (==) :: Eq a => a -> a -> Bool yet I'm giving it incorrect parameters, I'm giving it a [a] not a a (or am I ?) Or, does it take the only valid parameter I give it, the a, and then form a function out of this (a==), and then compose this new function with filter, which does take a function of this new type. Then, there is one parameter left, [a], which is what this new function needs...and so it all works out ! Why doesn't it try to take the 2 parameters for (==), and bomb out when it finds the second parameter is wrong ? Is this where the monomorphism restriction things comes into play ? Or now because I've given it a type, somehow it can now work things out.... ? Does anyone know of a good tutorial on how this all works ? :) Thanks From allbery at ece.cmu.edu Sat Oct 25 19:22:58 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Sat Oct 25 19:18:25 2008 Subject: [Haskell-beginners] Re: Function composition with more than 1 parameter In-Reply-To: References: <200810251248.53863.daniel.is.fischer@web.de> <200810251639.23116.daniel.is.fischer@web.de> Message-ID: <20FCD53A-183A-4E95-A855-2DB6BD023171@ece.cmu.edu> On 2008 Oct 25, at 18:44, Glurk wrote: >> Or, does it take the only valid parameter I give it, the a, and >> then form a > function out of this (a==), and then compose this new function with > filter, > which does take a function of this new type. Then, there is one > parameter > left, [a], which is what this new function needs...and so it all > works out ! This is it exactly. > Why doesn't it try to take the 2 parameters for (==) In reality there are no two-parameter functions. They are actually single-parameter functions which return functions. Thus only one argument will ever be consumed by a particular application. Symbolically: (\a b -> f a b) = \a -> (\b -> f a b) taking argument a and returning the function (\b -> f a b) with a having whatever value it was given in the first application (in other languages this is often referred to as a closure). -- 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 From daniel.is.fischer at web.de Sat Oct 25 19:36:00 2008 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 25 19:29:17 2008 Subject: [Haskell-beginners] Re: Function =?iso-8859-1?q?composition with more than 1 parameter?= In-Reply-To: References: <200810251639.23116.daniel.is.fischer@web.de> Message-ID: <200810260136.00519.daniel.is.fischer@web.de> Am Sonntag, 26. Oktober 2008 00:44 schrieb Glurk: > > Sometimes pointfree style is clearer and more readable, sometimes it > > makes things obscure. > > Get some experience writing both, pointfree and pointful versions, to > > develop > > > a feeling when to write pointfree and when not (and when to use mixed > > style, e.g. "hm x = length . matches x" might be clearer). > > I think that's good advice :) > Having a better understanding of how it works is never a bad thing ! :) > > I still have trouble understanding a simple one like :- > > matches = filter . (==) > > which actually is kind of nice and clean, but when I try to understand it I > get confused. In particular I wonder how it "knows" where the parameters go > ? > > (==) is of type (==) :: Eq a => a -> a -> Bool > > yet I'm giving it incorrect parameters, I'm giving it a [a] > not a a (or am I ?) For any composition f . g, the evaluation goes (f . g) x = f (g x), so (filter . (==)) x = filter ((==) x) Now if x has type a which is an instance of Eq, x is a suitable argument for (==) and ((==) x) has type a -> Bool, hence ((==) x) is a suitable argument for filter, so what happens is exactly what you describe below. > > Or, does it take the only valid parameter I give it, the a, and then form a > function out of this (a==), and then compose this new function with filter, > which does take a function of this new type. Then, there is one parameter > left, [a], which is what this new function needs...and so it all works out > ! > > Why doesn't it try to take the 2 parameters for (==), and bomb out when it > finds the second parameter is wrong ? Because as soon as (==) has received its first parameter, the section (a ==) becomes the first argument of filter, so the second argument to (==) is then provided by filter (you remember: filter prd (x:xs) | prd x = x:filter prd xs | otherwise = filter prd xs filter _ [] = [] ) > Is this where the monomorphism > restriction things comes into play ? Or now because I've given it a type, > somehow it can now work things out.... ? No, strictly speaking, every function has only one argument, it is only a matter of convenience to say that functions which return functions take several arguments. And it's the fact that a function has only one argument which is the reason why it works out. > > Does anyone know of a good tutorial on how this all works ? :) > > Thanks > From s0700157 at sms.ed.ac.uk Mon Oct 27 17:33:21 2008 From: s0700157 at sms.ed.ac.uk (Alex Shearn) Date: Mon Oct 27 17:28:43 2008 Subject: [Haskell-beginners] eval command? Message-ID: <490633A1.5000406@sms.ed.ac.uk> Hey all - I've been trying to write an IRC bot following the guide on the wiki, and we (those of us on the channel) were trying to get it to evaluate commands. So far, we have this for "eval" stuff, but is there anyway to specify a "parse in haskell" sort of thing? [code] -- Dispatch a command eval :: String -> Net () eval "!endbot" = write "QUIT" ":Exiting" >> io (exitWith ExitSuccess) eval x | "!haskbot " `isPrefixOf` x = privmsg (drop 9 x) eval _ = return () -- ignore everything else [/code] is there anyway to do something like: eval "!eval" `isPrefixOf` x = eval blah ? if this isn't beginners' stuff, let me know, i'll repost to the main mailing list. Many thanks, Alex Shearn -- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336. From allbery at ece.cmu.edu Mon Oct 27 19:00:00 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Mon Oct 27 18:55:18 2008 Subject: [Haskell-beginners] eval command? In-Reply-To: <490633A1.5000406@sms.ed.ac.uk> References: <490633A1.5000406@sms.ed.ac.uk> Message-ID: On 2008 Oct 27, at 17:33, Alex Shearn wrote: > Hey all - I've been trying to write an IRC bot following the guide > on the wiki, and we (those of us on the channel) were trying to get > it to evaluate commands. > So far, we have this for "eval" stuff, but is there anyway to > specify a "parse in haskell" sort of thing? No, although you could fake it with the GHC-API (which basically means your bot has all of GHC built into it). In any case, that's not really a good idea; consider what damage could be done by arbitrary code. A better idea is to link the code into a small program with a very restricted environment and run that with a timeout. See http://code.haskell.org/lambdabot/Plugin/Eval.hs. -- 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 From byorgey at seas.upenn.edu Mon Oct 27 19:45:26 2008 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon Oct 27 19:40:43 2008 Subject: [Haskell-beginners] eval command? In-Reply-To: References: <490633A1.5000406@sms.ed.ac.uk> Message-ID: <20081027234526.GA14436@GRW057-25.cis.upenn.edu> On Mon, Oct 27, 2008 at 07:00:00PM -0400, Brandon S. Allbery KF8NH wrote: > On 2008 Oct 27, at 17:33, Alex Shearn wrote: >> Hey all - I've been trying to write an IRC bot following the guide on the >> wiki, and we (those of us on the channel) were trying to get it to >> evaluate commands. >> So far, we have this for "eval" stuff, but is there anyway to specify a >> "parse in haskell" sort of thing? > > > No, although you could fake it with the GHC-API (which basically means your > bot has all of GHC built into it). > > In any case, that's not really a good idea; consider what damage could be > done by arbitrary code. A better idea is to link the code into a small > program with a very restricted environment and run that with a timeout. > See http://code.haskell.org/lambdabot/Plugin/Eval.hs. Brandon is right that this is difficult and tricky -- but fortunately, someone else has already done the hard work for you! Take a look at the mueval package [1], which should allow you to do what you want. -Brent [1] http://hackage.haskell.org/cgi-bin/hackage-scripts/package/mueval From andrew at swclan.homelinux.org Mon Oct 27 23:25:20 2008 From: andrew at swclan.homelinux.org (Andrew Sackville-West) Date: Mon Oct 27 23:20:45 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: <20081027234526.GA14436@GRW057-25.cis.upenn.edu> References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> Message-ID: <20081028032520.GA9991@delappy.swclan.homelinux.org> On Mon, Oct 27, 2008 at 07:45:26PM -0400, Brent Yorgey wrote: > On Mon, Oct 27, 2008 at 07:00:00PM -0400, Brandon S. Allbery KF8NH wrote: > > On 2008 Oct 27, at 17:33, Alex Shearn wrote: > >> Hey all - I've been trying to write an IRC bot following the guide on the > >> wiki, and we (those of us on the channel) were trying to get it to > >> evaluate commands. > >> So far, we have this for "eval" stuff, but is there anyway to specify a > >> "parse in haskell" sort of thing? > > > > > > No, although you could fake it with the GHC-API (which basically means your > > bot has all of GHC built into it). > > > > In any case, that's not really a good idea; consider what damage could be > > done by arbitrary code. A better idea is to link the code into a small > > program with a very restricted environment and run that with a timeout. > > See http://code.haskell.org/lambdabot/Plugin/Eval.hs. > > Brandon is right that this is difficult and tricky -- but fortunately, > someone else has already done the hard work for you! Take a look at > the mueval package [1], which should allow you to do what you want. this raises a question for me, being a bit of a schemer. Is there any parallel in haskell to the data is code model of the lisp family? For example, playing around in scheme with a symbolic differentiator, it is trivial to then evaluate the differentiated s-expression at arbitrary value by representing the expression, and it's derivative as a regular scheme expression. Is this something that can be done in haskell? My initial impression is no, that you'd have to parse it as an expression and evaluate it as you would in regular imperative languages. I'd love to hear otherwise. A -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20081027/9103e5ea/attachment.bin From allbery at ece.cmu.edu Mon Oct 27 23:26:58 2008 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Mon Oct 27 23:22:18 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: <20081028032520.GA9991@delappy.swclan.homelinux.org> References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> <20081028032520.GA9991@delappy.swclan.homelinux.org> Message-ID: On 2008 Oct 27, at 23:25, Andrew Sackville-West wrote: > this raises a question for me, being a bit of a schemer. Is there any > parallel in haskell to the data is code model of the lisp family? For > example, playing around in scheme with a symbolic differentiator, it > is trivial to then evaluate the differentiated s-expression at > arbitrary value by representing the expression, and it's derivative as > a regular scheme expression. > > Is this something that can be done in haskell? My initial impression > is no, that you'd have to parse it as an expression and evaluate it as > you would in regular imperative languages. I'd love to hear otherwise. You get this in a type-safe form with Template Haskell; you can operate on expressions at the AST level. -- 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 From news at n-mayr.de Tue Oct 28 11:30:37 2008 From: news at n-mayr.de (news@n-mayr.de) Date: Tue Oct 28 11:25:55 2008 Subject: [Haskell-beginners] gtk2hs treeViewSetReorderable Message-ID: <20081028163037.vsdq4ibhgkgo4g0k@webmail.df.eu> Hello, I am using a TreeView from the TreeList package and a ListStore as its data model. I set treeViewSetReorderable to True and now rows can be drag-n-dropped around. The data in the ListStore gets updated as expected. I want to get notified, when the order of the elements in the list changes (or just, when something is draged around - even if it is dropped on the position it was taken from). In the documentation it says "..., then the user can reorder the model by dragging and dropping rows. The developer can listen to these changes by connecting to the model's signals." After hours of seaching, I still don't find where I have to connect to. Have you any clue? Best wishes, Nikolas Link to documentation: http://www.haskell.org/gtk2hs/docs/current/Graphics-UI-Gtk-TreeList-TreeView.html#v%3AtreeViewSetReorderable From tonyhannan2 at gmail.com Tue Oct 28 19:00:00 2008 From: tonyhannan2 at gmail.com (Tony Hannan) Date: Tue Oct 28 18:55:13 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> <20081028032520.GA9991@delappy.swclan.homelinux.org> Message-ID: On Mon, Oct 27, 2008 at 11:26 PM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote: > On 2008 Oct 27, at 23:25, Andrew Sackville-West wrote: > >> this raises a question for me, being a bit of a schemer. Is there any >> parallel in haskell to the data is code model of the lisp family? For >> example, playing around in scheme with a symbolic differentiator, it >> is trivial to then evaluate the differentiated s-expression at >> arbitrary value by representing the expression, and it's derivative as >> a regular scheme expression. >> >> Is this something that can be done in haskell? My initial impression >> is no, that you'd have to parse it as an expression and evaluate it as >> you would in regular imperative languages. I'd love to hear otherwise. >> > > > You get this in a type-safe form with Template Haskell; you can operate on > expressions at the AST level. > > Yeah, but can you do this at run time? I though Template Haskell can only be used at compile time. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081028/0e38537e/attachment.htm From news at n-mayr.de Tue Oct 28 19:33:32 2008 From: news at n-mayr.de (news@n-mayr.de) Date: Tue Oct 28 19:28:47 2008 Subject: [Haskell-beginners] gtk2hs treeViewSetReorderable In-Reply-To: <20081028163037.vsdq4ibhgkgo4g0k@webmail.df.eu> References: <20081028163037.vsdq4ibhgkgo4g0k@webmail.df.eu> Message-ID: <20081029003332.pzo6rafiww80g40o@webmail.df.eu> I undertook a second quest searching the gtk2hs documentation. The solution is to have a look at Graphics.UI.Gtk.ModelView.CustomStore. On a custom model are all the functions available that get called when something interesting happens in the view. Only connect the model to the view, that's it. The functions get called on the model and there is no need for some external to register call-back functions at the view. There's still the possibility for the model to accept call-back functions if required. Nikolas Zitat von news@n-mayr.de: > Hello, > > I am using a TreeView from the TreeList package and a ListStore as its > data model. I set treeViewSetReorderable to True and now rows can be > drag-n-dropped around. The data in the ListStore gets updated as > expected. > > I want to get notified, when the order of the elements in the list > changes (or just, when something is draged around - even if it is > dropped on the position it was taken from). > > In the documentation it says "..., then the user can reorder the model > by dragging and dropping rows. The developer can listen to these > changes by connecting to the model's signals." > > After hours of seaching, I still don't find where I have to connect to. > Have you any clue? > > Best wishes, > Nikolas > > Link to documentation: > http://www.haskell.org/gtk2hs/docs/current/Graphics-UI-Gtk-TreeList-TreeView.html#v%3AtreeViewSetReorderable > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From andrew at swclan.homelinux.org Tue Oct 28 20:20:12 2008 From: andrew at swclan.homelinux.org (Andrew Sackville-West) Date: Tue Oct 28 20:15:38 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> <20081028032520.GA9991@delappy.swclan.homelinux.org> Message-ID: <20081029002011.GL25699@localhost.localdomain> On Tue, Oct 28, 2008 at 07:00:00PM -0400, Tony Hannan wrote: > On Mon, Oct 27, 2008 at 11:26 PM, Brandon S. Allbery KF8NH < > allbery@ece.cmu.edu> wrote: > > > On 2008 Oct 27, at 23:25, Andrew Sackville-West wrote: > > > >> this raises a question for me, being a bit of a schemer. Is there any > >> parallel in haskell to the data is code model of the lisp family? For > >> example, playing around in scheme with a symbolic differentiator, it > >> is trivial to then evaluate the differentiated s-expression at > >> arbitrary value by representing the expression, and it's derivative as > >> a regular scheme expression. > >> > >> Is this something that can be done in haskell? My initial impression > >> is no, that you'd have to parse it as an expression and evaluate it as > >> you would in regular imperative languages. I'd love to hear otherwise. > >> > > > > > > You get this in a type-safe form with Template Haskell; you can operate on > > expressions at the AST level. > > > > > Yeah, but can you do this at run time? I though Template Haskell can only be > used at compile time. That's what I'm talking about, run time evaluation of code/data. In scheme, I can take as input (+ (exp x 2) (* 2 x)) a representation of x^2 + 2x I can feed that "data" into a function (derive input) and get back (+ (* 2 x) 2) a representation of 2x +2, the derivative of the above. The function derive can manipulate that "Data" which is just a list, or s-expression like any other data (for example accessing the first element in the list, determining that it's a '+' and so calling derive recursively on the two following elements). then I can feed that result into (eval) with some information about what the value of x is and it will be evaluated just like a regular scheme expression. something like (it's been a while): (let ((x 2)) (eval default-environment (derive (input)))) where default-environment lets you get the information about the current environment into the eval environment so that x has a value. So now, what was "Data" a moment ago is treated as executable within the eval. The above (let) would spit out(in this case) a value of 6. Is such a thing possible in haskell? (I'm really just curious, I have no need of this functionality at this point). Oh, and I apologise for the fingernail clippings... A -- -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20081028/90bb0d32/attachment-0001.bin From rendel at daimi.au.dk Wed Oct 29 04:27:32 2008 From: rendel at daimi.au.dk (Tillmann Rendel) Date: Wed Oct 29 04:22:56 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: <20081028032520.GA9991@delappy.swclan.homelinux.org> References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> <20081028032520.GA9991@delappy.swclan.homelinux.org> Message-ID: <49081E74.7010402@daimi.au.dk> Andrew Sackville-West schrieb: > this raises a question for me, being a bit of a schemer. Is there any > parallel in haskell to the data is code model of the lisp family? No. > My initial impression is no, that you'd have to parse it as an > expression and evaluate it as you would in regular imperative > languages. I'd love to hear otherwise. I don't see how "code is data" is connected to imperative vs. purely functional. After all, lisp & co. are not purely functional, but feature "code is data". Another well-known symbolic language, which allows to treat code as data and vice versa, is prolog. Since Haskell features algebraic data types, and a reasonable flexible syntax, you do not need to do any parsing. Instead, you can write down the AST of the embedded language directly as part of your Haskell program. But you have to write an evaluator. With pattern matching, that is often very easy, though. Tillmann From andrew at swclan.homelinux.org Wed Oct 29 10:02:46 2008 From: andrew at swclan.homelinux.org (Andrew Sackville-West) Date: Wed Oct 29 09:58:02 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: <49081E74.7010402@daimi.au.dk> References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> <20081028032520.GA9991@delappy.swclan.homelinux.org> <49081E74.7010402@daimi.au.dk> Message-ID: <20081029140246.GA3441@localhost.localdomain> On Wed, Oct 29, 2008 at 09:27:32AM +0100, Tillmann Rendel wrote: > Andrew Sackville-West schrieb: >> this raises a question for me, being a bit of a schemer. Is there any >> parallel in haskell to the data is code model of the lisp family? > > No. > >> My initial impression is no, that you'd have to parse it as an >> expression and evaluate it as you would in regular imperative >> languages. I'd love to hear otherwise. > > I don't see how "code is data" is connected to imperative vs. purely > functional. After all, lisp & co. are not purely functional, but feature > "code is data". Another well-known symbolic language, which allows to > treat code as data and vice versa, is prolog. I didn't mean to connect the concept to imperative vs. functional. > > Since Haskell features algebraic data types, and a reasonable flexible > syntax, you do not need to do any parsing. Instead, you can write down > the AST of the embedded language directly as part of your Haskell > program. But you have to write an evaluator. With pattern matching, that > is often very easy, though. looks like I'm off to read about Template Haskell. thanks to all. A -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20081029/7cc44d4a/attachment.bin From andrew at swclan.homelinux.org Wed Oct 29 10:48:59 2008 From: andrew at swclan.homelinux.org (Andrew Sackville-West) Date: Wed Oct 29 10:44:15 2008 Subject: [Haskell-beginners] evaluation of expressions [was Re: eval command?] In-Reply-To: <20081029140246.GA3441@localhost.localdomain> References: <490633A1.5000406@sms.ed.ac.uk> <20081027234526.GA14436@GRW057-25.cis.upenn.edu> <20081028032520.GA9991@delappy.swclan.homelinux.org> <49081E74.7010402@daimi.au.dk> <20081029140246.GA3441@localhost.localdomain> Message-ID: <20081029144859.GA19190@localhost.localdomain> On Wed, Oct 29, 2008 at 07:02:46AM -0700, Andrew Sackville-West wrote: > On Wed, Oct 29, 2008 at 09:27:32AM +0100, Tillmann Rendel wrote: ... > > Since Haskell features algebraic data types, and a reasonable flexible > > syntax, you do not need to do any parsing. Instead, you can write down > > the AST of the embedded language directly as part of your Haskell > > program. But you have to write an evaluator. With pattern matching, that > > is often very easy, though. > > looks like I'm off to read about Template Haskell. And it looks like: http://www.haskell.org/tmrwiki/TemplateHaskell#head-96ad34fffdb541d4e334edeec77d71061beb2ec8 is a pretty good starting point. To write a symbolic differentiator in haskell, with evaluable results, this looks to be the way to start. A -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: Digital signature Url : http://www.haskell.org/pipermail/beginners/attachments/20081029/6b0af174/attachment.bin From cppljevans at suddenlink.net Thu Oct 30 23:07:59 2008 From: cppljevans at suddenlink.net (Larry Evans) Date: Thu Oct 30 21:58:33 2008 Subject: [Haskell-beginners] Control.Monad.State: State CTOR unneeded to create a State? Message-ID: <490A768F.5020707@suddenlink.net> Page: http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#2 suggests, with the content: Constructors State runState :: (s -> (a, s)) that State has a labeled constructor: http://www.haskell.org/onlinereport/exps.html#sect3.15.2 yet the Examples section: http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#4 shows, AFAICT, no call to the State CTOR: tick :: State Int Int tick = do n <- get put (n+1) return n I assume the n must be an Int (since it's in n+1), but what are the get and put. There is a get and put described here: http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#1 however, I don't see how they are relevant since they are defined for an already existing MonadState, and AFAICT, tick hasn't been defined yet since it's on the lhs of the =. Please, what am I missing? TIA -regards Larry From aslatter at gmail.com Thu Oct 30 22:20:50 2008 From: aslatter at gmail.com (Antoine Latter) Date: Thu Oct 30 22:15:57 2008 Subject: [Haskell-beginners] Control.Monad.State: State CTOR unneeded to create a State? In-Reply-To: <490A768F.5020707@suddenlink.net> References: <490A768F.5020707@suddenlink.net> Message-ID: <694519c50810301920u398b6058m83d827cd4e9c8f55@mail.gmail.com> On Thu, Oct 30, 2008 at 10:07 PM, Larry Evans wrote: > Page: > > http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#2 > > suggests, with the content: > > Constructors > State runState :: (s -> (a, s)) > that State has a labeled constructor: > > http://www.haskell.org/onlinereport/exps.html#sect3.15.2 > > yet the Examples section: > > http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#4 > > shows, AFAICT, no call to the State CTOR: > > tick :: State Int Int > tick = do n <- get > put (n+1) > return n > I assume the n must be an Int (since it's in n+1), but what are > the > get and put. There is a get and put described here: > > http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-State.html#1 > > however, I don't see how they are relevant since they are defined for > an already existing MonadState, and AFAICT, tick hasn't been defined > yet since it's on the lhs of the =. > > Please, what am I missing? > Hi Larry. Event thought 'get' and 'put' are defined in MonadState, the concrete implementation of them for "State s a" uses the 'State' constructor. They probably look something like so: get = State (\s -> (s,s)) put newS = State (\oldS ->( (), newS )) Also, the definitions of the 'Monad' class functions also need to use the 'State' constructor, for example return should look something like: return x = State (\s -> (x, s)) So when you use expressions in a 'do' block, or 'get' or 'put' then under the hood you're using the 'State' constructor. Then at the top-level you use 'runState :: State s a -> s -> (a,s)' to strip the constructor away. As long as you aren't writing anything that needs to dig to much into the workings of 'State', you shouldn't have to worry too much about it. I hope I haven't made things more confused ^_^ -Antoine