2013-02-27 68 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