我有這樣的單態代碼:約束類型變量的具體類型
import Data.Array.MArray
import Data.Array.IO (IOUArray)
import Data.Ix (Ix)
push :: IOUArray Int Int -> Int -> [Int] -> IO Int
push stack top [] = return top
push stack top (k:ks) = do
v <- readArray stack k
if v >= 0 then push stack top ks
else do writeArray stack k top
push stack k ks
爲push
一個完全多態的簽名是:
push :: (MArray a e m, Ix e, Num e) => a e e -> e -> [e] -> m e
但是我試着寫這部分多態特徵:
push :: MArray a e m => a Int e -> Int -> [Int] -> m Int
push :: MArray a e m => a Int Int -> Int -> [Int] -> m Int
push :: MArray a Int m => a Int Int -> Int -> [Int] -> m Int
push :: (MArray a e m, e ~ Int) => a e e -> e -> [e] -> m e
我得到的錯誤如can't deduce (e ~ Int)
,can't deduce (MArray a Int m) ...
和Non type-variable argument in constraint
,Illegal equational constraint
有沒有辦法約束的第一個簽名的類型變量e
的具體類型一樣Int
?
'0'不只是'Int'或'Integer',它可以是任何'Num a => a'。 –