编译器推断和OverloadedStrings

时间:2014-06-05 10:47:00

标签: haskell ghc

我有以下代码。

data RI a = Union (RI a) (RI a) | Kleene (RI a) |
            Concat (RI a) (RI a) | Const a | Empty | EmptySet deriving (Show)

instance Monoid (RI a) where
  mempty = EmptySet
  mappend = Union

instance IsString (RI Char) where
  fromString s = either (error "Incorrect Parse") (id) (getRE s)

instance IsString (RI Char -> RI Char) where
  fromString s = (\r -> mappend (fromString s) r)

runR :: RI Char -> String -> Bool 
runR r s = True

chain :: String -> Bool
chain = runR ("a" "b")

运行代码后,我得到:

re.hs:46:15:
    No instance for (IsString (a0 -> RI Char))
       arising from the literal ‘"a"’
    The type variable ‘a0’ is ambiguous
    Note: there is a potential instance available:
      instance IsString (RI Char -> RI Char) -- Defined at re.hs:39:10
    In the expression: "a"
    In the first argument of ‘runR’, namely ‘("a" "b")’
    In the expression: runR ("a" "b")

re.hs:46:19:
    No instance for (IsString a0) arising from the literal ‘"b"’
    The type variable ‘a0’ is ambiguous
    Note: there are several potential instances:
      instance IsString (RI Char -> RI Char) -- Defined at re.hs:39:10
      instance IsString (RI Char) -- Defined at re.hs:36:10
      instance IsString [Char] -- Defined in ‘Data.String’
      ...plus two others
    In the first argument of ‘"a"’, namely ‘"b"’
    In the first argument of ‘runR’, namely ‘("a" "b")’
    In the expression: runR ("a" "b")
Failed, modules loaded: none.

为链添加注释可以解决问题,但它很难看。

chain = runR ("a" ("b" :: RI Char))

问题是,为什么编译器不能自己解决这个问题? runR的第一个参数的类型为RI Char,因此我们必须以某种方式评估此类型的"a" "b"。从我定义的实例中,唯一的方法是使用RI Char -> RI Char的{​​{1}}实例和{" b"的"a"实例。不幸的是,编译器没有这样做。

0 个答案:

没有答案