haskell - 类型族和类型构造函数

标签 haskell type-families

我正在努力用类型同义词替换一些库中的多参数类型类。一切都很顺利,直到我需要使用类型构造函数。此代码的最后两行将无法编译。

{-# 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 的实例?

最佳答案

不应该是这个吗?

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

关于haskell - 类型族和类型构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118780/

相关文章:

haskell - 只有一个值可提升到种类级别的数据类型

haskell - 如果一种可以从另一种分配,是否可以比较两种类型?

optimization - 我怎样才能优化这个列表理解?

haskell - 如何将 Haskell 程序分发给非技术的最终用户?

haskell - 类型族导致不明确的变量错误

haskell - 如何使用类型族获得更好的错误消息?

haskell - 为什么这个 Haskell 代码使用fundeps 进行类型检查,但对类型族产生不可触碰的错误?

visual-studio - 视觉 haskell 2008

haskell - Emacs 主提示符中的函数名称自动补全 - haskell 模式

haskell - ghc-mod 期望 MonadBaseControl 具有 `StM` 关联的新类型而不是 `StT` 关联的类型