“你不明白欣德利 - 米尔纳的哪一部分?”

本文翻译自:“What part of Hindley-Milner do you not understand?”

I swear there used to be a T-shirt for sale featuring the immortal words: 发誓曾经有一件T恤出售,上面写着不朽的话:


What part of 什么部分

辛德米尔纳

do you not understand? 明白吗?


In my case, the answer would be... all of it! 就我而言,答案就是......全部!

In particular, I often see notation like this in Haskell papers, but I have no clue what any of it means. 特别是,我经常在Haskell论文中看到这样的符号,但我不知道它的含义是什么。 I have no idea what branch of mathematics it's supposed to be. 我不知道它应该是什么样的数学分支。

I recognise the letters of the Greek alphabet of course, and symbols such as "∉" (which usually means that something is not an element of a set). 我当然认识到希腊字母的字母,以及诸如“∉”之类的符号(通常意味着某些东西不是一组的元素)。

On the other hand, I've never seen "⊢" before ( Wikipedia claims it might mean "partition" ). 另一方面,我以前从未见过“⊢”( 维基百科称它可能意味着“分区” )。 I'm also unfamiliar with the use of the vinculum here. 我也不熟悉这里使用的vinculum。 (Usually it denotes a fraction, but that does not appear to be the case here.) (通常它表示一小部分,但不会出现在这里是这种情况。)

If somebody could at least tell me where to start looking to comprehend what this sea of symbols means, that would be helpful. 如果有人至少可以告诉我从哪里开始想要理解这个符号海洋的含义,那将会有所帮助。


#1楼

参考:https://stackoom.com/question/qaHw/你不明白欣德利-米尔纳的哪一部分


#2楼

if somebody could at least tell me where to start looking to comprehend what this sea of symbols means 如果有人能够至少告诉我从哪里开始想要理解这个符号海洋意味着什么

See " Practical Foundations of Programming Languages. ", chapters 2 and 3, on the style of logic through judgements and derivations. 参见“ 编程语言的实用基础 ”,第2章和第3章,通过判断和推导来讨论逻辑风格。 The entire book is now available on Amazon. 整本书现已在亚马逊上市。

Chapter 2 第2章

Inductive Definitions 归纳定义

Inductive definitions are an indispensable tool in the study of programming languages. 归纳定义是编程语言研究中不可或缺的工具。 In this chapter we will develop the basic framework of inductive definitions, and give some examples of their use. 在本章中,我们将开发归纳定义的基本框架,并举例说明它们的使用。 An inductive definition consists of a set of rules for deriving judgments , or assertions , of a variety of forms. 归纳定义包括一组用于推导各种形式的判断断言规则 Judgments are statements about one or more syntactic objects of a specified sort. 判断是关于指定排序的一个或多个句法对象的陈述。 The rules specify necessary and sufficient conditions for the validity of a judgment, and hence fully determine its meaning. 规则规定了判决有效性的必要和充分条件,从而充分确定其含义。

2.1 Judgments 2.1判决

We start with the notion of a judgment , or assertion about a syntactic object. 我们从判断的概念开始,或者关于句法对象的断言 We shall make use of many forms of judgment, including examples such as these: 我们将利用多种形式的判断,包括以下例子:

  • n natn is a natural number n nat - n是一个自然数
  • n = n1 + n2n is the sum of n1 and n2 n = n1 + n2 -nn1n2之和
  • τ typeτ is a type τ - τ是一种类型
  • e : τ — expression e has type τ eτ - 表达式e具有类型τ
  • ev — expression e has value v ë⇓N -表达式e具有值v

A judgment states that one or more syntactic objects have a property or stand in some relation to one another. 判断表明一个或多个句法对象具有彼此某种关系的属性或立场。 The property or relation itself is called a judgment form , and the judgment that an object or objects have that property or stand in that relation is said to be an instance of that judgment form. 财产或关系本身被称为判断形式 ,并且一个或多个对象在该关系中具有该属性或立场的判断被认为是该判断形式的实例 A judgment form is also called a predicate , and the objects constituting an instance are its subjects . 判断形式也称为谓词 ,构成实例的对象是其主语 We write a J for the judgment asserting that J holds of a . 我们为判断断言J持有a而了一个 J. When it is not important to stress the subject of the judgment, (text cuts off here) 当强调判断的主题并不重要时,(文字在这里切断)


#3楼

The notation comes from natural deduction . 符号来自自然演绎

⊢ symbol is called turnstile . ⊢符号称为十字转门

The 6 rules are very easy. 这6条规则非常简单。

