[英]Haskell: Creating Type Classes for Zippers
所以我一直在閱讀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
但是當我嘗試使用一些像列表這樣的簡單數據結構時:
-- 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 }
或二叉樹:
-- 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 }
我無法編譯,我只是為每個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
。
您還可以使用類型同義詞系列,而不是多參數類型類和功能依賴項。 在這些情況下,它們提供了更清晰,更易於理解的解決方案。 在這種情況下,類和實例將變為:
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 = ...
類型函數的樂趣是對已經熟悉Haskell的人類型同義詞系列的一個很好的介紹。 我還寫了一篇關於如何經常使用類型同義詞系列而不是之前的函數依賴項的文章。
希望這可以幫助!
( go'up
:你的go'up
命名方案是......具有創造性go'up
風格通常是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
(對於所有類型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
...
請參閱多參數類型類 。 它是后Haskell'98擴展,但Haskell實現廣泛支持它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.