
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?

The question is, is there some very important reason you can't do this?
firstCoord (P x _) = x
2009/3/9 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?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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
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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru

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
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
: 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 Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru
-- Eugene Kirpichov Web IR developer, market.yandex.ru

On Mon, Mar 9, 2009 at 10:37 AM, 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.
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

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 | jochem@functor.nl GPG: 0xE6FABFAB

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.

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
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.

You can use the record syntax to get around some of this:
data P { first :: Int, second :: Int }
firstCoord (P {first = f}) = f
2009/3/9 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?
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- /jve
participants (7)
-
Andrew Wagner
-
Daniel Fischer
-
Eugene Kirpichov
-
Jochem Berndsen
-
John Van Enk
-
Luke Palmer
-
Peter Verswyvelen