haskell - 在 GHC 7.6 下工作的代码的歧义检查和自由覆盖条件失败

标签 haskell ghc typeclass typechecking

我有一个聪明的类型级机制,可以在 GHC 7.6 上运行,但不能在更高版本上运行。回顾过去,我不完全确定它为什么会起作用,但无论如何我都想以某种方式取回此功能:

{-# LANGUAGE 
    PolyKinds 
  , FunctionalDependencies , FlexibleInstances , FlexibleContexts
  , OverlappingInstances
  , ScopedTypeVariables
  , TypeFamilies
  , UndecidableInstances
 #-}
module M where

import Data.Proxy

-- | A relation between a (maybe-partially-applied) type and that type fully
-- applied.
class Applied t (tab :: *) | t -> tab where
    -- | Fully apply a type @t@ with polymorphic arguments, yielding @tab@.
    applied :: Proxy t -> Proxy tab

instance Applied (t a) tab=> Applied t tab where
    applied _ = applied (Proxy :: Proxy (t a))

instance t ~ tab=> Applied t tab where -- always matches when `t` is kind `*`
    applied _ = Proxy :: Proxy tab

这取决于 GHC 7.6 上的 tagged 库。我们可以像这样使用它:

$ ghci-7.6.3
Prelude> :l M.hs
[1 of 1] Compiling M                ( M.hs, interpreted )
Ok, modules loaded: M.
*M> 
*M> :t applied (Proxy :: Proxy Either)
applied (Proxy :: Proxy Either) :: Proxy (Either a a1)
*M> (return $ Right 'a') == applied (Proxy :: Proxy Either)
True

然而,这至少不能在 GHC 7.8.3 或更高版本上编译:

$ ghci-7.8.3
GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l M.hs
[1 of 1] Compiling M                ( M.hs, interpreted )

M.hs:19:10:
    Could not deduce (Applied (t a0) tab)
      arising from the ambiguity check for an instance declaration
    from the context (Applied (t a) tab)
      bound by an instance declaration:
                 Applied (t a) tab => Applied t tab
      at M.hs:19:10-42
    The type variable ‘a0’ is ambiguous
    In the ambiguity check for:
      forall (k :: BOX) (k1 :: BOX) (t :: k1 -> k) tab (a :: k1).
      Applied (t a) tab =>
      Applied t tab
    To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
    In the instance declaration for ‘Applied t tab’

M.hs:19:10:
    Illegal instance declaration for ‘Applied t tab’
      The liberal coverage condition fails in class ‘Applied’
        for functional dependency: ‘t -> tab’
      Reason: lhs type ‘t’ does not determine rhs type ‘tab’
    In the instance declaration for ‘Applied t tab’
Failed, modules load

我认为 answer here是相关的,但我还不明白这个建议。

我有可能解决这个问题。我唯一使用此类的地方是在以下形式的签名中:

instance (Foo tab, Applied t tab)=> Bar (Proxy t) where

这可能表明我想使 Foo 成为多态的,但这是在一个庞大复杂的库中,我不知道这种改变是否可行。

最佳答案

如果我摆脱 FD 并启用 -XAllowAmbiguousTypes,您的 ghci 示例适用于 ghc-7.8.3。该扩展将要求您注释(使用 ScopedTypeVariables)applied 函数在 instance(Foo 选项卡,Applied t 选项卡)=> 中使用时的类型酒吧(代理 t)

关于haskell - 在 GHC 7.6 下工作的代码的歧义检查和自由覆盖条件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27851445/

相关文章:

haskell - 一个不平凡的共同类群是什么样子的?

c - 如何提升函数以在 C 中获取额外参数?

haskell - 在 Haskell 中将类型约束添加到实例声明的上下文中

haskell - 更好地显示 bool 公式

haskell - Cabal 有默认目标吗?

haskell - 使用 GHC.Generics 以字符串形式获取数据构造函数名称

exception - Haskell (ghc) Control.Exception 中,try 和 catch 的区别

haskell - 我如何解构/反构造符号?

scala - 是否可以将类型参数的类型参数传播到 Scala 中的参数化类?

scala - 如何将类型类模式与子类型相结合?