lang: types: Add a facility for printing consistent unification vars

When we look at unification variables from two different places, the
default printer will always start numbering them from ?1 and therefore
if we look at two unrelated systems, they might both print as ?1 when
they are in fact different pointers.

We don't collect them all by default since it's usually not necessary
except for debugging, but in those situations, we want a consistent
unification store which we can pass around to get sensible debug output.
This commit is contained in:
James Shubin
2024-07-01 14:16:11 -04:00
parent dc33d9aab7
commit 5c73e7c582

View File

@@ -197,3 +197,22 @@ func Iter(typ *Type, fn func(*Type) error) error {
return nil
}
// NewUnifiedState builds a new unified state store.
func NewUnifiedState() *UnifiedState {
return &UnifiedState{
table: make(map[*Elem]uint),
}
}
// UnifiedState stores a mapping of unification variable to unique id. This is
// most often used for printing consistent unification variables in your logs.
// It must be built with NewUnifiedState before it can be used or it will panic.
type UnifiedState struct {
table map[*Elem]uint
}
// String returns a representation of the input type using the specified state.
func (obj *UnifiedState) String(typ *Type) string {
return typ.string(obj.table)
}