go: types.sx scaffold — synth/check skeleton + 12 tests; Phase 3 starts [shapes-static-types-bidirectional]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
First slice of Phase 3 (bidirectional type checker).
lib/go/types.sx defines:
* go-ctx-empty / go-ctx-extend / go-ctx-lookup — context as a value.
* go-ctx-extend-field — consumes the (:field NAMES TYPE) shape from
the parser, binding every name to the shared type. This is the
cross-deliverable validation of the :field binding-group
observation made during Phase 2 func decls: parser produces it,
type checker consumes it, same shape end-to-end.
* go-predeclared — true / false / nil baked in. Full list expanded
on demand.
* go-synth — currently handles variable lookup; literals / calls /
binops follow in subsequent iterations.
* go-check — v0 defers to synth + structural type equality. Untyped-
constant flow and assignment-compatibility relations land later.
* Type errors carry first-class tags (:unbound, :mismatch,
:unsupported-synth) so consumers and tooling can dispatch.
Conformance.sh wired with new types suite. Scoreboard cleanup: drop
the "pending" types row since the suite is now real.
types 12/12, total 317/317. Phase 3 underway.
Sister-plan static-types-bidirectional diary updated with the
synth/check shape: judgment skeleton, error tag structure, and the
proposal that `check` should accept a `subtype?` predicate parameter
so each consumer (Go untyped-constants, TS variance, Rust lifetimes)
plugs in its own variance discipline without rewriting the judgment.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,7 @@ trap "rm -f $TMPFILE $OUTFILE" EXIT
|
||||
SUITES=(
|
||||
"lex|go-test-pass|go-test-count"
|
||||
"parse|go-parse-test-pass|go-parse-test-count"
|
||||
"types|go-types-test-pass|go-types-test-count"
|
||||
)
|
||||
|
||||
cat > "$TMPFILE" <<'EPOCHS'
|
||||
@@ -38,8 +39,10 @@ cat > "$TMPFILE" <<'EPOCHS'
|
||||
(load "lib/guest/pratt.sx")
|
||||
(load "lib/go/lex.sx")
|
||||
(load "lib/go/parse.sx")
|
||||
(load "lib/go/types.sx")
|
||||
(load "lib/go/tests/lex.sx")
|
||||
(load "lib/go/tests/parse.sx")
|
||||
(load "lib/go/tests/types.sx")
|
||||
EPOCHS
|
||||
|
||||
idx=0
|
||||
@@ -104,7 +107,6 @@ cat > lib/go/scoreboard.json <<JSON
|
||||
"total_pass": $TOTAL_PASS,
|
||||
"total": $TOTAL_COUNT,
|
||||
"suites": [$JSON_SUITES,
|
||||
{"name":"types","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"eval","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"runtime","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"stdlib","pass":0,"total":0,"status":"pending"},
|
||||
@@ -120,8 +122,7 @@ cat > lib/go/scoreboard.md <<MD
|
||||
|
||||
| | Suite | Pass | Total |
|
||||
|---|---|---|---|
|
||||
$MD_ROWS| ⬜ | types | 0 | 0 |
|
||||
| ⬜ | eval | 0 | 0 |
|
||||
$MD_ROWS| ⬜ | eval | 0 | 0 |
|
||||
| ⬜ | runtime | 0 | 0 |
|
||||
| ⬜ | stdlib | 0 | 0 |
|
||||
| ⬜ | e2e | 0 | 0 |
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"language": "go",
|
||||
"total_pass": 305,
|
||||
"total": 305,
|
||||
"total_pass": 317,
|
||||
"total": 317,
|
||||
"suites": [
|
||||
{"name":"lex","pass":129,"total":129,"status":"ok"},
|
||||
{"name":"parse","pass":176,"total":176,"status":"ok"},
|
||||
{"name":"types","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"types","pass":12,"total":12,"status":"ok"},
|
||||
{"name":"eval","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"runtime","pass":0,"total":0,"status":"pending"},
|
||||
{"name":"stdlib","pass":0,"total":0,"status":"pending"},
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
# Go-on-SX Scoreboard
|
||||
|
||||
**Total: 305 / 305 tests passing**
|
||||
**Total: 317 / 317 tests passing**
|
||||
|
||||
| | Suite | Pass | Total |
|
||||
|---|---|---|---|
|
||||
| ✅ | lex | 129 | 129 |
|
||||
| ✅ | parse | 176 | 176 |
|
||||
| ⬜ | types | 0 | 0 |
|
||||
| ✅ | types | 12 | 12 |
|
||||
| ⬜ | eval | 0 | 0 |
|
||||
| ⬜ | runtime | 0 | 0 |
|
||||
| ⬜ | stdlib | 0 | 0 |
|
||||
|
||||
111
lib/go/tests/types.sx
Normal file
111
lib/go/tests/types.sx
Normal file
@@ -0,0 +1,111 @@
|
||||
;; Go type-checker tests.
|
||||
|
||||
(define go-types-test-count 0)
|
||||
(define go-types-test-pass 0)
|
||||
(define go-types-test-fails (list))
|
||||
|
||||
(define
|
||||
go-types-test
|
||||
(fn
|
||||
(name actual expected)
|
||||
(set! go-types-test-count (+ go-types-test-count 1))
|
||||
(if
|
||||
(= actual expected)
|
||||
(set! go-types-test-pass (+ go-types-test-pass 1))
|
||||
(append! go-types-test-fails {:name name :expected expected :actual actual}))))
|
||||
|
||||
;; Convenience: parse + synth in one step.
|
||||
(define gtsy (fn (ctx src) (go-synth ctx (go-parse src))))
|
||||
(define gtchk (fn (ctx src ty) (go-check ctx (go-parse src) ty)))
|
||||
|
||||
;; ── context helpers ──────────────────────────────────────────────
|
||||
(go-types-test
|
||||
"ctx: empty lookup returns nil"
|
||||
(go-ctx-lookup go-ctx-empty "x")
|
||||
nil)
|
||||
|
||||
(go-types-test
|
||||
"ctx: extend then lookup"
|
||||
(go-ctx-lookup (go-ctx-extend go-ctx-empty "x" (list :ty-name "int")) "x")
|
||||
(list :ty-name "int"))
|
||||
|
||||
(go-types-test
|
||||
"ctx: shadow via extend"
|
||||
(go-ctx-lookup
|
||||
(go-ctx-extend
|
||||
(go-ctx-extend go-ctx-empty "x" (list :ty-name "int"))
|
||||
"x"
|
||||
(list :ty-name "string"))
|
||||
"x")
|
||||
(list :ty-name "string"))
|
||||
|
||||
(go-types-test
|
||||
"ctx: extend-field binds all names"
|
||||
(let
|
||||
((ctx (go-ctx-extend-field go-ctx-empty (list :field (list "a" "b" "c") (list :ty-name "int")))))
|
||||
(list
|
||||
(go-ctx-lookup ctx "a")
|
||||
(go-ctx-lookup ctx "b")
|
||||
(go-ctx-lookup ctx "c")
|
||||
(go-ctx-lookup ctx "d")))
|
||||
(list
|
||||
(list :ty-name "int")
|
||||
(list :ty-name "int")
|
||||
(list :ty-name "int")
|
||||
nil))
|
||||
|
||||
;; ── predeclared identifiers ──────────────────────────────────────
|
||||
(go-types-test
|
||||
"predeclared: true"
|
||||
(gtsy go-ctx-empty "true")
|
||||
(list :ty-name "bool"))
|
||||
|
||||
(go-types-test
|
||||
"predeclared: false"
|
||||
(gtsy go-ctx-empty "false")
|
||||
(list :ty-name "bool"))
|
||||
|
||||
(go-types-test
|
||||
"predeclared: nil"
|
||||
(gtsy go-ctx-empty "nil")
|
||||
(list :ty-untyped-nil))
|
||||
|
||||
;; ── synth: variable lookup ──────────────────────────────────────
|
||||
(go-types-test
|
||||
"synth: bound variable returns its type"
|
||||
(go-synth
|
||||
(go-ctx-extend go-ctx-empty "x" (list :ty-name "int"))
|
||||
(go-parse "x"))
|
||||
(list :ty-name "int"))
|
||||
|
||||
(go-types-test
|
||||
"synth: unbound variable is a type error"
|
||||
(go-synth go-ctx-empty (go-parse "ghost"))
|
||||
(list :type-error :unbound "ghost"))
|
||||
|
||||
;; ── check: structural type equality ─────────────────────────────
|
||||
(go-types-test
|
||||
"check: ident vs declared type — matching"
|
||||
(go-check
|
||||
(go-ctx-extend go-ctx-empty "x" (list :ty-name "int"))
|
||||
(go-parse "x")
|
||||
(list :ty-name "int"))
|
||||
:ok)
|
||||
|
||||
(go-types-test
|
||||
"check: ident vs declared type — mismatch"
|
||||
(go-check
|
||||
(go-ctx-extend go-ctx-empty "x" (list :ty-name "int"))
|
||||
(go-parse "x")
|
||||
(list :ty-name "string"))
|
||||
(list :type-error :mismatch (list :ty-name "string") (list :ty-name "int")))
|
||||
|
||||
(go-types-test
|
||||
"check: unbound propagates the synth error"
|
||||
(go-check go-ctx-empty (go-parse "ghost") (list :ty-name "int"))
|
||||
(list :type-error :unbound "ghost"))
|
||||
|
||||
;; ── report ──────────────────────────────────────────────────────
|
||||
(define
|
||||
go-types-test-summary
|
||||
(str "types " go-types-test-pass "/" go-types-test-count))
|
||||
125
lib/go/types.sx
Normal file
125
lib/go/types.sx
Normal file
@@ -0,0 +1,125 @@
|
||||
;; lib/go/types.sx — Go bidirectional type checker.
|
||||
;;
|
||||
;; Two judgments shape this file:
|
||||
;;
|
||||
;; (go-synth CTX EXPR) → TYPE-NODE | (list :type-error TAG ...)
|
||||
;; Given a context and an expression, produce a type.
|
||||
;;
|
||||
;; (go-check CTX EXPR EXPECTED) → :ok | (list :type-error TAG ...)
|
||||
;; Given a context, expression, and expected type, verify compatibility.
|
||||
;;
|
||||
;; The two judgments are mutually recursive. Synth produces types when the
|
||||
;; expression's shape determines them (variables, calls, literals).
|
||||
;; Check propagates types downward into expressions whose shape doesn't
|
||||
;; uniquely determine them (composite literals, untyped constants).
|
||||
;;
|
||||
;; Type representations reuse the parser's :ty-* AST nodes from
|
||||
;; lib/go/parse.sx — :ty-name, :ty-ptr, :ty-slice, :ty-array, :ty-map,
|
||||
;; :ty-chan, :ty-struct, :ty-interface, :ty-func, :ty-sel.
|
||||
;;
|
||||
;; Context: an association list of (NAME TYPE) bindings. Per-block scope
|
||||
;; via a fresh extension on entry.
|
||||
;;
|
||||
;; **Independent implementation.** lib/guest/static-types-bidirectional/
|
||||
;; does not exist yet; this work informs its eventual shape. Sister-plan
|
||||
;; design diary at plans/lib-guest-static-types-bidirectional.md tracks
|
||||
;; the chiselling insights as Phase 3 progresses.
|
||||
|
||||
;; ── context ───────────────────────────────────────────────────────
|
||||
|
||||
(define go-ctx-empty (list))
|
||||
|
||||
(define
|
||||
go-ctx-lookup
|
||||
(fn
|
||||
(ctx name)
|
||||
(cond
|
||||
(= (len ctx) 0)
|
||||
nil
|
||||
(= (first (first ctx)) name)
|
||||
(nth (first ctx) 1)
|
||||
:else (go-ctx-lookup (rest ctx) name))))
|
||||
|
||||
(define go-ctx-extend (fn (ctx name type) (cons (list name type) ctx)))
|
||||
|
||||
(define
|
||||
go-ctx-extend-field
|
||||
(fn
|
||||
(ctx field)
|
||||
(let
|
||||
((names (nth field 1)) (ty (nth field 2)))
|
||||
(cond
|
||||
(= (len names) 0)
|
||||
ctx
|
||||
:else (let
|
||||
((rest-ctx (go-ctx-extend ctx (first names) ty)))
|
||||
(cond
|
||||
(= (len names) 1)
|
||||
rest-ctx
|
||||
:else (go-ctx-extend-field rest-ctx (list :field (rest names) ty))))))))
|
||||
|
||||
;; ── predeclared identifiers ──────────────────────────────────────
|
||||
|
||||
(define
|
||||
go-predeclared
|
||||
(list
|
||||
(list "true" (list :ty-name "bool"))
|
||||
(list "false" (list :ty-name "bool"))
|
||||
(list "nil" (list :ty-untyped-nil))))
|
||||
|
||||
(define
|
||||
go-predeclared-lookup
|
||||
(fn
|
||||
(name)
|
||||
(cond
|
||||
(= (len go-predeclared) 0)
|
||||
nil
|
||||
:else (go-ctx-lookup go-predeclared name))))
|
||||
|
||||
;; ── type predicates ──────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
go-type-error?
|
||||
(fn
|
||||
(x)
|
||||
(and
|
||||
(list? x)
|
||||
(not (= (len x) 0))
|
||||
(= (first x) :type-error))))
|
||||
|
||||
(define go-type-equal? (fn (a b) (= a b)))
|
||||
|
||||
;; ── synth ────────────────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
go-synth
|
||||
(fn
|
||||
(ctx expr)
|
||||
(cond
|
||||
(and (list? expr) (= (first expr) :var))
|
||||
(let
|
||||
((name (nth expr 1)))
|
||||
(let
|
||||
((pre (go-predeclared-lookup name)))
|
||||
(cond
|
||||
(not (= pre nil))
|
||||
pre
|
||||
:else (let
|
||||
((t (go-ctx-lookup ctx name)))
|
||||
(cond (= t nil) (list :type-error :unbound name) :else t)))))
|
||||
:else (list :type-error :unsupported-synth expr))))
|
||||
|
||||
;; ── check ────────────────────────────────────────────────────────
|
||||
|
||||
(define
|
||||
go-check
|
||||
(fn
|
||||
(ctx expr expected)
|
||||
(let
|
||||
((got (go-synth ctx expr)))
|
||||
(cond
|
||||
(go-type-error? got)
|
||||
got
|
||||
(go-type-equal? got expected)
|
||||
:ok :else
|
||||
(list :type-error :mismatch expected got)))))
|
||||
Reference in New Issue
Block a user