Patrick Premont | 5 Nov 20:12 2012

Partial application in case expression rules

Occasionally I find it would be nice to be able to omit some or all constructor parameters from a pattern matching rule.

Perhaps the compiler could put them in for you and apply them to the right hand side of the rule ?

 

data Tree a = Leaf a| Node (Tree a) (Tree a)

 

treeFold :: (a -> a -> a) -> Tree a -> a

treeFold f = \case

  Leaf ->  id

  Node -> f `on` treeFold f

 

This could be desugared at some point to

 

treeFold f = \case

  Leaf a ->  id a

  Node a b -> (f `on` treeFold f) a b

 

I suppose such an extension has been considered before, but I could not find such a discussion.

(In http://haskell.1045720.n5.nabble.com/Partial-pattern-matching-td3087200.html the thread focused instead on case expressions over functions)

 

Any pointers to previous discussions ? Comments ?

 

Thank you!

 

Patrick Prémont

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Stephen Tetley | 5 Nov 22:11 2012
Picon

Re: Partial application in case expression rules

There's a Glasgow extension that gets you to this:

treeFold :: (a -> a -> a) -> Tree a -> a
treeFold f = \case
  Leaf {} ->  id
  Node {} -> f `on` treeFold f

Or maybe this if parens are needed:

treeFold :: (a -> a -> a) -> Tree a -> a
treeFold f = \case
  (Leaf {}) ->  id
  (Node {}) -> f `on` treeFold f
Stephen Tetley | 5 Nov 22:15 2012
Picon

Re: Partial application in case expression rules

Whoops - replied in haste...

There is a long extant GHC extension to elide constructor arguments

f (Leaf {}) = ...
f (Node {}) = ...

There is a recent extension for "lambda case" which you were also
using and I didn't look closely at.

On 5 November 2012 21:11, Stephen Tetley <stephen.tetley <at> gmail.com> wrote:
> There's a Glasgow extension that gets you to this:
>
> treeFold :: (a -> a -> a) -> Tree a -> a
> treeFold f = \case
>   Leaf {} ->  id
>   Node {} -> f `on` treeFold f
>
> Or maybe this if parens are needed:
>
> treeFold :: (a -> a -> a) -> Tree a -> a
> treeFold f = \case
>   (Leaf {}) ->  id
>   (Node {}) -> f `on` treeFold f
Brandon Allbery | 5 Nov 22:29 2012
Picon

Re: Partial application in case expression rules

On Mon, Nov 5, 2012 at 4:15 PM, Stephen Tetley <stephen.tetley <at> gmail.com> wrote:
There is a long extant GHC extension to elide constructor arguments
f (Leaf {}) = ...
f (Node {}) = ...

I don't think that's an extension, it falls out directly from how Haskell builds records on top of ADTs and is specified in the standard as such.  See http://www.haskell.org/onlinereport/decls.html at the end of the "Labelled Fields" section for Haskell98, or the middle of section 3.15.2 in http://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-490003.15 for the H'2010 version.

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b <at> gmail.com                                  ballbery <at> sinenomine.net
unix/linux, openafs, kerberos, infrastructure          http://sinenomine.net

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Patrick Premont | 6 Nov 00:04 2012

Re: Partial application in case expression rules

I had not thought of this record syntax. 
But it does not perform the currying effect shown in the example; it just drops the omitted fields.

Brandon Allbery <allbery.b <at> gmail.com> wrote:

>On Mon, Nov 5, 2012 at 4:15 PM, Stephen Tetley <stephen.tetley <at> gmail.com>wrote:
>
>> There is a long extant GHC extension to elide constructor arguments
>>
>f (Leaf {}) = ...
>> f (Node {}) = ...
>>
>
>I don't think that's an extension, it falls out directly from how Haskell
>builds records on top of ADTs and is specified in the standard as such.
> See http://www.haskell.org/onlinereport/decls.html at the end of the
>"Labelled Fields" section for Haskell98, or the middle of section 3.15.2 in
>http://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-490003.15for
>the H'2010 version.
>
>-- 
>brandon s allbery kf8nh                               sine nomine associates
>allbery.b <at> gmail.com                                  ballbery <at> sinenomine.net
>unix/linux, openafs, kerberos, infrastructure          http://sinenomine.net

Gmane