7

异构列表是 ghc 7.6 的新依赖类型工具的示例之一:

data HList :: [*] -> * where
  HNil :: HList '[]
  HCons:: a -> HList t -> HList (a ': t)

示例列表“li”编译良好:

li  = HCons "Int: " (HCons 234 (HCons "Integer: " (HCons 129877645 HNil)))

显然我们希望 HList 在 Show 类中,但我只能提出以下使用相互递归约束(超类)的工作类实例:

instance Show (HList '[]) where 
  show HNil = "[]"

instance (Show a, Show' (HList t)) => Show (HList (a ': t)) where
  show l  = "[" ++ show' l ++ "]"

class Show' a where
  show' :: a -> String

instance Show' (HList '[]) where
  show' HNil = ""

instance (Show a, Show' (HList t)) => Show' (HList (a ': t)) where
  show' (HCons h l) = case l of
    HNil      -> show h
    HCons _ _ -> show h ++ ", " ++ (show' l)

代码编译良好,li 显示正确。需要的编译标志是:

{-# LANGUAGE DataKinds, TypeOperators, KindSignatures, 
FlexibleContexts, GADTs, FlexibleInstances #-}

我尝试了以下更直接定义的许多变体,但如果我无法理解 ghc 错误消息,它就无法编译:

instance Show (HList '[]) where 
  show HNil = "[]"

instance (Show a, Show (HList t)) => Show (HList (a ': t)) where
  show l  = "[" ++ (show' l) ++ "]" where  
    show' (HCons h s) = case s of
      HNil      -> show h
      HCons _ _ -> show h ++ ", " ++ (show' s)

一些 Haskell / ghc 专家可能会理解为什么这不起作用,我很乐意听到原因。

谢谢

汉斯·彼得


谢谢你,hammar,你的两个很好的工作示例,改进了我的第一个示例。

但我仍然不明白为什么我的第二个例子不起作用。您说“... show' 只知道如何显示当前元素类型,而不知道如何显示其余元素类型。” 但是该评论是否也不适用于以下(工作)代码:

instance Show (HList '[]) where show HNil = "" 

instance (Show a, Show (HList t)) => Show (HList (a ': t)) where 
   show (HCons h t) = case t of
      HNil      -> show h 
      HCons _ _ -> show h ++ ", " ++ (show t) 
4

2 回答 2

5

正如 Nathan 在评论中所说,show'只知道如何显示当前元素类型而不知道剩余的元素类型。

与您的第一个示例一样,我们可以通过为 制作一个新的类型类来解决此问题show',尽管您只能使用一个Show实例:

-- Specializing show' to HLists avoids needing a Show' (HList ts) constraint
-- here, which would require UndecidableInstances.
instance (Show' ts) => Show (HList ts) where
  show xs = "[" ++ show' xs ++ "]"

class Show' ts where
  show' :: HList ts -> String

instance Show' '[] where
  show' HNil = ""

instance (Show a, Show' ts) => Show' (a ': ts) where
  show' (HCons a s) = case s of
    HNil     -> show a
    HCons {} -> show a ++ ", " ++ show' s

Show获取所有必要约束的另一种更骇人听闻的方法show'是使用ConstraintKinds直接构建所有必要约束的列表。

-- In addition to the extensions in the original code:
{-# LANGUAGE TypeFamilies, ConstraintKinds, UndecidableInstances #-}
import GHC.Exts

-- ShowTypes [a, b, c, ...] = (Show a, Show b, Show c, ...)
type family ShowTypes (a :: [*]) :: Constraint
type instance ShowTypes '[] = ()
type instance ShowTypes (a ': t) = (Show a, ShowTypes t) 

instance ShowTypes ts => Show (HList ts) where
  show xs = "[" ++ show' xs ++ "]"
    where
      show' :: ShowTypes ts => HList ts -> String
      show' HNil = ""
      show' (HCons h s) = case s of
        HNil     -> show h
        HCons {} -> show h ++ ", " ++ show' s
于 2012-10-20T18:05:28.243 回答
2

感谢 hammar 的第二个解决方案,我现在可以提供一种更通用的方法,它适用于一般类(但我想他无论如何都考虑到了这一点):

type family ConstrainedTypes (a :: [*]) (f :: * -> Constraint) :: Constraint
type instance ConstrainedTypes '[] b = ()
type instance ConstrainedTypes (a ': t) b = (b a, ConstrainedTypes t b) 

instance ConstrainedTypes ts Show => Show (HList ts) where
  show xs = "[" ++ show' xs ++ "]"
    where
      show' :: ConstrainedTypes ts Show => HList ts -> String
      show' HNil = ""
      show' (HCons h s) = case s of
        HNil     -> show h
        HCons {} -> show h ++ ", " ++ show' s

再次感谢您的大力帮助。

于 2012-10-21T05:51:33.633 回答