gpt4 book ai didi

Haskell 函数返回存在类型

转载 作者:行者123 更新时间:2023-12-04 05:11:57 26 4
gpt4 key购买 nike

是否可以编写一个 Haskell 函数来生成参数化类型,其中隐藏了确切的类型参数? IE。类似于 f :: T -> (exists a. U a) ?明显的尝试:

{-# LANGUAGE ExistentialQuantification #-}

data D a = D a

data Wrap = forall a. Wrap (D a)

unwrap :: Wrap -> D a
unwrap (Wrap d) = d

无法编译:
Couldn't match type `a1' with `a'
`a1' is a rigid type variable bound by
a pattern with constructor
Wrap :: forall a. D a -> Wrap,
in an equation for `unwrap'
at test.hs:8:9
`a' is a rigid type variable bound by
the type signature for unwrap :: Wrap -> D a at test.hs:7:11
Expected type: D a
Actual type: D a1
In the expression: d
In an equation for `unwrap': unwrap (Wrap d) = d

我知道这是一个人为的例子,但我很好奇是否有办法让 GHC 相信我不关心 D 的确切类型。被参数化,没有为 unwrap 的结果引入另一种存在包装类型.

澄清一下,我确实想要类型安全,但也希望能够应用函数 dToString :: D a -> String不关心 a (例如,因为它只是从 D 中提取一个字符串字段)到 unwrap 的结果.我意识到还有其他方法可以实现它(例如定义 wrapToString (Wrap d) = dToString d ),但我更感兴趣的是是否存在不允许这种隐藏在存在下的根本原因。

最佳答案

是的,你可以,但不是直接的方式。

{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}

data D a = D a

data Wrap = forall a. Wrap (D a)

unwrap :: Wrap -> forall r. (forall a. D a -> r) -> r
unwrap (Wrap x) k = k x

test :: D a -> IO ()
test (D a) = putStrLn "Got a D something"

main = unwrap (Wrap (D 5)) test

您不能返回 D something_unknown从您的函数中提取,但您可以将其提取并立即将其传递给另一个接受 D a 的函数, 如图所示。

关于Haskell 函数返回存在类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com