让重命名函数中断代码

cro*_*eea 15 haskell ghc

在将代码迭代到正确的版本时,我遇到了以下好奇心:

{-# LANGUAGE RankNTypes #-}

module Foo where

import Data.Vector.Generic.Mutable as M
import Control.Monad.Primitive

-- an in-place vector function with dimension
data DimFun v m r = 
  DimFun Int (v (PrimState m) r -> m ())

eval :: (PrimMonad m, MVector v r) => DimFun v m r -> v (PrimState m) r -> m ()
eval = error ""

iterateFunc :: (PrimMonad m, MVector v r)
            => (forall v' . (MVector v' r) => DimFun v' m r) -> DimFun v m r
iterateFunc = error ""

f :: (PrimMonad m, MVector v r)
      => DimFun v m r
f = error ""

iteratedF :: (MVector v r, PrimMonad m) 
           => v (PrimState m) r -> m ()
iteratedF y = 
    let f' = f
    in eval (iterateFunc f') y
Run Code Online (Sandbox Code Playgroud)

此代码无法编译:

Testing/Foo.hs:87:14:
    Could not deduce (MVector v0 r) arising from a use of ‘f’
    from the context (MVector v r, PrimMonad m)
      bound by the type signature for
                 iteratedF :: (MVector v r, PrimMonad m) =>
                              v (PrimState m) r -> m ()
      at Testing/Foo.hs:(84,14)-(85,39)
    The type variable ‘v0’ is ambiguous
    Relevant bindings include
      f' :: DimFun v0 m r (bound at Testing/Foo.hs:87:9)
      y :: v (PrimState m) r (bound at Testing/Foo.hs:86:11)
      iteratedF :: v (PrimState m) r -> m ()
        (bound at Testing/Foo.hs:86:1)
    In the expression: f
    In an equation for ‘f'’: f' = f
    In the expression: let f' = f in eval (iterateFunc f') y

Testing/Foo.hs:88:26:
    Couldn't match type ‘v0’ with ‘v'’
      because type variable ‘v'’ would escape its scope
    This (rigid, skolem) type variable is bound by
      a type expected by the context: MVector v' r => DimFun v' m r
      at Testing/Foo.hs:88:14-27
    Expected type: DimFun v' m r
      Actual type: DimFun v0 m r
    Relevant bindings include
      f' :: DimFun v0 m r (bound at Testing/Foo.hs:87:9)
    In the first argument of ‘iterateFunc’, namely ‘f'’
    In the first argument of ‘eval’, namely ‘(iterateFunc f')’
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

但是,如果我将定义更改iteratedF

iteratedF y = eval (iterateFunc f) y
Run Code Online (Sandbox Code Playgroud)

代码编译与GHC 7.8.2.这个问题不是关于看起来很奇怪的签名或数据类型,它只是这样:为什么重命名ff'破坏代码?这似乎对我来说是一个错误.

chi*_*chi 12

禁用单态限制,我可以编译你的代码.所以,只需添加

{-# LANGUAGE NoMonomorphismRestriction #-}
Run Code Online (Sandbox Code Playgroud)

在您的文件的开头.

类型错误的原因是定义

let f' = f
Run Code Online (Sandbox Code Playgroud)

不使用函数模式(例如f' x y = ...),因此单态性限制启动并强制f'为单态,同时iterateFunc需要多态函数.

或者,添加类型注释

let f' :: (PrimMonad m, MVector v r) => DimFun v m r
    f' = f
Run Code Online (Sandbox Code Playgroud)

  • @leftaroundabout我认为这只是GHCi (7认同)

lef*_*out 7

问题当然不是重命名,而是绑定到新变量.由于iterateFunc是Rank-2,它需要一个多态参数函数.当然,f是多态的v,所以它可以使用.但是当你编写时f' = f,不清楚f'应该是什么类型:相同的多态类型f,或者某种单态类型,可能取决于iteratedF编译器尚未推导出的另一个类型变量的某种关系.

编译器默认为单态选项; 正如chi所说,这是单态限制的错误,所以如果你把它关闭,你的代码实际编译.

即使没有RankNTypes代码中的单态限制,同样的问题也会出现,它无法完全避免.唯一可靠的解决方案是本地签名,通常是必需的ScopedTypeVariables.

  • 我不认为重命名不会在纯Haskell 98中造成麻烦.考​​虑`让f = show in f 1 ++ f True`. (4认同)
  • 然后是另一个可怕的单形态限制的情况. (2认同)