2013-02-27 65 views
2

我正在使用類型同義詞替換部分庫中的多參數類型類。一切都很順利,直到我需要使用類型構造函數。這段代碼的最後兩行不會被編譯。類型系列和類型構造函數

{-# LANGUAGE TypeFamilies, FlexibleContexts #-} 

import qualified Data.Map as M 

-- | A regular arrangement of tiles. 
class Eq (Index g) => Grid g where 
    type Index g 
    -- | Returns the indices of all tiles in a grid. 
    indices :: g -> [Index g] 
    -- plus other functions 


-- | A map from tile positions in a grid to values. 
data LGridMap g v = LGridMap { toGrid :: g, toMap :: M.Map (Index g) v } 

instance Grid g => Grid (LGridMap g v) where 
    type Index (LGridMap g v) = Index g 
    indices = indices . toGrid 


class GridMap gm where 
    type BaseGrid gm 
    type Value gm 

instance GridMap (LGridMap g v) where 
    BaseGrid gm = g -- line 26 
    Value = v  -- line 27 

的編譯錯誤我得到的是:

../Amy.hs:26:3: 
    Pattern bindings (except simple variables) not allowed in instance declarations 
     BaseGrid gm = g 

../Amy.hs:27:3: 
    Pattern bindings (except simple variables) not allowed in instance declarations 
     Value = v 
Failed, modules loaded: none. 

是否有更好的方法來定義LGridMap?有沒有辦法指定LGridMapGridMap的實例?

回答

5

不應該是這樣嗎?

instance GridMap (LGridMap g v) where 
    type BaseGrid (LGridMap g v) = g 
    type Value (LGridMap g v) = v