2

Data 类c仅在方法中提及类型变量,因此我似乎无法在实例的约束中提及它。

gfoldl :: Data a =>
  (forall d b. Data d => c (d -> b) -> d -> c b)
   -> (forall g. g -> c g)
   -> a
   -> c a

我可以为我的 HList 编写一个 foldl,但约束最终包括c,因此一种解决方案是将 替换c为具体的data C a.

instance (HBuild' '[] g,
        Typeable (HList a),
        HFoldl (GfoldlK C) (C g) a (C (HList a)))
  => Data (HList a) where

    gfoldl k z xs = c3 $ hFoldl
                                (c1 (GfoldlK k))
                                (c2 (z hBuild))
                                xs
        where
              c1 :: forall c a. GfoldlK c -> GfoldlK C
              c1 = unsafeCoerce

              c2 :: forall c. c g -> C g
              c2 = unsafeCoerce

              c3 :: forall c. C (HList a) -> c (HList a)
              c3 = unsafeCoerce

这似乎有效。但是有没有不安全强制的解决方案?完整代码在这里: http: //lpaste.net/93565

4

0 回答 0