Var rule is rather trivial rule - it says that if type for identifier is already present in your type environment, then to infer the type you just take it from the environment as is. Var规则是相当简单的规则 - 它表示如果类型环境中已存在标识符类型,那么要推断出类型,您只需从环境中获取它。

App rule says that if you have two identifiers e0 and e1 and can infer their types, then you can infer the type of application e0 e1 . App规则说如果你有两个标识符e0e1并且可以推断它们的类型,那么你可以推断应用程序的类型e0 e1 The rule reads like this if you know that e0 :: t0 -> t1 and e1 :: t0 (the same t0!), then application is well-typed and the type is t1 . 如果您知道e0 :: t0 -> t1e1 :: t0 (相同的t0!),则规则如下所示,然后应用程序是良好类型的,类型是t1

Abs and Let are rules to infer types for lambda-abstraction and let-in. AbsLet是推断lambda抽象和let-in类型的规则。

Inst rule says that you can substitute a type with less general one. Inst规则表示您可以用较不普遍的类型替换类型。


#4楼

This syntax, while it may look complicated, is actually fairly simple. 这种语法虽然看起来很复杂,但实际上相当简单。 The basic idea comes from formal logic: the whole expression is an implication with the top half being the assumptions and the bottom half being the result. 基本思想来自形式逻辑:整个表达是一种含义,上半部分是假设,下半部分是结果。 That is, if you know that the top expressions are true, you can conclude that the bottom expressions are true as well. 也就是说,如果你知道顶部表达式是真的,你可以得出结论底部表达式也是真的。

Symbols 符号

Another thing to keep in mind is that some letters have traditional meanings; 另外要记住的是,有些字母具有传统意义; particularly, Γ represents the "context" you're in—that is, what the types of other things you've seen are. 特别是,Γ代表你所处的“背景” - 即你所看到的其他类型的东西。 So something like Γ ⊢ ... means "the expression ... when you know the types of every expression in Γ . 所以像Γ ⊢ ...意味着“表达式...当你知道Γ中每个表达式的类型时。

The symbol essentially means that you can prove something. 符号实质上意味着你可以证明某些东西。 So Γ ⊢ ... is a statement saying "I can prove ... in a context Γ . These statements are also called type judgements. 所以Γ ⊢ ...是一份声明说:“我可以证明...在上下文Γ 。这些声明也被称为类型的判断。

Another thing to keep in mind: in math, just like ML and Scala, x : σ means that x has type σ . 另外要记住的是:在数学中,就像ML和Scala一样, x : σ表示x类型为σ You can read it just like Haskell's x :: σ . 您可以像Haskell的x :: σ一样阅读它。

What each rule means 每条规则的含义

So, knowing this, the first expression becomes easy to understand: if we know that x : σ ∈ Γ (that is, x has some type σ in some context Γ ), then we know that Γ ⊢ x : σ (that is, in Γ , x has type σ ). 所以,知道这一点,第一个表达式变得很容易理解:如果我们知道x : σ ∈ Γ (即x已某种类型的σ在某些情况下Γ ),那么我们就知道Γ ⊢ x : σ (即,在Γx具有类型σ )。 So really, this isn't telling you anything super-interesting; 所以真的,这并没有告诉你任何超级有趣的东西; it just tells you how to use your context. 它只是告诉你如何使用你的上下文。

The other rules are also simple. 其他规则也很简单。 For example, take [App] . 例如,拿[App] This rule has two conditions: e₀ is a function from some type τ to some type τ' and e₁ is a value of type τ . 该规则有两个条件: e₀是从某种类型τ到某种类型τ'的函数, e₁是类型τ的值。 Now you know what type you will get by applying e₀ to e₁ ! 现在你知道你将通过应用获得什么类型的e₀e₁ Hopefully this isn't a surprise :). 希望这不是一个惊喜:)。

The next rule has some more new syntax. 下一个规则有一些新的语法。 Particularly, Γ, x : τ just means the context made up of Γ and the judgement x : τ . 特别地, Γ, x : τ仅表示由Γ和判断x : τ组成的上下文。 So, if we know that the variable x has a type of τ and the expression e has a type τ' , we also know the type of a function that takes x and returns e . 因此,如果我们知道变量x的类型为τ且表达式e的类型为τ' ,我们也知道采用x并返回e的函数的类型。 This just tells us what to do if we've figured out what type a function takes and what type it returns, so it shouldn't be surprising either. 这只是告诉我们如果我们弄清楚函数采用什么类型以及它返回什么类型该怎么做,所以它也不应该令人惊讶。

