unify

package
v1.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package unify implements unification for type inference and elaboration.

The unification algorithm solves constraints of the form `t₁ = t₂` by finding substitutions for metavariables that make the terms equal.

This package implements Miller pattern unification, which handles metavariables applied to distinct bound variables (patterns like `?X x y` where x and y are distinct).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Zonk

func Zonk(solutions map[int]ast.Term, t ast.Term) ast.Term

Zonk applies all solutions to a term, fully substituting metavariables.

func ZonkFull

func ZonkFull(solutions map[int]ast.Term, t ast.Term) (ast.Term, error)

ZonkFull applies all solutions and errors if any metavariables remain.

Types

type Constraint

type Constraint struct {
	LHS ast.Term // Left-hand side
	RHS ast.Term // Right-hand side
}

Constraint represents an equality constraint between two terms.

type Unifier

type Unifier struct {
	// contains filtered or unexported fields
}

Unifier performs unification of type constraints.

func NewUnifier

func NewUnifier() *Unifier

NewUnifier creates a new unifier.

func (*Unifier) AddConstraint

func (u *Unifier) AddConstraint(lhs, rhs ast.Term)

AddConstraint adds a constraint to the worklist.

func (*Unifier) GetSolution

func (u *Unifier) GetSolution(id int) (ast.Term, bool)

GetSolution returns the solution for a metavariable if it exists.

func (*Unifier) Solve

func (u *Unifier) Solve() UnifyResult

Solve processes all constraints and returns the result.

type UnifyError

type UnifyError struct {
	Message     string
	Constraints []Constraint // Remaining constraints when error occurred
}

UnifyError represents an error during unification.

func (*UnifyError) Error

func (e *UnifyError) Error() string

type UnifyResult

type UnifyResult struct {
	// Solved maps metavariable IDs to their solutions.
	Solved map[int]ast.Term

	// Unsolved contains constraints that couldn't be solved.
	Unsolved []Constraint

	// Errors contains any errors encountered.
	Errors []UnifyError
}

UnifyResult represents the result of unification.

func Unify

func Unify(lhs, rhs ast.Term) UnifyResult

Unify is a convenience function that unifies two terms.

func UnifyAll

func UnifyAll(constraints []Constraint) UnifyResult

UnifyAll unifies multiple pairs of terms.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL