Quantcast

Partial pattern matching

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Partial pattern matching

Peter Verswyvelen-2
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Andrew Wagner
The question is, is there some very important reason you can't do this?

firstCoord (P x _) = x

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



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Eugene Kirpichov
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Jochem Berndsen-3
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Eugene Kirpichov
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Daniel Fischer-4
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Peter Verswyvelen-2
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 <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

Luke Palmer-2
In reply to this post by Eugene Kirpichov
On Mon, Mar 9, 2009 at 10:37 AM, Eugene Kirpichov <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Partial pattern matching

John Van Enk
In reply to this post by Peter Verswyvelen-2
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 <[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




--
/jve

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Loading...