索引异构列表

时间:2018-06-11 18:06:21

标签: haskell type-level-computation

我使用某种类型级编程在Haskell中构建了一个异构List。

data HList a where
  Singleton :: HList '[]
  Cons :: h -> HList t -> HList (h ': t)

现在我希望能够将此列表编入索引,但是对于使我这样做非常困难的类型存在一些问题。我可以很容易地得到这个列表的头部或尾部

head :: HList (h ': t) -> h
head (Cons a _) = a

tail :: HList (h ': t) -> HList t
tail (Cons _ b) = b

然而,对列表进行索引是非常不同的,因为输出的类型取决于我们传递的索引。天真地,我们的类型看起来像:

fromIndex :: (Num a) => a -> (HList b) -> ???

然而,确定???相当困难。因此,我们不必采取Num而是采取其他措施。我的想法(下面的代码)是创建一个具有函数依赖关系的新NaturalIndexType类,这样我们就可以根据输入的类型找到结果的类型。

{-# Language GADTs, DataKinds, TypeOperators, FunctionalDependencies, FlexibleInstances, FlexibleContexts, UndecidableInstances #-}

data Nat = Z | S Nat

data Natural a where
  Zero :: Natural 'Z
  Succ :: Natural a -> Natural ('S a)

data HList a where
 Singleton :: HList '[]
 Cons :: h -> HList t -> HList (h ': t)

class IndexType a b c | a b -> c
instance IndexType (Natural 'Z) (HList (h ': t)) h
instance IndexType (Natural n) (HList t) a => IndexType (Natural ('S n)) (HList (h ': t)) a

fromIndex :: (IndexType (Natural n) (HList l) a) => (Natural n) -> (HList l) -> a
fromIndex (Zero) (Cons x Singleton) = x
fromIndex (Succ a) (Cons _ (xs)) = fromIndex a xs

我们的IndexType课程确实有用。如果我只测试类型类

class Test a | -> a
  where test :: a
instance (IndexType (Natural ('S ('S ('S 'Z)))) (HList (Int ': String ': Char ': (Int -> String) ': Int ': '[])) a) => Test a

我们得到了正确的结果:

*Main> :t test
test :: Int -> String

然而,ghc无法验证我们的类型签名,我们得到了相当单一的错误:

test.hs:28:39: error:
    • Could not deduce: h ~ a
      from the context: n ~ 'Z
        bound by a pattern with constructor: Zero :: Natural 'Z,
                 in an equation for ‘fromIndex’
        at test.hs:28:12-15
      or from: l ~ (h : t)
        bound by a pattern with constructor:
                   Cons :: forall h (t :: [*]). h -> HList t -> HList (h : t),
                 in an equation for ‘fromIndex’
        at test.hs:28:19-34
      or from: t ~ '[]
        bound by a pattern with constructor: Singleton :: HList '[],
                 in an equation for ‘fromIndex’
        at test.hs:28:26-34
      ‘h’ is a rigid type variable bound by
        a pattern with constructor:
          Cons :: forall h (t :: [*]). h -> HList t -> HList (h : t),
        in an equation for ‘fromIndex’
        at test.hs:28:19-34
      ‘a’ is a rigid type variable bound by
        the type signature for:
          fromIndex :: forall (n :: Nat) (l :: [*]) a.
                       IndexType (Natural n) (HList l) a =>
                       Natural n -> HList l -> a
        at test.hs:27:1-81
    • In the expression: x
      In an equation for ‘fromIndex’:
          fromIndex (Zero) (Cons x Singleton) = x
    • Relevant bindings include
        x :: h (bound at test.hs:28:24)
        fromIndex :: Natural n -> HList l -> a (bound at test.hs:28:1)
   |
28 | fromIndex (Zero) (Cons x Singleton) = x
   |                                       ^

test.hs:29:36: error:
    • Could not deduce (IndexType (Natural a1) (HList t) a)
        arising from a use of ‘fromIndex’
      from the context: IndexType (Natural n) (HList l) a
        bound by the type signature for:
                   fromIndex :: forall (n :: Nat) (l :: [*]) a.
                                IndexType (Natural n) (HList l) a =>
                                Natural n -> HList l -> a
        at test.hs:27:1-81
      or from: n ~ 'S a1
        bound by a pattern with constructor:
                   Succ :: forall (a :: Nat). Natural a -> Natural ('S a),
                 in an equation for ‘fromIndex’
        at test.hs:29:12-17
      or from: l ~ (h : t)
        bound by a pattern with constructor:
                   Cons :: forall h (t :: [*]). h -> HList t -> HList (h : t),
                 in an equation for ‘fromIndex’
        at test.hs:29:21-31
    • In the expression: fromIndex a xs
      In an equation for ‘fromIndex’:
          fromIndex (Succ a) (Cons _ (xs)) = fromIndex a xs
   |
29 | fromIndex (Succ a) (Cons _ (xs)) = fromIndex a xs
   |                                    ^^^^^^^^^^^^^^
Failed, no modules loaded.

可以构建索引功能吗?有没有办法让GHC推断出我的类型签名是正确的?

2 个答案:

答案 0 :(得分:3)

fromIndex的案例有不同的类型!他们需要在实例中

class IndexType (n :: Nat) (xs :: [Type]) (i :: Type) | n xs -> i where
   fromIndex :: Natural n -> HList xs -> i

 instance IndexType Z (x ': xs) x where
   fromIndex Zero (Cons x _) = x

 instance IndexType n xs a => IndexType (S n) (x ': xs) a where
   fromIndex (Succ n) (Cons _ xs) = fromIndex n xs

(我略微改变了fromIndex :: Natural n -> HList xs -> i的类型。这实际上并没有改变任何东西 - 你的解决方案同样适用,如果你在一个呼叫fromIndex时有更多令人困惑的错误消息意想不到的背景。)

答案 1 :(得分:0)

我定义了以下内容:

-- The type of numbers n such that xs !! n = x
-- Compare to Nat
data Elem (x :: k) (xs :: [k]) where
  Here  :: Elem x (x : xs)
  There :: Elem x xs -> Elem x (y : xs)

然后你会发现HList与#34;索引函数同构#34; (以Vect n aFin n -> a同构的方式)涉及此类型:

indexHList :: forall xs. HList xs -> (forall x. Elem x xs -> x)
indexHList (Cons x _) Here = x
indexHList (Cons _ xs) (There i) = indexHList xs i
indexHList Singleton impossible = case impossible of {}

-- unindexHList ::ish forall xs. (forall x. Elem x xs -> x) -> HList xs
-- is a bit more work (and doesn't really have that type)
-- but is conceptually the other half of the isomorphism.

用法:

xs :: HList [Int, String, HList '[]]
xs = Cons 5 $ Cons "hello" $ Cons Singleton $ Singleton
-- Here :: Elem Int (Int:_)
indexHList xs Here == 5
-- Here :: Elem String (String:_)
-- There Here :: Elem String (_:String:_)
indexHList xs (There Here) == "hello"

与基于class的技术相比,Elem x xs基本上是exists n. (Natural n, IndexType (Natural n) (HList xs) x)。作为一种可以检查的数据类型,它比类更容易操作。

相关问题