Skip to content

Commit

Permalink
Fix resolution from root module
Browse files Browse the repository at this point in the history
Resolving symbols in modules from the root context was not working
correctly.  The problem was that symbols were initialised in a
module-by-module basis.  Therefore, symbols in non-root modules were
being initialised after resolution of the root module.
  • Loading branch information
DavePearce committed Dec 21, 2024
1 parent f039ee5 commit 0b49f75
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 14 deletions.
32 changes: 23 additions & 9 deletions pkg/corset/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ func ResolveCircuit(srcmap *sexp.SourceMaps[Node], circuit *Circuit) (*GlobalSco
}
// Construct resolver
r := resolver{srcmap}
// Allocate declared input columns
errs := r.resolveDeclarations(scope, circuit)
// Initialise all columns
errs1 := r.initialiseDeclarations(scope, circuit)
// Finalise all columns / declarations
errs2 := r.resolveDeclarations(scope, circuit)
//
if len(errs) > 0 {
return nil, errs
if len(errs1)+len(errs2) > 0 {
return nil, append(errs1, errs2...)
}
// Done
return scope, errs
return scope, nil
}

// Resolver packages up information necessary for resolving a circuit and
Expand All @@ -42,6 +44,22 @@ type resolver struct {
srcmap *sexp.SourceMaps[Node]
}

// Initialise all columns from their declaring constructs.
func (r *resolver) initialiseDeclarations(scope *GlobalScope, circuit *Circuit) []SyntaxError {
// Input columns must be allocated before assignemts, since the hir.Schema
// separates these out.
errs := r.initialiseDeclarationsInModule(scope.Module(""), circuit.Declarations)
//
for _, m := range circuit.Modules {
// Process all declarations in the module
merrs := r.initialiseDeclarationsInModule(scope.Module(m.Name), m.Declarations)
// Package up all errors
errs = append(errs, merrs...)
}
//
return errs
}

