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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user