Skip to content

Break out TcTyVar into its own type

Right now, the type Var has three constructors: Id, TyVar, and TcTyVar. The last of these should never occur in a Core program, but exists only in the type-checker. This ticket is a refactoring that moves TcTyVar outside of Var and into its own type.

Here is a rough sketch of the proposed future (all modulo field renaming where necessary to avoid duplicates):

data Var
  = Id {-# UNPACK #-} !Id
  | TyVar {-# UNPACK #-} !TyVar

data Id = MkId ...       -- stuff that's in today's Id constructor
data TyVar = MkTyVar ... -- stuff that's in today's TyVar constructor
  
data MetaTyVar = MkMeta ...   -- stuff that's in today's MetaTv constructor of TcTyVarDetails, plus a Name and Type
data SkolemTyVar = MkSkol ... -- stuff that's in today's SkolemTv constructor of TcTyVarDetails, plus a Name and Type
data RuntimeUnkTyVar = MkRtUnk Name Type  -- NB: today's RuntimeUnk constructor of TcTyVarDetails carries no data

data TcTyVar
  = SkolemTv {-# UNPACK #-} !SkolemTyVar
  | MetaTv {-# UNPACK #-} !MetaTyVar
  | RuntimeUnk {-# UNPACK #-} !RuntimeUnkTyVar

data TcCoVar = TcCoVar ...  -- evolved from today's CoercionHole

data Type = ... | TcTyVarTy TcTyVar      -- new constructor in Type
data Coercion = ... | TcCoVarCo TcCoVar  -- new constructor in Coercion, replacing HoleCo

data TcOrCoreVar
  = TcTyVar !TcTyVar  -- can't unpack a sum
  | TcCoVar {-# UNPACK #-} !TcCoVar
  | Var !Var   -- can't unpack a sum

The observation that leads to this is that we rarely want to mix TcTyVars with other TyVars. So I think TcOrCoreVar will have few uses. In particular, when we collect free variables, we often know, in advance, whether we want Tc vars or Core vars. This will require new functions that walk over types/coercions collecting variables, but I think that's acceptable. Also, note the addition of TyVar and Id types (unpacked into Var), allowing us to keep these separate and statically checked. Naturally, spotting TcTyVarTy or TcCoVarCo in Lint will be an error.

There is no pressing need for this refactor, but I think it will smoke out some bugs and be cleaner going forward.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information