简体   繁体   English

多态推理

[英]Polymorphic reasoning

I am learning Haskell and in internet I've found is paper from Philip Wadler. 我正在学习Haskell,在互联网上我找到了来自Philip Wadler的论文
I read it and did not understand at all, but it somehow connects to polymorphic function. 我读它并且根本不理解,但它以某种方式连接到多态函数。

For example: 例如:

polyfunc :: a -> a -> a

It is a polymorphic function of any type. 它是任何类型的多态函数。

What is the free theorem in connection of the example polyfunc ? 连接示例polyfunc的自由定理是什么?

I feel like if I actually understood that paper then any code I wrote would be coauthored by God. 我觉得如果我真的理解那篇论文,那么我写的任何代码都会被上帝合着。

My best guess for this problem though is that all polyfunc can do is either always return the first argument or always return the second argument. 我对这个问题的最好猜测是,所有polyfunc都可以做的总是返回第一个参数或总是返回第二个参数。 So there are actually only two implementations of polyfunc , 所以实际上只有两个polyfunc实现,

polyfuncA a _ = a
polyfuncB _ b = b

The paper gives you a way to prove that claim. 本文为您提供了证明该声明的方法。

This is a very important concept. 这是一个非常重要的概念。 For example, I've been involved in data quality research previously. 例如,我以前参与过数据质量研究。 This free theorem says that there is no function which can select the best data from two arbitrary pieces of data. 这个自由定理说没有能从两个任意数据中选择最佳数据的函数。 We have to know something more. 我们必须了解更多。 Its actually a no-brainer that I was surprised to find some people willing to overlook. 实际上,我很惊讶地发现有些人愿意忽略它。

I've never really understood the algorithm laid out in that paper either, so I thought I would try to figure it out. 我从来没有真正理解那篇论文中提出的算法,所以我想我会试着弄明白。

(1) Type of function in question
f :: a -> a -> a

(2) Rephrasing as a relation
f : ∀X. X -> X -> X

(3) By parametricity
(f, f) ∈ ∀X. X -> X -> X

(4) By definition of ∀ on relations
for all Q : A <=> A',
  (fA, fA') ∈ Q -> Q -> Q

(5) Applying definition of -> on relations to the first -> in (4)
for all Q : A <=> A',
  for all (x, x') ∈ Q,
    (fA x, fA' x') ∈ Q -> Q

(6) Applying definition of -> on relations to (5)
for all Q : A <=> A',
  for all (x, x') ∈ Q,
    for all (y, y') ∈ Q,
      (fA x y, fA' x' y') ∈ Q

At this point I was done expanding the relational definition, but wasn't sure how to get this back from relations into terms of functions and types, so I went and found a webapp that will automatically derive the free theorem for a type . 在这一点上,我完成了扩展关系定义,但不知道如何从关系到函数和类型的术语,所以我去找到一个webapp,它将自动导出一个类型的自由定理 I won't spoil (yet) what result it gives, but looking at it did help me figure out the next step in my proof. 我不会破坏(但)它给出的结果,但看着它确实帮助我弄清楚我的证明的下一步。

The next step is to get back into function-land from relation-land, by noting that Q can be the type of any function at all and this will still hold. 下一步是从关系土地回到功能土地,注意到Q可以是任何功能的类型,这仍然有效。

(7) Specializing Q to a function g :: p -> q
for all p, q
  for all g :: p -> q
    where g x = x'
      and g y = y'
        g (f x y) = f x' y'

(8) By definitions of x' and y'
for all p, q
  for all g :: p -> q
    g (f x y) = f (g x) (g y)

That looks true, right? 这看起来是真的,对吗? It is equivalent to use g to transform both elements and then let f choose between them, or to let f choose an element and then transform it with g . 它相当于使用g来转换两个元素然后让f在它们之间进行选择,或者让f选择一个元素然后用g转换它。 By parametricity, f can't change whether it chooses the left or right element based on anything that g does. 通过参数化, f不能改变它是否根据g所做的任何事情选择左或右元素。

Of course the claim given in trevor cook's answer is true: f must either always choose its first argument, or always choose its second. 当然,特雷弗·库克的回答中提出的主张是正确的: f必须总是选择第一个参数,或者总是选​​择第二个参数。 I'm not sure whether the free theorem I derived is equivalent to that, or is a weaker version of it. 我不确定我推导出的自由定理是否等同于它,或者它是否是一个较弱的版本。


And incidentally, this is a special case of something that is already covered explicitly in the paper. 顺便提一下,这是本文已明确涵盖的特殊情况。 It gives the theorem for 它给出了定理

k :: x -> y -> x

which of course is the same as your function f , where x ~ a and y ~ a . 这当然与你的函数f相同,其中x ~ ay ~ a The result that it gives is the same as the one I described: 它给出的结果与我描述的结果相同:

for all a, b, x, y
  a (k x y) = k (a x) (b y)

if we choose b=a to make the two results equivalent. 如果我们选择b=a来使两个结果等价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM