불완전 추론에 작동하지 않습니다 컴파일 : 당신은 다음과 같은 오류가타입 클래스, 큐, 목록에 대한 인스턴스와 다음 하스켈 프로그램을 컴파일하려고 인해 GHC 7.8
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
import qualified Data.List as L
class Queue c e where
new :: c
insert :: e -> c -> c
extract :: c -> (e, c)
empty :: c -> Bool
instance Queue [e] e where
new = []
insert = (:)
extract (x:xs) = (x,xs)
empty = L.null
toList :: Queue c e => c -> [e]
toList queue | empty queue = []
toList queue = let (first,rest) = extract queue in first : toList rest
main = print (new ([] :: [Int]))
:
를[1 of 1] Compiling Main (afff.hs, afff.o)
afff.hs:19:16:
Could not deduce (Queue c e1) arising from a use of ‘empty’
from the context (Queue c e)
bound by the type signature for toList :: Queue c e => c -> [e]
at afff.hs:18:11-31
The type variable ‘e1’ is ambiguous
Relevant bindings include
queue :: c (bound at afff.hs:19:8)
toList :: c -> [e] (bound at afff.hs:19:1)
Note: there is a potential instance available:
instance Queue [e] e -- Defined at afff.hs:12:10
In the expression: empty queue
In a stmt of a pattern guard for
an equation for ‘toList’:
empty queue
In an equation for ‘toList’: toList queue | empty queue = []
afff.hs:22:8:
No instance for (Show a0) arising from a use of ‘print’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in ‘GHC.Real’
...plus 24 others
In the expression: print (new ([] :: [Int]))
In an equation for ‘main’: main = print (new ([] :: [Int]))
afff.hs:22:15:
No instance for (Queue ([Int] -> a0) e0)
arising from a use of ‘new’
In the first argument of ‘print’, namely ‘(new ([] :: [Int]))’
In the expression: print (new ([] :: [Int]))
In an equation for ‘main’: main = print (new ([] :: [Int]))
shell returned 1
왜 GHC가 "e1 = Int"를 추론 할 수없는 이유가 무엇입니까?
아, 그건 의미가 있습니다, 감사합니다! 좋은 대답. – MaiaVictor