1

次のコードの場合forall a r、 の型からの削除はgo「Typeable (D r) の重複するインスタンス」で失敗します。なぜだろう?

{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module M where

import Data.Typeable (Proxy, Typeable, cast)

class C r where
    data D r :: *

deriving instance Typeable D

data A = forall r . A (D r)

go :: forall r a . (Typeable a, Typeable (D r)) => a -> Proxy r -> A
go a _ = case cast a of
  Just (b :: D r) -> A b
  Nothing -> error "fail to cast"

エラーには、「選択はのインスタンス化に依存します」とも書かれていますが、rそれは提供されたによって固定されていませんProxy rか?

4

1 に答える 1

4

これは、Haskell でスコープ型変数がどのように機能するかです。ここで再利用していることに注意してくださいr

go :: forall r a . (Typeable a, Typeable (D r)) => a -> Proxy r -> A
go a _ = case cast a of
  Just (b :: D r) -> A b  -- this r is the same as the r above

明示的な がないforall場合、型変数は署名に対してローカルであると解釈されます。つまり、コードは次のように読み取られます。

go :: (Typeable a1, Typeable (D r1)) => a1 -> Proxy r1 -> A  -- renaming local variables
go a _ = case cast a of
  Just (b :: D r) -> A b   -- r bears no relation to r1

したがって、型エラー。

(ただし、エラーが発生する混乱します。)Overlapping instances

于 2015-06-29T11:54:44.177 に答える