
I have often wanted a shorthand syntax for testing if a value matches a given pattern. I want to implement such an extension for jhc but can't decide an appropriate syntax so I thought I'd ask the group. basically I want something like /Left (Just _)/ expands to \x -> case x of Left (Just _) -> True _ -> False so you can do things like when (/Just _/ x) $ putStrLn "x is something" or map /Left (Foo _ 'x')/ xs to get a list of booleans saying whether the xs match or not. however, the '/' syntax clearly doesn't work, nor would anything that conflicts with normal haskell syntax. does anyone have any better ideas? John -- John Meacham - ⑆repetae.net⑆john⑈

john:
I have often wanted a shorthand syntax for testing if a value matches a given pattern. I want to implement such an extension for jhc but can't decide an appropriate syntax so I thought I'd ask the group. basically I want something like
/Left (Just _)/ expands to
\x -> case x of Left (Just _) -> True _ -> False
Something like pattern guards? f x | Just _ <- x = putStrLn "something" -- Don

On Fri, Jan 27, 2006 at 12:28:23PM +1100, Donald Bruce Stewart wrote:
john:
I have often wanted a shorthand syntax for testing if a value matches a given pattern. I want to implement such an extension for jhc but can't decide an appropriate syntax so I thought I'd ask the group. basically I want something like
/Left (Just _)/ expands to
\x -> case x of Left (Just _) -> True _ -> False
Something like pattern guards?
f x | Just _ <- x = putStrLn "something"
hmm.. how about (| Left (Just _) |) since | cannot be used as a section so it can be unambigously lexed as a different symbol. I think I like it. any other ideas? John -- John Meacham - ⑆repetae.net⑆john⑈

John Meacham
hmm.. how about
(| Left (Just _) |)
since | cannot be used as a section so it can be unambigously lexed as a different symbol. I think I like it. any other ideas?
Not sure if it matters, but the 'banana bracket' syntax is already used by the arrows extension: http://darcs.haskell.org/darcsweb/darcsweb.cgi?r=testsuite;a=headblob;f=/tes... http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/testsuite/tests/ghc-regres... -- Shae Matijs Erisson - http://www.ScannedInAvian.com/ - Sockmonster once said: You could switch out the unicycles for badgers, and the game would be the same.

John Meacham wrote:
On Fri, Jan 27, 2006 at 12:28:23PM +1100, Donald Bruce Stewart wrote:
john:
I have often wanted a shorthand syntax for testing if a value matches a given pattern. I want to implement such an extension for jhc but can't decide an appropriate syntax so I thought I'd ask the group. basically I want something like
/Left (Just _)/ expands to
\x -> case x of Left (Just _) -> True _ -> False
Something like pattern guards?
f x | Just _ <- x = putStrLn "something"
hmm.. how about
(| Left (Just _) |)
since | cannot be used as a section so it can be unambigously lexed as a different symbol. I think I like it. any other ideas?
John
(@ Left (Just _) @) would fit in with the use of @ in as-patterns Also, the as-pattern syntax could be stolen for expressions so that exp@pat would evaluate to True or False when this syntactic form appears in an expression instead of a pattern ie to give the following equivalence: (@ pat @) exp === exp @ pat Regards, Brian.

On 27/01/06, Brian Hulley
John Meacham wrote:
On Fri, Jan 27, 2006 at 12:28:23PM +1100, Donald Bruce Stewart wrote:
john:
I have often wanted a shorthand syntax for testing if a value matches a given pattern. I want to implement such an extension for jhc but can't decide an appropriate syntax so I thought I'd ask the group. basically I want something like
/Left (Just _)/ expands to
\x -> case x of Left (Just _) -> True _ -> False
Something like pattern guards?
f x | Just _ <- x = putStrLn "something"
hmm.. how about
(| Left (Just _) |)
since | cannot be used as a section so it can be unambigously lexed as a different symbol. I think I like it. any other ideas?
John
(@ Left (Just _) @) would fit in with the use of @ in as-patterns Also, the as-pattern syntax could be stolen for expressions so that exp@pat would evaluate to True or False when this syntactic form appears in an expression instead of a pattern ie to give the following equivalence:
(@ pat @) exp === exp @ pat
Regards, Brian.
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. - Cale

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 $... John -- John Meacham - ⑆repetae.net⑆john⑈

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
participants (7)
-
Brian Hulley
-
Cale Gibbard
-
dons@cse.unsw.edu.au
-
John Meacham
-
Shae Matijs Erisson
-
Simon Marlow
-
Taral