go: types.sx — interface satisfaction (structural method-set check) + 7 tests [shapes-static-types-bidirectional]
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Phase 3 cont. The headline Go-distinguishing typing feature: interfaces
are satisfied *structurally and silently* — no `implements` declaration,
no nominal subtyping. Any type whose method set contains all the
interface's methods (with matching signatures) satisfies it.
Method declarations now type-check via go-check-method-decl:
* Receiver type extracted (T or *T → "T") via go-extract-recv-ty-name.
* Method signature (:ty-func PARAMS RESULTS) bound under a mangled
key "#method/RECV-NAME/METHOD-NAME" in ctx.
* Body checked with receiver + params extended into the body ctx.
go-iface-satisfies? CTX TY-NAME IFACE-TYPE walks the interface's
:method elements; for each, looks up #method/TY-NAME/METHOD-NAME and
compares (PARAMS, RESULTS) tuples. Embedded interfaces (:embed
elements) skipped in v0 — recursive interface resolution later.
Tests:
* method-decl binds under #method/Point/String
* pointer-receiver method also keys the base type
* Point with String() satisfies interface { String() string }
* empty type does NOT satisfy Stringer
* arity-mismatch method fails satisfaction
* multi-method satisfaction works
* partial method-set fails
types 72/72, total 377/377. Phase 3 sub-deliverable list is now
substantially complete; only AST-path error context remains as a UX
sharpener.
Sister-plan static-types-bidirectional diary updated with the
**constraint-satisfies? pluggable predicate** kit-API proposal —
third pluggable point after synth/check + assignable?. Go interfaces,
Haskell typeclasses, Rust traits, and TS structural subtyping all
answer "does this value-type fit this constraint-type?" with
different machinery; the kit's check uses constraint-satisfies? when
EXPECTED is itself a constraint type.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -531,8 +531,103 @@
|
||||
(go-ctx-extend ctx name ty))
|
||||
(and (list? decl) (= (first decl) :func-decl))
|
||||
(go-check-func-decl ctx decl)
|
||||
(and (list? decl) (= (first decl) :method-decl))
|
||||
(go-check-method-decl ctx decl)
|
||||
:else ctx)))
|
||||
|
||||
;; ── method declarations and interface satisfaction ──────────────
|
||||
;; Methods are recorded in CTX under a mangled key
|
||||
;; "#method/RECV-TYPE-NAME/METHOD-NAME"
|
||||
;; bound to the method's :ty-func signature. Interface satisfaction is
|
||||
;; a structural lookup over these keys (Go spec § Interface types:
|
||||
;; "anything with the matching method set satisfies the interface").
|
||||
|
||||
(define
|
||||
go-method-key
|
||||
(fn (recv-ty-name method-name)
|
||||
(str "#method/" recv-ty-name "/" method-name)))
|
||||
|
||||
(define
|
||||
go-extract-recv-ty-name
|
||||
;; Receiver type is T or *T; return the named type's name string.
|
||||
(fn (recv-ty)
|
||||
(cond
|
||||
(and (list? recv-ty) (= (first recv-ty) :ty-name))
|
||||
(nth recv-ty 1)
|
||||
(and (list? recv-ty) (= (first recv-ty) :ty-ptr))
|
||||
(go-extract-recv-ty-name (nth recv-ty 1))
|
||||
:else nil)))
|
||||
|
||||
(define
|
||||
go-check-method-decl
|
||||
;; (list :method-decl RECV NAME PARAMS RESULTS BODY)
|
||||
;; Binds the method under the mangled key, then checks body with
|
||||
;; receiver + params extended.
|
||||
(fn (ctx decl)
|
||||
(let ((recv (nth decl 1)) (name (nth decl 2))
|
||||
(params (nth decl 3)) (results (nth decl 4))
|
||||
(body (nth decl 5)))
|
||||
(let ((recv-ty (nth recv 2)))
|
||||
(let ((recv-name (go-extract-recv-ty-name recv-ty)))
|
||||
(let ((sig (list :ty-func
|
||||
(go-decl-params-to-ty-list params) results)))
|
||||
(let ((ctx2
|
||||
(cond
|
||||
(= recv-name nil) ctx
|
||||
:else
|
||||
(go-ctx-extend ctx
|
||||
(go-method-key recv-name name) sig))))
|
||||
(cond
|
||||
(= body nil) ctx2
|
||||
(and (list? body) (= (first body) :block))
|
||||
(let ((body-ctx
|
||||
(go-extend-with-params
|
||||
(go-ctx-extend-field ctx2 recv) params)))
|
||||
(let ((err
|
||||
(go-check-block body-ctx
|
||||
(nth body 1) results)))
|
||||
(cond
|
||||
(go-type-error? err) err
|
||||
:else ctx2)))
|
||||
:else ctx2))))))))
|
||||
|
||||
(define
|
||||
go-iface-elems-satisfied?
|
||||
;; Each :method element in ELEMS must have a matching method in CTX
|
||||
;; under #method/TY-NAME/M-NAME. :embed elements are skipped in v0
|
||||
;; (they'd need recursive interface resolution).
|
||||
(fn (ctx ty-name elems)
|
||||
(cond
|
||||
(= (len elems) 0) true
|
||||
:else
|
||||
(let ((e (first elems)))
|
||||
(cond
|
||||
(= (first e) :method)
|
||||
(let ((m-name (nth e 1)) (m-params (nth e 2))
|
||||
(m-results (nth e 3)))
|
||||
(let ((found (go-ctx-lookup ctx
|
||||
(go-method-key ty-name m-name))))
|
||||
(cond
|
||||
(= found nil) false
|
||||
(and (= (nth found 1) m-params)
|
||||
(= (nth found 2) m-results))
|
||||
(go-iface-elems-satisfied? ctx ty-name (rest elems))
|
||||
:else false)))
|
||||
(= (first e) :embed)
|
||||
(go-iface-elems-satisfied? ctx ty-name (rest elems))
|
||||
:else
|
||||
(go-iface-elems-satisfied? ctx ty-name (rest elems)))))))
|
||||
|
||||
(define
|
||||
go-iface-satisfies?
|
||||
;; Does the type named TY-NAME satisfy the interface IFACE-TYPE
|
||||
;; under context CTX? Structural method-set match per Go spec.
|
||||
(fn (ctx ty-name iface-type)
|
||||
(cond
|
||||
(not (and (list? iface-type) (= (first iface-type) :ty-interface)))
|
||||
false
|
||||
:else (go-iface-elems-satisfied? ctx ty-name (nth iface-type 1)))))
|
||||
|
||||
;; ── function-decl checking ──────────────────────────────────────
|
||||
|
||||
(define
|
||||
|
||||
Reference in New Issue
Block a user