0
我覺得這應該很容易,但我堅持了很長一段時間。爲數據類型列表創建實例 - Haskell
haskell有可能爲某些數據類型的列表創建一個類型的實例嗎?
我想實現的是以下內容。
class Rename a where
findSub :: a -> a
-- A 'normal' instance would look like this
instance Rename Atom where
findSub ......
-- Now i want to acchieve something like this
instance Rename ([] Atom) where
findSub ......
當我寫這篇文章如何過我得到以下錯誤:
* Illegal instance declaration for `Rename [Atom]'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
* In the instance declaration for `Rename ([] Atom)'
Failed, modules loaded: none.
我想知道如何解決這個問題,爲什麼這是不允許的。
在此先感謝。
我不傾向於看到'UndecidableInstances'出現在錯誤的代碼中;我傾向於在完美的代碼中看到更多,Haskell的極其有限的終止標準不足以證明是好的。 – dfeuer
@dfeuer,一旦你對類型類很熟悉,我同意,但是通常初學者往往會認爲類型類的分辨率倒退或者比它強大得多(例如''實例向量=>數量類'等),這很快導致這些擴展被「需要」(儘管當它們被啓用時它仍然不能按預期工作)。沒有? – luqui
我傾向於更多地考慮'OverlappingInstances','IncoherentInstances','AllowAmbiguousTypes'和(當不使用多參數類型類時)'FlexibleInstances'。但你也許是對的。 – dfeuer