# Partial pattern matching

9 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Partial pattern matching

 In Haskell, a data constructor can be used partially applied:data Pair a b = P a bf = P 1however, I cannot do "partial pattern matching", e.g firstCoord (P x) = xdoes not work.I guess a very important reason must exist why this is the case? _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 The question is, is there some very important reason you can't do this?firstCoord (P x _) = x2009/3/9 Peter Verswyvelen In Haskell, a data constructor can be used partially applied:data Pair a b = P a b f = P 1however, I cannot do "partial pattern matching", e.g firstCoord (P x) = xdoes not work.I guess a very important reason must exist why this is the case? _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 In reply to this post by Peter Verswyvelen-2 P x is indistinguishable neither in compile-time nor in run-time from the value \y -> P x y. And pattern matching and equality on functions is, of course, undecidable. 2009/3/9 Peter Verswyvelen <[hidden email]>: > In Haskell, a data constructor can be used partially applied: > data Pair a b = P a b > f = P 1 > however, I cannot do "partial pattern matching", e.g > firstCoord (P x) = x > does not work. > I guess a very important reason must exist why this is the case? > > > > > > _______________________________________________ > Haskell-Cafe mailing list > [hidden email] > http://www.haskell.org/mailman/listinfo/haskell-cafe> > -- Eugene Kirpichov Web IR developer, market.yandex.ru _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 In reply to this post by Peter Verswyvelen-2 Peter Verswyvelen wrote: > In Haskell, a data constructor can be used partially applied: > data Pair a b = P a b > > f = P 1 > > however, I cannot do "partial pattern matching", e.g > > firstCoord (P x) = x > > does not work. > > I guess a very important reason must exist why this is the case? > What would be the type of firstCoord? Typically, you'd use data Pair a b = P { firstCoord :: a                   , secondCoord :: b                   } or firstCoord (P x _) = x secondCoord (P _ y) = y Regards, -- Jochem Berndsen | [hidden email] GPG: 0xE6FABFAB _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 In reply to this post by Eugene Kirpichov I mean, there is no way to write a firstCoord function so that it would work, for example, on '\y -> P 42 y' and yield 42. Except for this one: firstCoord proj = case (proj undefined) of P x y -> x However, this requires proj to be non-strict in its remaining argument. But this will actually work if you pass "P x" to it for some x, because it *is* non-strict in the remaining argument. 2009/3/9 Eugene Kirpichov <[hidden email]>: > P x is indistinguishable neither in compile-time nor in run-time from > the value \y -> P x y. > > And pattern matching and equality on functions is, of course, undecidable. > > 2009/3/9 Peter Verswyvelen <[hidden email]>: >> In Haskell, a data constructor can be used partially applied: >> data Pair a b = P a b >> f = P 1 >> however, I cannot do "partial pattern matching", e.g >> firstCoord (P x) = x >> does not work. >> I guess a very important reason must exist why this is the case? >> >> >> >> >> >> _______________________________________________ >> Haskell-Cafe mailing list >> [hidden email] >> http://www.haskell.org/mailman/listinfo/haskell-cafe>> >> > > > > -- > Eugene Kirpichov > Web IR developer, market.yandex.ru > -- Eugene Kirpichov Web IR developer, market.yandex.ru _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 In reply to this post by Peter Verswyvelen-2 Am Montag, 9. März 2009 17:30 schrieb Peter Verswyvelen: > In Haskell, a data constructor can be used partially applied: > data Pair a b = P a b > > f = P 1 > > however, I cannot do "partial pattern matching", e.g > > firstCoord (P x) = x > > does not work. > > I guess a very important reason must exist why this is the case? For one, the type. If x :: a, then P x :: b -> Pair a b, so we'd have firstCoord :: (b -> Pair a b) -> a But you can pattern-match only on constructors of the appropriate type. P is not a constructor of (b -> Pair a b) (function types don't have constructors), so you can't match on a partially applied constructor. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 Yes of course, P x is a function, and you can't pattern match against functions, I knew that. How silly of me, I could have guessed that myself. On Mon, Mar 9, 2009 at 5:43 PM, Daniel Fischer wrote: Am Montag, 9. März 2009 17:30 schrieb Peter Verswyvelen: > In Haskell, a data constructor can be used partially applied: > data Pair a b = P a b > > f = P 1 > > however, I cannot do "partial pattern matching", e.g > > firstCoord (P x) = x > > does not work. > > I guess a very important reason must exist why this is the case? For one, the type. If x :: a, then P x :: b -> Pair a b, so we'd have firstCoord :: (b -> Pair a b) -> a But you can pattern-match only on constructors of the appropriate type. P is not a constructor of (b -> Pair a b) (function types don't have constructors), so you can't match on a partially applied constructor. _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Partial pattern matching

 In reply to this post by Eugene Kirpichov On Mon, Mar 9, 2009 at 10:37 AM, Eugene Kirpichov wrote: I mean, there is no way to write a firstCoord function so that it would work, for example, on '\y -> P 42 y' and yield 42. Except for this one: firstCoord proj = case (proj undefined) of P x y -> x However, this requires proj to be non-strict in its remaining argument. But this will actually work if you pass "P x" to it for some x, because it *is* non-strict in the remaining argument.That's brilliant.  Any function which behaves like "P x" but is strict in the second argument is in fact a different function.  So this trick is a semidecidable pattern for this function. Bring in the unamb, so we can do interesting things with semidecidable predicates :-)   (I still don't like the proof obligation of unamb, and would like to see it picked up to a higher level of abstraction where the usage is always correct) Luke _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe