2016-06-25 4 views
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»?

ответ

3

Вы должны просто переименовать call в c.

EDIT: Попробуйте аннотирования 2.0 в Double:

print $ c (2.0 :: Double) 

GHC не знает, какой тип использовать для 2.0, это может быть любой тип Fractional. Чтобы исправить это, мы вынуждаем 2.0 быть типа Double, явно указывая его тип.

+0

cut'n'paste error - обновил сообщение –

+2

... что GHC предлагал в «Вероятное исправление: используйте аннотацию типа, чтобы указать, что должно быть« t0 ».» – chi

+0

@chi Да, это так, но сообщения об ошибках GHC кажутся немного загадочными в начале, по крайней мере, IMO. – ThreeFx

Смежные вопросы