【发布时间】:2018-05-03 15:59:19
【问题描述】:
我希望得到以下示例来进行类型检查:
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module Foo where
f :: Int -> (forall f. Functor f => Secret f) -> Int
f x _ = x
g :: (forall f. Functor f => Secret f) -> Int
g = f 4
type family Secret (f :: * -> *) :: * where
Secret f = Int
我知道可能无法推断和检查 gs 类型(尽管在这种特定情况下很明显,因为它只是一个部分应用程序):Secret 不是单射的,没有办法告诉Functor 实例应该期望的编译器。因此,它失败并显示以下错误消息:
• Could not deduce (Functor f0)
from the context: Functor f
bound by a type expected by the context:
forall (f :: * -> *). Functor f => Secret f
at src/Datafix/Description.hs:233:5-7
The type variable ‘f0’ is ambiguous
These potential instances exist:
instance Functor IO -- Defined in ‘GHC.Base’
instance Functor Maybe -- Defined in ‘GHC.Base’
instance Functor ((,) a) -- Defined in ‘GHC.Base’
...plus one other
...plus 9 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: f 4
In an equation for ‘g’: g = f 4
|
233 | g = f 4
| ^^^
因此需要程序员的一些指导,如果我可以这样写g,那将很容易被接受:
g :: (forall f. Functor f => Secret f) -> Int
g h = f 4 (\\f -> h @f)
\\ 是 System Fw 的大 lambda 的假设语法,即类型抽象。我可以用丑陋的Proxys 来模拟这个,但是还有其他一些 GHC Haskell 功能可以让我写这样的东西吗?
【问题讨论】:
-
f的声明中出现歧义错误。你打开了AllowAmbiguousTypes吗? -
是的,我愿意。给我一点时间来编辑带有所有必要扩展名的问题。
标签: haskell type-families existential-type system-f