[Haskell-cafe] Re: unary pattern matching
Simon Marlow
simonmarhaskell at gmail.com
Wed Feb 1 06:19:13 EST 2006
John Meacham wrote:
> On Fri, Jan 27, 2006 at 04:57:14AM -0500, Cale Gibbard wrote:
>> Or if we're going to allow @ as an infix operator, we could use (@
>> pat), reminiscent of section notation. (exp @) of course would make no
>> sense, seeing as there's no representation for patterns as values.
>
> oooh. I like that. what fixity do you think @ should have? perhaps even
> lower than $...
I like it too, but I discovered you can get pretty close with a type class:
--------
class Match a b where
(@@) :: a -> b -> Bool
instance Match a b => Match (Maybe a) (Maybe b) where
Just a @@ Just b = a @@ b
Nothing @@ Nothing = True
_ @@ _ = False
instance Match a () where
x @@ () = True
--------
The trick is that () behaves like a wildcard. eg:
*Main> Just undefined @@ Just ()
True
*Main> Just Nothing @@ Just ()
True
*Main> Just Nothing @@ Just (Just ())
False
*Main> Just Nothing @@ Just Nothing
<interactive>:1:13:
Ambiguous type variables `a', `a1' in the constraint:
`Match a a1' arising from use of `@@' at <interactive>:1:13-14
Probable fix: add a type signature that fixes these type variable(s)
*Main> Just (Just undefined) @@ Just ()
True
*Main> Just (Just undefined) @@ Just (Just ())
True
Cheers,
Simon
More information about the Haskell-Cafe
mailing list