From 5c73e7c58205719b3ccc18b9ca95f763c1aa1583 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 1 Jul 2024 14:16:11 -0400 Subject: [PATCH] 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. --- lang/types/util.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lang/types/util.go b/lang/types/util.go index fe342fae..32c33c9a 100644 --- a/lang/types/util.go +++ b/lang/types/util.go @@ -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) +}