如何给GHC提示构造QuantifiedConstraints?

时间:2018-12-27 18:33:01

标签: haskell ghc quantified-constraints

如果我有方法:

proveBar :: forall x . SingI x => Dict (Barable (Foo x))
proveBar = ...

然后我如何将其作为上下文传递给:

useBar :: forall foo . (forall x. SingI x => Barable (foo x)) => ...
useBar = ...

foo绑定到Foo


Dict在此处https://hackage.haskell.org/package/constraints-0.10.1/docs/Data-Constraint.html#g:2

中定义

1 个答案:

答案 0 :(得分:1)

你不能拥有你拥有的东西。

要将useBarfoo ~ Foo一起使用,您需要有(forall x. SingI x => Barable (Foo x))的证据。

proveBar上进行模式匹配将不起作用,因为到您匹配Dict时,x不再具有通用资格;您已将x限制为特定(未指定)类型。

您真正需要的是对类型为Dict (forall x. SingI x => Barable (Foo x))的类型进行模式匹配,但是GHC当前不支持此类型:

• Illegal polymorphic type: forall x. SingI x => Barable (Foo x)
  GHC doesn't yet support impredicative polymorphism
• In the type signature: proveBar' :: Dict (forall x. SingI x => Barable (Foo x))

相反,如果您有以下形式的证据

proveBar :: SingIBarable Foo

data SingIBarable foo where
  SingIBarable :: (forall x. SingI x => Barable (foo x)) -> SingIBarable foo

然后,您将可以通过useBar上的模式匹配来使用proveBar