row-types icon indicating copy to clipboard operation
row-types copied to clipboard

Allow label in pattern matching?

Open Wizek opened this issue 5 years ago • 1 comments

I've been reading the Example.hs file, and want to share some quick thoughts. This is a low priority ticket on my part, feel free to only respond to this days or weeks from now at your leisure. I'm writing now because I don't want to forget about it.

So, reading this section:

get2D'' :: (r ≈ "x" .== Double .+ "y" .== Double, Disjoint r rest)
        => Rec (r .+ rest)
        -> Rec r
get2D'' ((Label :: Label "x") :== n1 :+ (Label :: Label "y") :== n2 :+ _)
          = #x .== n1 .+ #y .== n2

It would indeed be much nicer if we could just write:

get2D'' :: (r ≈ "x" .== Double .+ "y" .== Double, Disjoint r rest)
        => Rec (r .+ rest)
        -> Rec r
get2D'' (#x :== n1 :+ #y :== n2 :+ _)
          = #x .== n1 .+ #y .== n2

Trying to do this now, we get:

> :t \(#a) -> 1
<interactive>:1:3: error: Parse error in pattern: #a

A parse error. So, it might be quite trivial to fix this upstream in GHC's parser, just make #a de-sugar to (Label :: Label "a") in a pattern context as well. And I am guessing/hoping/wondering that GHC folks would be very happy to take this contribution. I've briefly looked if there may be a trac ticket even open about this, but I haven't found any yet. So maybe the only reason this isn't working is because no one was inconvenienced by it enough to fix it or raise it.

What do you think?

Wizek avatar Jul 26 '18 12:07 Wizek

It would be great if this were implemented. There is https://ghc.haskell.org/trac/ghc/ticket/13116, but no work has been done on it.

dwincort avatar Jul 26 '18 14:07 dwincort