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:
125
semen.scm
125
semen.scm
@@ -83,6 +83,10 @@
|
||||
((or ('fn . _)
|
||||
('pub 'fn . _)
|
||||
('extern 'fn . _)) (process-fn sex-form acc))
|
||||
((or ('spec . _)
|
||||
('pub 'spec . _)) (process-spec sex-form acc))
|
||||
((or ('realize . _)
|
||||
('pub 'realize . _)) (process-realize sex-form acc))
|
||||
(('ops . _) (process-ops sex-form acc))
|
||||
(('pub 'ops . _)
|
||||
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
|
||||
@@ -99,14 +103,19 @@
|
||||
((or ('var . _)
|
||||
('pub 'var . _)
|
||||
('extern 'var . _)) (process-global-var sex-form acc))
|
||||
(('include . includes) (process-includes sex-form includes acc))
|
||||
;; `pub' on either of these is what read-public-interface already
|
||||
;; accepts, and a module whose spec defaults call printf has to be
|
||||
;; able to say so
|
||||
((or ('include . includes)
|
||||
('pub 'include . includes)) (process-includes sex-form includes acc))
|
||||
((or ('define name . _)
|
||||
('pub 'define name . _))
|
||||
(add-define name sex-form)
|
||||
(cons sex-form acc))
|
||||
(('comment . _) (cons sex-form acc))
|
||||
|
||||
(('import . modules)
|
||||
((or ('import . modules)
|
||||
('pub 'import . modules))
|
||||
(process-imports (get-modules-public-forms modules) acc))
|
||||
|
||||
((or ('defmacro . rest)
|
||||
@@ -272,6 +281,118 @@
|
||||
(with-docstring doc processed
|
||||
(append (hash-table-ref env :lambda-aux-code) acc))))))
|
||||
|
||||
;;; (spec (printable T) (fn show ((self (* const T))) void))
|
||||
;;;
|
||||
;;; declares a contract and emits nothing: a spec method only exists
|
||||
;;; once some type realizes it, and then once per type.
|
||||
(define (process-spec form acc)
|
||||
(let* ((core (if (eq? (car form) 'pub) (cdr form) form))
|
||||
(head (and (pair? (cdr core)) (cadr core)))
|
||||
(methods (remove comment-form? (cddr core))))
|
||||
(unless (and (list? head) (= 2 (length head))
|
||||
(symbol? (car head)) (symbol? (cadr head)))
|
||||
(sex-error form "a spec is (spec (name T) methods ...)" form))
|
||||
(let ((name (car head)))
|
||||
(when (get-spec name)
|
||||
(sex-error form "spec is already declared" name))
|
||||
(for-each (lambda (m)
|
||||
(unless (and (pair? m) (eq? (car m) 'fn)
|
||||
(pair? (cdr m)) (symbol? (second m)))
|
||||
(sex-error form "a spec takes fn forms" m)))
|
||||
methods)
|
||||
(add-spec! name (cadr head) methods)
|
||||
acc)))
|
||||
|
||||
;;; (realize (printable expr) (fn show ((self (* const expr))) void ...))
|
||||
;;;
|
||||
;;; is the spec's methods for one type: the ones written here, plus a
|
||||
;;; copy of every default the spec carries, each with the parameter
|
||||
;;; substituted and the name mangled to exprːprintableːshow. They land
|
||||
;;; in the ops registry beside inherent methods, so (.show e) finds
|
||||
;;; them and a name used twice on one type is caught either way.
|
||||
(define (process-realize form acc)
|
||||
(let* ((pub? (eq? (car form) 'pub))
|
||||
(core (if pub? (cdr form) form))
|
||||
(head (and (pair? (cdr core)) (cadr core)))
|
||||
(given (remove comment-form? (cddr core))))
|
||||
(unless (and (list? head) (= 2 (length head))
|
||||
(symbol? (car head)) (symbol? (cadr head)))
|
||||
(sex-error form "a realization is (realize (spec type) methods ...)" form))
|
||||
(let* ((name (car head))
|
||||
(type (cadr head))
|
||||
(spec (or (get-spec name)
|
||||
(sex-error form "no such spec" name))))
|
||||
(when (realizes? name type)
|
||||
(sex-error form "spec is already realized for" name type))
|
||||
(add-realization! name type)
|
||||
(let ((wanted (spec-methods spec))
|
||||
(parameter (spec-parameter spec)))
|
||||
(check-realization form name type parameter wanted given)
|
||||
(fold (lambda (entry acc)
|
||||
(process-realized-method form name type
|
||||
(and pub? (cdr entry)) (car entry) acc))
|
||||
acc
|
||||
(realization-methods type parameter wanted given))))))
|
||||
|
||||
;;; What is written here, which a pub realization exports, and then
|
||||
;;; every default nobody overrode, which it does not: a default is
|
||||
;;; synthesized again in every unit that imports the realization, so
|
||||
;;; exporting one too would be a second definition of one symbol.
|
||||
(define (realization-methods type parameter wanted given)
|
||||
(append (map (lambda (g) (cons g #t)) given)
|
||||
(filter-map
|
||||
(lambda (w)
|
||||
(and (> (length w) 4) ; carries a body, so a default
|
||||
(not (find (lambda (g) (eq? (second g) (second w))) given))
|
||||
(cons (substitute-parameter parameter type w) #f)))
|
||||
wanted)))
|
||||
|
||||
(define (check-realization form name type parameter wanted given)
|
||||
(for-each
|
||||
(lambda (g)
|
||||
(unless (and (pair? g) (eq? (car g) 'fn) (pair? (cdr g)) (symbol? (second g)))
|
||||
(sex-error form "a realization takes fn forms" g))
|
||||
(let ((w (find (lambda (w) (eq? (second w) (second g))) wanted)))
|
||||
(unless w
|
||||
(sex-error form "spec has no such method" name (second g)))
|
||||
(let ((expected (substitute-parameter parameter type w)))
|
||||
(unless (equal? (arglist-types (sex-fn-arglist expected))
|
||||
(arglist-types (sex-fn-arglist g)))
|
||||
(sex-error form "method's arguments do not match the spec" (second g)))
|
||||
(unless (equal? (sex-fn-return-type expected) (sex-fn-return-type g))
|
||||
(sex-error form "method's return type does not match the spec"
|
||||
(second g))))))
|
||||
given)
|
||||
(let ((missing (filter-map
|
||||
(lambda (w)
|
||||
(and (= (length w) 4) ; no body, so required
|
||||
(not (find (lambda (g) (eq? (second g) (second w))) given))
|
||||
(second w)))
|
||||
wanted)))
|
||||
(when (pair? missing)
|
||||
(apply sex-error form "realization is missing a method of" name missing))))
|
||||
|
||||
;;; A realization is public or private as a unit -- its methods do
|
||||
;;; not carry `pub' of their own, the way an ops block's do, because
|
||||
;;; the spec already says which of them there are.
|
||||
(define (process-realized-method form spec type pub? method acc)
|
||||
(let* ((name (second method))
|
||||
(qualified (qualify-op (qualify-op type spec) name))
|
||||
(renamed (rename-fn method qualified)))
|
||||
(when (get-method type name)
|
||||
(sex-error form "method is already defined on" type name))
|
||||
(add-method! type name qualified)
|
||||
(process-fn (copy-form-source! form (if pub? (cons 'pub renamed) renamed))
|
||||
acc)))
|
||||
|
||||
;;; T in a spec's method stands for whatever realizes it
|
||||
(define (substitute-parameter parameter type form)
|
||||
(cond ((eq? form parameter) type)
|
||||
((pair? form) (recons form
|
||||
(substitute-parameter parameter type (car form))
|
||||
(substitute-parameter parameter type (cdr form))))
|
||||
(else form)))
|
||||
|
||||
;;; (ops vec2
|
||||
;;; (pub fn length ((self (* const vec2))) float ...))
|
||||
;;;
|
||||
|
||||
Reference in New Issue
Block a user