45

I'm working on a plugin for GHC, so I'm reading the documentation for some of its implementation.

The verb "to zonk" is all over the place, but I can't track down an explanation of what it means to zonk something or (in broad terms) when one might want to. I can find plenty of notes about complicated circumstances under which it is necessary to zonk or not to zonk something, but without a clue as to what the big picture is I am having a lot of trouble following.

Doug McClean
  • 14,265
  • 6
  • 48
  • 70
  • 10
    ["Remember to zonk the skolems of an implication"](https://git.haskell.org/ghc.git/commitdiff/c32bb5d0c09a7e55197191f152c6875b398717cf) is so euphonic! – duplode Aug 08 '15 at 03:24
  • 7
    "Zonking walks over a type, returning a new type in which unification variables are replaced by the types they unified to." —[an email written Simon Peyton-Jones](https://mail.haskell.org/pipermail/glasgow-haskell-users/2013-August/024209.html). I don't remember enough about it to leave a more helpful answer than that, though. – Antal Spector-Zabusky Aug 08 '15 at 04:38

1 Answers1

36

An un-zonked type can have type variables which are mutable references filled in during unification (and this mutability is heavily used by the type checker to increase performance). Zonking traverses a type and replaces all mutable references with the type that they dereference to; thus, the resulting structure is immutable and requires no dereferencing to interpret.

Note that these type variables are meta-variables, i.e. they don't correspond to the type variables introduced by polymorphism; rather, they are unification variables to be replaced by real types. The choice of replacement is decided by the type checking/type inference process, and then the actual replacement is done during zonking.

This notion of zonking extends naturally to other intermediate representations of the typechecker that contain types.

Cactus
  • 27,075
  • 9
  • 69
  • 149