2012-10-19 31 views
19

我想要實現類似於標準數組包中的有界數組,但使用了數組。修復由有界數據類型索引的數組?

什麼是實現這一目標的好方法?

這是我嘗試過,但必須有比包裝的定製功能所需的一切更好的方式,檢查範圍:

import Data.Array.Repa 

data C = A | F | L deriving (Eq,Enum,Ord,Bounded,Show) 

data Ballot c = Ballot { 
    vote::Array U (Z :. Int) Int 
    } deriving Show 

mkBallot::(Eq c ,Enum c,Ord c, Bounded c, Show c) => c -> Ballot c 
mkBallot c = Ballot $ fromListUnboxed (Z :. max) (genSc c) 
where 
    max = (fromEnum (maxBound `asTypeOf` c)) + 1 

genSc::(Eq c,Enum c,Ord c,Bounded c,Show c) => c -> [Int] 
genSc c = [ f x | x <- enumFrom (minBound `asTypeOf` c) , let f v = if x == c then 1 else 0] 

showScore c b = index (vote b) (Z :. ((fromEnum c))) 

我也試圖推導(SH Shape實例:C )但無濟於事,我無法真正瞭解如何實現Shape類中爲我的數據類型聲明的一些接口。我正在寫這個問題,希望別人有辦法,但如果沒有,我會再試一次。謝謝!

回答

2

您可以爲有界枚舉的包裝創建一個形狀實例。我不確定這是否是最好的方式,但我認爲它有點像你想要的。

{-# LANGUAGE ScopedTypeVariables #-} 

import Data.Array.Repa 

這裏我們對有界的東西做一個形狀實例。我們需要索引結束「完整」數組。

data Idx a = Idx a | EOI 
      deriving (Eq, Ord, Show) 

fromIdx :: forall a . (Bounded a, Enum a) => Idx a -> 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) => Int -> Idx a 
toIdx i | i < 0 = error "negative index" 
toIdx i = case compare i range of 
    LT -> Idx $ toEnum (i + fromEnum (minBound :: a)) 
    EQ -> EOI 
    GT -> error "out of range" 
    where 
    range = fromEnum (maxBound :: a) - fromEnum (minBound :: a) + 1 

instance (Bounded a, Enum a, Ord a) => 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 >= n1 && n <= n2 
    listOfShape n = [fromIdx n] 
    shapeOfList [i] = toIdx i 
    shapeOfList _ = error "unsupported shape" 
    deepSeq (Idx n) x = n `seq` x 
    deepSeq _ x = x 

就這樣,中籤部分很簡單,乾淨:

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) => c -> Ballot c 
mkBallot c = Ballot $ fromListUnboxed EOI vec 
    where 
    vec = map (fromEnum . (== c)) [minBound .. maxBound] 
+0

我會看看這個。 – user1105045