Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can make a shape instance for a wrapper around your bounded enum. I'm not sure this is the best way, but it sort of does what you want, I think.</p> <pre><code>{-# LANGUAGE ScopedTypeVariables #-} import Data.Array.Repa </code></pre> <p>Here we make a shape instance over bounded things. We need an end-of-index for "full" arrays.</p> <pre><code>data Idx a = Idx a | EOI deriving (Eq, Ord, Show) fromIdx :: forall a . (Bounded a, Enum a) =&gt; Idx a -&gt; Int fromIdx EOI = fromEnum (maxBound :: a) - fromEnum (minBound :: a) + 1 fromIdx (Idx x) = fromEnum x - fromEnum (minBound :: a) toIdx :: forall a . (Bounded a, Enum a) =&gt; Int -&gt; Idx a toIdx i | i &lt; 0 = error "negative index" toIdx i = case compare i range of LT -&gt; Idx $ toEnum (i + fromEnum (minBound :: a)) EQ -&gt; EOI GT -&gt; error "out of range" where range = fromEnum (maxBound :: a) - fromEnum (minBound :: a) + 1 instance (Bounded a, Enum a, Ord a) =&gt; Shape (Idx a) where rank _ = 1 zeroDim = Idx minBound unitDim = Idx $ succ minBound intersectDim EOI n = n intersectDim n EOI = n intersectDim (Idx n1) (Idx n2) = Idx $ min n1 n2 addDim = error "undefined" size = fromIdx sizeIsValid _ = True toIndex _ n = fromIdx n fromIndex _ i = toIdx i inShapeRange _ _ EOI = error "bad index" inShapeRange n1 n2 n = n &gt;= n1 &amp;&amp; n &lt;= n2 listOfShape n = [fromIdx n] shapeOfList [i] = toIdx i shapeOfList _ = error "unsupported shape" deepSeq (Idx n) x = n `seq` x deepSeq _ x = x </code></pre> <p>With that, the ballot part is easy and clean:</p> <pre><code>data C = A | F | L deriving (Eq, Enum, Ord, Bounded, Show) data Ballot c = Ballot { vote :: Array U (Idx c) Int } deriving Show mkBallot :: (Eq c, Enum c, Ord c, Bounded c, Show c) =&gt; c -&gt; Ballot c mkBallot c = Ballot $ fromListUnboxed EOI vec where vec = map (fromEnum . (== c)) [minBound .. maxBound] </code></pre>
 

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