// Process all assignment column declarations. These are more complex than for
// input columns, since there can be dependencies between them. Thus, we cannot
// simply resolve them in one linear scan.
Expand All @@ -65,10 +83,6 @@ func (r *resolver) resolveDeclarations(scope *GlobalScope, circuit *Circuit) []S
// to process all columns before we can sure that they are all declared
// correctly.
func (r *resolver) resolveDeclarationsInModule(scope *ModuleScope, decls []Declaration) []SyntaxError {
// Columns & Assignments
if errors := r.initialiseDeclarationsInModule(scope, decls); len(errors) > 0 {
return errors
}
// Aliases
if errors := r.initialiseAliasesInModule(scope, decls); len(errors) > 0 {
return errors
Expand Down
4 changes: 4 additions & 0 deletions pkg/test/valid_corset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ func Test_Lookup_08(t *testing.T) {
Check(t, false, "lookup_08")
}

func Test_Lookup_09(t *testing.T) {
Check(t, false, "lookup_09")
}

// ===================================================================
// Interleaving
// ===================================================================
Expand Down
5 changes: 4 additions & 1 deletion testdata/constant_invalid_10.lisp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
;;error:9:13-14:symbol X already declared
;;error:12:13-14:symbol X already declared
;;error:13:22-37:expected loobean constraint (found 𝔽)
;;error:14:22-45:expected loobean constraint (found 𝔽)
;;error:15:22-46:expected loobean constraint (found 𝔽)
(defconst
X 1
ONE X
Expand Down
175 changes: 175 additions & 0 deletions testdata/lookup_09.accepts
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
;; { "X": [], "m1.A": [] }
;; { "X": [0], "m1.A": [0] }
;; Check initial padding row!
{ "X": [0], "m1.A": [1] }
;;
{ "X": [0,0], "m1.A": [0,0] }
{ "X": [0,0], "m1.A": [0,1] }
{ "X": [0,0], "m1.A": [1,0] }
{ "X": [1,0], "m1.A": [0,1] }
{ "X": [1,0], "m1.A": [1,0] }
{ "X": [1,0], "m1.A": [1,1] }
{ "X": [0,1], "m1.A": [0,1] }
{ "X": [0,1], "m1.A": [1,0] }
{ "X": [0,1], "m1.A": [1,1] }
{ "X": [1,1], "m1.A": [0,1] }
{ "X": [1,1], "m1.A": [1,0] }
{ "X": [1,1], "m1.A": [1,1] }
{ "X": [1,1], "m1.A": [1,2] }
{ "X": [2,2], "m1.A": [2,2] }
{ "X": [2,2], "m1.A": [2,1] }
{ "X": [2,2], "m1.A": [1,2] }
{ "X": [1,2], "m1.A": [2,1] }
{ "X": [1,2], "m1.A": [1,2] }
{ "X": [2,1], "m1.A": [2,1] }
{ "X": [2,1], "m1.A": [1,2] }
{ "X": [1,1], "m1.A": [2,1] }
{ "X": [1,1], "m1.A": [1,2] }
{ "X": [1,1], "m1.A": [1,1] }
;;
{ "X": [0,0,0], "m1.A": [2,0,1] }
{ "X": [1,0,0], "m1.A": [2,0,1] }
{ "X": [0,1,0], "m1.A": [2,0,1] }
{ "X": [0,0,1], "m1.A": [2,0,1] }
{ "X": [1,0,1], "m1.A": [2,0,1] }
{ "X": [0,1,1], "m1.A": [2,0,1] }
{ "X": [1,1,0], "m1.A": [2,0,1] }
{ "X": [1,1,1], "m1.A": [2,0,1] }
{ "X": [0,0,0], "m1.A": [0,2,1] }
{ "X": [1,0,0], "m1.A": [0,2,1] }
{ "X": [0,1,0], "m1.A": [0,2,1] }
{ "X": [0,0,1], "m1.A": [0,2,1] }
{ "X": [1,0,1], "m1.A": [0,2,1] }
{ "X": [0,1,1], "m1.A": [0,2,1] }
{ "X": [1,1,0], "m1.A": [0,2,1] }
{ "X": [1,1,1], "m1.A": [0,2,1] }
{ "X": [0,0,0], "m1.A": [0,1,2] }
{ "X": [1,0,0], "m1.A": [0,1,2] }
{ "X": [0,1,0], "m1.A": [0,1,2] }
{ "X": [0,0,1], "m1.A": [0,1,2] }
{ "X": [1,0,1], "m1.A": [0,1,2] }
{ "X": [0,1,1], "m1.A": [0,1,2] }
{ "X": [1,1,0], "m1.A": [0,1,2] }
{ "X": [1,1,1], "m1.A": [0,1,2] }
{ "X": [0,0,0], "m1.A": [2,1,0] }
{ "X": [1,0,0], "m1.A": [2,1,0] }
{ "X": [0,1,0], "m1.A": [2,1,0] }
{ "X": [0,0,1], "m1.A": [2,1,0] }
{ "X": [1,0,1], "m1.A": [2,1,0] }
{ "X": [0,1,1], "m1.A": [2,1,0] }
{ "X": [1,1,0], "m1.A": [2,1,0] }
{ "X": [1,1,1], "m1.A": [2,1,0] }
{ "X": [0,0,0], "m1.A": [1,2,0] }
{ "X": [1,0,0], "m1.A": [1,2,0] }
{ "X": [0,1,0], "m1.A": [1,2,0] }
{ "X": [0,0,1], "m1.A": [1,2,0] }
{ "X": [1,0,1], "m1.A": [1,2,0] }
{ "X": [0,1,1], "m1.A": [1,2,0] }
{ "X": [1,1,0], "m1.A": [1,2,0] }
{ "X": [1,1,1], "m1.A": [1,2,0] }
{ "X": [0,0,0], "m1.A": [1,0,2] }
{ "X": [1,0,0], "m1.A": [1,0,2] }
{ "X": [0,1,0], "m1.A": [1,0,2] }
{ "X": [0,0,1], "m1.A": [1,0,2] }
{ "X": [1,0,1], "m1.A": [1,0,2] }
{ "X": [0,1,1], "m1.A": [1,0,2] }
{ "X": [1,1,0], "m1.A": [1,0,2] }
{ "X": [1,1,1], "m1.A": [1,0,2] }
{ "X": [0,0,0], "m1.A": [2,1,1] }
{ "X": [1,0,0], "m1.A": [2,1,1] }
{ "X": [0,1,0], "m1.A": [2,1,1] }
{ "X": [0,0,1], "m1.A": [2,1,1] }
{ "X": [1,0,1], "m1.A": [2,1,1] }
{ "X": [0,1,1], "m1.A": [2,1,1] }
{ "X": [1,1,0], "m1.A": [2,1,1] }
{ "X": [1,1,1], "m1.A": [2,1,1] }
{ "X": [0,0,0], "m1.A": [1,2,1] }
{ "X": [1,0,0], "m1.A": [1,2,1] }
{ "X": [0,1,0], "m1.A": [1,2,1] }
{ "X": [0,0,1], "m1.A": [1,2,1] }
{ "X": [1,0,1], "m1.A": [1,2,1] }
{ "X": [0,1,1], "m1.A": [1,2,1] }
{ "X": [1,1,0], "m1.A": [1,2,1] }
{ "X": [1,1,1], "m1.A": [1,2,1] }
{ "X": [0,0,0], "m1.A": [1,1,2] }
{ "X": [1,0,0], "m1.A": [1,1,2] }
{ "X": [0,1,0], "m1.A": [1,1,2] }
{ "X": [0,0,1], "m1.A": [1,1,2] }
{ "X": [1,0,1], "m1.A": [1,1,2] }
{ "X": [0,1,1], "m1.A": [1,1,2] }
{ "X": [1,1,0], "m1.A": [1,1,2] }
{ "X": [1,1,1], "m1.A": [1,1,2] }
;;
{ "X": [3,3,3], "m1.A": [2,3,1] }
{ "X": [1,3,3], "m1.A": [2,3,1] }
{ "X": [3,1,3], "m1.A": [2,3,1] }
{ "X": [3,3,1], "m1.A": [2,3,1] }
{ "X": [1,3,1], "m1.A": [2,3,1] }
{ "X": [3,1,1], "m1.A": [2,3,1] }
{ "X": [1,1,3], "m1.A": [2,3,1] }
{ "X": [1,1,1], "m1.A": [2,3,1] }
{ "X": [3,3,3], "m1.A": [3,2,1] }
{ "X": [1,3,3], "m1.A": [3,2,1] }
{ "X": [3,1,3], "m1.A": [3,2,1] }
{ "X": [3,3,1], "m1.A": [3,2,1] }
{ "X": [1,3,1], "m1.A": [3,2,1] }
{ "X": [3,1,1], "m1.A": [3,2,1] }
{ "X": [1,1,3], "m1.A": [3,2,1] }
{ "X": [1,1,1], "m1.A": [3,2,1] }
{ "X": [3,3,3], "m1.A": [3,1,2] }
{ "X": [1,3,3], "m1.A": [3,1,2] }
{ "X": [3,1,3], "m1.A": [3,1,2] }
{ "X": [3,3,1], "m1.A": [3,1,2] }
{ "X": [1,3,1], "m1.A": [3,1,2] }
{ "X": [3,1,1], "m1.A": [3,1,2] }
{ "X": [1,1,3], "m1.A": [3,1,2] }
{ "X": [1,1,1], "m1.A": [3,1,2] }
{ "X": [3,3,3], "m1.A": [2,1,3] }
{ "X": [1,3,3], "m1.A": [2,1,3] }
{ "X": [3,1,3], "m1.A": [2,1,3] }
{ "X": [3,3,1], "m1.A": [2,1,3] }
{ "X": [1,3,1], "m1.A": [2,1,3] }
{ "X": [3,1,1], "m1.A": [2,1,3] }
{ "X": [1,1,3], "m1.A": [2,1,3] }
{ "X": [1,1,1], "m1.A": [2,1,3] }
{ "X": [3,3,3], "m1.A": [1,2,3] }
{ "X": [1,3,3], "m1.A": [1,2,3] }
{ "X": [3,1,3], "m1.A": [1,2,3] }
{ "X": [3,3,1], "m1.A": [1,2,3] }
{ "X": [1,3,1], "m1.A": [1,2,3] }
{ "X": [3,1,1], "m1.A": [1,2,3] }
{ "X": [1,1,3], "m1.A": [1,2,3] }
{ "X": [1,1,1], "m1.A": [1,2,3] }
{ "X": [3,3,3], "m1.A": [1,3,2] }
{ "X": [1,3,3], "m1.A": [1,3,2] }
{ "X": [3,1,3], "m1.A": [1,3,2] }
{ "X": [3,3,1], "m1.A": [1,3,2] }
{ "X": [1,3,1], "m1.A": [1,3,2] }
{ "X": [3,1,1], "m1.A": [1,3,2] }
{ "X": [1,1,3], "m1.A": [1,3,2] }
{ "X": [1,1,1], "m1.A": [1,3,2] }
;;
{ "X": [3,3,3], "m1.A": [1,2,3] }
{ "X": [1,3,3], "m1.A": [1,2,3] }
{ "X": [3,1,3], "m1.A": [1,2,3] }
{ "X": [3,3,1], "m1.A": [1,2,3] }
{ "X": [1,3,1], "m1.A": [1,2,3] }
{ "X": [3,1,1], "m1.A": [1,2,3] }
{ "X": [1,1,3], "m1.A": [1,2,3] }
{ "X": [1,1,1], "m1.A": [1,2,3] }
{ "X": [3,3,3], "m1.A": [2,1,3] }
{ "X": [1,3,3], "m1.A": [2,1,3] }
{ "X": [3,1,3], "m1.A": [2,1,3] }
{ "X": [3,3,1], "m1.A": [2,1,3] }
{ "X": [1,3,1], "m1.A": [2,1,3] }
{ "X": [3,1,1], "m1.A": [2,1,3] }
{ "X": [1,1,3], "m1.A": [2,1,3] }
{ "X": [1,1,1], "m1.A": [2,1,3] }
{ "X": [3,3,3], "m1.A": [1,3,2] }
{ "X": [1,3,3], "m1.A": [1,3,2] }
{ "X": [3,1,3], "m1.A": [1,3,2] }
{ "X": [3,3,1], "m1.A": [1,3,2] }
{ "X": [1,3,1], "m1.A": [1,3,2] }
{ "X": [3,1,1], "m1.A": [1,3,2] }
{ "X": [1,1,3], "m1.A": [1,3,2] }
{ "X": [1,1,1], "m1.A": [1,3,2] }
5 changes: 5 additions & 0 deletions testdata/lookup_09.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defcolumns X)
(deflookup test (m1.A) (X))

(module m1)
(defcolumns A)
37 changes: 37 additions & 0 deletions testdata/lookup_09.rejects
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ "X": [1], "m1.A": [2] }
;;
{ "X": [1,2], "m1.A": [1,1] }
{ "X": [2,1], "m1.A": [1,1] }
{ "X": [2,2], "m1.A": [1,1] }
;;
{ "X": [1,2], "m1.A": [1,3] }
{ "X": [1,2], "m1.A": [3,1] }
{ "X": [1,2], "m1.A": [1,1] }
{ "X": [2,1], "m1.A": [1,1] }
{ "X": [2,1], "m1.A": [1,3] }
{ "X": [2,1], "m1.A": [3,1] }
{ "X": [2,2], "m1.A": [1,3] }
{ "X": [2,2], "m1.A": [3,1] }
;;
{ "X": [3,3,3], "m1.A": [1,2,1] }
{ "X": [1,3,3], "m1.A": [1,2,1] }
{ "X": [3,1,3], "m1.A": [1,2,1] }
{ "X": [3,3,1], "m1.A": [1,2,1] }
{ "X": [1,3,1], "m1.A": [1,2,1] }
{ "X": [3,1,1], "m1.A": [1,2,1] }
{ "X": [1,1,3], "m1.A": [1,2,1] }
{ "X": [1,1,1], "m1.A": [2,2,2] }
{ "X": [1,1,1], "m1.A": [2,2,3] }
{ "X": [1,1,1], "m1.A": [3,2,2] }
{ "X": [1,1,1], "m1.A": [2,3,2] }
{ "X": [3,3,3], "m1.A": [1,1,2] }
{ "X": [1,3,3], "m1.A": [1,1,2] }
{ "X": [3,1,3], "m1.A": [1,1,2] }
{ "X": [3,3,1], "m1.A": [1,1,2] }
{ "X": [1,3,1], "m1.A": [1,1,2] }
{ "X": [3,1,1], "m1.A": [1,1,2] }
{ "X": [1,1,3], "m1.A": [1,1,2] }
{ "X": [1,1,1], "m1.A": [2,2,2] }
{ "X": [1,1,1], "m1.A": [3,2,2] }
{ "X": [1,1,1], "m1.A": [2,3,2] }
{ "X": [1,1,1], "m1.A": [2,2,3] }
3 changes: 1 addition & 2 deletions testdata/lookup_invalid_06.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
;;error:4:18-22:unknown symbol
;;error:4:23-27:unknown symbol
;;error:3:23-27:conflicting context
(defcolumns X Y)
(deflookup test (m1.A m2.B) (X Y))

Expand Down
3 changes: 1 addition & 2 deletions testdata/lookup_invalid_07.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
;;error:4:21-25:unknown symbol
;;error:4:26-30:unknown symbol
;;error:3:26-30:conflicting context
(defcolumns X)
(deflookup test ((+ m1.A m2.B)) (X))

Expand Down

0 comments on commit 0b49f75

Please sign in to comment.