Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I like the record solution, but it is easy to do with typeclasses</p> <pre><code>{-# LANGUAGE ViewPatterns, ScopedTypeVariables #-} import qualified Data.Set as Set data Color = W' | U' | B' | R' | G' deriving (Show, Eq, Ord) data Card = Card (Set.Set Color) newtype W a = W a newtype U a = U a newtype B a = B a newtype R a = R a newtype G a = G a class ToColors x where toColors :: x -&gt; [Color] reify :: x instance ToColors () where toColors _ = [] reify = () instance ToColors a =&gt; ToColors (W a) where toColors (W a) = W':toColors a reify = W reify --other instances members :: Set.Set Color -&gt; [Color] -&gt; Bool members s = foldl (\b e -&gt; b &amp;&amp; (Set.member e s)) True viewColors :: forall a. ToColors a =&gt; Card -&gt; Maybe a viewColors (Card s) = let a = reify :: a in if members s (toColors a) then (Just a) else Nothing foo :: Card -&gt; String foo (viewColors -&gt; Just (W (B ()))) = "card is white and black" foo _ = "whatever" </code></pre> <p>this could easily be reworked to get other syntaxes. Like, you could define the colors to be types that don't take parameters, and then use an infix heterogeneous list constructor. Either way it does not care about order.</p> <p>Edit: if you want to match exact sets that is easy also--just replace the <code>members</code> function like so</p> <pre><code>viewColors :: forall a. ToColors a =&gt; Card -&gt; Maybe a viewColors (Card s) = let a = reify :: a in if s == (Set.fromList . toColors $ a) then (Just a) else Nothing </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload