remove spec and realize
They were too much surface for a language this small: a nominal contract layer, per-type default synthesis and a pub interface of their own. Type introspection and macros are the intended replacement, so the storage, the forms and the tests that pin them go; the ops method registry they leaned on stays. The implementation is kept at tag 0.0.1.
This commit is contained in:
142
semen.scm
142
semen.scm
@@ -104,10 +104,6 @@
|
||||
((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))
|
||||
@@ -125,8 +121,7 @@
|
||||
('pub 'var . _)
|
||||
('extern 'var . _)) (process-global-var sex-form 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
|
||||
;; accepts: a module may export what its interface needs
|
||||
((or ('include . includes)
|
||||
('pub 'include . includes)) (process-includes sex-form includes acc))
|
||||
((or ('define name . _)
|
||||
@@ -319,141 +314,6 @@
|
||||
(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)
|
||||
;; every method is registered before any body is walked, so they
|
||||
;; may call one another in any order, as an ops block's may; one
|
||||
;; called above its definition is declared ahead of them all
|
||||
(let* ((methods (map (lambda (entry)
|
||||
(register-realized-method form name type
|
||||
(and pub? (cdr entry))
|
||||
(car entry)))
|
||||
(realization-methods type parameter wanted given)))
|
||||
(bodies (map (lambda (m) (process-fn m (list))) methods))
|
||||
(early (filter (lambda (m) (called-before? m methods bodies))
|
||||
methods)))
|
||||
(append (append-map identity (reverse bodies))
|
||||
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc))
|
||||
acc
|
||||
early)))))))
|
||||
|
||||
;;; 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 (spec-default? w)
|
||||
(not (find (lambda (g) (eq? (second g) (second w))) given))
|
||||
(cons (substitute-parameter parameter type w) #f)))
|
||||
wanted)))
|
||||
|
||||
;;; A docstring or a comment is not a body, so
|
||||
;;;
|
||||
;;; (fn s ((self (* const T))) void "what s is for")
|
||||
;;;
|
||||
;;; is still a method every realization has to write.
|
||||
(define (spec-default? method)
|
||||
(let-values (((doc fn) (extract-fn-docstring (strip-fn-header-comments method))))
|
||||
(any (lambda (form) (not (comment-form? form)))
|
||||
(drop fn (fn-header-length fn)))))
|
||||
|
||||
(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 (not (spec-default? w))
|
||||
(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. The method's fn
|
||||
;;; form, renamed to pointːsizedːtotal, once it is registered under
|
||||
;;; `total' and its signature known.
|
||||
(define (register-realized-method form spec type pub? method)
|
||||
(let* ((name (second method))
|
||||
(qualified (qualify-op (qualify-op type spec) name))
|
||||
(renamed (copy-form-source! form (rename-fn method qualified)))
|
||||
(fn (if pub? (copy-form-source! form (cons 'pub renamed)) renamed)))
|
||||
(when (get-method type name)
|
||||
(sex-error form "method is already defined on" type name))
|
||||
(add-method! type name qualified)
|
||||
(process-fn (sex-fn-prototype fn) (list))
|
||||
fn))
|
||||
|
||||
;;; 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