Fof*_*Fof 4 haskell types unification ghci
可以查询ghci的统一类型吗?
例如,如果我想知道之间的统一类型(Int -> Bool)以及(a -> Bool)如何查询到ghci?
我想要解决的是Haskell第三版的练习13.23 :功能编程工艺.
你如何使用Haskell系统来检查两个类型表达式是否是统一的,如果是这样,它们的统一是什么?提示:您可以在Haskell中进行虚拟定义,其中定义的值(锆石)与其自身等同:
Run Code Online (Sandbox Code Playgroud)zircon = zircon像这样定义的值可以声明为您想要的任何类型.
谢谢,
塞巴斯蒂安.
一种方法是使用.作为一个函数,不是很有趣,但它的类型很好:它强制它的两个参数和它的返回类型统一.所以:asTypeOf:: a -> a -> aasTypeOf
> :t asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool)
asTypeOf (undefined :: Int -> Bool) (undefined :: a -> Bool)
:: Int -> Bool
Run Code Online (Sandbox Code Playgroud)
所以你可以看到这两种类型统一到了Int -> Bool.对于一个稍微有趣的例子,让我们统一Maybe a并f (Bool, c):
> :t asTypeOf (undefined :: Maybe a) (undefined :: f (Bool, c))
asTypeOf (undefined :: Maybe a) (undefined :: f (Bool, c))
:: Maybe (Bool, c)
Run Code Online (Sandbox Code Playgroud)
另一方面,为了练习,我鼓励你尝试手工统一.一旦掌握了它,这并不难,而且是一种你会反复使用的技能.
| 归档时间: |
|
| 查看次数: |
136 次 |
| 最近记录: |