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:
31
types.scm
31
types.scm
@@ -109,12 +109,41 @@
|
||||
(let ((entry (assq name variants)))
|
||||
(and entry (cdr entry))))
|
||||
|
||||
;;; A spec is a parameter and a list of methods, each of which either
|
||||
;;; 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) #t))
|
||||
|
||||
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
|
||||
(define (realizes? spec type)
|
||||
(hash-table-exists? +realize-db+ (cons spec type)))
|
||||
|
||||
;;; Both of these refuse a second declaration of the same name, which
|
||||
;;; is per translation unit rather than per process -- so a process
|
||||
;;; that compiles two units has to start the second one clean.
|
||||
(define (reset-declarations!)
|
||||
(set! +sum-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)))
|
||||
|
||||
;;; Methods of an `ops' block: (type . method) -> the qualified name
|
||||
;;; the call is rewritten to.
|
||||
|
||||
Reference in New Issue
Block a user