gpt4 book ai didi

haskell - 如何查询统一类型到ghci?

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

可以查询 ghci 的统一类型吗?

例如,如果我想知道 (Int -> Bool) 之间的统一类型和 (a -> Bool)我怎样才能查询到 ghci?

我试图解决的是来自 Haskell: The Craft of Functional Programming 第三版的练习 13.23。 .

How can you use the Haskell system to check whether two type expressions are unifiable, and if so what is their unification? Hint: you can make dummy definitions in Haskell in which the defined value, zircon say, is equated with itself:

zircon = zircon

Values defined like this can be declared to have any type you wish.



谢谢,
塞巴斯蒂安。

最佳答案

一种方法是使用 asTypeOf :: a -> a -> a .作为函数,asTypeOf不是很有趣,但它的类型很好:它强制它的两个参数和它的返回类型统一。所以:

> :t asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool)
asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool)
:: Int -> Bool

所以你可以看到这两种类型统一为 Int -> Bool .对于一个稍微有趣的例子,让我们统一 Maybe af (Bool, c) :
> :t asTypeOf (undefined :: Maybe a) (undefined :: f (Bool, c))
asTypeOf (undefined :: Maybe a) (undefined :: f (Bool, c))
:: Maybe (Bool, c)

另一方面,出于练习的目的,我鼓励您尝试手动统一。一旦你掌握了它,这并不难,而且是一项你会一遍又一遍地使用的技能。

关于haskell - 如何查询统一类型到ghci?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183951/

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