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 . _)
|
((or ('fn . _)
|
||||||
('pub 'fn . _)
|
('pub 'fn . _)
|
||||||
('extern 'fn . _)) (process-fn sex-form acc))
|
('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))
|
(('ops . _) (process-ops sex-form acc))
|
||||||
(('pub 'ops . _)
|
(('pub 'ops . _)
|
||||||
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
|
(sex-error sex-form "pub belongs on the methods, not on ops" sex-form))
|
||||||
@@ -125,8 +121,7 @@
|
|||||||
('pub 'var . _)
|
('pub 'var . _)
|
||||||
('extern 'var . _)) (process-global-var sex-form acc))
|
('extern 'var . _)) (process-global-var sex-form acc))
|
||||||
;; `pub' on either of these is what read-public-interface already
|
;; `pub' on either of these is what read-public-interface already
|
||||||
;; accepts, and a module whose spec defaults call printf has to be
|
;; accepts: a module may export what its interface needs
|
||||||
;; able to say so
|
|
||||||
((or ('include . includes)
|
((or ('include . includes)
|
||||||
('pub 'include . includes)) (process-includes sex-form includes acc))
|
('pub 'include . includes)) (process-includes sex-form includes acc))
|
||||||
((or ('define name . _)
|
((or ('define name . _)
|
||||||
@@ -319,141 +314,6 @@
|
|||||||
(with-docstring doc processed
|
(with-docstring doc processed
|
||||||
(append (hash-table-ref env :lambda-aux-code) acc))))))
|
(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
|
;;; (ops vec2
|
||||||
;;; (pub fn length ((self (* const vec2))) float ...))
|
;;; (pub fn length ((self (* const vec2))) float ...))
|
||||||
;;;
|
;;;
|
||||||
|
|||||||
@@ -46,10 +46,7 @@
|
|||||||
get-underlying-type type-match type-pattern-matches?
|
get-underlying-type type-match type-pattern-matches?
|
||||||
map-fields
|
map-fields
|
||||||
get-name-type get-return-type type-of
|
get-name-type get-return-type type-of
|
||||||
;; what a generic asks before it expands:
|
get-method))
|
||||||
;; (realizes? 'ord type) beats a wall of C
|
|
||||||
;; errors two stages downstream
|
|
||||||
realizes? get-method))
|
|
||||||
,@body)))
|
,@body)))
|
||||||
|
|
||||||
(define (get-macro name)
|
(define (get-macro name)
|
||||||
|
|||||||
@@ -270,7 +270,6 @@
|
|||||||
(fold
|
(fold
|
||||||
(lambda (form acc)
|
(lambda (form acc)
|
||||||
(check-written-out form)
|
(check-written-out form)
|
||||||
(check-realized-spec form visible hidden)
|
|
||||||
(fold (lambda (use acc) (check-type-use form use visible hidden acc))
|
(fold (lambda (use acc) (check-type-use form use visible hidden acc))
|
||||||
acc
|
acc
|
||||||
(append-map (lambda (type) (type-uses type #f))
|
(append-map (lambda (type) (type-uses type #f))
|
||||||
@@ -292,22 +291,6 @@
|
|||||||
(_ #f)))
|
(_ #f)))
|
||||||
(_ #f)))
|
(_ #f)))
|
||||||
|
|
||||||
;;; A pub realization goes to importers, who look its spec up; one they
|
|
||||||
;;; cannot see failed there, in whichever named this module before the
|
|
||||||
;;; spec's, and said nothing of where it was written
|
|
||||||
(define (check-realized-spec form visible hidden)
|
|
||||||
(match form
|
|
||||||
(('pub 'realize ((? symbol? spec) _) . _)
|
|
||||||
(let ((kept (assq spec hidden)))
|
|
||||||
(when (and kept (not (assq spec visible)))
|
|
||||||
(sex-error form
|
|
||||||
(string-append "importers cannot see this spec; "
|
|
||||||
(if (fourth kept)
|
|
||||||
"import its module with pub"
|
|
||||||
"make it pub"))
|
|
||||||
spec))))
|
|
||||||
(_ #f)))
|
|
||||||
|
|
||||||
;;; USE is (name . behind-a-pointer?)
|
;;; USE is (name . behind-a-pointer?)
|
||||||
(define (check-type-use form use visible hidden acc)
|
(define (check-type-use form use visible hidden acc)
|
||||||
(let* ((name (car use))
|
(let* ((name (car use))
|
||||||
@@ -363,9 +346,6 @@
|
|||||||
(remove comment-form? variants)))
|
(remove comment-form? variants)))
|
||||||
(('ops type . methods)
|
(('ops type . methods)
|
||||||
(append-map interface-types (filter pub-form? methods)))
|
(append-map interface-types (filter pub-form? methods)))
|
||||||
(('pub 'realize spec . methods)
|
|
||||||
(append-map (lambda (m) (interface-types (cons 'pub m)))
|
|
||||||
(remove comment-form? methods)))
|
|
||||||
(_ (list))))
|
(_ (list))))
|
||||||
|
|
||||||
(define (field-types fields)
|
(define (field-types fields)
|
||||||
@@ -409,24 +389,23 @@
|
|||||||
((pair? bound) (append-map bound-uses (cdr bound)))
|
((pair? bound) (append-map bound-uses (cdr bound)))
|
||||||
(else (list))))
|
(else (list))))
|
||||||
|
|
||||||
;;; The types, constants and specs FORMS declare, as (name kind body?):
|
;;; The types and constants FORMS declare, as (name kind body?):
|
||||||
;;;
|
;;;
|
||||||
;;; (pub record tally) -> (tally record #f)
|
;;; (pub record tally) -> (tally record #f)
|
||||||
;;; (sum colour ((none ()))) -> (colour sum #t)
|
;;; (sum colour ((none ()))) -> (colour sum #t)
|
||||||
;;; (define (sq x) (* x x)) -> (sq define #t)
|
;;; (define (sq x) (* x x)) -> (sq define #t)
|
||||||
;;; (spec (shown T) ...) -> (shown spec #t)
|
|
||||||
(define (declared-types forms)
|
(define (declared-types forms)
|
||||||
(filter-map
|
(filter-map
|
||||||
(lambda (form)
|
(lambda (form)
|
||||||
(and (pair? form)
|
(and (pair? form)
|
||||||
(let* ((core (strip-pub form))
|
(let* ((core (strip-pub form))
|
||||||
(name (and (pair? core) (pair? (cdr core))
|
(name (and (pair? core) (pair? (cdr core))
|
||||||
(if (and (memq (car core) '(define spec))
|
(if (and (eq? (car core) 'define)
|
||||||
(pair? (second core)))
|
(pair? (second core)))
|
||||||
(car (second core))
|
(car (second core))
|
||||||
(second core)))))
|
(second core)))))
|
||||||
(and (memq (car core)
|
(and (memq (car core)
|
||||||
'(record struct union enum sum typedef define spec))
|
'(record struct union enum sum typedef define))
|
||||||
(symbol? name)
|
(symbol? name)
|
||||||
(list name
|
(list name
|
||||||
(car core)
|
(car core)
|
||||||
@@ -520,19 +499,6 @@
|
|||||||
(copy-form-source! form
|
(copy-form-source! form
|
||||||
`(ops ,type ,@(map public-fn-interface methods))))))
|
`(ops ,type ,@(map public-fn-interface methods))))))
|
||||||
|
|
||||||
;;; A realization leaves with the methods written in it, reduced to
|
|
||||||
;;; prototypes. Its defaults do not travel: the importer synthesizes
|
|
||||||
;;; its own static copies from the spec, which is why a default is
|
|
||||||
;;; static even when the realization is pub.
|
|
||||||
(define (realize-public-interface form)
|
|
||||||
(let ((core (cdr form)))
|
|
||||||
(copy-form-source!
|
|
||||||
form
|
|
||||||
`(pub realize ,(second core)
|
|
||||||
;; pub goes back on at the realize, not on its methods
|
|
||||||
,@(map (lambda (m) (cdr (public-fn-interface (cons 'pub m))))
|
|
||||||
(remove comment-form? (cddr core)))))))
|
|
||||||
|
|
||||||
;;; TODO: use semen facilities to analyze modules
|
;;; TODO: use semen facilities to analyze modules
|
||||||
(define (process-public-interface-form form acc)
|
(define (process-public-interface-form form acc)
|
||||||
(match form
|
(match form
|
||||||
@@ -543,13 +509,11 @@
|
|||||||
(('pub 'var . _)
|
(('pub 'var . _)
|
||||||
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
|
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
|
||||||
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
|
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
|
||||||
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'spec 'struct 'sum 'typedef 'union) . _)
|
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'struct 'sum 'typedef 'union) . _)
|
||||||
(cons (copy-form-source! form (cdr form)) acc))
|
(cons (copy-form-source! form (cdr form)) acc))
|
||||||
(('ops . _)
|
(('ops . _)
|
||||||
(let ((exported (ops-public-interface form)))
|
(let ((exported (ops-public-interface form)))
|
||||||
(if exported (cons exported acc) acc)))
|
(if exported (cons exported acc) acc)))
|
||||||
(('pub 'realize . _)
|
|
||||||
(cons (realize-public-interface form) acc))
|
|
||||||
(('pub . _)
|
(('pub . _)
|
||||||
(sex-error form "pub must be followed by a definition" form))
|
(sex-error form "pub must be followed by a definition" form))
|
||||||
(_ acc)))
|
(_ acc)))
|
||||||
|
|||||||
@@ -42,16 +42,6 @@ ahead of its pub forms."
|
|||||||
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
|
"(fn f () void (var s shape (shape/circle 1.0)) (var out float 0.0) "
|
||||||
(apply string-append body) ")"))
|
(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"
|
(test-group "encapsulation"
|
||||||
|
|
||||||
;; A record is a struct plus a typedef of its own name, so the bare
|
;; A record is a struct plus a typedef of its own name, so the bare
|
||||||
@@ -296,104 +286,6 @@ ahead of its pub forms."
|
|||||||
(test-assert "and is declared once"
|
(test-assert "and is declared once"
|
||||||
(reports? "(sum t ((a ((x int))) (a ())))" "variant is declared twice")))
|
(reports? "(sum t ((a ((x int))) (a ())))" "variant is declared twice")))
|
||||||
|
|
||||||
;; 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 typedef realizes what its type does"
|
|
||||||
(reports? (string-append (spec-src "") "(typedef alias v)(realize (p alias))")
|
|
||||||
"spec is already realized for"))
|
|
||||||
(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 docstring is not a body"
|
|
||||||
(reports? (string-append "(record v ((x int)))"
|
|
||||||
"(spec (p T) (fn s ((a (* const T))) void \"doc\"))"
|
|
||||||
"(realize (p v))")
|
|
||||||
"realization is missing a method of"))
|
|
||||||
(test-assert "nor is a comment"
|
|
||||||
(reports? (string-append "(record v ((x int)))"
|
|
||||||
"(spec (p T) (fn s ((a (* const T))) int\n"
|
|
||||||
" ;; must be implemented\n"
|
|
||||||
" ))"
|
|
||||||
"(realize (p v))")
|
|
||||||
"realization is missing a method of"))
|
|
||||||
(test-assert "a documented default is still a default"
|
|
||||||
(emits? (string-append "(record v ((x int)))"
|
|
||||||
"(spec (p T) (fn s ((a (* const T))) int \"doc\" (return 1)))"
|
|
||||||
"(realize (p v))")
|
|
||||||
"/* doc */"))
|
|
||||||
(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 "but not on how a pointer is spelled"
|
|
||||||
(emits? (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)))")
|
|
||||||
"void vːpːs (const v * a)"))
|
|
||||||
(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 an if over the tag whose arms bind the payload. What
|
;; match is an if over the tag whose arms bind the payload. What
|
||||||
;; it refuses is why it exists.
|
;; it refuses is why it exists.
|
||||||
(test-group "match"
|
(test-group "match"
|
||||||
@@ -557,17 +449,6 @@ ahead of its pub forms."
|
|||||||
(interface-reports? "(pub var n _ 42)" "write it out"))
|
(interface-reports? "(pub var n _ 42)" "write it out"))
|
||||||
(test-assert "all of it"
|
(test-assert "all of it"
|
||||||
(interface-reports? "(pub var p (* _) 0)" "write it out"))
|
(interface-reports? "(pub var p (* _) 0)" "write it out"))
|
||||||
;; a spec is seen by importers as a type is: pub, or not at all
|
|
||||||
(test-assert "a pub realization of a private spec is refused"
|
|
||||||
(interface-reports? (string-append "(spec (shown T) (fn show ((self (* const T))) int))"
|
|
||||||
"(pub record m ((c int)))"
|
|
||||||
"(pub realize (shown m) (fn show ((self (* const m))) int (return 1)))")
|
|
||||||
"importers cannot see this spec; make it pub"))
|
|
||||||
(test "and of a pub one is not"
|
|
||||||
'()
|
|
||||||
(interface-of (string-append "(pub spec (shown T) (fn show ((self (* const T))) int))"
|
|
||||||
"(pub record m ((c int)))"
|
|
||||||
"(pub realize (shown m) (fn show ((self (* const m))) int (return 1)))")))
|
|
||||||
(test "a private one is solved where it is"
|
(test "a private one is solved where it is"
|
||||||
'()
|
'()
|
||||||
(interface-of "(var n _ 42)"))
|
(interface-of "(var n _ 42)"))
|
||||||
|
|||||||
@@ -145,12 +145,6 @@
|
|||||||
(let ((err (error-of sexc "--public-interface" "gen/broken.sex")))
|
(let ((err (error-of sexc "--public-interface" "gen/broken.sex")))
|
||||||
(and err (irregex-search "broken\\.sex:8: expanding boom: nope" err))))
|
(and err (irregex-search "broken\\.sex:8: expanding boom: nope" err))))
|
||||||
|
|
||||||
;;; ...while a realization of a spec importers cannot see is refused by
|
|
||||||
;;; the module that writes it, not by an importer naming it first
|
|
||||||
(test-assert "a pub realization of a spec imported without pub is refused"
|
|
||||||
(let ((err (error-of sexc "specs/money.sex" "-C" "-o" "-")))
|
|
||||||
(and err (irregex-search "money.sex:8: importers cannot see this spec" err) #t)))
|
|
||||||
|
|
||||||
(test "the app runs through both"
|
(test "the app runs through both"
|
||||||
'("hello, world"
|
'("hello, world"
|
||||||
"hello, sex"
|
"hello, sex"
|
||||||
@@ -160,7 +154,6 @@
|
|||||||
"mood 1"
|
"mood 1"
|
||||||
"closure 15 21 201"
|
"closure 15 21 201"
|
||||||
"vec2 7 7"
|
"vec2 7 7"
|
||||||
"vec2(3 4)"
|
|
||||||
"tally 2"
|
"tally 2"
|
||||||
"board 42"
|
"board 42"
|
||||||
"ledger 2"
|
"ledger 2"
|
||||||
|
|||||||
@@ -38,8 +38,6 @@
|
|||||||
(var v vec2 #(vec2 : 3 4))
|
(var v vec2 #(vec2 : 3 4))
|
||||||
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
(printf "vec2 %d %d\n" (.manhattan v) (vec2/manhattan (& v)))
|
||||||
|
|
||||||
(.show-line v)
|
|
||||||
|
|
||||||
;; only the handle crossed: (. t n) here is an incomplete type
|
;; only the handle crossed: (. t n) here is an incomplete type
|
||||||
(var t (* tally) (tally/make))
|
(var t (* tally) (tally/make))
|
||||||
(.bump t)
|
(.bump t)
|
||||||
|
|||||||
@@ -110,20 +110,6 @@
|
|||||||
;;; called, so there is no function for two units to both define.
|
;;; called, so there is no function for two units to both define.
|
||||||
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
(pub sum colour ((rgb ((r int) (g int) (b int))) (none ())))
|
||||||
|
|
||||||
;;; A spec and a realization over the boundary. The written method
|
|
||||||
;;; travels as a prototype and keeps external linkage; the default
|
|
||||||
;;; does not travel at all -- the importer synthesizes its own static
|
|
||||||
;;; copy from the spec, which is why a default is static even here.
|
|
||||||
(pub spec (printable T)
|
|
||||||
(fn show ((self (* const T))) void)
|
|
||||||
(fn show-line ((self (* const T))) void
|
|
||||||
(.show self)
|
|
||||||
(printf "\n")))
|
|
||||||
|
|
||||||
(pub realize (printable vec2)
|
|
||||||
(fn show ((self (* const vec2))) void
|
|
||||||
(printf "vec2(%d %d)" (. self x) (. self y))))
|
|
||||||
|
|
||||||
;;; Not `pub': invisible to importers, and static in the generated C.
|
;;; Not `pub': invisible to importers, and static in the generated C.
|
||||||
(fn unused-helper () void
|
(fn unused-helper () void
|
||||||
(printf "private\n"))
|
(printf "private\n"))
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
;;; Imports shown without pub, so its importers cannot see the spec
|
|
||||||
;;; this realizes: refused here, rather than in whichever importer
|
|
||||||
;;; names money before shown
|
|
||||||
(import shown)
|
|
||||||
|
|
||||||
(pub record money ((cents int)))
|
|
||||||
|
|
||||||
(pub realize (shown money)
|
|
||||||
(fn show ((self (* const money))) int (return (. self cents))))
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
(pub spec (shown T)
|
|
||||||
(fn show ((self (* const T))) int))
|
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
"-5")
|
"-5")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; A recursive sum, an inherent method on it, and a spec realized for
|
;;; A recursive sum and an inherent method on it: every layer of
|
||||||
;;; it: every layer of encapsulation in one program, built at once.
|
;;; encapsulation in one program, built at once.
|
||||||
|
|
||||||
(include stdio.h)
|
(include stdio.h)
|
||||||
|
|
||||||
@@ -19,20 +19,15 @@
|
|||||||
(match (* self)
|
(match (* self)
|
||||||
((lit (v)) (return v))
|
((lit (v)) (return v))
|
||||||
((neg (e)) (return (- (.eval e))))
|
((neg (e)) (return (- (.eval e))))
|
||||||
((add (l r)) (return (+ (.eval l) (.eval r)))))))
|
((add (l r)) (return (+ (.eval l) (.eval r))))))
|
||||||
|
(pub fn show ((self (* const expr))) void
|
||||||
(spec (printable T)
|
|
||||||
(fn show ((self (* const T))) void)
|
|
||||||
(fn show-line ((self (* const T))) void ; default, calls the required one
|
|
||||||
(.show self)
|
|
||||||
(printf "\n")))
|
|
||||||
|
|
||||||
(realize (printable expr)
|
|
||||||
(fn show ((self (* const expr))) void
|
|
||||||
(match (* self)
|
(match (* self)
|
||||||
((lit (v)) (printf "%g" v))
|
((lit (v)) (printf "%g" v))
|
||||||
((neg (e)) (printf "-") (.show e))
|
((neg (e)) (printf "-") (.show e))
|
||||||
((add (l r)) (printf "(") (.show l) (printf " + ") (.show r) (printf ")")))))
|
((add (l r)) (printf "(") (.show l) (printf " + ") (.show r) (printf ")"))))
|
||||||
|
(pub fn show-line ((self (* const expr))) void
|
||||||
|
(.show self)
|
||||||
|
(printf "\n")))
|
||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
(var two expr (expr/lit 2.0))
|
(var two expr (expr/lit 2.0))
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
(input)
|
|
||||||
(output "$12.34"
|
|
||||||
"(3, 4)"
|
|
||||||
"max 1234"
|
|
||||||
"realizes ord: money 1, point 0"
|
|
||||||
"calls its own: 9")
|
|
||||||
(return 0)
|
|
||||||
|
|
||||||
;;; A spec is a contract and emits nothing; a realization is the
|
|
||||||
;;; spec's methods for one type, mangled typeːspecːmethod. A default
|
|
||||||
;;; is copied into every realizing type with the parameter
|
|
||||||
;;; substituted, so the `show' it calls is that type's own.
|
|
||||||
;;;
|
|
||||||
;;; What the layer is for is the last line: a generic asks `realizes?'
|
|
||||||
;;; before it expands, and says so itself rather than leaving a wall
|
|
||||||
;;; of C errors two stages downstream.
|
|
||||||
|
|
||||||
(include stdio.h)
|
|
||||||
|
|
||||||
(record money ((cents int)))
|
|
||||||
(record point ((x int) (y int)))
|
|
||||||
|
|
||||||
(spec (printable T)
|
|
||||||
(fn show ((self (* const T))) void)
|
|
||||||
(fn show-line ((self (* const T))) void ; default, calls the required one
|
|
||||||
(.show self)
|
|
||||||
(printf "\n")))
|
|
||||||
|
|
||||||
(spec (ord T)
|
|
||||||
(fn less ((a (* const T)) (b (* const T))) bool))
|
|
||||||
|
|
||||||
(realize (printable money)
|
|
||||||
(fn show ((self (* const money))) void
|
|
||||||
(printf "$%d.%02d" (/ (. self cents) 100) (% (. self cents) 100))))
|
|
||||||
|
|
||||||
(realize (printable point)
|
|
||||||
(fn show ((self (* const point))) void
|
|
||||||
(printf "(%d, %d)" (. self x) (. self y))))
|
|
||||||
|
|
||||||
(realize (ord money)
|
|
||||||
(fn less ((a (* const money)) (b (* const money))) bool
|
|
||||||
(return (< (. a cents) (. b cents)))))
|
|
||||||
|
|
||||||
;;; A bounded generic: the macro refuses before it expands
|
|
||||||
(defmacro (max-of type)
|
|
||||||
(unless (realizes? 'ord type)
|
|
||||||
(error "does not realize ord" type))
|
|
||||||
`(fn ,(cat 'max- type) ((a (* const ,type)) (b (* const ,type))) ,type
|
|
||||||
(if (,(cat type '/ord/less) a b)
|
|
||||||
(return (* b))
|
|
||||||
(return (* a)))))
|
|
||||||
|
|
||||||
(max-of money)
|
|
||||||
|
|
||||||
(defmacro (ord? type) (if (realizes? 'ord type) 1 0))
|
|
||||||
|
|
||||||
;;; A realization's methods may call one another, whatever order they
|
|
||||||
;;; come in: `total', written, calls the default `doubled', which calls
|
|
||||||
;;; the later default `plus-one', which calls `base', written below them
|
|
||||||
(spec (sized T)
|
|
||||||
(fn total ((self (* const T))) int)
|
|
||||||
(fn doubled ((self (* const T))) int (return (* 2 (.plus-one self))))
|
|
||||||
(fn plus-one ((self (* const T))) int (return (+ 1 (.base self))))
|
|
||||||
(fn base ((self (* const T))) int))
|
|
||||||
|
|
||||||
(realize (sized point)
|
|
||||||
(fn total ((self (* const point))) int (return (+ 1 (.doubled self))))
|
|
||||||
(fn base ((self (* const point))) int (return (. self x))))
|
|
||||||
|
|
||||||
(pub fn main () int
|
|
||||||
(var m money #(money : 1234))
|
|
||||||
(var p point #(point : 3 4))
|
|
||||||
|
|
||||||
(.show-line m) ; the default, through the registry
|
|
||||||
(point/printable/show-line (& p)) ; and spelled out
|
|
||||||
|
|
||||||
(var cheap money #(money : 150))
|
|
||||||
(var big money (max-money (& m) (& cheap)))
|
|
||||||
(printf "max %d\n" (. big cents))
|
|
||||||
|
|
||||||
(printf "realizes ord: money %d, point %d\n" (ord? money) (ord? point))
|
|
||||||
(printf "calls its own: %d\n" (.total p))
|
|
||||||
(return 0))
|
|
||||||
39
types.scm
39
types.scm
@@ -34,12 +34,6 @@
|
|||||||
variant-fields
|
variant-fields
|
||||||
reset-declarations!
|
reset-declarations!
|
||||||
|
|
||||||
add-spec!
|
|
||||||
get-spec
|
|
||||||
spec-parameter
|
|
||||||
spec-methods
|
|
||||||
add-realization!
|
|
||||||
realizes?
|
|
||||||
normalize-fields
|
normalize-fields
|
||||||
|
|
||||||
add-method!
|
add-method!
|
||||||
@@ -324,43 +318,14 @@
|
|||||||
((memq (car type) '(const volatile)) (ops-key (cadr type)))
|
((memq (car type) '(const volatile)) (ops-key (cadr type)))
|
||||||
(else #f)))
|
(else #f)))
|
||||||
|
|
||||||
;;; A spec is a parameter and a list of methods, each of which either
|
;;; Sums, constructors and methods refuse a second declaration
|
||||||
;;; declares a signature or also carries a default body:
|
|
||||||
;;;
|
|
||||||
;;; (spec (printable T) (fn show ((self (* const T))) void))
|
|
||||||
;;; -> printable -> (T (fn show ((self (* const T))) void))
|
|
||||||
;;;
|
|
||||||
;;; And a realization is only ever asked whether it exists -- the
|
|
||||||
;;; methods it provides land in +ops-db+ like any others.
|
|
||||||
(define +spec-db+ (make-hash-table))
|
|
||||||
(define +realize-db+ (make-hash-table))
|
|
||||||
|
|
||||||
(define (add-spec! name parameter methods)
|
|
||||||
(hash-table-set! +spec-db+ name (cons parameter methods)))
|
|
||||||
|
|
||||||
(define (get-spec name)
|
|
||||||
(hash-table-ref/default +spec-db+ name #f))
|
|
||||||
|
|
||||||
(define (spec-parameter spec) (car spec))
|
|
||||||
(define (spec-methods spec) (cdr spec))
|
|
||||||
|
|
||||||
(define (add-realization! spec type)
|
|
||||||
(hash-table-set! +realize-db+ (cons spec (type-key type)) #t))
|
|
||||||
|
|
||||||
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
|
|
||||||
(define (realizes? spec type)
|
|
||||||
(known type (hash-table-exists? +realize-db+ (cons spec (type-key type)))))
|
|
||||||
|
|
||||||
;;; Sums, constructors, methods and specs refuse a second declaration
|
|
||||||
;;; of the same name, which is per translation unit rather than per
|
;;; of the same name, which is per translation unit rather than per
|
||||||
;;; process -- so a process that compiles two units has to start the
|
;;; process -- so a process that compiles two units has to start the
|
||||||
;;; second one clean.
|
;;; second one clean.
|
||||||
(define (reset-declarations!)
|
(define (reset-declarations!)
|
||||||
(set! +sum-db+ (make-hash-table))
|
(set! +sum-db+ (make-hash-table))
|
||||||
(set! +constructor-db+ (make-hash-table))
|
(set! +constructor-db+ (make-hash-table))
|
||||||
(set! +ops-db+ (make-hash-table))
|
(set! +ops-db+ (make-hash-table)))
|
||||||
(set! +spec-db+ (make-hash-table))
|
|
||||||
(set! +realize-db+ (make-hash-table)))
|
|
||||||
|
|
||||||
;;; Type matcher macro
|
;;; Type matcher macro
|
||||||
;;; (type-match type
|
;;; (type-match type
|
||||||
|
|||||||
Reference in New Issue
Block a user