我有一個類型強制轉換到多參數類型在Haskell
class IntegerAsType a where
value :: a -> Integer
data T5
instance IntegerAsType T5 where value _ = 5
newtype (Num a, IntegerAsType n) => PolyRing a n = PolyRing [a]
我的主要問題是:我怎麼定義一個特定PolyRing
變量?
它應該是這樣的:
x = [1, 2, 3] :: Integer T5
(我認爲) 的問題是:什麼是::
後的正確語法?
,我發現了錯誤
Couldn't match expected type `PolyRing Integer T5'
with actual type `[t0]'
In the expression: [1, 2, 3] :: PolyRing Integer T5
In an equation for `x': x = [1, 2, 3] :: PolyRing Integer T5
另外,我正在尋找一種更好的方式來實現這一點。特別是,我真的很喜歡類型a
從列表元素的類型推斷,而IntegerAsType n
必須指定(它應該不取決於列表的長度,即使這是可能的)。
事情我試過到目前爲止:
x = [1,2,3] :: PolyRing (Integer, T5)
x = [1,2,3] :: PolyRing Integer T5