Questions tagged [hindley-milner]

In type theory, Hindley–Milner (HM) is a classical type inference method with parametric polymorphism for the lambda calculus.

In , Hindley–Milner (HM) is a classical type method with for the lambda calculus.

One property that makes the Hindley-Milner type theory important is its completeness and its ability to deduce the most general type of a given source without the need of any type annotations or other hints. HM is a fast algorithm that can compute a type almost in linear time with respect to the size of the source, which makes it of practical use for the types of large programs. HM is used for several functional programming languages. It was first implemented as part of the type system of the programming language . Since then, HM has been extended in various ways, most notably by constrained types as used in , which it has a strong association with.

The original paper

A Theory of Type Polymorphism in Programming

Languages implementing Hindley-Milner

See also

84 questions
7
votes
2 answers

How does Rust solve mutability for Hindley-Milner?

I've read that Rust has very good type inference using Hindley-Milner. Rust also has mutable variables and AFAIK there must be some constraints when a HM algorithm works with mutability because it could over-generalize. The following code: let mut…
Peter Lenkefi
  • 1,306
  • 11
  • 29
7
votes
0 answers

Implementing an affine type system

In an affine type system, resources can be used at most once. Starting from a Hindley-Milner type system, it seems that a simple way to enforce affinity is to simply remove a variable from the current typing context whenever the typing rule for…
max
  • 1,048
  • 10
  • 20
7
votes
1 answer

Encode rank-2 polymorphism equivalent in SML

runST is a Haskell function that statically constrains the usable lifetime of a resource through types. To do this it uses rank-2 polymorphism. Standard ML's simpler type system only offers rank-1 polymorphism. Can Standard ML still use types to…
Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
7
votes
1 answer

Global type inference in the Scheme compiler Stalin

I am looking into the Scheme compiler Stalin. It is big and complex. Also, if I understood correctly, the author was planning to write a series of papers detailing aspects of the implementation, but never got around to doing that. The aspect of…
yotsov
  • 775
  • 5
  • 11
6
votes
1 answer

Is there an effective way to generate a function given a generic (esp. with monads) type signature in Haskell?

I have already seen a variety of questions of the form "Given type signature XXX, find implementation in Haskell." Therefore it is natural to ask whether this can be generalized or algorithmized. A similar question is here. However, it is clear that…
Trebor
  • 307
  • 2
  • 10
6
votes
7 answers

Which programming languages support functions that take themselves as arguments?

I'm doing an academic exercise (for personal growth). I want to find programming languages that allow you to define functions that are capable of accepting themselves (i.e., pointers to themselves) as arguments. For example, in JavaScript: function…
6
votes
1 answer

Hindley Milner type inference for mutually recursive functions

I'm making a strongly typed toy functional programming language. It uses the Hindley Milner algorithm as type inference algorithm. Implementing the algorithm, I have a question on how to infer types of the mutually recursive functions. let rec f n =…
6
votes
1 answer

why does Haskell require numbers to be disambiguated for printf but not for show?

Why is printf "%d\n" 3 ambiguous but not show 3? Could the printf module be rewritten to provide automatic disambiguation? Presumably something like show must be done at the lower levels of printf ... or is there some crucial difference between…
Rick Majpruz
  • 641
  • 3
  • 16
6
votes
2 answers

Algorithm W using recursion schemes

I wanted to be able to formulate the hindley-milner type inference algorithm using fix-point data types and recursion schemes. Disregarding all detail apart from the actual recursion parts: w env term = case term of Lam n e -> lam (w (modify1…
5
votes
1 answer

Are function parameters not polymorphic in Algorithm W (or Haskell)?

I am implementing Algorithm W for a toy language. I came across a case that I imagined would type check, but doesn't. I tried the same in Haskell, and to my surprise it didn't work there either. > (\id -> id id 'a') (\x -> x) Couldn't match type…
waxwing
  • 18,547
  • 8
  • 66
  • 82
5
votes
2 answers

What's wrong with my Haskell type synonym?

I have two functions for controlling loops, continue and break: type Control a = (a -> a) -> a -> a continue :: Control a continue = id break :: Control a break = const id Then, I wanted to simplify the Control type synonym. Hence, I wrote: type…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
4
votes
1 answer

How does Haskell perform Beta conversion to derive a type?

I'm learning Haskell by taking fp-course exercise. There is a question block my way. I don't know how Haskell infer lift2 (<$>) (,)'s type, and turn out Functor k => (a1 -> k a2) -> a1 -> k (a1, a2). I have tried out lift2 (<$>)'s type, and verified…
4
votes
1 answer

Generalized HM vs. Higher-Order Unification

AFAIK, unification used in the Hindley-Milner type system can be generalized to unify higher-kinded types by allowing type vars in constructor position and by relaxing the arity constraint in such cases: f a ~ T a1 b1 f ~ T a1 -- generatifity +…
4
votes
1 answer

Hindley-Milner type inference for overloaded functions

How does the Hindley-Milner algorithm work when there are overloaded functions? In a simple form (without overloads) it looks clean: y = arr[x1] //it's Generic. x1: int, arr: T[], y:T z = y+1// z:int, y:int => T:int, arr: int[]. Oh. All types are…
4
votes
1 answer

Would Hindley Milner type inference be useful to PyPy for RPython?

Does PyPy do static type checking at compile time to catch type errors at compile time? And if not would something like HM type inference be useful to catch those errors at compile time?
user782220
  • 10,677
  • 21
  • 72
  • 135