Skip to content

Commit

Permalink
Rework Resolution Algorithm
Browse files Browse the repository at this point in the history
This updates the resolution algorithm to support arbitrary declarations
which can introduce arbitrary bindings, rather than just e.g. column
bindings.  This allows for a more flexible resolution algorithm that can
support functions which depend on each other, etc.
  • Loading branch information
DavePearce committed Dec 6, 2024
1 parent 83af113 commit 5b22e80
Show file tree
Hide file tree
Showing 9 changed files with 875 additions and 473 deletions.
585 changes: 516 additions & 69 deletions pkg/corset/ast.go

Large diffs are not rendered by default.

48 changes: 35 additions & 13 deletions pkg/corset/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type BindingId struct {
// Binding represents an association between a name, as found in a source file,
// and concrete item (e.g. a column, function, etc).
type Binding interface {
// Returns the context associated with this binding.
IsBinding()
// Determine whether this binding is finalised or not.
IsFinalised() bool
}

// ColumnBinding represents something bound to a given column.
Expand All @@ -33,19 +33,23 @@ type ColumnBinding struct {
module string
// Determines whether this is a computed column, or not.
computed bool
// Determines whether this column must be proven (or not).
mustProve bool
// Column's length multiplier
multiplier uint
// Column's datatype
datatype sc.Type
dataType sc.Type
}

// NewColumnBinding constructs a new column binding in a given module.
func NewColumnBinding(module string, computed bool, multiplier uint, datatype sc.Type) *ColumnBinding {
return &ColumnBinding{math.MaxUint, module, computed, multiplier, datatype}
func NewColumnBinding(module string, computed bool, mustProve bool, multiplier uint, datatype sc.Type) *ColumnBinding {
return &ColumnBinding{math.MaxUint, module, computed, mustProve, multiplier, datatype}
}

// IsBinding ensures this is an instance of Binding.
func (p *ColumnBinding) IsBinding() {}
// IsFinalised checks whether this binding has been finalised yet or not.
func (p *ColumnBinding) IsFinalised() bool {
return p.multiplier != 0
}

// Context returns the of this column. That is, the module in which this colunm
// was declared and also the length multiplier of that module it requires.
Expand Down Expand Up @@ -74,20 +78,38 @@ type ParameterBinding struct {
index uint
}

// IsBinding ensures this is an instance of Binding.
func (p *ParameterBinding) IsBinding() {}
// IsFinalised checks whether this binding has been finalised yet or not.
func (p *ParameterBinding) IsFinalised() bool {
panic("")
}

// FunctionBinding represents the binding of a function application to its
// physical definition.
type FunctionBinding struct {
// arity determines the number of arguments this function takes.
arity uint
// Flag whether or not is pure function
pure bool
// Types of parameters
paramTypes []sc.Type
// Type of return
returnType sc.Type
// body of the function in question.
body Expr
}

// IsBinding ensures this is an instance of Binding.
func (p *FunctionBinding) IsBinding() {}
// NewFunctionBinding constructs a new function binding.
func NewFunctionBinding(pure bool, paramTypes []sc.Type, returnType sc.Type, body Expr) FunctionBinding {
return FunctionBinding{pure, paramTypes, returnType, body}
}

// IsFinalised checks whether this binding has been finalised yet or not.
func (p *FunctionBinding) IsFinalised() bool {
return p.returnType != nil
}

// Arity returns the number of parameters that this function accepts.
func (p *FunctionBinding) Arity() uint {
return uint(len(p.paramTypes))
}

// Apply a given set of arguments to this function binding.
func (p *FunctionBinding) Apply(args []Expr) Expr {
Expand Down
5 changes: 0 additions & 5 deletions pkg/corset/compiler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package corset

import (
"fmt"

"github.com/consensys/go-corset/pkg/hir"
"github.com/consensys/go-corset/pkg/sexp"
)
Expand Down Expand Up @@ -72,9 +70,6 @@ func (p *Compiler) Compile() (*hir.Schema, []SyntaxError) {
}
// Convert global scope into an environment by allocating all columns.
environment := scope.ToEnvironment()
// Check constraint contexts (e.g. for constraints, lookups, etc)
// Type check constraints
fmt.Println("Translating Circuit...")
// Finally, translate everything and add it to the schema.
return TranslateCircuit(environment, p.srcmap, &p.circuit)
}
2 changes: 1 addition & 1 deletion pkg/corset/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (p GlobalEnvironment) Module(name string) *ModuleScope {
func (p GlobalEnvironment) Column(module string, name string) *ColumnBinding {
// Lookup the given binding, expecting that it is a column binding. If not,
// then this will fail.
return p.Module(module).Bind(nil, name, false).(*ColumnBinding)
return p.Module(module).Column(name)
}

// ContextFrom constructs a trace context for a given module and length
Expand Down
Loading

0 comments on commit 5b22e80

Please sign in to comment.