The next one just tells you how to handle let statements. 下一个只是告诉你如何处理let语句。 If you know that some expression e₁ has a type τ as long as x has a type σ , then a let expression which locally binds x to a value of type σ will make e₁ have a type τ . 如果你知道有些表达e₁的类型为τ只要x有一个类型σ ,然后let表达式,局部结合x到类型的值σ将会使e₁有一个类型τ Really, this just tells you that a let statement essentially lets you expand the context with a new binding—which is exactly what let does! 实际上,这只是告诉你一个let语句本质上允许你用一个新的绑定扩展上下文 - 这正是let做的!

The [Inst] rule deals with sub-typing. [Inst]规则处理子类型。 It says that if you have a value of type σ' and it is a sub-type of σ ( represents a partial ordering relation) then that expression is also of type σ . 它说的是,如果你有类型的值σ' ,它是一个子类型的σ表示偏序关系),那么表达式也是类型σ

The final rule deals with generalizing types. 最终规则涉及泛化类型。 A quick aside: a free variable is a variable that is not introduced by a let-statement or lambda inside some expression; 快速搁置:自由变量是一个变量,它不是由某个表达式中的let语句或lambda引入的; this expression now depends on the value of the free variable from its context.The rule is saying that if there is some variable α which is not "free" in anything in your context, then it is safe to say that any expression whose type you know e : σ will have that type for any value of α . 这个表达式现在依赖于来自其上下文的自由变量的值。规则是说如果在你的上下文中有任何变量α 不是 “自由”的,那么可以肯定地说任何类型为你的表达式知道e : σ将具有任何 α值的类型。

How to use the rules 如何使用规则

So, now that you understand the symbols, what do you do with these rules? 那么,既然您已了解符号,那么您对这些规则有何看法? Well, you can use these rules to figure out the type of various values. 好吧,您可以使用这些规则来确定各种值的类型。 To do this, look at your expression (say fxy ) and find a rule that has a conclusion (the bottom part) that matches your statement. 为此,请查看您的表达式(例如fxy )并查找与您的语句匹配的结论(底部)的规则。 Let's call the thing you're trying to find your "goal". 让我们称之为你想要找到你的“目标”。 In this case, you would look at the rule that ends in e₀ e₁ . 在这种情况下,你会看到以e₀ e₁结尾的规则。 When you've found this, you now have to find rules proving everything above the line of this rule. 当你发现这一点时,你现在必须找到规则,证明这条规则的所有内容。 These things generally correspond to the types of sub-expressions, so you're essentially recursing on parts of the expression. 这些东西通常对应于子表达式的类型,因此您实际上是对表达式的某些部分进行递归。 You just do this until you finish your proof tree, which gives you a proof of the type of your expression. 您只需执行此操作,直到完成校对树,这样可以证明表达式的类型。

So all these rules do is specify exactly—and in the usual mathematically pedantic detail :P—how to figure out the types of expressions. 因此,所有这些规则都是精确地指定 - 并且在通常的数学上迂腐的细节:P-如何找出表达式的类型。

Now, this should sound familiar if you've ever used Prolog—you're essentially computing the proof tree like a human Prolog interpreter. 现在,如果您曾经使用Prolog,这应该听起来很熟悉 - 您实际上是像人类Prolog解释器一样计算证明树。 There is a reason Prolog is called "logic programming"! Prolog被称为“逻辑编程”是有原因的! This is also important as the first way I was introduced to the HM inference algorithm was by implementing it in Prolog. 这也很重要,因为我介绍HM推理算法的第一种方法是在Prolog中实现它。 This is actually surprisingly simple and makes what's going on clear. 这实际上非常简单,并且清楚地表明了这一点。 You should certainly try it. 你当然应该尝试一下。

Note: I probably made some mistakes in this explanation and would love it if somebody would point them out. 注意:我可能在这个解释中犯了一些错误,并且如果有人指出它们会很喜欢它。 I'll actually be covering this in class in a couple of weeks, so I'll be more confident then :P. 我实际上会在几周内在课堂上报道,所以我会更自信:P。


