Haskell:为Zippers创建类型类

ram*_*ion 4 haskell design-patterns typeclass zipper

所以我一直在阅读Haskell中的Zipper模式(以及其他函数语言,我想)来遍历和修改数据结构,我认为这是一个很好的机会让我磨练我的技能来创建类型Haskell中的类,因为类可以为我编写代码提供一个通用的遍历接口,而不依赖于遍历的数据结构.

我想我可能需要两个类 - 一个用于根数据结构,一个用于创建用于遍历第一个的特殊数据结构:

module Zipper where

class Zipper z where
  go'up :: z -> Maybe z
  go'down :: z -> Maybe z
  go'left :: z -> Maybe z
  go'right :: z -> Maybe z

class Zippable t where
  zipper :: (Zipper z) => t -> z
  get :: (Zipper z) => z -> t
  put :: (Zipper z) => z -> t -> z
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用一些像列表这样的简单数据结构时:

-- store a path through a list, with preceding elements stored in reverse
data ListZipper a = ListZipper { preceding :: [a], following :: [a] }

instance Zipper (ListZipper a) where
  go'up ListZipper { preceding = [] } = Nothing
  go'up ListZipper { preceding = a:ps, following = fs } = 
      Just $ ListZipper { preceding = ps, following = a:fs }
  go'down ListZipper { following = [] } = Nothing
  go'down ListZipper { preceding = ps, following = a:fs } = 
      Just $ ListZipper { preceding = a:ps, following = fs }
  go'left _ = Nothing
  go'right _ = Nothing

instance Zippable ([a]) where
  zipper as = ListZipper { preceding = [], following = as }
  get = following
  put z as = z { following = as }
Run Code Online (Sandbox Code Playgroud)

或二叉树:

-- binary tree that only stores values at the leaves
data Tree a = Node { left'child :: Tree a, right'child :: Tree a } | Leaf a
-- store a path down a Tree, with branches not taken stored in reverse
data TreeZipper a = TreeZipper { branches :: [Either (Tree a) (Tree a)], subtree :: Tree a }

instance Zipper (TreeZipper a) where
  go'up TreeZipper { branches = [] } = Nothing
  go'up TreeZipper { branches = (Left l):bs, subtree = r } =  
      Just $ TreeZipper { branches = bs, subtree = Node { left'child = l, right'child = r } }
  go'up TreeZipper { branches = (Right r):bs, subtree = l } =  
      Just $ TreeZipper { branches = bs, subtree = Node { left'child = l, right'child = r } }
  go'down TreeZipper { subtree = Leaf a } = Nothing
  go'down TreeZipper { branches = bs, subtree = Node { left'child = l, right'child = r } } =
      Just $ TreeZipper { branches = (Right r):bs, subtree = l }
  go'left TreeZipper { branches = [] } = Nothing
  go'left TreeZipper { branches = (Right r):bs } = Nothing
  go'left TreeZipper { branches = (Left l):bs, subtree = r } =
      Just $ TreeZipper { branches = (Right r):bs, subtree = l }
  go'right TreeZipper { branches = [] } = Nothing
  go'right TreeZipper { branches = (Left l):bs } = Nothing
  go'right TreeZipper { branches = (Right r):bs, subtree = l } =
      Just $ TreeZipper { branches = (Left l):bs, subtree = r }

instance Zippable (Tree a) where
  zipper t = TreeZipper { branches = [], subtree = t }
  get TreeZipper { subtree = s } = s
  put z s = z { subtree = s }
Run Code Online (Sandbox Code Playgroud)

我无法编译,我只是为我的每个Zippable实例定义得到了很多这样的错误:

Zipper.hs:28:14:
    Couldn't match expected type `z'
           against inferred type `ListZipper a'
      `z' is a rigid type variable bound by
          the type signature for `zipper' at Zipper.hs:10:20
    In the expression: ListZipper {preceding = [], following = as}
    In the definition of `zipper':
        zipper as = ListZipper {preceding = [], following = as}
    In the definition for method `zipper'

所以我不确定从哪里开始.我怀疑我的问题是我试图将这两个实例绑定在一起,当(Zipper z) =>声明只是想z成为任何一个时Zipper.

Mar*_*ijn 8

您还可以使用类型同义词系列而不是多参数类型类和功能依赖项.在这些情况下,它们提供了更清晰,更易于理解的解决方案.在这种情况下,类和实例将变为:

class Zippable t where
  type ZipperType t :: *
  enter :: t -> ZipperType t
  focus :: ZipperType t -> t

instance Zippable [a] where
  type ZipperType [a] = ListZipper a
  enter = ...
  focus = ...
Run Code Online (Sandbox Code Playgroud)

类型函数的乐趣是对已经熟悉Haskell的人类型同义词系列的一个很好的介绍.我还写了一篇关于如何经常使用类型同义词系列而不是之前的函数依赖项的文章.

希望这可以帮助!


eph*_*ent 7

(旁白:你的go'up命名方案是......具有创造性.Haskell风格通常是camelCase.)

你走在正确的轨道上.你写的内容相当于下面的内容.

{-# LANGUAGE RankNTypes #-}
instance Zippable [a] where
    zipper = ... :: forall z. (Zipper z) => [a] -> z
    get = ... :: forall z. (Zipper z) => z -> [a]
    set = ... :: forall z. (Zipper z) => z -> [a] -> z
Run Code Online (Sandbox Code Playgroud)

(对于所有类型z,给定Zipper z,存在zipper :: [a] -> z.)

你需要定义zipper = ... :: [a] -> ListZipper a,这显然过于严格.

您的代码将通过以下最小的更改进行类型检查:

{-# LANGUAGE MultiParamTypeClasses #-}
class (Zipper z) => Zippable z t where
    zipper :: t -> z
    get :: z -> t
    set :: z -> t -> z
instance Zippable (ListZipper a) [a] where
    ...
instance Zippable (TreeZipper a) (Tree a) where
    ...
Run Code Online (Sandbox Code Playgroud)

请参阅多参数类型类.它是后Haskell'98扩展,但Haskell实现广泛支持它.

  • 将功能依赖性t-> z添加到Zippable可能是个好主意.否则,当您尝试使用这些类时,您将遇到类型歧义...(另请参阅http://www.haskell.org/haskellwiki/Functional_dependencies) (3认同)