lang: Plumb through a context into unification

If we have a long type unification, we might want to cancel it early.
This also helps us visualize where we want context to be seen.
This commit is contained in:
James Shubin
2024-03-16 00:30:47 -04:00
parent a8b945e36e
commit 10319dd641
7 changed files with 32 additions and 15 deletions

View File

@@ -32,6 +32,7 @@
package unification
import (
"context"
"fmt"
"sort"
"strings"
@@ -45,7 +46,8 @@ type Unifier struct {
AST interfaces.Stmt
// Solver is the solver algorithm implementation to use.
Solver func([]interfaces.Invariant, []interfaces.Expr) (*InvariantSolution, error)
// XXX: Solver should be a solver interface, not a function signature.
Solver func(context.Context, []interfaces.Invariant, []interfaces.Expr) (*InvariantSolution, error)
Debug bool
Logf func(format string, v ...interface{})
@@ -63,7 +65,7 @@ type Unifier struct {
// type. This function and logic was invented after the author could not find
// any proper literature or examples describing a well-known implementation of
// this process. Improvements and polite recommendations are welcome.
func (obj *Unifier) Unify() error {
func (obj *Unifier) Unify(ctx context.Context) error {
if obj.AST == nil {
return fmt.Errorf("the AST is nil")
}
@@ -96,7 +98,7 @@ func (obj *Unifier) Unify() error {
exprMap := ExprListToExprMap(exprs) // makes searching faster
exprList := ExprMapToExprList(exprMap) // makes it unique (no duplicates)
solved, err := obj.Solver(invariants, exprList)
solved, err := obj.Solver(ctx, invariants, exprList)
if err != nil {
return err
}