[Haskell-cafe] [OT] Yet Another program written in Haskell: a language tag parser
The program is written in Haskell, hence my shameless ad here: http://www.bortzmeyer.org/gabuzomeu-parsing-language-tags.html GaBuZoMeu is a set of programs to parse and check language tags (see the RFC 4646 produced by the IETF Working Group LTRU - Language Tag Registry Update). Language tags are used by several protocols (like HTTP) or languages (like XML) to indicate the human language you want or have. Examples of language tags are fr (French), apa (Apache), se-FI (Swedish, as practiced in Finland) or uk-Latn (ukrainian in the latin script). An IANA registry stores the registered values (http://www.iana.org/assignments/language-subtag-registry). A language tag can be well-formed (syntactically correct) or not. This can be tested without access to the registry. A langauge tag can be valid or not (all its subtags registered). This depends on the copy of the registry you use (since the registry changes). GaBuZoMeu includes the following programs: * check-wf checks the well-formedness of tag(s). Example of use: check-wf fr en-AU. * check-valid checks the validity of tag(s). Same use. GaBuZoMeu is written in Haskell (http://www.haskell.org/) so you'll need an Haskell compiler such as ghc (http://haskell.org/ghc/). To compile, just type make. To check that everything is OK, you can type make test. You should get zero Errors and zero Failures. GaBuZoMeu is distributed as free software, under the GNU General Public Licence. Remarks, patches and bug reports are welcome. Written and maintained by Stephane Bortzmeyer [EMAIL PROTECTED]. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Why is type 'b' forced to be type 'm a' and not possibly 'm a - m a'
Vivian McPhail wrote: class Forkable a where fork :: String - a - a - a What I would like to be able to do is differentiate between Forkable (m a - b) and Forkable (function type - b). Have you tried this combination of instances? instance Forkable (IO a) where ... -- and similarly for all the concrete -- monad types you will use fork with instance (Forkable a, Forkable b) = Forkable (a - b) where ... Alternatively, since the fork function seems to be all about propagating a value (the String), would Control.Monad.Reader serve your purpose? Regards, Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Numeric type classes
On 2006-09-12, Brian Hulley [EMAIL PROTECTED] wrote: Bryan Burgers wrote: That being said, I'll have to play the other side of the coin: it would probably be a little bit of a pain to have to define instances of each data declaration (Integer, Int, Float, Matrix, Complex, etc.) on each of these seperate classes--especially when being in a certain class usually implies being in another (ie, the definition of a set being a field requires that that set is a group, right?) Aaron Denney wrote: On 2006-09-12, Bryan Burgers [EMAIL PROTECTED] wrote: And another problem I can see is that, for example, the Integers are a group over addition, and also a group over multiplication; Not over multiplication, no, because there is no inverse. I know of no good way to express that a given data type obeys the same interface two (or more) ways. Some OO languages try to handle the case of of an abstract base class being inherited twice through two different intermediate classes, but none of them do it well. How about: data Multiply = Multiply data Add = Add class Group c e where group :: c - e - e - e identity :: c - e inverse :: c - e - e instance Group Multiply Rational where group Multiply x y = ... identity Multiply = 1 inverse Multiply x = ... instance Group Add Rational where group Add x y = ... identity Add = 0 inverse Add x = ... (+) :: Group Add a = a - a - a (+) = group Add (*) = group Multiply class (Group Multiply a, Group Add a) = Field a where ... It's not horrible, but it's somewhat cumbersome, much like passing around dictionaries explicitly is. -- Aaron Denney -- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Numeric type classes
On 2006-09-12, Jacques Carette [EMAIL PROTECTED] wrote: First, as already pointed out in http://www.haskell.org/pipermail/haskell-cafe/2006-April/015404.html there is a lot of relevant previous work in this area. I'm afraid I don't see the relevance. This is very easy to do in 'raw' category theory, as concepts are not _nominal_, so a functor from one type to another can explicitly do a renaming if necessary. Computer programming is of course extremely nominal to provide abstraction and seperation of concerns. Yes, anonymous functions are handy, but I could give them up if I had named local functions. Yes, you can even go to unlambda and only use combinators. Practically we find names extremely useful. Various algebraic specification languages have thus adopted this too, so that you are not forced to give unique names to all your concepts, you can in fact give them meaningful names 'in context', and use a remapping when you want to say that you obey a particular interface. This sounds neat, but I'd be worried about how cumbersome it was in practice. This is an old conversation, see http://www.haskell.org/pipermail/haskell/2005-October/016621.html for example. Thanks. The ML interface paper looks quite interesting. Are you aware of any implementations? -- Aaron Denney -- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Java or C to Haskell
I'm trying to write in Haskell a function that in Java would be something like this: char find_match (char[] l1, char[] l2, char e){ //l1 and l2 are not empty int i = 0; while (l2){ char aux = l2[i]; char[n] laux = l2; while(laux){ int j = 0; if(laux[j] = aux) laux[j] = e; j++; } if compare (l1, laux) return aux; else i++; } return ''; } compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. It works like this: find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) I'm trying to use map for scanning the list l2, then using some function for substitute the char, and finally something like zipWith x == y laux l1 for comparing the lists, but I can't find a way for putting all this together!! -- View this message in context: http://www.nabble.com/Java-or-C-to-Haskell-tf2303820.html#a6403589 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
crespi.albert: I'm trying to write in Haskell a function that in Java would be something like this: char find_match (char[] l1, char[] l2, char e){ //l1 and l2 are not empty int i = 0; while (l2){ char aux = l2[i]; char[n] laux = l2; while(laux){ int j = 0; if(laux[j] = aux) laux[j] = e; j++; } if compare (l1, laux) return aux; else i++; } return ''; } Yikes! compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. It works like this: find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) That's almost a spec there :) How about: import Data.List findMatch s t c | Just n - elemIndex c s = Just (t !! n) | otherwise = Nothing Using it in GHCi: findMatch 4*ha 4*5a 'h' Just '5' findMatch 4*ns 4dhnn 'k' Nothing -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
On Wed, Sep 20, 2006 at 01:31:22AM -0700, Carajillu wrote: I'm trying to write in Haskell a function that in Java would be something like this: char find_match (char[] l1, char[] l2, char e){ //l1 and l2 are not empty int i = 0; while (l2){ char aux = l2[i]; char[n] laux = l2; while(laux){ int j = 0; if(laux[j] = aux) laux[j] = e; j++; } if compare (l1, laux) return aux; else i++; } return ''; } compare function just compares the two lists and return true if they are equal, or false if they are not. I know that this is far too simple. But I'm simple minded: comp [] [] = True comp (x:xs) (y:ys) = if x == y then comp xs ys else False andrea ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
dons: crespi.albert: I'm trying to write in Haskell a function that in Java would be something like this: char find_match (char[] l1, char[] l2, char e){ //l1 and l2 are not empty int i = 0; while (l2){ char aux = l2[i]; char[n] laux = l2; while(laux){ int j = 0; if(laux[j] = aux) laux[j] = e; j++; } if compare (l1, laux) return aux; else i++; } return ''; } Yikes! compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. It works like this: find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) That's almost a spec there :) Ah, I see I misread the spec :) Time for some tea. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
On Wed, Sep 20, 2006 at 01:31:22AM -0700, Carajillu wrote: compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. I forgot, obviously, that lists are an instance of the Eq class... so, this is enough: comp l1 l2 = if l1 == l2 then True else False You never stop learning! andrea ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
wow, the simpliest ever! Andrea Rossato wrote: On Wed, Sep 20, 2006 at 01:31:22AM -0700, Carajillu wrote: compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. I forgot, obviously, that lists are an instance of the Eq class... so, this is enough: comp l1 l2 = if l1 == l2 then True else False You never stop learning! andrea ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- View this message in context: http://www.nabble.com/Java-or-C-to-Haskell-tf2303820.html#a6404305 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
mailing_list: On Wed, Sep 20, 2006 at 01:31:22AM -0700, Carajillu wrote: compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. I forgot, obviously, that lists are an instance of the Eq class... so, this is enough: comp l1 l2 = if l1 == l2 then True else False You never stop learning! andrea which you would just write as: comp = (==) and then you'd just use == anyway :) -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
That works good, but I have a problem with the return type, I forgot to mention... can it be a [char]?? Donald Bruce Stewart wrote: crespi.albert: I'm trying to write in Haskell a function that in Java would be something like this: char find_match (char[] l1, char[] l2, char e){ //l1 and l2 are not empty int i = 0; while (l2){ char aux = l2[i]; char[n] laux = l2; while(laux){ int j = 0; if(laux[j] = aux) laux[j] = e; j++; } if compare (l1, laux) return aux; else i++; } return ''; } Yikes! compare function just compares the two lists and return true if they are equal, or false if they are not. it is really a simple function, but I've been thinking about it a lot of time and I can't get the goal. It works like this: find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) That's almost a spec there :) How about: import Data.List findMatch s t c | Just n - elemIndex c s = Just (t !! n) | otherwise = Nothing Using it in GHCi: findMatch 4*ha 4*5a 'h' Just '5' findMatch 4*ns 4dhnn 'k' Nothing -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- View this message in context: http://www.nabble.com/Java-or-C-to-Haskell-tf2303820.html#a6404324 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
Yes, they must be equal the whole way, I like this recursive solution :) Ketil Malde-3 wrote: Carajillu [EMAIL PROTECTED] writes: compare function just compares the two lists and return true if they are equal, or false if they are not. find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) Must they be equal the whole way, or just up to the occurrence of the searched-for character? find_match (x:xs) (y:ys) c | x==c = Just y | x/=y = Nothing | True = find_match xs ys c find_match [] [] _ = Nothing Or, to check the whole list: find_match (x:xs) (y:ys) c | x==c xs == ys = Just y | x/=y = Nothing | True = find_match xs ys c find_match [] [] _ = Nothing -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- View this message in context: http://www.nabble.com/Java-or-C-to-Haskell-tf2303820.html#a6404344 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
Andrea Rossato [EMAIL PROTECTED] writes: I forgot, obviously, that lists are an instance of the Eq class... so, this is enough: comp l1 l2 = if l1 == l2 then True else False Or why not: comp l1 l2 = l1 == l2 Or simply: comp = (==) :-) -k -- If I haven't seen further, it is by standing in the footprints of giants ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
On Wed, Sep 20, 2006 at 07:20:23PM +1000, Donald Bruce Stewart wrote: comp l1 l2 = if l1 == l2 then True else False You never stop learning! andrea which you would just write as: comp = (==) and then you'd just use == anyway :) this is why I came to love haskell: it remembers me when I was doing mathematics at the high school. The most challenging stuff, for me, was finding a way to simplifying expressions... It's just an endless chess game. Sending mails to this mailing list, I mean. Andrea ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] System threads?
On Tue, 2006-09-19 at 19:45 -0700, Lyle Kopnicky wrote: Hi folks, I'm working on a project for which the solution is highly parallelizable. I've been writing it so far for GHC as a single-threaded app. I'd like to be able to split the job into multiple pieces, and spawn different system threads for each piece, so they will run on separate CPUs. Either each thread needs to write to the same IO handle, or else the results need to be collected when the threads are merged, so the results can all be printed. I'm working on Linux. What's the easiest way to accomplish this? Glasgow Parallel Haskell? Is there a Posix.Threads library? I understand that Haskell threads (Control.Concurrent) all run in one system thread, so would not take advantage of SMP. In GHC 6.6 they can run on more than one system thread so take advantage of SMP. GHC 6.6 will be out soon but you can start now by trying a recent day's 6.5 snapshot. See the GHC website's download section. Duncan ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Optimization problem
On Tue, Sep 19, 2006 at 08:06:07PM +0200, [EMAIL PROTECTED] wrote: For our optimization problem, it's only a matter of constructors on the right hand side. They should pop out before do not look on any arguments, so it's safe to cry so you just know, i'm a Just. It seems the appropriate encoding would be for the shape to be an inductive datatype and the contents (as well as the lists) to be coinductive, as access to the contents is gated through the shape. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] sections for record settors
Hello Tim, Wednesday, September 20, 2006, 1:28:47 AM, you wrote: rec {field=val} but there is no settor function. It would be nice if there was some sort of section-like syntax to access the settor function, like: you can use DriFT which generates setter, isA and many other functions (or you can use Template Haskell to implement it yourself and share your implementation with us :) http://repetae.net/john/computer/haskell/DrIFT/drop/DrIFT-2.1.1.tar.gz http://repetae.net/john/computer/haskell/DrIFT/drift.ps -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Java or C to Haskell
Carajillu [EMAIL PROTECTED] writes: That works good, but I have a problem with the return type, I forgot to mention... can it be a [char]?? If that's what you want, how about this: import Maybe find_match l1 l2 c = fmap catMaybes . sequence $ zipWith match l1 l2 where match a b | a == c = Just (Just b) | a == b = Just Nothing | otherwise = Nothing although part of the reason for writing it like that is to make you work hard to understand it ;-) ... and it returns Maybe [Char] since I can't bring myself to use to indicate failure... but judicious use of catMaybes . concat. maybeToList might help with that. -- Jón Fairbairn [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
... and if you want to search strings not single characters: findmatch s t e = take m . drop n $ t where m' = length e (n, m) = f 0 s f i s | take m' s == e = (i, m') | null s = (0, 0) | otherwise = f (i+1) (tail s) findmatch asdfasdf asdfxvdf fas == fxv (this one skips equality checks before *and* after the match. feel free post the necessary modifications. :) matthias On Wed, Sep 20, 2006 at 02:22:29AM -0700, Carajillu wrote: To: haskell-cafe@haskell.org From: Carajillu [EMAIL PROTECTED] Date: Wed, 20 Sep 2006 02:22:29 -0700 (PDT) Subject: Re: [Haskell-cafe] Java or C to Haskell Yes, they must be equal the whole way, I like this recursive solution :) Ketil Malde-3 wrote: Carajillu [EMAIL PROTECTED] writes: compare function just compares the two lists and return true if they are equal, or false if they are not. find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) Must they be equal the whole way, or just up to the occurrence of the searched-for character? find_match (x:xs) (y:ys) c | x==c = Just y | x/=y = Nothing | True = find_match xs ys c find_match [] [] _ = Nothing Or, to check the whole list: find_match (x:xs) (y:ys) c | x==c xs == ys = Just y | x/=y = Nothing | True = find_match xs ys c find_match [] [] _ = Nothing -k signature.asc Description: Digital signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Why is type 'b' forced to be type 'm a' and not possibly 'm a - m a'
Vivian McPhail wrote: class Forkable a where fork :: String - a - a - a What I would like to be able to do is differentiate between Forkable (m a - b) and Forkable (function type - b). Have you tried this combination of instances? instance Forkable (IO a) where ... -- and similarly for all the concrete -- monad types you will use fork with instance (Forkable a, Forkable b) = Forkable (a - b) where ... Alternatively, since the fork function seems to be all about propagating a value (the String), would Control.Monad.Reader serve your purpose? The value that gets 'forked' is not actually the string, it is the result of a monadic computation. Regards, Tom I have tried: instance Forkable (USM NRef) where... Which is my Monad The problem lies with instance (Forkable a, Forkable b) = Forkable (a - b) where fork n a1 a2 a = fork n (a1 a) (a2 a) because I need the arg a to be evaluated before it gets passed to a1 and a2. This definition does the right thing when type 'a' is a function type, because it is not a value, but with something like 'm a - (m a - m a) - m a' with Forkable (a - b) the first arg gets evaluated twice, to be more concrete: With (and golden white) eggs I want the 'eggs' that is passed to 'golden' to be the same as the 'eggs' that is passed to 'white', i.e. - and1 (golden2 eggs3) (white4 eggs3) and not - and1 (golden2 eggs3) (white4 eggs5) So to do this I need to be able to recognise the case where the 'a' of (a - b) is of type 'm a' so that I can evaluate it -- doesn't typecheck instance (Monad m, Forkable (m a), Forkable b) = Forkable (m a - b) where fork n a1 a2 a = do a' - a fork n (a1 $ return a') (a2 $ return a') Tom suggested that I might be able to use the Reader monad, but I'm not clear as to how I could do this. Cheers, Vivian -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.405 / Virus Database: 268.12.5/451 - Release Date: 19/09/2006 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Trying to write a TCP proxy
Hello, I'm trying to use haskell to put together a TCP proxy I can put between my browser and my webserver. This is as far as I got. The webserver isn't returning my request: listen = withSocketsDo $ do putStrLn Listening... socket - listenOn $ PortNumber 8082 (handleToClient, hostName, portNumber) - accept socket putStrLn Connected to client contents - hGetContents handleToClient putStrLn Received from client: putStrLn contents handleToServer - connectTo 127.0.0.1 (PortNumber 8080) putStrLn Connected to server hPutStrLn handleToServer contents putStrLn Waiting for server to respond hPutStrLn handleToServer \r\n\r\n putStrLn Waiting for server to respond result - hGetContents handleToServer putStrLn result hClose handleToServer hClose handleToClient sClose socket Am I doing something wrong? Thanks, -John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Trying to write a TCP proxy
Actually, it blocks on: putStrLn contents It even blocks if I replace it with: print $ length contents Is there some kind of magic happening here? -John On 9/20/06, John Ky [EMAIL PROTECTED] wrote: Hello, I'm trying to use haskell to put together a TCP proxy I can put between my browser and my webserver. This is as far as I got. The webserver isn't returning my request: listen = withSocketsDo $ do putStrLn Listening... socket - listenOn $ PortNumber 8082 (handleToClient, hostName, portNumber) - accept socket putStrLn Connected to client contents - hGetContents handleToClient putStrLn Received from client: putStrLn contents handleToServer - connectTo 127.0.0.1 (PortNumber 8080) putStrLn Connected to server hPutStrLn handleToServer contents putStrLn Waiting for server to respond hPutStrLn handleToServer \r\n\r\n putStrLn Waiting for server to respond result - hGetContents handleToServer putStrLn result hClose handleToServer hClose handleToClient sClose socket Am I doing something wrong? Thanks, -John ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Trying to write a TCP proxy
On Wed, 20 Sep 2006, John Ky wrote: Actually, it blocks on: putStrLn contents It even blocks if I replace it with: print $ length contents Is there some kind of magic happening here? No, but you're trying to do magic - it can't get all of contents until the connection's dropped. -- [EMAIL PROTECTED] A problem that's all in your head is still a problem. Brain damage is but one form of mind damage. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to write a TCP proxy
Hello John, Wednesday, September 20, 2006, 3:59:36 PM, you wrote: I'm trying to use haskell to put together a TCP proxy I can put between my browser and my webserver. This is as far as I got. The webserver isn't returning my request: hSetBuffering handleToServer LineBuffering may help -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to write a TCP proxy
Hi Bulat, Thanks. Yes it helps with an earlier implementation I wrote (below). But surely there must be a better way to write this. My code is way to verbose. -John --- doProxyServer handleToClient handleToServer = do eof - hIsEOF handleToServer if not eof then do ready - hReady handleToServer if ready then do text - hGetChar handleToServer putChar text hPutChar handleToClient text doProxyServer handleToClient handleToServer else do hPutStr handleToClient \n doProxyClient handleToClient handleToServer else do hClose handleToServer hClose handleToClient doProxyClient handleToClient handleToServer = do ready - hReady handleToClient if ready then do text - hGetChar handleToClient putChar text hPutChar handleToServer text doProxyClient handleToClient handleToServer else do hPutStr handleToServer \n doProxyServer handleToClient handleToServer listen = withSocketsDo $ do putStrLn Listening... socket - listenOn $ PortNumber 8082 (handleToClient, hostName, portNumber) - accept socket putStrLn Connected to client handleToServer - connectTo localhost (PortNumber 8080) hSetBuffering handleToServer LineBuffering putStrLn Connected to server doProxyClient handleToClient handleToServer hClose handleToServer putStrLn Closed server hClose handleToClient putStrLn Closed client sClose socket main = listen On 9/20/06, Bulat Ziganshin [EMAIL PROTECTED] wrote: Hello John, Wednesday, September 20, 2006, 3:59:36 PM, you wrote: I'm trying to use haskell to put together a TCP proxy I can put between my browser and my webserver. This is as far as I got. The webserver isn't returning my request: hSetBuffering handleToServer LineBuffering may help -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Trying to write a TCP proxy
Hi again, Given that putStrLn contents did manage to print out the HTTP header before blocking, am I correct in coming to the conlusion that 'contents' is evaluated lazily? So Monads don't actually eliminate laziness? -John On 9/20/06, Philippa Cowderoy [EMAIL PROTECTED] wrote: On Wed, 20 Sep 2006, John Ky wrote: Actually, it blocks on: putStrLn contents It even blocks if I replace it with: print $ length contents Is there some kind of magic happening here? No, but you're trying to do magic - it can't get all of contents until the connection's dropped. -- [EMAIL PROTECTED] A problem that's all in your head is still a problem. Brain damage is but one form of mind damage. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ICFP programming contest 2006 results: video stream
Malcolm Wallace has recorded the ICFP programming contest results announcement as video, straight from the ICFP conference in Portland. He's posted it to Google Video, and it's available to download (120M) or stream from Google video, here: http://video.google.com/videoplay?docid=6419094369756184531 Thanks Malcolm! -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Java or C to Haskell
How about something like this? import Data.List findMatch xs ys k = lookup k . concat $ zipWith zip (substrings xs) (substrings ys) where substrings = nonempty . map (nonempty . inits) . tails where nonempty = filter (not . null) On 20/09/06, Matthias Fischmann [EMAIL PROTECTED] wrote: ... and if you want to search strings not single characters: findmatch s t e = take m . drop n $ t where m' = length e (n, m) = f 0 s f i s | take m' s == e = (i, m') | null s = (0, 0) | otherwise = f (i+1) (tail s) findmatch asdfasdf asdfxvdf fas == fxv (this one skips equality checks before *and* after the match. feel free post the necessary modifications. :) matthias On Wed, Sep 20, 2006 at 02:22:29AM -0700, Carajillu wrote: To: haskell-cafe@haskell.org From: Carajillu [EMAIL PROTECTED] Date: Wed, 20 Sep 2006 02:22:29 -0700 (PDT) Subject: Re: [Haskell-cafe] Java or C to Haskell Yes, they must be equal the whole way, I like this recursive solution :) Ketil Malde-3 wrote: Carajillu [EMAIL PROTECTED] writes: compare function just compares the two lists and return true if they are equal, or false if they are not. find_match 4*ha 4*5a 'h' returns '5' (5 matches with the h) find_match 4*ns 4dhnn k returns '' (no match at all - lists are different anyway) Must they be equal the whole way, or just up to the occurrence of the searched-for character? find_match (x:xs) (y:ys) c | x==c = Just y | x/=y = Nothing | True = find_match xs ys c find_match [] [] _ = Nothing Or, to check the whole list: find_match (x:xs) (y:ys) c | x==c xs == ys = Just y | x/=y = Nothing | True = find_match xs ys c find_match [] [] _ = Nothing -k -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.1 (GNU/Linux) iD8DBQFFER17TXPx/Y0ym6oRAvNZAKCrLeJQxP0PjJAOz2KDi/S0hi7/ywCeMOfH XIOJJcMs9yFsg2IajkmHX7Y= =+bkI -END PGP SIGNATURE- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Numeric type classes
Whenever people start discussing the Numeric type classes, the true scope of what a refactoring can (and should?) be is frequently under-estimated. The 'structure' of algebraic objects in mathematics has been studied quite a lot (in mathematics and in CS, but not so much by programming language people it seems). So I point out work like http://www-sop.inria.fr/cafe/Manuel.Bronstein/libaldor/html/ which already has a richer set of type classes, and that's just Aldor's prelude. When you get going, you get the Algebra library (http://www-sop.inria.fr/cafe/Manuel.Bronstein/algebra/) which is _huge_. And most of the discussion on Numeric has been around the algebraic (Monoid, Ring, Normed, etc) structures that Numeric right now 'hides'. [Hopefully this answers your 'relevance' question]. Computer programming is of course extremely nominal to provide abstraction and seperation of concerns. Yes, anonymous functions are handy, but I could give them up if I had named local functions. Yes, you can even go to unlambda and only use combinators. Practically we find names extremely useful. I am NOT arguing for no names! I also like names. What I am arguing for is to a) be able to use names whenever convenient and more importantly b) be able to provide _renamings_ when previously chosen names are _inconvenient_. In many ways, this is what ML's with type foo = bar qualifiers allow you to do to a certain extent when putting together modules/functors. It is also the basic idea behind the Adaptor and the Proxy patterns in OO. All these solve the same problem: how do you get around the issue that names in a module/class/whatever have been chosen in one way, and you need to use them in another. Various algebraic specification languages have thus adopted this too, so that you are not forced to give unique names to all your concepts, you can in fact give them meaningful names 'in context', and use a remapping when you want to say that you obey a particular interface. This sounds neat, but I'd be worried about how cumbersome it was in practice. In practice, name clashes do not appear that often, so unique names are quite common. Name clashes tend to appear only for the most basic concepts that are highly polymorphic (like Monoid and Group!). But the same happens with generalized Container data-structures too (you can 'push' onto both a Stack and a Queue, but might want to use different names even though the concepts are essentially the same). It appears to work quite well. See Specware http://www.specware.org/index.html and many of the splendid papers available at http://www.kestrel.edu/home/publications/ Another line of work is Maude (http://maude.cs.uiuc.edu/), with explicit renamings http://maude.cs.uiuc.edu/maude2-manual/html/node78.html and more importantly VIEWs http://maude.cs.uiuc.edu/maude2-manual/html/node81.html (which have been talked about a lot on the various Haskell mailing lists, but Maude has had it implemented for quite some time). There are plenty of others, like CASL (http://www.cofi.info/CASL.html) and the OBJ family (http://cseclassic.ucsd.edu/~goguen/sys/obj.html) with similar features. In other words, the specification language people have been down this road quite some time ago, and seem to have worked out a fair bit of it. PL people should now liberally borrow all these good ideas IMNSHO. Thanks. The ML interface paper looks quite interesting. Are you aware of any implementations? No - but pressure is slowly building to do so. It is not an easy task, but as the Ocaml developers themselves are discovering as they are heavily 'functorising' some of their legacy code, there is a real need. I would be willing to believe that if there was a real push to use common type classes across GHC/Hugs/nhc/etc, the same phenomenon would 'appear'. Jacques ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Numeric type classes
On 2006-09-20, Jacques Carette [EMAIL PROTECTED] wrote: [Hopefully this answers your 'relevance' question]. Yes. I was focusing on the more narrow aspect, rather than what had started this thread. In other words, the specification language people have been down this road quite some time ago, and seem to have worked out a fair bit of it. PL people should now liberally borrow all these good ideas IMNSHO. Thank you for the references. -- Aaron Denney -- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Trying to write a TCP proxy
On Thu, Sep 21, 2006 at 12:26:03AM +1000, John Ky wrote: Given that putStrLn contents did manage to print out the HTTP header before blocking, am I correct in coming to the conlusion that 'contents' is evaluated lazily? hGetContents breaks the rules of the IO monad - it returns a value (the contents list) that is not entirely pure - evaluation of this value causes execution of IO actions, namely reading from the handle. This way of operation is not a rule, but an exception. BTW, try to implement hGetContents in pure Haskell 98, using only the functions specified there, but excluding (hGetContents, getContents, readFile and interact). As a test try this code on a big or infinite input, like /dev/zero: import IO main = hGetContents stdin = print . take 100 hGetContents may cause some confusion, but it also is very handy. It lets you treat file contents as a lazy list, which allows you to use very high-level code to process the data without having to load the whole file into memory. Best regards Tomasz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: MonadList?
Michael Shulman wrote: Ah, excellent. So it sounds like at least in Haskell prime, I'll probably be able to use MonadPlus to do what I want, because MaybeT and ErrorT will be instances of MonadOr instead. I'm not sure if this is part of Haskell Prime, though. -- Ashley Yakeley ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Either e Monad
On Tuesday 19 September 2006 09:40, Deokhwan Kim wrote: Albert Lai wrote: Deokhwan Kim [EMAIL PROTECTED] writes: Where is the Monad instance declaration of Either e? It is in Control.Monad.Error as well. Strange: the doc doesn't state it. Thanks a lot, Albert! I found the declaration in libraries/mtl/Control/Monad/Error.hs of the ghc source distribution: instance (Error e) = Monad (Either e) where return= Right Left l = _ = Left l Right r = k = k r fail msg = Left (strMsg msg) I think I have a problem that can be solved by using Either as a Monad. In my program I have the following function `update` which I would like to write shorter: type BoundVarsMap = [Binding] type Binding = (Name, Maybe Exp) type Name = String data Exp = Var Name | App Exp Exp | Abs Name Exp | ... -- update n e bvsMap returns Just the bvsMap where the first occurence of -- (n, _) has been updated to (n, Just e). Nothing is returned if n was not -- found in the bvsMap update :: Name - Exp - BoundVarsMap - Maybe BoundVarsMap update _ _ []= Nothing update n e ((bn, me):bvsMap) | n == bn = Just ((bn, Just e):bvsMap) | otherwise = update n e bvsMap = return . ((bn, me):) Example: update b (Var z) [(a, Nothing), (b, Nothing), (c, Nothing)] = Just [(a,Nothing),(b,Just (Var z)),(c,Nothing)] Example: update x (Var z) [(a, Nothing), (b, Nothing), (c, Nothing)] = Nothing My question is: can I write `update` shorter by abstracting over the recursion on the list? I thought I could do it by somehow folding some function over the bvsMap which encodes the n == bn case as Left ... and the otherwise case as Right ... Then somehow using sequence to combine the resulting list. But I can't get it to work. It's late now here in Holland (~02:00) and I'm losing my concentration ;-) So I will just ask if somebody knows a shorter version. Thanks, Bas van Dijk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Trouble with simple embedding of shell in haskell
I'm just tried writing a function to allow convenient embedding of shell commands, but I'm running into behavior I don't really understand somewhere at the intersection of lazy evaluation, IO, and threading. The function in question is: sh :: String - String - IO String sh cmd = \input - do (stdin, stdout, _, pid) - runInteractiveCommand cmd forkIO $ hPutStr stdin input hClose stdin -- get exit status to prevent zombie accumulation forkIO $ waitForProcess pid return () hGetContents stdout Simple uses like this work (prints hi and returns): return hi = sh cat = putStrLen However, this blocks: return hi = sh cat = sh cat = putStrLen this prints hi\nblar and returns return hi = sh cat = (\s - putStrLn blar sh cat s) = putStrLn and this always returns but sometimes prints nothing and sometimes prints hi\n sh echo hi there = sh cat = putStrLn All these were tried with ghc 6.4.1 compiled with -threaded Just wondering if anybody has a good explanation of this range of behavior and/or a working rewrite of this function. -George ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] [newbie] How to test this function?
I've written a function that looks similar to this one getList = find 5 where find 0 = return [] find n = do ch - getChar if ch `elem` ['a'..'e'] then do tl - find (n-1) return (ch : tl) else find n First, how do I fix the identation of the if then else? Second, I want to test this function, without hitting the filesystem. In C++ I would use a istringstream. I couldn't find a function that returns a Handle from a String. The closer thing that may work that I could find was making a pipe and convertind the file descriptor. Can I simplify that function to take it out of the IO monad? How? I thought about getContents, but that eats all input. Thanks in advance, Bruno ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [newbie] How to test this function?
br1: I've written a function that looks similar to this one getList = find 5 where find 0 = return [] find n = do ch - getChar if ch `elem` ['a'..'e'] then do tl - find (n-1) return (ch : tl) else find n First, how do I fix the identation of the if then else? getList = find 5 where find 0 = return [] find n = do ch - getChar if ch `elem` ['a'..'e'] then do tl - find (n-1) return (ch : tl) else find n Second, I want to test this function, without hitting the filesystem. In C++ I would use a istringstream. I couldn't find a function that returns a Handle from a String. The closer thing that may work that I could find was making a pipe and convertind the file descriptor. Can I simplify that function to take it out of the IO monad? How? I thought about getContents, but that eats all input. Refactor! The reason your getList is hard to test, is that you're mixing side effecting monadic code with pure computations. Let's untangle that, and then test the the referentially transparent parts simply with QuickCheck. And remember that since getContents uses lazy IO, it only eats as much input as you ask it to. So let's refactor this, partitioning off the side effecting IO code: getList :: IO [Char] getList = take5 `fmap` getContents -- a thin IO skin take5 :: [Char] - [Char] take5 = take 5 . filter (`elem` ['a'..'e']) -- the actual worker Now we can test the 'guts' of the algorithm, the take5 function, in isolation. Let's use QuickCheck. First we need an Arbitrary instance for the Char type -- this takes care of generating random Chars for us to test with. I'll restrict it to a range of nice chars just for simplicity: import Data.Char import Test.QuickCheck instance Arbitrary Char where arbitrary = choose ('\32', '\128') coarbitrary c = variant (ord c `rem` 4) So now we can write some simple tests. An easy one, a [Char] is equal to itself: *A quickCheck ((\s - s == s) :: [Char] - Bool) OK, passed 100 tests. Reversing twice is the identity: *A quickCheck ((\s - (reverse.reverse) s == s) :: [Char] - Bool) OK, passed 100 tests. Ok, so what properties does take5 have? Well, for one, the length of the string returned by take5 should be 5, no? *A quickCheck (\s - length (take5 s) == 5) Falsifiable, after 0 tests: Ah, but what if the input file is small :) Thanks quickCheck. Let's modify that then: *A quickCheck (\s - length (take5 s) = 5) OK, passed 100 tests. Ok good. You can probably come up with some more things to check for now. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [newbie] How to test this function?
dons: br1: Second, I want to test this function, without hitting the filesystem. In C++ I would use a istringstream. I couldn't find a function that returns a Handle from a String. The closer thing that may work that I could find was making a pipe and convertind the file descriptor. Can I simplify that function to take it out of the IO monad? How? I thought about getContents, but that eats all input. Oh, another thing to check would be that the correct characters are returned, such as: *A quickCheck (\s - all (`elem` ['a'..'e']) (take5 s)) OK, passed 100 tests. So for all strings QuickCheck produced, all Chars in the string returned by take5 where elements of ['a'..'e']. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [newbie] How to test this function?
dons: br1: Second, I want to test this function, without hitting the filesystem. In C++ I would use a istringstream. I couldn't find a function that returns a Handle from a String. The closer thing that may work that I could find was making a pipe and convertind the file descriptor. Can I simplify that function to take it out of the IO monad? How? I thought about getContents, but that eats all input. I've summarised this little introduction to QuickCheck on the haskell.org wiki here, http://haskell.org/haskellwiki/Introduction_to_QuickCheck -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe