這裏是我的嘗試至今:約束返回類型的上下文
module Main where
data FooT = One | Two deriving (Show, Read)
{-
That is what I want
foo :: (Show a, Read a) => a
foo = One
-}
--class Footable (Show a, Read a) => a where
class Footable a where
--foo2 :: (Show a, Read a) => a
foo2 :: a
instance Footable FooT where
foo2 = One
-- test = print foo2
我想測試編譯。我不認爲這個問題圍繞着普遍的量化而展開。 GHC說,一個是一個「嚴格的類型變量」 編輯(剛性類型的變量),但我真的不理解這是什麼。這個問題似乎正如我在我的評論@寫sepp2k它可能對生存型,但我無意中發現了一個奇怪的行爲可能與this
編輯
:
這確實編譯:
{-# LANGUAGE OverlappingInstances, FlexibleInstances, OverlappingInstances,
UndecidableInstances, MonomorphismRestriction, PolymorphicComponents #-}
{-# OPTIONS_GHC -fno-monomorphism-restriction #-}
module Main where
class (Num a) => Numable a where
foo2 :: a
instance (Num a) => Numable a where
foo2 = 1
instance Numable Int where
foo2 = 2
instance Numable Integer where
foo2 = 3
--test = foo2 + foo2 -- This does NOT compile (ambiguous a)
test = (foo2::Integer) + foo2 --this works
但這並不(`a」是剛性類型的變量消息)
{-# LANGUAGE OverlappingInstances, FlexibleInstances, OverlappingInstances,
UndecidableInstances, MonomorphismRestriction, PolymorphicComponents #-}
{-# OPTIONS_GHC -fno-monomorphism-restriction #-}
module Main where
data FooT = One | Two deriving (Show, Read)
data BarT = Ten deriving (Show, Read)
class (Show a, Read a) => Footable a where
foo2 :: a
instance (Show a, Read a) => Footable a where
foo2 = Ten
instance Footable FooT where
foo2 = One
main = print foo2
這是因爲1 ::(Num t)=> t。我可以像這樣定義一些東西(typeconstructor,consts dunno)嗎?
反引號不起作用格式化的多行代碼。您必須縮進四個空格(或使用代碼按鈕或Ctrl-K)。 – sepp2k 2010-07-03 10:27:19
和撇號混亂的配色方案。但現在很好。 – supertux 2010-07-03 10:35:11
你應該真的開始發佈整個錯誤消息。該消息的重要組成部分,是「無法匹配預期型'一個」反推斷類型'巴特」,而不是‘一個是剛性類型變量...’(不描述錯誤 - 它只是告訴你一個地方來自)。 – sepp2k 2010-07-03 15:13:51