原來,儘管存在非常簡單的想法,但使用existential/rank-n類型的確很難。存在型包裝的必要性
爲什麼要將存在類型打包成data
類型是必要的?
我有以下簡單的例子:
{-# LANGUAGE RankNTypes, ImpredicativeTypes, ExistentialQuantification #-}
module Main where
c :: Double
c = 3
-- Moving `forall` clause from here to the front of the type tuple does not help,
-- error is the same
lists :: [(Int, forall a. Show a => Int -> a)]
lists = [ (1, \x -> x)
, (2, \x -> show x)
, (3, \x -> c^x)
]
data HRF = forall a. Show a => HRF (Int -> a)
lists' :: [(Int, HRF)]
lists' = [ (1, HRF $ \x -> x)
, (2, HRF $ \x -> show x)
, (3, HRF $ \x -> c^x)
]
如果我註釋掉的lists
定義,代碼編譯成功。如果我不評論它,我會收到以下錯誤:
test.hs:8:21:
Could not deduce (a ~ Int)
from the context (Show a)
bound by a type expected by the context: Show a => Int -> a
at test.hs:8:11-22
`a' is a rigid type variable bound by
a type expected by the context: Show a => Int -> a at test.hs:8:11
In the expression: x
In the expression: \ x -> x
In the expression: (1, \ x -> x)
test.hs:9:21:
Could not deduce (a ~ [Char])
from the context (Show a)
bound by a type expected by the context: Show a => Int -> a
at test.hs:9:11-27
`a' is a rigid type variable bound by
a type expected by the context: Show a => Int -> a at test.hs:9:11
In the return type of a call of `show'
In the expression: show x
In the expression: \ x -> show x
test.hs:10:21:
Could not deduce (a ~ Double)
from the context (Show a)
bound by a type expected by the context: Show a => Int -> a
at test.hs:10:11-24
`a' is a rigid type variable bound by
a type expected by the context: Show a => Int -> a at test.hs:10:11
In the first argument of `(^)', namely `c'
In the expression: c^x
In the expression: \ x -> c^x
Failed, modules loaded: none.
爲什麼會發生這種情況?第二個例子不應該等同於第一個例子嗎?這些n-rank類型的用法有什麼區別?當我想要這樣的多態時,是否有可能遺漏額外的ADT定義並僅使用簡單類型?
非常感謝您的解釋。所有答案都非常有幫助,但我認爲我的答案中有「缺少拼圖」,所以我將其標記爲已接受。 –