forked from alex-eg/sex
implement spec and realize
A spec is a contract that emits nothing; a realization is that contract's methods for one type, mangled typeːspecːmethod. A default is copied into every realizing type with the parameter substituted, so the `show' a default calls is that type's own and the whole thing stays static dispatch with nothing left at runtime. The methods land in the ops registry beside inherent ones, which is what lets (.show v) find them with no second lookup -- and makes the duplicate check already there catch a spec method colliding with an ops method or with another spec's. Coherence came free. A realization's signatures are checked against the spec, arguments and return type both, after substituting the parameter; a missing required method is named, and so is one the spec has not got. That is what makes a spec a contract rather than a naming convention. The bound is asked rather than declared: realizes? goes into the macro API beside type-of, and a generic calls it and raises its own error. That is the return this document was written for -- `plain does not realize ord' where the line is, instead of a wall of C errors from a monomorphization two stages downstream. A default is static even when the realization is pub, and does not travel on export: every importing unit synthesizes its own copy, so sending one too would be a second definition of one symbol. The cost is that a module whose defaults call printf has to pub include stdio.h -- which is also why pub include and pub import now work, both having been accepted by read-public-interface and refused by semen.
This commit is contained in:
@@ -25,6 +25,16 @@
|
||||
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
|
||||
(apply string-append body) ")"))
|
||||
|
||||
(define (spec-src . body)
|
||||
(string-append
|
||||
"(record v ((x int)))"
|
||||
"(spec (p T)"
|
||||
" (fn s ((self (* const T))) void)"
|
||||
" (fn both ((self (* const T))) void (.s self)))"
|
||||
"(realize (p v)"
|
||||
" (fn s ((self (* const v))) void (return)))"
|
||||
(apply string-append body)))
|
||||
|
||||
(test-group "encapsulation"
|
||||
|
||||
;; A record is a struct plus a typedef of its own name, so the bare
|
||||
@@ -127,6 +137,79 @@
|
||||
(test-assert "a variant is a name and a field list"
|
||||
(reports? "(sum t (a))" "a variant is")))
|
||||
|
||||
;; A spec is a contract that emits nothing; a realization is its
|
||||
;; methods for one type. The refusals are what make it a contract.
|
||||
(test-group "spec and realize"
|
||||
(test-assert "a spec on its own emits nothing"
|
||||
(not (emits? "(spec (p T) (fn s ((a (* const T))) void))" "p")))
|
||||
(test-assert "a realization mangles type, spec and method"
|
||||
(emits? (spec-src "") "void vːpːs"))
|
||||
(test-assert "a default is copied per realizing type"
|
||||
(emits? (spec-src "") "vːpːboth"))
|
||||
(test-assert "and its parameter is substituted"
|
||||
(emits? (spec-src "") "void vːpːboth (const v * self)"))
|
||||
(test-assert "a default calls that type's own required method"
|
||||
(emits? (spec-src "") "vːpːs(self)"))
|
||||
(test-assert "a default is static even when the realization is pub"
|
||||
(emits? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||
" (fn d ((a (* const T))) void (.s a)))"
|
||||
"(pub realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
"static void vːpːd"))
|
||||
(test-assert "but a written method is not"
|
||||
(not (emits? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(pub realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
"static void vːpːs")))
|
||||
(test-assert "a spec method answers to .method"
|
||||
(emits? (spec-src "(fn f () void (var a v #(v : 1)) (.s (& a)))") "vːpːs(&a)"))
|
||||
|
||||
(test-assert "a spec is declared once"
|
||||
(reports? "(spec (p T) (fn s ((a T)) void))(spec (p T) (fn q ((a T)) void))"
|
||||
"spec is already declared"))
|
||||
(test-assert "a type realizes a spec once"
|
||||
(reports? (string-append (spec-src "") "(realize (p v)"
|
||||
" (fn s ((a (* const v))) void (return)))")
|
||||
"spec is already realized for"))
|
||||
(test-assert "a realization names a spec that exists"
|
||||
(reports? "(record v ((x int)))(realize (nope v))" "no such spec"))
|
||||
(test-assert "every required method is provided"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||
" (fn q ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"realization is missing a method of"))
|
||||
(test-assert "and the missing one is named"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void)"
|
||||
" (fn q ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"q"))
|
||||
(test-assert "a method the spec has not got is refused"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return))"
|
||||
" (fn z ((a (* const v))) void (return)))")
|
||||
"spec has no such method"))
|
||||
(test-assert "a return type has to match the spec"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) int (return 1)))")
|
||||
"return type does not match"))
|
||||
(test-assert "so do the arguments"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a v)) void (return)))")
|
||||
"arguments do not match"))
|
||||
(test-assert "a method name already taken on the type is refused"
|
||||
(reports? (string-append "(record v ((x int)))"
|
||||
"(ops v (fn s ((a (* const v))) void (return)))"
|
||||
"(spec (p T) (fn s ((a (* const T))) void))"
|
||||
"(realize (p v) (fn s ((a (* const v))) void (return)))")
|
||||
"method is already defined on")))
|
||||
|
||||
;; match is a switch over the tag whose arms bind the payload. What
|
||||
;; it refuses is why it exists.
|
||||
(test-group "match"
|
||||
|
||||
Reference in New Issue
Block a user