根据函数的类型确定函数行为 [英] determining function behavior from the type of the function

查看:104
本文介绍了根据函数的类型确定函数行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个例子取自Real World Haskell -

  ghci> :输入fst 
fst ::(a,b) - > a

它们显示 fst 函数,然后按照这个段落...



结果类型 fst a 。我们已经提到参数多态使得真正的类型不可访问: fst 没有足够的信息构建 a 类型的值,也无法将 a 转换为 b ,所以唯一可能的有效行为(省略无限循环或崩溃)第一个元素。



我觉得我错过了这个段落的基本点,也许对Haskell很重要。为什么 fst 函数返回类型 b ?为什么它不能把这个元组作为参数,而是简单地返回一个 Int (或者任何其他类型不是 a )?我不明白为什么它必须返回类型 a



p>

解决方案

如果它执行了任何这些操作,它的类型将会改变。报价说的是,鉴于我们知道 fst 的类型是(a,b) - >一个,我们可以做出这些推论。如果它有不同的类型,我们将无法这样做。



例如,请参阅

  snd ::(a,b) - > a 
snd(x,y)= y

不会进行类型检查,所以我们知道类型(a,b) - >的值。一个的行为不能像 snd



参数性基本上是一个事实,即一个多态函数某种类型的人必须遵守某些法律通过结构 - 也就是说,没有那种不遵守它们的良好类型的表达。因此,为了有可能用它来证明 fst 的事情,我们必须首先知道 fst 的类型。



特别注意这个词多态性:我们不能对非多态类型做类似的推论。例如,

  myFst ::(Int,String) - > Int 
myFst(a,b)= a

类型检查, / p>

  myFst ::(Int,String) - > Int 
myFst(a,b)= 42

甚至

  myFst ::(Int,String) - > Int 
myFst(a,b)=长度b

参数性主要依赖于多态函数不能查看它所调用的类型。因此, fst 知道的类型 a 的唯一值就是它给出的值:元组的第一个元素。


New to Haskell so sorry if this is very basic

This example is taken from "Real World Haskell" -

ghci> :type fst  
fst :: (a, b) -> a

They show the type of the fst function and then follow it with this paragraph...

"The result type of fst is a. We've already mentioned that parametric polymorphism makes the real type inaccessible: fst doesn't have enough information to construct a value of type a, nor can it turn an a into a b. So the only possible valid behaviour (omitting infinite loops or crashes) it can have is to return the first element of the pair."

I feel like I am missing the fundamental point of the paragraph, and perhaps something important about Haskell. Why couldn't the fst function return type b? Why couldn't it take the tuple as a param, but simply return an Int ( or any other type that is NOT a)? I don't understand why it MUST return type a?

Thanks

解决方案

If it did any of those things, its type would change. What the quote is saying is that, given that we know fst is of type (a, b) -> a, we can make those deductions about it. If it had a different type, we would not be able to do so.

For instance, see that

snd :: (a, b) -> a
snd (x, y) = y

does not type-check, and so we know a value of type (a, b) -> a cannot behave like snd.

Parametricity is basically the fact that a polymorphic function of a certain type must obey certain laws by construction — i.e., there is no well-typed expression of that type that does not obey them. So, for it to be possible to prove things about fst with it, we must first know fst's type.

Note especially the word polymorphism there: we can't do the same kind of deductions about non-polymorphic types. For instance,

myFst :: (Int, String) -> Int
myFst (a, b) = a

type-checks, but so does

myFst :: (Int, String) -> Int
myFst (a, b) = 42

and even

myFst :: (Int, String) -> Int
myFst (a, b) = length b

Parametricity relies crucially on the fact that a polymorphic function can't "look" at the types it is called with. So the only value of type a that fst knows about is the one it's given: the first element of the tuple.

这篇关于根据函数的类型确定函数行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