[Haskell-cafe] MultiCase alternative

Li-yao Xia lysxia at gmail.com
Thu Jun 15 14:42:44 UTC 2017


Hi Paul,

This looks like the or-patterns proposal:

https://github.com/ghc-proposals/ghc-proposals/pull/43

I'm not sure what you call MultiCase though. Is the above what you were 
refering to?

Here are some alternatives with current Haskell, the first one is to be 
preferred:

- Factor out the alternatives in a function.

       ...
         case x of
           ...
           C c -> f c
           D c -> f c
       where
         f c = ...

- If your C and D constructors have similar enough meanings, you might 
want to fuse them into a single constructor with a separate tag:

       data T = A TA | B TB | CD CorD TC
       data CorD = C | D

   so that you can write

       case x of
         ...
         CD _ c -> ...

- With pattern synonyms + view patterns, although it takes some effort 
to set up and confuses the exhaustiveness checker.

       matchCorD :: T -> Maybe C
       matchCorD (C c) = Just c
       matchCorD (D c) = Just c
       matchCorD _ = Nothing

       pattern CD :: C -> T
       pattern CD c <- (matchCorD -> Just c)

   you can now write

       case x of
         ...
         CD c -> ...

Li-yao


On 06/15/2017 10:11 AM, Baa wrote:
> Hello, Everyone!
>
> As I understand "MultiCase" proposal was not approved, so my question
> is: is there alternatives to multi-case? I have code like:
>
>    case x of
>      A a -> ..
>      B b -> ..
>      C c -> --same--
>      D c -> --same--
>
> and I want to merge code of "C c" with "D c" branch. Anywhere, in
> general: is any alternatives to make "OR"s in patterns? I don't see how
> to do it with pattern guards, pattern synonyms, views extensions and
> don't see another related extensions.
>
> Something like "(C|D) c -> ..."
>
>
> ===
> Best regards, Paul
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



More information about the Haskell-Cafe mailing list