1

GHC はこのコードに文句を言います:

{-# LANGUAGE TypeFamilies, MultiParamTypeClasses, ScopedTypeVariables #-}

class Test a b where
    data Data a b
    data Marker a b
    test :: Marker a b -> Bool

work :: (Test a b, Test a2 b2) => Data a b -> Data a2 b2 
work =
    let (m :: Marker a2 b2) = undefined
    in if test m then undefined else undefined

メッセージ付き:

You cannot bind scoped type variables `a2', `b2'
  in a pattern binding signature
In the pattern: m :: Marker a2 b2

述語はいくつかの関数で使用されているため、関数の実際のwork機能をTestクラスに移動したくありません。test

4

2 に答える 2

2

型変数をスコープに入れるとコンパイルされます。

work :: forall a b a2 b2. (Test a b, Test a2 b2) => Data a b -> Data a2 b2 
work =
    let (m :: Marker a2 b2) = undefined
    in if test m then undefined else undefined

explicit がなければforall、型変数a2b2let バインディングは新しい型変数です。

于 2013-01-06T18:29:10.010 に答える
2

forall a b a2 b2.a2、b2でのご使用Marker a2 b2は新品扱いとなります。を使用forallすると、それらがスコープに含まれます。

work :: forall a b a2 b2. (Test a b, Test a2 b2) => Data a b -> Data a2 b2 

forall キーワードの詳細

于 2013-01-06T18:32:18.407 に答える