2013-02-28 50 views
9

我想在我的代碼中添加一些本地函數的類型簽名。但是,它們的類型涉及匿名的,存在邊界的類型變量。由於它們是匿名的,我不知道如何編寫類型簽名。我怎樣才能引用這種類型的變量?引用一個存在型變量

在以下示例中,go的類型爲[b] -> Int -> Int,其中b是由模式匹配T (x_shared:xs) g綁定的類型。我可以寫什麼類型的簽名?

data T = forall a. T [a] (a -> a -> Int) 

f :: T -> Int 
f (T (x_shared:xs) g) = go xs 0 
    where 
    -- go :: what type? 
    go (x:xs) n = go xs $! n + g x_shared x 
    go []  n = n 
+0

您mightfind這個有用:http://stackoverflow.com/questions/13828602/outer-bound-type-declarations-without-scopedtypevariables – 2013-02-28 05:36:26

回答

15

隨着ScopedTypeVariables擴展,你可以添加一個類型註解g並介紹類型變量a的範圍。

f (T (x_shared:xs) (g :: a -> a -> Int)) = go xs 0 

然後,你可以寫一個go類型簽名與a

go :: [a] -> Int -> Int 
+3

我不知道,你可以在模式中引入類型變量!我猜你每天都會學到一些知道的東西。 – 2013-02-28 05:19:19