Gõ suy luận với các loại sản phẩm


15

Tôi đang làm việc trên một trình biên dịch cho một ngôn ngữ nối và muốn thêm hỗ trợ suy luận kiểu. Tôi hiểu Hindley, Milner, nhưng tôi đã học lý thuyết loại khi tôi đi, vì vậy tôi không chắc làm thế nào để thích nghi với nó. Là hệ thống sau đây âm thanh và có thể suy luận chắc chắn?

Một thuật ngữ là một nghĩa đen, một thành phần của các thuật ngữ, một trích dẫn của một thuật ngữ hoặc nguyên thủy.

e::=x|ee|[e]|

All terms denote functions. For two functions e1 and e2, e1e2=e2e1, that is, juxtaposition denotes reverse composition. Literals denote niladic functions.

The terms other than composition have basic type rules:

x:ι[Lit]Γe:σΓ[e]:α.ασ×α[Quot],α not free in Γ

Notably absent are rules for application, since concatenative languages lack it.

A type is either a literal, a type variable, or a function from stacks to stacks, where a stack is defined as a right-nested tuple. All functions are implicitly polymorphic with respect to the “rest of the stack”.

τ::=ι|α|ρρρ::=()|τ×ρσ::=τ|α.σ

This is the first thing that seems suspect, but I don’t know exactly what’s wrong with it.

To help readability and cut down on parentheses, I’ll assume that ab=b×(a) in type schemes. I’ll also use a capital letter for a variable denoting a stack, rather than a single value.

There are six primitives. The first five are pretty innocuous. dup takes the topmost value and produces two copies of it. swap changes the order of the top two values. pop discards the top value. quote takes a value and produces a quotation (function) that returns it. apply applies a quotation to the stack.

dup::Ab.AbAbbswap::Abc.AbcAcbpop::Ab.AbAquote::Ab.AbA(C.CCb)apply::AB.A(AB)B

The last combinator, compose, ought to take two quotations and return the type of their concatenation, that is, [e1][e2]compose=[e1e2]. In the statically typed concatenative language Cat, the type of compose is very straightforward.

compose::ABCD.A(BC)(CD)A(BD)

However, this type is too restrictive: it requires that the production of the first function exactly match the consumption of the second. In reality, you have to assume distinct types, then unify them. But how would you write that type?

compose::ABCDE.A(BC)(DE)A

If you let denote a difference of two types, then I think you can write the type of compose correctly.

compose::ABCDE.A(BC)(DE)A((DC)B((CD)E))

This is still relatively straightforward: compose takes a function f1:BC and one f2:DE. Its result consumes B atop the consumption of f2 not produced by f1, and produces D atop the production of f1 not consumed by f2. This gives the rule for ordinary composition.

Γe1:AB.ABΓe2:CD.CDΓe1e2:((CB)A((BC)D))[Comp]

However, I don’t know that this hypothetical actually corresponds to anything, and I’ve been chasing it around in circles for long enough that I think I took a wrong turn. Could it be a simple difference of tuples?

A.()A=()A.A()=AABCD.ABCD=BD iff A=Cotherwise=undefined

Is there something horribly broken about this that I’m not seeing, or am I on something like the right track? (I’ve probably quantified some of this stuff wrongly and would appreciate fixes in that area as well.)


How do you use variables in your grammar? This question should help you in handling the "subtyping" you seem to need.
jmad

1
@jmad: I’m not sure I understand the question. Type variables are just there for the sake of formally defining type schemes, and the language itself doesn’t have variables at all, just definitions, which can be [mutually] recursive.
Jon Purdy

Fair enough. Can you say why (perhaps with an example) the rule for compose is too restrictive? I have the impression that this is fine like this. (e.g. the restriction C=D could be handled by unification like for application in like in the λ-calculus)
jmad

@jmad: Sure. Consider twice defined as dup compose apply, which takes a quotation and applies it twice. [1 +] twice is fine: you’re composing two functions of type ιι. But [pop] twice is not: if Ab.f1,f2:AbA, the problem is that AAb, so the expression is disallowed even though it ought to be valid and have type Ab.AbbA. The solution is of course to put the qualifier in the right place, but I’m mainly wondering how to actually write the type of compose without some circular definition.
Jon Purdy

Câu trả lời:


9

The following rank-2 type

compose:ABCδ.δ (α.α AαB) (β.β BβC)δ (γ.γ AγC)
seems to be sufficiently general. It is much more polymorphic than the type proposed in the question. Here variable quantify over contiguous chunks of stack, which captures multi-argument functions.

Greek letters are used for the rest-of-the-stack variables for clarity only.

It expresses the constraints that the output stack of the first element on the stack needs to be the same as the input stack of the second element. Appropriately instantiating the variable B for the two actually arguments is the way of getting the constraints to work properly, rather than defining a new operation, as you propose in the question.

Type checking rank-2 types is undecidable in general, I believe, though some work has been done that gives good results in practice (for Haskell):

  • Simon L. Peyton Jones, Dimitrios Vytiniotis, Stephanie Weirich, Mark Shields: Practical type inference for arbitrary-rank types. J. Funct. Program. 17(1): 1-82 (2007)

The type rule for composition is simply:

Γe1:α.α Aα BΓe1:α.α Bα CΓe1 e2:α.α Aα C

To get the type system to work in general, you need the following specialisation rule:

Γe:α.α Aα BΓe:α.C Aα C B

Thanks, this was very helpful. This type is correct for functions of a single argument, but it doesn’t support multiple arguments. For instance, dup + should have type ιι because + has type ιιι. But type inference in the absence of annotations is an absolute requirement, so clearly I need to go back to the drawing board. I have an idea for another approach to pursue, though, and will blog about it if it works out.
Jon Purdy

1
The stack types quantify over stack fragments, so there is no problem dealing with two argument functions. I'm not sure how this applies to dup +, as that does not use compose, as you defined it above.
Dave Clarke

Er, right, I meant [dup] [+] compose. But I read αB as B×α; say B=ι×ι; then you have (ι×ι)×α and not ι×(ι×α). The nesting isn’t right, unless you flip the stack around so that the top is the last (deepest nested) element.
Jon Purdy

I may be building my stack in the wrong direction. I don't think the nesting matters, so long as the pairs building up the stack do not appear in the programming language. (I'm planning to update my answer, but need to do a little research first.)
Dave Clarke

Yeah, nesting is pretty much an implementation detail.
Jon Purdy
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.