Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 477629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:36:36+00:00 2026-05-13T00:36:36+00:00

In Andrew Koenig’s An anecdote about ML type inference , the author uses implementation

  • 0

In Andrew Koenig’s An anecdote about ML type inference, the author uses implementation of merge sort as a learning exercise for ML and is pleased to find an “incorrect” type inference.

Much to my surprise, the compiler reported a type of

'a list -> int list

In other words, this sort function accepts a list of any type at all and returns a list of integers.

That is impossible. The output must be a permutation of the input; how can it possibly have a different type? The reader will surely find my first impulse familiar: I wondered if I had uncovered a bug in the compiler!

After thinking about it some more, I realized that there was another way in which the function could ignore its argument: perhaps it sometimes didn’t return at all. Indeed, when I tried it, that is exactly what happened: sort(nil) did return nil, but sorting any non-empty list would go into an infinite recursion loop.

When translated to Haskell

split [] = ([], [])
split [x] = ([x], [])
split (x:y:xs) = (x:s1, y:s2)
  where (s1,s2) = split xs

merge xs [] = xs
merge [] ys = ys
merge xx@(x:xs) yy@(y:ys)
  | x < y     = x : merge xs yy
  | otherwise = y : merge xx ys

mergesort [] = []
mergesort xs = merge (mergesort p) (mergesort q)
  where (p,q) = split xs

GHC infers a similar type:

*Main> :t mergesort
mergesort :: (Ord a) => [t] -> [a]

How does the Damas–Hindley–Milner algorithm infer this type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:36:37+00:00Added an answer on May 13, 2026 at 12:36 am

    This is indeed a remarkable example; an infinite loop being detected, essentially, at compile time! There is nothing special about the Hindley–Milner inference in this example; it just proceeds as usual.

    Note that ghc gets the types of split and merge correctly:

    *Main> :t split
    split :: [a] -> ([a], [a])
    *Main> :t merge
    merge :: (Ord t) => [t] -> [t] -> [t]
    

    Now when it comes to mergesort, it is, in general, a function t1→t2 for some types t1 and t2. Then it sees the first line:

    mergesort [] = []
    

    and realizes that t1 and t2 must be list types, say t1=[t3] and t2=[t4]. So mergesort must be a function [t3]→[t4]. The next line

    mergesort xs = merge (mergesort p) (mergesort q) 
      where (p,q) = split xs
    

    tells it that:

    • xs must be an input to split, i.e., of type [a] for some a (which it already is, for a=t3).
    • So p and q are also of type [t3], since split is [a]→([a],[a])
    • mergesort p, therefore, (recall that mergesort is believed to be of type [t3]→[t4]) is of type [t4].
    • mergesort q is of type [t4] for exactly the same reason.
    • As merge has type (Ord t) => [t] -> [t] -> [t], and the inputs in the expression merge (mergesort p) (mergesort q) are both of type [t4], the type t4 must be in Ord.
    • Finally, the type of merge (mergesort p) (mergesort q) is the same as both its inputs, namely [t4]. This fits with the previously known type [t3]→[t4] for mergesort, so there are no more inferences to be done and the “unification” part of the Hindley–Milner algorithm is complete. mergesort is of type [t3]→[t4] with t4 in Ord.

    That’s why you get:

    *Main> :t mergesort 
    mergesort :: (Ord a) => [t] -> [a]
    

    (The description above in terms of logical inference is equivalent to what the algorithm does, but the specific sequence of steps the algorithm follows is simply that given on the Wikipedia page, for example.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was reading Andrew Kennedy's blog post series on units of measurement in F#
I was recommeded a book called: Accelerated C++ Practical Programming by Example by Andrew
INSERT INTO tblExcel (ename, position, phone, email) VALUES ('Burton, Andrew', 'Web Developer / Network
I exchanged emails with Marc-André Cournoyer of RefactorMyCode.com about supporting Delphi on his site.
As Andrew Hare suggested in his answer : Create a field to store all
Updated question given Andrew Hare's correct answer: Given the following C# classes: public class
Tony Andrews in another question gave an example of: IF p_c_courtesies_cd || p_c_language_cd ||
I came across this article written by Andrei Alexandrescu and Petru Marginean many years
We have a restriction that a class cannot act as a base-class for more
int n = 5; for(int i = 0;i!=n;i++)//condition != { //executing 5times } int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.