haskell - 使用来自类型级别 map 的额外信息装饰类型级别列表

标签 haskell gadt type-families type-level-computation data-kinds

我有一个异构列表,它的类型反射(reflect)了它包含的值的类型。我可以通过检查包含的每个类型都满足约束来将所有元素转换为字符串:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}   

import GHC.Exts
import GHC.TypeLits

import Data.Proxy

type family AllSatisfy (f :: k -> Constraint) (xs :: [k]) :: Constraint where
    AllSatisfy f '[] = ()
    AllSatisfy f (x ': xs) = (f x, AllSatisfy f xs)

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

type family Keys (xs :: [(a, b)]) :: [a] where
    Keys '[] = '[]
    Keys ( '(a, b) ': xs) = a ': Keys xs

type family Values (xs :: [(a, b)]) :: [b] where
    Values '[] = '[]
    Values ( '(a, b) ': xs) = b ': Values xs

showHList :: AllSatisfy Show xs => HList xs -> [String]
showHList HNil = []
showHList (HCons x xs) = show x : showHList xs

我希望能够通过类型级关联列表指定一些额外信息,该列表由 HList 中的类型索引。像这样的东西:

showWithKey :: forall (keyMap :: [(*, Symbol)]) (b :: Symbol) (rest :: [(*, Symbol)]).
               (AllSatisfy Show (Keys keyMap)
               ,AllSatisfy KnownSymbol (Values keyMap)
               ) =>
               Proxy keyMap -> HList (Keys keyMap) -> [(String, String)]
showWithKey _ HNil = []
showWithKey _ (HCons (x :: a) (xs :: HList as)) =
    let p = (Proxy @keyMap) :: Proxy ( '(a, b) ': rest )
    in (show x, symbolVal (Proxy @b)) : (showWithKey (Proxy @rest) xs)

现在,很清楚如果 (Keys keyMap) 是非空的,那么 keyMap,但是 GHC 很难解决这个问题:

Could not deduce: keyMap ~ ('(a, b) : rest)
      from the context: (AllSatisfy Show (Keys keyMap),
                         AllSatisfy KnownSymbol (Values keyMap))
        bound by the type signature for:
                   showWithKey :: (AllSatisfy Show (Keys keyMap),
                                   AllSatisfy KnownSymbol (Values keyMap)) =>
                                  Proxy keyMap -> HList (Keys keyMap) -> [(String, String)]
      or from: Keys keyMap ~ (a : as)
        bound by a pattern with constructor:
                   HCons :: forall a (as :: [ghc-prim-0.5.0.0:GHC.Types.*]).
                            a -> HList as -> HList (a : as),
                 in an equation for ‘showWithKey’
      ‘keyMap’ is a rigid type variable bound by
        the type signature for:
          showWithKey :: forall (keyMap :: [(ghc-prim-0.5.0.0:GHC.Types.*,
                                             Symbol)]) (b :: Symbol) (rest :: [(ghc-prim-0.5.0.0:GHC.Types.*,
                                                                                Symbol)]).
                         (AllSatisfy Show (Keys keyMap),
                          AllSatisfy KnownSymbol (Values keyMap)) =>
                         Proxy keyMap -> HList (Keys keyMap) -> [(String, String)]
      Expected type: Proxy ('(a, b) : rest)
        Actual type: Proxy keyMap 

我如何重写它以便 GHC 能够解决问题?

最佳答案

根据 user2407038 所说的一些线索,我创建了 depMap 类型的具体表示,然后创建了一个类型类来描述一个不完全单一但至少是规范的值类型。

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}

import GHC.Exts
import GHC.TypeLits

import Data.Proxy

type family AllSatisfy (f :: k -> Constraint) (xs :: [k]) :: Constraint where
    AllSatisfy f '[] = ()
    AllSatisfy f (x ': xs) = (f x, AllSatisfy f xs)

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

type family Keys (xs :: [(a, b)]) :: [a] where
    Keys '[] = '[]
    Keys ( '(a, b) ': xs) = a ': Keys xs

type family Values (xs :: [(a, b)]) :: [b] where
    Values '[] = '[]
    Values ( '(a, b) ': xs) = b ': Values xs

showHList :: AllSatisfy Show xs => HList xs -> [String]
showHList HNil = []
showHList (HCons x xs) = show x : showHList xs

data SKeyMap :: [(*, Symbol)] -> * where
  SKeyNil :: SKeyMap '[]
  SKeyCons :: Proxy a -> Proxy s -> SKeyMap xs -> SKeyMap ( '(a, s) ': xs )

class KnownKeyMap (keyMap :: [(*, Symbol)]) where
    sKeyMap :: SKeyMap keyMap


instance KnownKeyMap '[] where
    sKeyMap = SKeyNil

instance KnownKeyMap keyMap => KnownKeyMap ( '(a, s) ': keyMap ) where
    sKeyMap = SKeyCons Proxy Proxy sKeyMap

showWithKey' :: forall (keyMap :: [(*, Symbol)]) .
               (AllSatisfy Show (Keys keyMap)
               ,AllSatisfy KnownSymbol (Values keyMap)
               ) =>
               SKeyMap keyMap -> HList (Keys keyMap) -> [(String, String)]
showWithKey' SKeyNil HNil = []
showWithKey' (SKeyCons _ sp skRest) (HCons (x :: a) (xs :: HList as)) =
    (show x, symbolVal sp) : (showWithKey' skRest xs)

showWithKey :: forall (keyMap :: [(*, Symbol)]) .
               (KnownKeyMap keyMap
               ,AllSatisfy Show (Keys keyMap)
               ,AllSatisfy KnownSymbol (Values keyMap)
               ) =>
               HList (Keys keyMap) -> [(String, String)]
showWithKey = showWithKey' (sKeyMap @keyMap)

关于haskell - 使用来自类型级别 map 的额外信息装饰类型级别列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39965006/

相关文章:

haskell - let 绑定(bind)中的 N 种类型

ocaml - 异构列表上的这种类型错误是什么意思?

haskell - 如何派生具有类型族的记录的实例

haskell - 如何在 Haskell 中实现部分注入(inject)类型族?

haskell - 如何使用 Data.Time.Clock 在 Haskell 中获取系统时间?

haskell - 在 hedis 之上构建一个 monad,一个 haskell redis 库

polymorphism - 有人可以解释这个 OCaml 程序中使用的类型语法吗?

haskell - 如何创建 `Var` 数据类型的 `Free` 实例?

haskell - 如何使用自由单子(monad)来表达延续单子(monad)?

haskell - 类型级程序的分层模块名称