首页 文章

GHC 7.7中引入的自由覆盖条件打破了GHC 7.6中有效的代码

提问于
浏览
9

The idea

我正在写一个DSL,它编译成Haskell .

该语言的用户可以定义自己的不可变数据结构和相关功能 . 通过关联函数,我指的是属于数据结构的函数 . 例如,用户可以写(以“pythonic”伪代码):

data Vector a:
  x,y,z :: a
  def method1(self, x):
      return x

(这相当于下面的代码,但也显示了相关函数beheva类似于具有开放世界假设的类型类):

data Vector a:
  x,y,z :: a
def Vector.method1(self, x):
  return x

在此示例中, method1 是与 Vector 数据类型关联的函数,可以像 v.testid(5) (其中 vVector 数据类型的实例)一样使用 .

我正在将这样的代码翻译成Haskell代码,但是我遇到了一个问题,我试图解决很长一段时间 .

The problem

我试图将代码从GHC 7.6移到GHC 7.7 (which is pre-release of 7.8)(较新的版本可以编译from sources) . 该代码在GHC 7.6下完美运行,但不在GHC 7.7下 . 我想问你如何修复它以使其在新版本的编译器中工作?

Example code

让我们看一下生成的简化版本(由我的编译器)Haskell代码:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FunctionalDependencies #-}

import Data.Tuple.OneTuple

------------------------------
-- data types
------------------------------
data Vector a = Vector {x :: a, y :: a, z :: a} deriving (Show)
-- the Vector_testid is used as wrapper over a function "testid". 
newtype Vector_testid a = Vector_testid a

------------------------------
-- sample function, which is associated to data type Vector
------------------------------
testid (v :: Vector a) x = x

------------------------------
-- problematic function (described later)
------------------------------
testx x = call (method1 x) $ OneTuple "test"

------------------------------
-- type classes
------------------------------
-- type class used to access "method1" associated function
class Method1 cls m func | cls -> m, cls -> func where 
    method1 :: cls -> m func

-- simplified version of type class used to "evaluate" functions based on 
-- their input. For example: passing empty tuple as first argument of `call` 
-- indicates evaluating function with default arguments (in this example 
-- the mechanism of getting default arguments is not available)
class Call a b where
    call :: a -> b

------------------------------
-- type classes instances
------------------------------
instance (out ~ (t1->t1)) => Method1 (Vector a) Vector_testid out where
  method1 = (Vector_testid . testid)

instance (base ~ (OneTuple t1 -> t2)) => Call (Vector_testid base) (OneTuple t1 -> t2) where
    call (Vector_testid val) = val

------------------------------
-- example usage
------------------------------
main = do
    let v = Vector (1::Int) (2::Int) (3::Int)
    -- following lines equals to a pseudocode of ` v.method1 "test" `
    -- OneTuple is used to indicate, that we are passing single element.
    -- In case of more or less elements, ordinary tuples would be used.
    print $ call (method1 v) $ OneTuple "test"
    print $ testx v

代码编译并与GHC 7.6一起正常工作 . 当我尝试使用GHC 7.7编译它时,我收到以下错误:

debug.hs:61:10:
    Illegal instance declaration for
      ‛Method1 (Vector a) Vector_testid out’
      The liberal coverage condition fails in class ‛Method1’
        for functional dependency: ‛cls -> func’
      Reason: lhs type ‛Vector a’ does not determine rhs type ‛out’
    In the instance declaration for
      ‛Method1 (Vector a) Vector_testid out’

该错误是由检查功能依赖性可以执行的新规则引起的,即 liberal coverage condition (据我所知,这是 coverage condition 使用 -XUndecidableInstances 放宽了)

Some attemps to fix the problem

我试图通过将 Method1 的定义更改为:来解决此问题:

class Method1 cls m func | cls -> m where 
    method1 :: cls -> m func

这解决了功能依赖的问题,但后面的行:

testx x = call (method1 x) $ OneTuple "test"

不再允许,导致编译错误(在7.6和7.7版本中):

Could not deduce (Method1 cls m func0)
  arising from the ambiguity check for ‛testx’
from the context (Method1 cls m func,
                  Call (m func) (OneTuple [Char] -> s))
  bound by the inferred type for ‛testx’:
             (Method1 cls m func, Call (m func) (OneTuple [Char] -> s)) =>
             cls -> s
  at debug.hs:50:1-44
The type variable ‛func0’ is ambiguous
When checking that ‛testx’
  has the inferred type ‛forall cls (m :: * -> *) func s.
                         (Method1 cls m func, Call (m func) (OneTuple [Char] -> s)) =>
                         cls -> s’
Probable cause: the inferred type is ambiguous

EDIT:

使用类型族也是不可能解决这个问题的(据我所知) . 如果我们用以下代码(或simmilar)替换 Method1 类型类和实例:

class Method1 cls m | cls -> m where 
    type Func cls
    method1 :: cls -> m (Func cls)

instance Method1 (Vector a) Vector_testid where
    type Func (Vector a) = (t1->t1)
    method1 = (Vector_testid . testid)

我们会得到明显的错误 Not in scope: type variable ‛t1’ ,因为类型族不允许使用类型,这种类型不会出现在类型表达式的LHS上 .

The final question

如何让这个想法在GHC 7.7下运作?我知道新的 liberal coverage condition 允许GHC开发人员在类型检查方面取得一些进展,但它应该可以在某种程度上可以实现在GHC 7.6中工作而不是编译器版本 .

(不强迫我的DSL用户引入任何其他类型 - 到目前为止的一切,如类型类实例,我正在使用Template Haskell进行创建)

1 回答

  • 8

    这不是GHC 7.7中的错误 . 当它允许违反功能依赖性的实例时,这是GHC中的一个长期存在的错误 . 幸运的是,这个问题终于得到了解决 . GHC 7.7发出的错误消息非常详细,指出了您的实例 Method1 (Vector a) Vector_testid out 的问题 . 回想一下功能依赖的含义 . 特定

    class C a b | a -> b
    

    如果类型 abb1 都是 C a bC a b1 都成立,那么 bb1 必须相同 . 我们来看看你的实例:

    Method1 (Vector a) Vector_testid (t1->t1)
    

    如果我们的类型 bb1 满足 Method1 (Vector Int) Vector_testid (b->b)Method1 (Vector a) Vector_testid (b1->b1) ,则一无所知意味着 bb1 必须相同 . 因此你的实例是不正确的 . GHC 7.6和之前接受该计划的事实是GHC中一个众所周知的错误(每年讨论一次) .

    你似乎想要的是定义类似的东西

    Method1 (Vector a) Vector_testid (forall t. t -> t)
    

    唉,虽然存在许多工作环境,但不允许使用此语法 . 例如,一个涉及Apply类(例如,参见HList文件) . 更简单的方法如下

    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE UndecidableInstances #-}
    {-# LANGUAGE FunctionalDependencies #-}
    
    -- import Data.Tuple.OneTuple
    newtype OneTuple x = OneTuple x deriving Show
    
    ------------------------------
    -- data types
    ------------------------------
    data Vector a = Vector {x :: a, y :: a, z :: a} deriving (Show)
    
    -- testx x = call (method1 x) $ OneTuple "test"
    testx x = call x Method1 $ OneTuple "test"
    
    -- associate methods to classes
    class Methods cls m x y | cls m x -> y where
      call :: cls -> m -> x -> y
    
    instance Methods (Vector a) Method1 x x where
      call self _ x = x
    
    data Method1 = Method1 -- method label
    

相关问题