#5楼

  • The horizontal bar means that "[above] implies [below]". 水平条表示“[以上] 暗示 [下方]”。
  • If there are multiple expressions in [above], then consider them anded together; 如果在[以上] 的多个表达式 ,并考虑它们一起相与 ; all of the [above] must be true in order to guarantee the [below]. 所有[上述]必须为真以保证[下方]。
  • : means has type :表示有类型
  • means is in . 意思是在 (Likewise means "is not in".) (同样表示“不在”。)
  • Γ is usually used to refer to an environment or context; Γ通常用于指代环境或上下文; in this case it can be thought of as a set of type annotations, pairing an identifier with its type. 在这种情况下,它可以被认为是一组类型注释,将标识符与其类型配对。 Therefore x : σ ∈ Γ means that the environment Γ includes the fact that x has type σ . 因此x : σ ∈ Γ意味着环境Γ包括x具有类型σ的事实。
  • can be read as proves or determines. 可以作为证明或确定。 Γ ⊢ x : σ means that the environment Γ determines that x has type σ . Γ ⊢ x : σ表示环境Γ确定x具有类型σ
  • , is a way of including specific additional assumptions into an environment Γ . ,包括具体的附加假设到环境中的一种方式Γ
    Therefore, Γ, x : τ ⊢ e : τ' means that environment Γ , with the additional, overriding assumption that x has type τ , proves that e has type τ' . 因此, Γ, x : τ ⊢ e : τ'意味着环境Γ以及x具有类型τ的附加的超越假设 ,证明e具有类型τ'

As requested: operator precedence, from highest to lowest: 根据要求:运营商优先级,从最高到最低:

  • Language-specific infix and mixfix operators, such as λ x . e 特定于语言的中缀和mixfix运算符,例如λx λ x . e λ x . e , ∀ α . σ λ x . e∀ α . σ ∀ α . σ , and τ → τ' , let x = e0 in e1 , and whitespace for function application. ∀ α . σ ,和τ → τ'let x = e0 in e1 ,以及用于功能应用的空白。
  • :
  • and
  • , (left-associative) , (左联想)
  • whitespace separating multiple propositions (associative) 分隔多个命题的空格(关联)
  • the horizontal bar 横杠

#6楼

There are two ways to think of e : σ. 有两种方法可以考虑e:σ。 One is "the expression e has type σ", another is "the ordered pair of the expression e and the type σ". 一个是“表达式e具有类型σ”,另一个是“表达式e和类型σ的有序对”。

View Γ as the knowledge about the types of expressions, implemented as a set of pairs of expression and type, e : σ. 查看Γ作为关于表达式类型的知识,实现为一组表达式和类型,e:σ。

The turnstile ⊢ means that from the knowledge on the left, we can deduce what's on the right. 旋转门⊢意味着从左侧的知识中,我们可以推断出右侧的内容。

The first rule [Var] can thus be read: 因此可以读取第一个规则[Var]:
If our knowledge Γ contains the pair e : σ, then we can deduce from Γ that e has type σ. 如果我们的知识Γ包含对e:σ,那么我们可以从Γ推导出e具有类型σ。

The second rule [App] can be read: 第二条规则[App]可以读取:
If we from Γ can deduce that e_0 has the type τ → τ', and we from Γ can deduce that e_1 has the type τ, then we from Γ can deduce that e_0 e_1 has the type τ'. 如果我们从Γ可以推断出e_0具有类型τ→τ',并且我们从Γ可以推断出e_1具有类型τ,那么我们从Γ可以推断出e_0 e_1具有类型τ'。

It's common to write Γ, e : σ instead of Γ ∪ {e : σ}. 写Γ,e:σ而不是Γ∪{e:σ}是很常见的。

The third rule [Abs] can thus be read: 因此可以读取第三条规则[Abs]:
If we from Γ extended with x : τ can deduce that e has type τ', then we from Γ can deduce that λx.e has the type τ → τ'. 如果我们从Γ延伸到x:τ可以推导出e具有类型τ',那么我们从Γ可以推导出λx.e具有类型τ→τ'。

The fourth rule [Let] is left as an exercise. 第四条规则[让]留作练习。 :-) :-)

The fifth rule [Inst] can be read: 第五条规则[Inst]可以读取:
If we from Γ can deduce that e has type σ', and σ' is a subtype of σ, then we from Γ can deduce that e has type σ. 如果我们从Γ可以推导出e具有类型σ',并且σ'是σ的子类型,那么我们从Γ可以推导出e具有类型σ。

The sixth and last rule [Gen] can be read: 可以阅读第六个也是最后一个规则[Gen]:
If we from Γ can deduce that e has type σ, and α is not a free type variable in any of the types in Γ, then we from Γ can deduce that e has type ∀α σ. 如果我们从Γ可以推导出e具有类型σ,并且α不是Γ中的任何类型中的自由类型变量,那么我们从Γ可以推导出e具有类型∀ασ。

发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105383961