2014-12-22 25 views
1

以下代碼無法編譯,並顯示以下錯誤消息。 f應該只是一個狀態monad,它在運行時創建一個帶有單個int「42」的長度爲1的向量。我懷疑一些歧義rununstream之間發生的事情很像show . read,但我無法弄清楚如何解決它:使用Data.Vector「推斷的類型不明確」錯誤

{-# LANGUAGE NoMonomorphismRestriction #-} 

import Data.Vector.Generic.New (run, unstream) 
import Data.Vector.Fusion.Stream (singleton) 

f = run . unstream . singleton $ (42 :: Int) 

main = return() 

錯誤:

main.hs:6:1: 

Could not deduce (Data.Vector.Generic.Base.Vector v0 Int) 
    arising from the ambiguity check for `f' 
from the context (Data.Vector.Generic.Base.Vector v Int) 
    bound by the inferred type for `f': 
      Data.Vector.Generic.Base.Vector v Int => 
      GHC.ST.ST s (Data.Vector.Generic.Base.Mutable v s Int) 
    at sort.hs:6:1-44 
Possible fix: 
    add an instance declaration for 
    (Data.Vector.Generic.Base.Vector v0 Int) 
When checking that `f' 
    has the inferred type `forall (v :: * -> *) s. 
         Data.Vector.Generic.Base.Vector v Int => 
         GHC.ST.ST s (Data.Vector.Generic.Base.Mutable v s Int)' 
Probable cause: the inferred type is ambiguous 

main.hs:6:1: 

Could not deduce (Data.Vector.Generic.Base.Mutable v0 
        ~ Data.Vector.Generic.Base.Mutable v) 
from the context (Data.Vector.Generic.Base.Vector v Int) 
    bound by the inferred type for `f': 
      Data.Vector.Generic.Base.Vector v Int => 
      GHC.ST.ST s (Data.Vector.Generic.Base.Mutable v s Int) 
    at sort.hs:6:1-44 
NB: `Data.Vector.Generic.Base.Mutable' is a type function, and may not be injective 
Expected type: Data.Vector.Generic.Base.Mutable v s Int 
    Actual type: Data.Vector.Generic.Base.Mutable v0 s Int 
Expected type: GHC.ST.ST 
       s (Data.Vector.Generic.Base.Mutable v s Int) 
    Actual type: GHC.ST.ST 
       s (Data.Vector.Generic.Base.Mutable v0 s Int) 
When checking that `f' 
    has the inferred type `forall (v1 :: * -> *) s1. 
         Data.Vector.Generic.Base.Vector v1 Int => 
         GHC.ST.ST s1 (Data.Vector.Generic.Base.Mutable v1 s1 Int)' 
Probable cause: the inferred type is ambiguous 

回答

1

這是show . read的問題,但有一個轉折。

我們正在撰寫這裏的兩個功能是

unstream :: forall s v a. (Vector v) => Stream a -> New v a 
run :: forall s v' a'. New v' a' -> ST s (Mutable v' s a') 

撰寫他們產生New v a ~ New v' a',由於New是一種數據類型,它是單射;因此,我們有v ~ v'a ~ a',用於:

run . unstream :: forall s v a. (Vector v) => Stream a -> ST s (Mutable v s a) 

然而,v的選擇是由類型Stream a -> ST s (Mutable v s a)不確定,因爲Mutable是一個類型的家庭,因此不射的。這就是它變得像show . read :: forall a. (Show a, Read a) => String -> String;它很難看到,因爲v似乎發生在該類型中。

想想你在更具體的類型中使用它會發生什麼,例如

run . unstream :: forall s. Stream Int -> ST s (MVector s a) 

有知道什麼v應該是剛剛從Mutable v ~ MVector沒有可能的方式。

所有這些都表明了一種類型run . unstream . singleton多態,不作售前到的v選擇,通過只要求它在調用的地方進行傳遞:

{- LANGUAGE ScopedTypeVariables #-} 

f :: forall s v a. (Vector v a) => Proxy v -> a -> ST s (Mutable v s a) 
f _ = run . (unstream :: Stream a -> New v a) . singleton 
1

一般情況下可以解決這樣的歧義附加一個類型註釋。在這種情況下,問題是:「在流創建New v a時應使用Vector v a的哪個實例?」。這可以通過在rununstream上添加註釋來解決,但是看起來好像unstream上的註釋會更少(手指)鍵入。就像這樣:

f = run . (unstream :: Stream Int -> New {- put something concrete here -} Int) . singleton $ 42 
相關問題