我希望在函數f中定義一個函數f',以便它們的類型規範都引用相同的類型變量。但是,當我嘗試這樣做時,編譯器出現編譯錯誤,它假定外部的m
和內部的m
不是相同的類型變量。任何提示如何解決這個問題?如何引用haskell類型規範中已有的類型變量?
f :: (Monad m) => (String -> Int -> String -> m()) -> [String] -> m()
f _ (x:_) = f' Nothing x
where
f' :: (Maybe Int) -> String -> m() -- when I comment this line, the code compiles
f' _ _ = return()
main = undefined
檢查http://www.haskell.org/haskellwiki/Scoped_type_variables – dg123
謝謝,這解決了它。如果您將此更改爲答案,我會將其標記爲正確。 – tohava
對於這個特定的例子,你也可以通過給內部類型簽名添加一個'(Monad m)'約束來編譯它。 –