<p>I am developing a verifier for the ActionScript 3 language and I have gone through implementing one before for my personal language.</p>
<p>I had a type inference limitation with the following construct:</p>
<pre><code>package {
    public class C1 {
        public function f.&lt;T&gt;(callbackFn: () =&gt; T): void {}
    }
}

const o = new C1;
o.f(() =&gt; 10); // Error: `o.f` must be argumented
o.f.&lt;Number&gt;(() =&gt; 10); // Successful
</code></pre>
<p>I was using sort of a bidirectional type checking, where solving an expression was given a context with an &quot;expected type&quot; or none, such as in:</p>
<pre><code>enum E {
    const M1;
}
const e: E = &quot;m1&quot;; // Successful
const e: E = &quot;m2&quot;; // Error: &quot;m2&quot; is not a known member
</code></pre>
<p>If a type annotation was missing, an expression would be resolved with no expected type and its resolved type determined the type of a binding:</p>
<pre><code>const x = 10;
</code></pre>
<p>Most of my expectations were meet, except for the above <code>o.f.&lt;T&gt;()</code> case, where I wanted to be able to omit the <code>.&lt;T&gt;</code> list. Is this possible with my previous model or do I have to do something different?</p>

<type-checker>
Generic method: type inference
<p>If you want to stick with bidirectional checking, you could try implementing the system presented in <a href="https://dl.acm.org/doi/abs/10.1145/2544174.2500582" rel="nofollow noreferrer">Complete and easy bidirectional typechecking for higher-rank polymorphism</a> (<a href="https://arxiv.org/pdf/1306.6032.pdf" rel="nofollow noreferrer">pdf</a>). To my knowledge, AS3 doesn’t support parametric polymorphism at all (the unusual <code>.&lt;T&gt;</code> syntax is only used with the <code>Vector</code> class and nothing else), so I assume this is a question about your “personal language”. Given your example, I suspect you don’t actually need the “higher-rank” part—the example only needs rank-1 polymorphism—but the linked paper’s presentation is exceptionally clear and thorough, and the system would not be substantially simpler if restricted to rank-1.</p>
<p>In general, parametric polymorphism can be challenging to handle using local type inference schemes, including bidirectional typechecking. The “complete and easy” paper is one of the best approaches I’ve seen, but it still requires annotations on bindings of polymorphic type. Crucially, however, it does not require annotations on <em>uses</em> of polymorphic definitions, so it is able to handle the example in your question.</p>
<p>While clever, the approach is not magic: it requires the addition of a mutable context of “solver variables” representing unknown types that are incrementally instantiated as the typechecker traverses the program. This mechanism effectively adds a small amount of nonlocal type inference to an otherwise local system. However, the system does not require full unification, which distinguishes it from global inference schemes such as <a href="https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system" rel="nofollow noreferrer">Hindley–Milner</a>.</p>

CC BY-SA 4.0
2023-11-17T17:15:27.000Z
2023-11-17T17:15:27.000Z
Alexis King
CC BY-SA 4.0
2023-11-17T18:59:32.710Z
Right, AS3 does not have generics. I am implementing them because the [folks at Adobe AIR have probably suggested this feature](https://github.com/airsdk/Adobe-Runtime-Support/discussions/2152). I have an [AS3 parser in Rust](https://github.com/hydroper/as3parser), so it does add the `.<T>` syntax for normal use. I will have to see what I will do about type inference though, as I'm not deep into algebra...
Hydroper
CC BY-SA 4.0
2023-11-18T16:22:41.403Z
@Hydroper There is not really any algebra in the paper I linked! If you find the notation intimidating, consider reading [this answer](https://langdev.stackexchange.com/a/2693), and I would encourage you to ask questions about any other confusion you might have. I promise it’s not as complicated as it looks.
Alexis King
# Generic method: type inference
Posted by: username_0 on 2023-11-17
I am developing a verifier for the ActionScript 3 language and I have gone through implementing one before for my personal language.
I had a type inference limitation with the following construct:
package {
    public class C1 {
        public function f.(callbackFn: () => T): void {}
    }
}

const o = new C1;
o.f(() => 10); // Error: `o.f` must be argumented
o.f.(() => 10); // Successful

I was using sort of a bidirectional type checking, where solving an expression was given a context with an "expected type" or none, such as in:
enum E {
    const M1;
}
const e: E = "m1"; // Successful
const e: E = "m2"; // Error: "m2" is not a known member

If a type annotation was missing, an expression would be resolved with no expected type and its resolved type determined the type of a binding:
const x = 10;

Most of my expectations were meet, except for the above o.f.() case, where I wanted to be able to omit the . list. Is this possible with my previous model or do I have to do something different?
**Tags:** type-checker
---
## 1 answers
### Answer by username_1 on 2023-11-17. Score: 4
If you want to stick with bidirectional checking, you could try implementing the system presented in [Complete and easy bidirectional typechecking for higher-rank polymorphism](https://dl.acm.org/doi/abs/10.1145/2544174.2500582) ([pdf](https://arxiv.org/pdf/1306.6032.pdf)). To my knowledge, AS3 doesn’t support parametric polymorphism at all (the unusual `.` syntax is only used with the `Vector` class and nothing else), so I assume this is a question about your “personal language”. Given your example, I suspect you don’t actually need the “higher-rank” part—the example only needs rank-1 polymorphism—but the linked paper’s presentation is exceptionally clear and thorough, and the system would not be substantially simpler if restricted to rank-1.

In general, parametric polymorphism can be challenging to handle using local type inference schemes, including bidirectional typechecking. The “complete and easy” paper is one of the best approaches I’ve seen, but it still requires annotations on bindings of polymorphic type. Crucially, however, it does not require annotations on *uses* of polymorphic definitions, so it is able to handle the example in your question.

While clever, the approach is not magic: it requires the addition of a mutable context of “solver variables” representing unknown types that are incrementally instantiated as the typechecker traverses the program. This mechanism effectively adds a small amount of nonlocal type inference to an otherwise local system. However, the system does not require full unification, which distinguishes it from global inference schemes such as [Hindley–Milner](https://en.wikipedia.org/wiki/Hindley%E2%80%93Milner_type_system).
> **Comments:**
> **username_0** (2023-11-17): Right, AS3 does not have generics. I am implementing them because the [folks at Adobe AIR have probably suggested this feature](https://github.com/airsdk/Adobe-Runtime-Support/discussions/2152). I have an [AS3 parser in Rust](https://github.com/hydroper/as3parser), so it does add the `.<T>` syntax for normal use. I will have to see what I will do about type inference though, as I'm not deep into algebra...  
> **username_1** (2023-11-18): @Hydroper There is not really any algebra in the paper I linked! If you find the notation intimidating, consider reading [this answer](https://langdev.stackexchange.com/a/2693), and I would encourage you to ask questions about any other confusion you might have. I promise it’s not as complicated as it looks.  
---
