如何为Mu递归类型编写Show实例

时间:2018-06-20 10:27:11

标签: haskell show

我想为以下类型的列表编写Show的实例:

newtype Mu f = Mu (forall a. (f a -> a) -> a)
data ListF a r = Nil | Cons a r deriving (Show)
type List a = Mu (ListF a)

模块Data.Functor.Foldable定义了它,但是将其转换为Fix,这是我要避免的事情。

如何定义此Show实例?

3 个答案:

答案 0 :(得分:4)

“跟随类型!” 的口号全天为我们服务。

从您的代码中进行一些重命名,以便于理解,

{-# LANGUAGE RankNTypes #-}

data ListF a r = Nil | Cons a r deriving (Show)
newtype List a = Mu {runMu :: forall r. (ListF a r -> r) -> r}

这样我们就可以拥有

fromList :: [a] -> List a
fromList (x:xs) = Mu $ \g -> g   -- g :: ListF a r -> r
                               (Cons x $                 -- just make all types fit
                                  runMu (fromList xs) g)
fromList []     = Mu $ \g -> g Nil

{-   or, equationally,
runMu (fromList (x:xs)) g = g (Cons x $ runMu (fromList xs) g)
runMu (fromList [])     g = g Nil 

     such that (thanks, @dfeuer!)
runMu (fromList [1,2,3]) g = g (Cons 1 (g (Cons 2 (g (Cons 3 (g Nil))))))
-}

我们想要

instance (Show a) => Show (List a) where
 -- show :: List a -> String
 show (Mu f) = "(" ++ f showListF ++ ")"            -- again, just make the types fit

...我们必须产生一个字符串;我们只能致电 f;它的论点是什么?根据其类型

  where
  showListF :: Show a => ListF a String -> String   -- so that, f showListF :: String !
  showListF Nil        = ...
  showListF (Cons x s) = ...

这里没有其他方法可以连接导线。

以此print $ fromList [1..5]打印(1 2 3 4 5 )

实际上,这是chianswer的详细版本。

编辑: g用于“代数”(感谢@chi!),f(在Mu f中)用于“折叠”。现在,这种类型的含义变得更加清晰:给定一个“代数”(归约函数),一个Mu f值将在该“折叠函数”所表示的“固有列表”的折叠中使用它。它在折叠的 each 步骤中使用一步还原语义表示列表的折叠。

答案 1 :(得分:2)

首先定义自己的代数

showOneLayer :: Show a => ListF a String -> String
showOneLayer ... = ...

然后

instance Show a => Show (Mu (ListF a)) where
   show (Mu f) = f showOneLayer

答案 2 :(得分:1)

如WillNess所示,您可能希望用newtype来包装List

newtype Mu f = Mu {reduce :: forall a. (f a -> a) -> a}
-- I've added a field name for convenience.

data ListF a r = Nil | Cons a r
  deriving (Show, Functor, Foldable, Traversable)
  -- You'll probably want these other instances at some point.

newtype List a = List {unList :: Mu (ListF a)}

WillNess还编写了一个有用的fromList函数;这是另一个版本:

fromList :: Foldable f => f a -> List a
fromList xs =
  List $ Mu $ foldr (\a as g -> g (Cons a (as g))) ($ Nil) xs

现在让我们编写一个基本(不太正确)的版本。我将打开ScopedTypeVariables以添加类型签名,而不会造成重复。

instance Show a => Show (List a) where
  showsPrec _ xs = reduce (unList xs) go
    where
      go :: ListF a ShowS -> ShowS
      go Nil = id
      go (Cons x r) = (',':) . showsPrec 0 x . r

这将显示一个列表,类似于:

show (fromList []) = ""
show (fromList [1]) = ",1"
show (fromList [1,2]) = ",1,2"

嗯。我们需要安装前导[和尾随],并以某种方式处理多余的前导逗号。做到这一点的一种好方法是跟踪我们是否在第一个列表元素上:

instance Show a => Show (List a) where
  showsPrec _ (List xs) = ('[':) . reduce xs go False . (']':)
    where
      go :: ListF a (Bool -> [Char] -> [Char]) -> Bool -> [Char] -> [Char]
      go Nil _ = id
      go (Cons x r) started =
        (if started then (',':) else id)
        . showsPrec 0 x
        . r True

现在我们实际上可以正确地显示事物了!

但是实际上,我们遇到了比必要多得多的麻烦。我们真正需要的只是一个Foldable实例:

instance Foldable List where
  foldr c n (List (Mu g)) = g $ \case
    Nil -> n
    Cons a as -> c a as

那我们就可以写

instance Show a => Show (List a) where
  showsPrec p xs = showsPrec p (toList xs)