0
У меня есть два модуля определен следующим образом:Распечатайте символ в основном методе
Foo.hs
module Foo
where
class F t where
c :: t -> Char
instance F Double where
c s = 'x'
Main.hs
import Foo
main :: IO()
main = do
print $ (c 2.0)
Когда я компилирую эти два модуля, которые я получаю эта ошибка:
ghc Foo.hs Main.hs
[1 of 2] Compiling Foo (Foo.hs, Foo.o)
[2 of 2] Compiling Main (Main.hs, Main.o)
Main.hs:6:12: error:
• Ambiguous type variable ‘t0’ arising from a use of ‘c’
prevents the constraint ‘(F t0)’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instance exist:
instance [safe] F Double -- Defined in ‘Foo’
• In the second argument of ‘($)’, namely ‘(c 2.0)’
In a stmt of a 'do' block: print $ (c 2.0)
In the expression: do { print $ (c 2.0) }
Main.hs:6:14: error:
• Ambiguous type variable ‘t0’ arising from the literal ‘2.0’
prevents the constraint ‘(Fractional t0)’ from being solved.
Probable fix: use a type annotation to specify what ‘t0’ should be.
These potential instances exist:
instance Fractional Double -- Defined in ‘GHC.Float’
instance Fractional Float -- Defined in ‘GHC.Float’
...plus one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the first argument of ‘c’, namely ‘2.0’
In the second argument of ‘($)’, namely ‘(c 2.0)’
In a stmt of a 'do' block: print $ (c 2.0)
Как сделать Я исправляю это, чтобы он печатал «x»?
cut'n'paste error - обновил сообщение –
... что GHC предлагал в «Вероятное исправление: используйте аннотацию типа, чтобы указать, что должно быть« t0 ».» – chi
@chi Да, это так, но сообщения об ошибках GHC кажутся немного загадочными в начале, по крайней мере, IMO. – ThreeFx