2015-10-23 96 views
4

我對以下程序感到困惑。在RankNTypes中使用函數組合時出現類型錯誤

{-# LANGUAGE RankNTypes #-} 
newtype A a = A (forall f. Applicative f => f a) 

good :: a -> A a 
good x = A $ pure $ x 

bad :: a -> A a 
bad x = A . pure $ x 

當嘗試編譯,我得到這個錯誤訊息,抱怨bad

Couldn't match type `f0 a' 
       with `forall (f :: * -> *). Applicative f => f a' 
Expected type: f0 a -> A a 
    Actual type: (forall (f :: * -> *). Applicative f => f a) -> A a 
Relevant bindings include 
    x :: a (bound at huh.hs:8:6) 
    bad :: a -> A a (bound at huh.hs:8:1) 
In the first argument of `(.)', namely `A' 
In the expression: A . pure 

爲什麼功能good類型檢測,而GHC不服功能bad?我能做些什麼來使後一版本工作?據我所知,這兩個例子應該是等價的。

+0

爲什麼downvote? – Hjulle

+2

[相關](http://stackoverflow.com/questions/9468963/runst-and-function-composition)。 – user3237465

+0

以及這一個:http://stackoverflow.com/questions/8343239/type-error-with-rank-2-types-and-function-composition。 – Hjulle

回答

2

正如一些評論中所解釋的那樣,問題在於類型系統無法預測這是一種有效的類型,即使它是。在this answer中有一個提示,您可以明確指定.的類型來解決問題。

這段代碼的工作原理:

-- Type specialized composition 
(.!) :: ((forall f. Applicative f => f b) -> c) -> 
     (a -> (forall f. Applicative f => f b)) -> a -> c 
(.!) f g x = f(g x) 

-- Use the new version of composition 
notBad :: a -> A a 
notBad x = A .! pure $ x 
相關問題