# Row polymorphism crash course

With type inference, structural records types can make a statically typed language feel almost like a scripting language, without giving up type safety:

`// Works for any record with the fields width, height : Floatarea2d v = v.width * v.height`

Row polymorphism can be used to implement this, and…

# Type Inference by Example, Part 7

Continuing where we left off in part 6, let’s add in mutually recursive functions with generics.

Consider the following example:

`function even(x) { return x == 0 || odd(x - 1); }function odd(x) { return x != 0 && even(x - 1); }`

We say that these functions are…

# Type Inference by Example, Part 6

Continuing where we left off in part 5, let’s extend the language and reconstruct the missing type annotations in the syntax tree.

First, we’ll extend the lambda function syntax and application so that it supports multiple arguments.

`case class ELambda(  parameters : List[Parameter],  returnType : Option[Type],  body…`

# Type Inference by Example, Part 5

Continuing where we left off in part 4, let’s finish implementing a first version of the type inference — and see a small demo.

The first thing we’ll need is a language to infer types for. Let’s start with the lambda calculus:

`sealed abstract class Expressioncase class ELambda(x : String…`

# Type Inference by Example, Part 4

Continuing where we left off in part 3, let’s take a look at unification.

The first thing we’ll need is a representation for types. Let’s look at the types we’ve seen so far:

• `Int` — a plain type
• `Array<Int>` — a generic type
• `(Int, Int) => Int` — a function…

# Type Inference by Example, Part 3

Continuing where we left off in part 2, we’ll now consider calling a generic function:

`function map<A, B>(array : Array<A>, body : A => B) : Array<B>`

So this is the classic `map` function that applies a lambda function `body` to each element of the `array` and returns a new…

# Type Inference by Example, Part 2

Continuing where we left off in part 1, let’s look at a more advanced example that uses local variables and type constructors:

`function range(from, to) {    let numbers = [];    for(let i = from; i <= to; i++) {        numbers[i - from] = i;    }    return numbers;}`

As in…

# Type Inference by Example

If you’re implementing a programming language, you might be pondering how to do type inference. Instead of going through the theory, we’ll go through some examples to build an intuition for how it works, and finally see how to put that into code.

Consider the following function:

`function f(x) {…`

## Joakim Ahnfelt-Rønne

MSc Computer Science, working with functional programming in the industry — github.com/ahnfelt

Get the Medium app