Reviving "Typing Haskell in Haskell"

Hi all,

I’ve been studying THIH by Mark P Jones recently (Typing Haskell in Haskell); it’s a good, executable introduction* to the mechanics of type inference in our favorite language.

There is a copy of the original repository on Hackage (uploaded by Gwern in 2008 , thih: Typing Haskell In Haskell ), but until today it did not have a backup maintainer.
I think it would be valuable to save this project from bitrot and perhaps feature it in a more central place in our ecosystem. It certainly deserves better Haddock documentation and more readable tests.

I did the minimal amout of work to make it build under a recent GHC (9.0.2) : GitHub - ocramz/thih: typing haskell in haskell and my plan is to add docstrings.

I’m currently waiting to hear back from Prof. Jones, fingers crossed this project will have his “blessing”.


* Not the only one though : “Algorithm W step by step” (Algorithm-W-Step-By-Step/AlgorithmW.pdf at master · wh5a/Algorithm-W-Step-By-Step · GitHub) is equally awesome albeit slightly less general.

11 Likes

I think it would be great to have a simple implementation of the type system to do research on. I wanted to see what it would take to implement MLF and also found THIH, but I bounced off.

There’s also this: Typing Haskell with an Attribute Grammar.

6 Likes

I made a markdown version of this in 2014 Typing Haskell in Haskell · GitHub

I also copied the thih source and iterated on it to produce Duet GitHub - chrisdone/duet: A tiny language, a subset of Haskell aimed at aiding teachers teach Haskell which is a Haskell subset. The thing missing from THIH which I had to figure out was converting type class contexts into actual dictionaries. That’s a whole extra stage. Plus kind checking for data declarations and type checking of instances and stuff like that.

(I’ve since done a complete ground up implementation of a compiler (https://inflex.io/) with type classes based on this experience but the architecture is quite different (Inflex - Google Slides), but it’s not open source yet.)

7 Likes

I think followers of this topic will be interested in A Static Semantics for Haskell by Karl-Filip Faxén. There was an effort at ZuriHac to formalize the paper, and the repo is here: GitHub - BinderDavid/haskell-spec: Formal specification of the Haskell Language Report

It also seems that Inflex, which Chris Done mentioned above has been open sourced and abandoned, and it is available at GitHub - chrisdone-archive/inflex: Pure, statically typed, content-addressable, programming language for spreadsheet use

1 Like

Fortunately, we are doing exactly that! We have a paper that formalizes an extremely big part of GHC Haskell’s typesystem, and we are also developing a typechecker that implements this formalism – Kind of like Practical Type Inference for Arbitrary Rank Types but much bigger

The math is nearly finished, and most of my time is currently dedicated to the implementation.

Here are the extensions that the formalism currently specifies:

  • ImpredicativeTypes
  • RequiredTypeArguments
  • ShallowSubsumption
  • DeepSubsumption
  • Orpatterns
  • PatternSignatures
  • ViewPatterns
  • PatternSynonyms
  • PatternSignatureBinds
  • GADTs
  • Existentials
  • RankNTypes
  • PolyKinds
  • LambdaCase
  • LambdaCases
  • TypeApplication
  • ScopedTypeVariables
  • Modern Scoped Type variables (proposal 448)

Things that will be added soon:

  • MonoLocalBinds
  • Guards
  • Implicit Quantification
  • Monomorphism restriction
  • Levity Polymorphism

One of the main goals of the implementation is to be in a one-to-one – and when possible, even line-to-line – correspondence with the rules.

The paper doesn’t give an specification for kindchecking datatypes, classes, synonyms, pattern synonyms, and type family declarations. Instead, it assumes that these are already in the environment. Also, if I’m not mistaken, the solver is planned to be able to solve everything GHC solves other than FunctionalDependencies and ImplicitParamters. Additionally we might not support representational equality.

Here is the implementation (WIP): typechecker · master · Artin Ghasivand Ghasivand / haskell · GitLab

And here is the early draft of the paper itself: https://gitlab.haskell.org/Ei30metry/haskell/-/jobs/artifacts/master/raw/haskell.pdf?job=build-pdf

4 Likes