RE: [Haskell-cafe] View patterns
| However, as somebody pointed out, the Java version is polymorphic. | Assuming that length() is defined for multiple types of container, the | Java version works with lists, arrays, sets, etc. If you try to do this | in Haskell, you end up with A standard way to do it would be this: class ListLike c where lcase :: c a -> LCase a (c a) lcons :: a -> c a -> c a lnil :: c a data LCase a b = LNil | LCons a b f :: ListLike c => c (c Int) -> Int f (lcase -> LCons (lcase -> LCons x (lcase -> LNil)) (lcase -> LCons (lcase -> LCons y (lcase -> LCons _ (lcase -> LNil))) (lcase -> LNil))) = x+y Simon | -Original Message- | From: haskell-cafe-boun...@haskell.org [mailto:haskell-cafe-boun...@haskell.org] On | Behalf Of Andrew Coppin | Sent: 27 February 2010 18:11 | To: haskell-cafe@haskell.org | Subject: [Haskell-cafe] View patterns | | One somewhat neat thing about Haskell is that you can say | | case list of | [[x], [y,_], [z,_,_]] -> x + y + z | _ -> 0 | | In Java, you'd have to write something like | | if (list.length() == 3) | { | List t1 = list.at(0); | if (t1.length() == 1) | { | int x = t1.at(0); | List t2 = list.at(1); | if (t2.length() == 2) | ... | | I can't even be bothered to finish typing all that lot! | | However, as somebody pointed out, the Java version is polymorphic. | Assuming that length() is defined for multiple types of container, the | Java version works with lists, arrays, sets, etc. If you try to do this | in Haskell, you end up with | | case size c of | 3 -> | case (c ! 0, c ! 1, c ! 2) of | (xs, ys, zs) | size x == 1 && size y == 2 & size z == 3 -> (xs ! | 0) + (ys ! 0) + (zs ! 0) | _ -> 0 | _ -> 0 | | or similar. Which is shorter than Java, but nowhere near as nice as the | original list-only version. | | Now I was under the impression that "view patterns" fix this problem, | but it seems they don't: | | case c of | (size -> 3) -> | case (c ! 0, c ! 1, c ! 2) of | (size -> 1, size -> 2, size -> 3) -> (c ! 0 ! 0) + (c ! 1 ! 0) + | (c ! 2 ! 0) | | Any suggestions? | | ___ | Haskell-Cafe mailing list | Haskell-Cafe@haskell.org | http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns
On 28 February 2010 05:55, Andrew Coppin wrote: > It won't work for arbitrarily complex structures, however. My main point was > that if you make the constructors abstract and provide functions to query > the structure, now you can't pattern match against it. We do, however, have guards which helps reduce the boiler plate (nested if statements, etc.). -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns
Ozgur Akgun wrote: A humble suggestion: Have a *lazy* "to list" method for your /lists, arrays, sets, etc./ and use the nice list-only version. Yeah, that works quite nicely. It won't work for arbitrarily complex structures, however. My main point was that if you make the constructors abstract and provide functions to query the structure, now you can't pattern match against it. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns
A humble suggestion: Have a *lazy* "to list" method for your *lists, arrays, sets, etc.* and use the nice list-only version. On 27 February 2010 18:11, Andrew Coppin wrote: > One somewhat neat thing about Haskell is that you can say > > case list of > [[x], [y,_], [z,_,_]] -> x + y + z > _ -> 0 > > In Java, you'd have to write something like > > if (list.length() == 3) > { > List t1 = list.at(0); > if (t1.length() == 1) > { > int x = t1.at(0); > List t2 = list.at(1); > if (t2.length() == 2) > ... > > I can't even be bothered to finish typing all that lot! > > However, as somebody pointed out, the Java version is polymorphic. Assuming > that length() is defined for multiple types of container, the Java version > works with lists, arrays, sets, etc. If you try to do this in Haskell, you > end up with > > case size c of > 3 -> > case (c ! 0, c ! 1, c ! 2) of > (xs, ys, zs) | size x == 1 && size y == 2 & size z == 3 -> (xs ! 0) + > (ys ! 0) + (zs ! 0) > _ -> 0 > _ -> 0 > > or similar. Which is shorter than Java, but nowhere near as nice as the > original list-only version. > > Now I was under the impression that "view patterns" fix this problem, but > it seems they don't: > > case c of > (size -> 3) -> > case (c ! 0, c ! 1, c ! 2) of > (size -> 1, size -> 2, size -> 3) -> (c ! 0 ! 0) + (c ! 1 ! 0) + (c ! > 2 ! 0) > > Any suggestions? > > ___ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -- Ozgur Akgun ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns and warnings about overlapping or non-exhaustive patterns
Svein Ove Aas wrote: > [...] > > For the time being, it will *work*, you just won't get useful > warnings. Hopefully it's going to be fixed for 10.2. > Hmm I don't find #2395 anywhere on http://hackage.haskell.org/trac/ghc/milestone/6.10.2 :( //Stephan -- Früher hieß es ja: Ich denke, also bin ich. Heute weiß man: Es geht auch so. - Dieter Nuhr ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns and warnings about overlapping or non-exhaustive patterns
Hi Stephan, > I'm working on a data structure that uses Data.Sequence a lot, so views > are important and I tried to simplify my code using view patterns. > > The problem is, that I keep getting warnings about both overlapping and > non-exhaustive pattern matches. A simple test case: http://hackage.haskell.org/trac/ghc/ticket/2395 Add yourself to the CC list if it matters to you! Thanks Neil ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] View patterns and warnings about overlapping or non-exhaustive patterns
On Wed, Mar 11, 2009 at 5:22 PM, Stephan Friedrichs wrote: > Hi, > > I'm working on a data structure that uses Data.Sequence a lot, so views > are important and I tried to simplify my code using view patterns. > > The problem is, that I keep getting warnings about both overlapping and > non-exhaustive pattern matches. A simple test case: > The view pattern implementation is currently incomplete, specifically in that it is unable to decide whether a pattern match using them is overlapping or non-exhaustive. Arguably the warnings should be suppressed instead.. For the time being, it will *work*, you just won't get useful warnings. Hopefully it's going to be fixed for 10.2. -- Svein Ove Aas ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] view patterns
Hi everyone, Yes, the current overlap checker thinks all view patterns are overlapped. The pattern overlap/exhaustiveness checker needs to be rewritten to account for GADTs and view patterns. You can use -fno-warn-overlapping-patterns to suppress these warning (along with any actual overlaps, though, unfortunately). -Dan On Nov05, Cale Gibbard wrote: > 2008/11/5 Cetin Sert <[EMAIL PROTECTED]>: > > :1:4: > > Warning: Pattern match(es) are overlapped > > In the definition of `emp': > > emp ((has -> True)) = ... > > emp ((has -> False)) = ... > > > > Why do I get this error in ghc or when I try to compile a file with view > > patterns? > > (using -fglasgow-exts and -XViewPatterns, ghc 6.10.1) > > This is a bug which appears to be known about: > http://hackage.haskell.org/trac/ghc/ticket/2395 > > - Cale > ___ > 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] view patterns
2008/11/5 Cetin Sert <[EMAIL PROTECTED]>: > :1:4: > Warning: Pattern match(es) are overlapped > In the definition of `emp': > emp ((has -> True)) = ... > emp ((has -> False)) = ... > > Why do I get this error in ghc or when I try to compile a file with view > patterns? > (using -fglasgow-exts and -XViewPatterns, ghc 6.10.1) This is a bug which appears to be known about: http://hackage.haskell.org/trac/ghc/ticket/2395 - Cale ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] view patterns
Hello Cetin, Wednesday, November 5, 2008, 8:34:14 AM, you wrote: > let emp (has -> True) = False; emp (has -> False) = True > Warning: Pattern match(es) are overlapped proibably it's because GHC can't check view patterns for overlaps? -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe