我可以基于Church编码和Rank-N类型的存在性来实现异构列表吗?

时间:2017-06-25 08:38:55

标签: haskell types existential-type higher-rank-types church-encoding

在我尝试理解存在类型时,我已经读过教会编码以及Rank-N类型扩展将足以在没有存在量化的情况下在Haskell中对它们进行编码。我找到了这个直截了当的例子:

type Obj = forall y. (forall x. (Show x) => x -> y) -> y

obj :: Obj
obj f = f "hello"

app :: Obj -> String
app obj = obj (\x -> show x)

在Haskell Wiki中,我偶然发现了以下基于存在类型的异构列表示例:

data Obj = forall a. (Show a) => Obj a

xs :: [Obj]
xs = [Obj 1, Obj "foo", Obj 'c']

doShow :: [Obj] -> String
doShow [] = ""
doShow ((Obj x):xs) = show x ++ doShow xs

现在我尝试用Church表达这个实现,并且因非法多态类型错误而失败:

type Obj = forall y. (forall x. (Show x) => x -> y) -> y 

obj1 :: Obj 
obj1 f = f 1 

obj2 :: Obj 
obj2 f = f "foo" 

obj3 :: Obj 
obj3 f = f 'c' 

xs :: [Obj] 
xs = [obj1, obj2, obj3]

doShow :: [Obj] -> String 
doShow [] = "" 
doShow (obj:xs) = obj (\x -> show x ++ doShow xs)

我猜这个翻译很简单,只是错误。存在类型可以用Church / Rank-N编码是正确的吗?  它是如何正确完成的?

0 个答案:

没有答案