【发布时间】:2021-05-04 15:29:58
【问题描述】:
使用generics-sop 库,我有以下功能:
f :: (Applicative m) => (forall b. m (ref b)) -> m (NP I '[ref x1, ref x2])
f act =
sequence_NP (act :* act :* Nil)
我如何将其推广到一个 n 产品,即在每个位置都使用act,多态到返回类型?
相关定义为:
data NP :: (k -> Type) -> [k] -> Type where
Nil :: NP f '[]
(:*) :: f x -> NP f xs -> NP f (x ': xs)
sequence_NP :: (SListI xs, Applicative f) => NP f xs -> f (NP I xs)
显而易见的方法是使用pure_NP
pure_NP :: forall f xs. SListI xs => (forall a. f a) -> NP f xs
如下:
f :: (Applicative m, _) => (forall b. m (ref b)) -> m (NP I refs)
f act =
sequence_NP (pure_NP act)
但这不能编译:
• Could not deduce: a ~ ref b0
from the context: (Applicative m, All Top refs)
bound by the inferred type of
f :: (Applicative m, All Top refs) =>
(forall b. m (ref b)) -> m (NP I refs)
at /home/ari/fcm-deepref-experiment/test/QuickCheck.hs:(163,1)-(164,27)
‘a’ is a rigid type variable bound by
a type expected by the context:
forall a. m a
at /home/ari/fcm-deepref-experiment/test/QuickCheck.hs:164:24-26
Expected type: m a
Actual type: m (ref b0)
• In the first argument of ‘pure_NP’, namely ‘act’
In the first argument of ‘sequence_NP’, namely ‘(pure_NP act)’
In the expression: sequence_NP (pure_NP act)
• Relevant bindings include
act :: forall b. m (ref b)
(bound at /home/ari/fcm-deepref-experiment/test/QuickCheck.hs:163:3)
f :: (forall b. m (ref b)) -> m (NP I refs)
(bound at /home/ari/fcm-deepref-experiment/test/QuickCheck.hs:163:1)
因为它期望所有的acts 都是同一类型,但它们不是:它具有多态类型。
我假设我应该使用cpure_NP,
cpure_NP :: forall c xs proxy f. All c xs => proxy c -> (forall a. c a => f a) -> NP f xs
约束版本 pf pure_NP,但我不知道如何设置约束。
【问题讨论】:
标签: haskell metaprogramming existential-type ghc-generics