Compare commits
4 Commits
0.0.1
...
1c6d0aae62
| Author | SHA1 | Date | |
|---|---|---|---|
| 1c6d0aae62 | |||
| 9a3c01e96b | |||
| 16a1c3b487 | |||
| 673787837c |
@@ -215,11 +215,6 @@ forms, and what remains."
|
||||
acc))))))
|
||||
(else (loop (cdr fs) (cons (car fs) acc))))))
|
||||
|
||||
(define (walk-generic-toplevel form)
|
||||
(cond ((atom? form) (atom-to-fmt-c form))
|
||||
((list? form) (map walk-generic-toplevel form))
|
||||
(else (sex-error form "malformed form" form))))
|
||||
|
||||
(define (walk-expr form)
|
||||
(match form
|
||||
((? vector?) (walk-initializer (vector->list form)))
|
||||
|
||||
304
infer.scm
304
infer.scm
@@ -1,8 +1,9 @@
|
||||
;;; Type inference, layer 0: the type representation and unification.
|
||||
;;;
|
||||
;;; Nothing in the compiler calls this unit yet. It is the ground floor
|
||||
;;; of the pass described in Type-inference.org -- built and tested on
|
||||
;;; its own before a single form is routed through it.
|
||||
;;; semen solves the wildcards of a declared type with it -- `(var n _
|
||||
;;; (strlen s))', `(var p (* _) (& x))' -- and asks it what an operand
|
||||
;;; decays or promotes to. It is the ground floor of the pass described
|
||||
;;; in Type-inference.org.
|
||||
;;;
|
||||
;;; Two representations meet here. *Surface* types are the forms the
|
||||
;;; rest of the compiler passes around -- `int', `(* const char)',
|
||||
@@ -28,54 +29,23 @@
|
||||
(chicken module)
|
||||
matchable
|
||||
srfi-1
|
||||
srfi-69
|
||||
types
|
||||
utils)
|
||||
|
||||
(export
|
||||
;; The IR
|
||||
tvar?
|
||||
tvar-id
|
||||
tvar-classes
|
||||
tvar-rigid?
|
||||
fresh-tvar
|
||||
fresh-rigid-tvar
|
||||
|
||||
prim-type? prim-name prim-quals make-prim
|
||||
ptr-type? ptr-target ptr-quals make-ptr
|
||||
array-type? array-elt array-size make-array-type
|
||||
fn-type? fn-ret fn-args fn-variadic? make-fn-type
|
||||
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
|
||||
alias-type? alias-name alias-expansion alias-quals make-alias
|
||||
unknown-type? the-unknown-type
|
||||
|
||||
resolve
|
||||
prim-type? prim-name
|
||||
ptr-type?
|
||||
array-type? array-elt
|
||||
underlying
|
||||
c-primitive?
|
||||
type-quals
|
||||
free-tvars
|
||||
decay
|
||||
|
||||
;; The boundary
|
||||
parse-type
|
||||
unparse-type
|
||||
|
||||
;; Constraints
|
||||
register-class!
|
||||
add-instance!
|
||||
entails?
|
||||
default-tvar!
|
||||
default-type-variables!
|
||||
|
||||
;; Unification
|
||||
unify
|
||||
|
||||
;; Type schemes
|
||||
scheme? scheme-vars scheme-constraints scheme-type
|
||||
make-scheme
|
||||
generalize
|
||||
instantiate
|
||||
substitute)
|
||||
unify)
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
;;; The IR
|
||||
@@ -84,26 +54,16 @@
|
||||
;;; A type variable is a mutable cell. `ref' is #f while unsolved and
|
||||
;;; the type it stands for once bound -- union-find, with the path
|
||||
;;; compression done in `resolve'.
|
||||
;;;
|
||||
;;; `classes' is the list of type classes the variable must satisfy
|
||||
;;; (`numeric', and one day `ord'); see "constraints" below. `rigid?'
|
||||
;;; marks a variable that must not unify with anything but itself --
|
||||
;;; unused until a `fn' grows type parameters, and five lines now
|
||||
;;; against an IR change later.
|
||||
(define-record-type <tvar>
|
||||
(%make-tvar id ref classes rigid?)
|
||||
(%make-tvar ref)
|
||||
tvar?
|
||||
(id tvar-id)
|
||||
(ref tvar-ref tvar-ref-set!)
|
||||
(classes tvar-classes tvar-classes-set!)
|
||||
(rigid? tvar-rigid?))
|
||||
(ref tvar-ref tvar-ref-set!))
|
||||
|
||||
;;; A primitive or otherwise nominal type. `name' is the list of words
|
||||
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all
|
||||
;;; one node, and so is a name we have never parsed a declaration for
|
||||
;;; (`size-t', `GLuint'). The two cases are told apart by
|
||||
;;; `c-primitive?', which is what keeps a constraint over an unparsed C
|
||||
;;; typedef from being an error.
|
||||
;;; `c-primitive?'.
|
||||
(define-record-type <prim>
|
||||
(make-prim name quals)
|
||||
prim-type?
|
||||
@@ -153,25 +113,8 @@
|
||||
(expansion alias-expansion)
|
||||
(quals alias-quals))
|
||||
|
||||
;;; `?'. Sex has full C interop, so `printf', `SDL-CreateWindow' and
|
||||
;;; `size-t' arrive from headers nobody parsed. Rather than reject
|
||||
;;; every real program, the lattice gets a top element: `?' is
|
||||
;;; consistent with every type and constrains nothing.
|
||||
(define-record-type <unknown>
|
||||
(%make-unknown)
|
||||
unknown-type?)
|
||||
|
||||
(define the-unknown-type (%make-unknown))
|
||||
|
||||
(define tvar-counter 0)
|
||||
|
||||
(define (fresh-tvar . classes)
|
||||
(set! tvar-counter (+ tvar-counter 1))
|
||||
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #f))
|
||||
|
||||
(define (fresh-rigid-tvar . classes)
|
||||
(set! tvar-counter (+ tvar-counter 1))
|
||||
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #t))
|
||||
(define (fresh-tvar)
|
||||
(%make-tvar #f))
|
||||
|
||||
;;; Follow a bound variable to what it stands for, compressing the path
|
||||
;;; on the way out. Every procedure that looks at a type's shape starts
|
||||
@@ -207,16 +150,6 @@
|
||||
((fn-type? t) (make-ptr t (list)))
|
||||
(else (resolve type)))))
|
||||
|
||||
(define (free-tvars type)
|
||||
(let collect ((t type) (acc (list)))
|
||||
(let ((t (resolve t)))
|
||||
(cond ((tvar? t) (if (memq t acc) acc (cons t acc)))
|
||||
((ptr-type? t) (collect (ptr-target t) acc))
|
||||
((array-type? t) (collect (array-elt t) acc))
|
||||
((alias-type? t) (collect (alias-expansion t) acc))
|
||||
((fn-type? t) (fold collect (collect (fn-ret t) acc) (fn-args t)))
|
||||
(else acc)))))
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
;;; Surface -> IR
|
||||
;;; ---------------------------------------------------------------
|
||||
@@ -357,7 +290,6 @@
|
||||
(let ((t (resolve type)))
|
||||
(cond
|
||||
((tvar? t) '_)
|
||||
((unknown-type? t) '?)
|
||||
((alias-type? t) (qualify (alias-quals t) (list (alias-name t))))
|
||||
((prim-type? t) (qualify (prim-quals t) (prim-name t)))
|
||||
((agg-type? t) (qualify (agg-quals t) (agg-spelling t)))
|
||||
@@ -397,69 +329,9 @@
|
||||
(else surface)))
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
;;; Constraints
|
||||
;;; C's own types
|
||||
;;; ---------------------------------------------------------------
|
||||
|
||||
;;; `(numeric a)' is already a type class, so it is written as one from
|
||||
;;; the start: one representation, one table, one entailment check. A
|
||||
;;; trait bound `(ord (struct circle))' is the same shape, discharged
|
||||
;;; the same way, and reported by the same procedure -- which is the
|
||||
;;; whole reason to build it this way while there is only one kind of
|
||||
;;; constraint to build.
|
||||
;;;
|
||||
;;; `default' is the type an unresolved constraint falls back to, the
|
||||
;;; way Haskell defaults `Num a' to Integer. `test' is how the built-in
|
||||
;;; classes say "every arithmetic type" without enumerating twenty
|
||||
;;; spellings as instances; a user trait has no test and lives entirely
|
||||
;;; in the instance table. `strict?' marks a class that must not be
|
||||
;;; guessed at: static dispatch needs a real instance, so `?' fails it.
|
||||
(define-record-type <type-class>
|
||||
(%make-type-class name default test strict?)
|
||||
type-class?
|
||||
(name type-class-name)
|
||||
(default type-class-default)
|
||||
(test type-class-test)
|
||||
(strict? type-class-strict?))
|
||||
|
||||
(define +classes+ (make-hash-table))
|
||||
(define +instances+ (make-hash-table))
|
||||
|
||||
(define (register-class! name default test strict?)
|
||||
(hash-table-set! +classes+ name (%make-type-class name default test strict?)))
|
||||
|
||||
(define (get-class name)
|
||||
(or (hash-table-ref/default +classes+ name #f)
|
||||
(error "no such type class" name)))
|
||||
|
||||
;;; Instances key on the *resolved* type, so `(impl show for size-t)'
|
||||
;;; and `(impl show for unsigned long)' collide rather than quietly
|
||||
;;; coexisting as two instances of one C type.
|
||||
(define (instance-key type)
|
||||
(unparse-type (underlying type)))
|
||||
|
||||
(define (add-instance! class-name type)
|
||||
(hash-table-set! +instances+ (cons class-name (instance-key type)) #t))
|
||||
|
||||
(define (has-instance? class-name type)
|
||||
(hash-table-exists? +instances+ (cons class-name (instance-key type))))
|
||||
|
||||
;;; #t, #f, or 'unknown -- and the third answer is the important one.
|
||||
;;; A C name we never parsed a declaration for might well be numeric;
|
||||
;;; saying #f there would reject working programs, and saying #t would
|
||||
;;; invent knowledge. 'unknown means "do not constrain, do not
|
||||
;;; complain".
|
||||
(define (entails? class-name type)
|
||||
(let ((cls (get-class class-name))
|
||||
(t (underlying type)))
|
||||
(cond
|
||||
((tvar? t) 'unknown)
|
||||
;; `?' is consistent with every type, but it entails nothing:
|
||||
;; there is no instance to select and no name to mangle.
|
||||
((unknown-type? t) (if (type-class-strict? cls) #f 'unknown))
|
||||
((has-instance? class-name t) #t)
|
||||
((type-class-test cls) => (lambda (test) (test t)))
|
||||
(else #f))))
|
||||
|
||||
(define +integer-words+ '(char short int long signed unsigned bool _Bool))
|
||||
(define +float-words+ '(float double))
|
||||
(define +known-words+ (append '(void) +integer-words+ +float-words+))
|
||||
@@ -470,97 +342,18 @@
|
||||
(and (prim-type? t)
|
||||
(every (lambda (word) (memq word +known-words+)) (prim-name t))))
|
||||
|
||||
(define (void-type? t)
|
||||
(and (prim-type? t) (equal? (prim-name t) '(void))))
|
||||
|
||||
(define (arithmetic-type? t)
|
||||
(cond ((and (agg-type? t) (eq? (agg-kind t) 'enum)) #t) ; an enum is an integer
|
||||
((not (prim-type? t)) #f)
|
||||
((not (c-primitive? t)) 'unknown)
|
||||
((void-type? t) #f)
|
||||
(else #t)))
|
||||
|
||||
(define (integral-type? t)
|
||||
(cond ((and (agg-type? t) (eq? (agg-kind t) 'enum)) #t)
|
||||
((not (prim-type? t)) #f)
|
||||
((not (c-primitive? t)) 'unknown)
|
||||
((void-type? t) #f)
|
||||
((any (lambda (word) (memq word +float-words+)) (prim-name t)) #f)
|
||||
(else #t)))
|
||||
|
||||
(define (floating-type? t)
|
||||
(cond ((not (prim-type? t)) #f)
|
||||
((not (c-primitive? t)) 'unknown)
|
||||
(else (and (any (lambda (word) (memq word +float-words+)) (prim-name t))
|
||||
#t))))
|
||||
|
||||
(define (scalar-type? t)
|
||||
(cond ((ptr-type? t) #t)
|
||||
((array-type? t) #t) ; decays to one
|
||||
((fn-type? t) #t) ; likewise
|
||||
(else (arithmetic-type? t))))
|
||||
|
||||
;;; The built-ins. They are ordinary classes, registered the same way a
|
||||
;;; trait will be -- that is the point.
|
||||
(register-class! 'numeric 'int arithmetic-type? #f)
|
||||
(register-class! 'integral 'int integral-type? #f)
|
||||
(register-class! 'floating 'double floating-type? #f)
|
||||
(register-class! 'scalar #f scalar-type? #f)
|
||||
|
||||
;;; A constraint that survives to the end of a function is defaulted:
|
||||
;;; `(numeric a)' with nothing else known is an `int'. A *strict*
|
||||
;;; class has no default and no business guessing, so an unresolved one
|
||||
;;; is an error -- the rule is worth stating while there is only one
|
||||
;;; kind of constraint to state it about.
|
||||
(define (default-tvar! v form)
|
||||
(let ((strict (find (lambda (c) (type-class-strict? (get-class c)))
|
||||
(tvar-classes v))))
|
||||
(cond
|
||||
(strict (sex-error form "unresolved constraint" (list strict (unparse-type v))))
|
||||
((find (lambda (c) (type-class-default (get-class c))) (tvar-classes v))
|
||||
=> (lambda (c)
|
||||
(tvar-ref-set! v (parse-type (type-class-default (get-class c))))
|
||||
#t))
|
||||
(else #f))))
|
||||
|
||||
;;; Default every variable still open in TYPE. Returns #t when none is
|
||||
;;; left unsolved, so a caller can tell "inferred" from "give up and
|
||||
;;; ask for the type in writing".
|
||||
(define (default-type-variables! type form)
|
||||
(fold (lambda (v ok) (and (default-tvar! v form) ok))
|
||||
#t
|
||||
(free-tvars type)))
|
||||
|
||||
(define (check-classes classes type form)
|
||||
(for-each
|
||||
(lambda (c)
|
||||
(when (eq? #f (entails? c type))
|
||||
(sex-error form "type does not satisfy a constraint"
|
||||
(list c (unparse-type type)))))
|
||||
classes))
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
;;; Unification
|
||||
;;; ---------------------------------------------------------------
|
||||
|
||||
;;; Consistency in the gradual-typing sense rather than equality: `?'
|
||||
;;; succeeds against anything and binds nothing, which is what keeps
|
||||
;;; the pass from rejecting every program that includes a C header.
|
||||
;;;
|
||||
;;; FORM is carried only so a failure can say where it was written.
|
||||
(define (unify t1 t2 form)
|
||||
(let ((a (resolve t1))
|
||||
(b (resolve t2)))
|
||||
(cond
|
||||
((eq? a b) #t)
|
||||
((unknown-type? a) #t)
|
||||
((unknown-type? b) #t)
|
||||
;; Whichever side is free takes the binding: `(unify a r)' and
|
||||
;; `(unify r a)' both leave `a' bound to `r'. Two rigid and
|
||||
;; distinct is the mismatch `eq?' above let through.
|
||||
((and (tvar? a) (not (tvar-rigid? a))) (bind-tvar! a b form))
|
||||
((and (tvar? b) (not (tvar-rigid? b))) (bind-tvar! b a form))
|
||||
((or (tvar? a) (tvar? b)) (type-mismatch a b form))
|
||||
((tvar? a) (bind-tvar! a b form))
|
||||
((tvar? b) (bind-tvar! b a form))
|
||||
;; A typedef unifies as what it stands for. Its name survives in
|
||||
;; whichever side is printed later, since neither side is rebuilt.
|
||||
((alias-type? a) (unify (alias-expansion a) b form))
|
||||
@@ -614,16 +407,7 @@
|
||||
;; Without recursive types this cannot trigger. It is four lines,
|
||||
;; and the alternative to having it is a hang.
|
||||
((occurs? v t) (sex-error form "recursive type" (unparse-type v)))
|
||||
;; A rigid variable is a type *parameter*: inside a generic body it
|
||||
;; stands for one specific unknown type and must not be solved.
|
||||
((tvar-rigid? v) (type-mismatch v t form))
|
||||
(else
|
||||
(when (tvar? t)
|
||||
(tvar-classes-set! t (lset-union eq? (tvar-classes t) (tvar-classes v))))
|
||||
(tvar-ref-set! v t)
|
||||
(unless (tvar? t)
|
||||
(check-classes (tvar-classes v) t form))
|
||||
#t)))
|
||||
(else (tvar-ref-set! v t) #t)))
|
||||
|
||||
(define (occurs? v type)
|
||||
(let ((t (resolve type)))
|
||||
@@ -634,57 +418,3 @@
|
||||
((fn-type? t) (or (occurs? v (fn-ret t))
|
||||
(any (lambda (a) (occurs? v a)) (fn-args t))))
|
||||
(else #f))))
|
||||
|
||||
;;; ---------------------------------------------------------------
|
||||
;;; Type schemes
|
||||
;;; ---------------------------------------------------------------
|
||||
|
||||
;;; Nothing generalizes yet -- every `fn' in Sex carries a written
|
||||
;;; signature and there is no polymorphism to abstract over. These are
|
||||
;;; here because they are ten lines on top of unification and because
|
||||
;;; they are exactly what a `fn' with type parameters needs, and
|
||||
;;; because a scheme without a constraint list is the wrong shape for
|
||||
;;; every bounded generic. `(forall vars constraints type)' it is,
|
||||
;;; from the start.
|
||||
(define-record-type <scheme>
|
||||
(make-scheme vars constraints type)
|
||||
scheme?
|
||||
(vars scheme-vars)
|
||||
(constraints scheme-constraints)
|
||||
(type scheme-type))
|
||||
|
||||
;;; Quantify over everything free in TYPE that is not also free in the
|
||||
;;; environment, carrying each variable's class constraints along as
|
||||
;;; the scheme's context.
|
||||
(define (generalize type env-tvars)
|
||||
(let ((vars (lset-difference eq? (free-tvars type) env-tvars)))
|
||||
(make-scheme vars
|
||||
(append-map (lambda (v)
|
||||
(map (lambda (c) (cons c v)) (tvar-classes v)))
|
||||
vars)
|
||||
type)))
|
||||
|
||||
(define (instantiate scheme)
|
||||
(let ((subst (map (lambda (v) (cons v (fresh-tvar (tvar-classes v))))
|
||||
(scheme-vars scheme))))
|
||||
(substitute (scheme-type scheme) subst)))
|
||||
|
||||
;;; Structural copy with the variables in SUBST replaced. Copying is
|
||||
;;; how a generic body must be handled anyway -- `form-type' is keyed
|
||||
;;; by cons cell, one form one type, so an instantiation gets fresh
|
||||
;;; cells rather than a second type for the same cell.
|
||||
(define (substitute type subst)
|
||||
(let ((t (resolve type)))
|
||||
(cond
|
||||
((tvar? t) (let ((hit (assq t subst))) (if hit (cdr hit) t)))
|
||||
((ptr-type? t) (make-ptr (substitute (ptr-target t) subst) (ptr-quals t)))
|
||||
((array-type? t) (make-array-type (substitute (array-elt t) subst)
|
||||
(array-size t)))
|
||||
((alias-type? t) (make-alias (alias-name t)
|
||||
(substitute (alias-expansion t) subst)
|
||||
(alias-quals t)))
|
||||
((fn-type? t) (make-fn-type (substitute (fn-ret t) subst)
|
||||
(map (lambda (a) (substitute a subst))
|
||||
(fn-args t))
|
||||
(fn-variadic? t)))
|
||||
(else t))))
|
||||
|
||||
@@ -1181,16 +1181,12 @@
|
||||
;;; member access.
|
||||
|
||||
;;; The type of an expression as a surface type, or #f when nothing
|
||||
;;; here can say. Every form it visits is recorded in `form-type', so
|
||||
;;; asking once types the whole subtree.
|
||||
;;; here can say.
|
||||
;;;
|
||||
;;; 42 -> int (. p x) -> that field's type
|
||||
;;; "hi" -> (* const char) (& p) -> (* (struct point))
|
||||
;;; (area 3 4) -> what `area' returns
|
||||
(define (expression-type expr env)
|
||||
(set-form-type! expr (compute-expression-type expr env)))
|
||||
|
||||
(define (compute-expression-type expr env)
|
||||
(cond
|
||||
((and (number? expr) (exact? expr)) 'int)
|
||||
((number? expr) 'double)
|
||||
@@ -1948,9 +1944,6 @@
|
||||
form))
|
||||
|
||||
;;; Utils
|
||||
(define (non-empty-list? form)
|
||||
(and (list? form)
|
||||
(not (null? form))))
|
||||
|
||||
(define (sex-fn? form)
|
||||
"The `form` must be toplevel.
|
||||
|
||||
@@ -7,10 +7,8 @@
|
||||
(chicken string))
|
||||
|
||||
(export
|
||||
register-macro
|
||||
cat
|
||||
comment
|
||||
get-macro
|
||||
macro?
|
||||
apply-macro
|
||||
defmacro)
|
||||
|
||||
@@ -564,6 +564,19 @@
|
||||
"struct p* x = &s"))
|
||||
(test-assert "a wildcard inside an array"
|
||||
(emits? (in-fn "(var t [_ 3] #([int 3] : 1 2 3))") "int t[3]"))
|
||||
;; each `_' is its own variable: solving one leaves the rest open
|
||||
(test-assert "every wildcard in a signature is its own"
|
||||
(emits? "(fn g ((x int)) char (return 0))
|
||||
(fn f () void (var h (fn ((_)) _) g))"
|
||||
"char (*h)(int) = g"))
|
||||
(test-assert "and the signature's arity has to match"
|
||||
(reports? "(fn g ((x int) (y int)) int (return 0))
|
||||
(fn f () void (var h (fn ((_)) _) g))"
|
||||
"type mismatch"))
|
||||
(test-assert "a typedef answers as itself"
|
||||
(emits? "(typedef count int) (fn g () count (return 0))
|
||||
(fn f () void (var x _ (g)))"
|
||||
"count x = g()"))
|
||||
;; a compound literal carries its own type, where a brace
|
||||
;; initializer has none and takes one from its context
|
||||
(test-assert "a compound literal answers a bare wildcard"
|
||||
|
||||
190
tests/infer.scm
190
tests/infer.scm
@@ -3,7 +3,7 @@
|
||||
;;; Names registered in the type database are prefixed, since the
|
||||
;;; database is one table shared by every suite in the linked binary.
|
||||
|
||||
(import infer types (chicken sort))
|
||||
(import infer types)
|
||||
|
||||
;;; Parse and print a surface type again. Everything in this suite goes
|
||||
;;; through this pair, which is deliberate: they are the only thing the
|
||||
@@ -85,23 +85,10 @@
|
||||
|
||||
;; Each _ is its own variable: solving one must not solve the rest.
|
||||
(let ((t (parse-type '(fn ((_)) _))))
|
||||
(unify (car (fn-args t)) (parse-type 'int) #f)
|
||||
(unify t (parse-type '(fn ((int)) _)) #f)
|
||||
(test "one hole at a time" '(fn ((int)) _) (unparse-type t))))
|
||||
|
||||
(test-group "structure"
|
||||
(test-assert "a pointer is a pointer" (ptr-type? (parse-type '(* char))))
|
||||
(test "and knows what it points at" 'char
|
||||
(unparse-type (ptr-target (parse-type '(* char)))))
|
||||
(test "quals sit on the level they were written at" '(const)
|
||||
(ptr-quals (parse-type '(const * char))))
|
||||
(test "an unsized array has no size" #f (array-size (parse-type '(¤ int))))
|
||||
(test "a sized one does" 16 (array-size (parse-type '(¤ int 16))))
|
||||
(test "an aggregate is nominal" 'point (agg-name (parse-type '(struct point))))
|
||||
(test-assert "a variadic signature says so"
|
||||
(fn-variadic? (parse-type '(fn ((* const char) (...)) int))))
|
||||
(test-assert "and a plain one does not"
|
||||
(not (fn-variadic? (parse-type '(fn ((int)) int)))))
|
||||
|
||||
(test-group "decay"
|
||||
;; decay: the conversion C performs at a call site, an operand of
|
||||
;; `+', or the left half of a subscript.
|
||||
(test "an array decays to a pointer" '(* int) (unparse-type (decay (parse-type '(¤ int 16)))))
|
||||
@@ -115,7 +102,7 @@
|
||||
(test-error "and not with another one"
|
||||
(unify (parse-type 'int) (parse-type 'char) #f))
|
||||
|
||||
(let ((a (fresh-tvar)))
|
||||
(let ((a (parse-type '_)))
|
||||
(unify a (parse-type '(* const char)) #f)
|
||||
(test "a variable takes the shape it is unified with"
|
||||
'(* const char) (unparse-type a)))
|
||||
@@ -125,8 +112,8 @@
|
||||
(unify p (parse-type '(* int)) #f)
|
||||
(test "a partial type is completed by one step" '(* int) (unparse-type p)))
|
||||
|
||||
(let ((a (fresh-tvar))
|
||||
(b (fresh-tvar)))
|
||||
(let ((a (parse-type '_))
|
||||
(b (parse-type '_)))
|
||||
(unify a b #f)
|
||||
(unify b (parse-type 'double) #f)
|
||||
(test "two variables joined then solved" 'double (unparse-type a)))
|
||||
@@ -152,168 +139,7 @@
|
||||
(add-typedef 'i-count '(typedef i-count int))
|
||||
(test-assert "a typedef unifies with its target"
|
||||
(unify (parse-type 'i-count) (parse-type 'int) #f))
|
||||
(let ((a (fresh-tvar)))
|
||||
(let ((a (parse-type '_)))
|
||||
(unify a (parse-type 'i-count) #f)
|
||||
(test "and keeps its name when it is the one printed"
|
||||
'i-count (unparse-type a)))
|
||||
|
||||
(test-group "the unknown type"
|
||||
(test-assert "? is consistent with anything"
|
||||
(unify the-unknown-type (parse-type '(struct point)) #f))
|
||||
(test-assert "in either order"
|
||||
(unify (parse-type 'int) the-unknown-type #f))
|
||||
;; ...and binds nothing. Degrading to ? is what keeps an
|
||||
;; unparsed C declaration from poisoning everything it touches.
|
||||
(let ((a (fresh-tvar)))
|
||||
(unify a the-unknown-type #f)
|
||||
(test "a variable met with ? stays open" '_ (unparse-type a))))
|
||||
|
||||
(test-group "occurs check"
|
||||
;; Unreachable without recursive types, and the alternative to
|
||||
;; having it is not an error but a hang.
|
||||
(let ((a (fresh-tvar)))
|
||||
(test-error "a variable may not contain itself"
|
||||
(unify a (make-ptr a (list)) #f))))
|
||||
|
||||
(test-group "rigid variables"
|
||||
(let ((r (fresh-rigid-tvar))
|
||||
(a (fresh-tvar)))
|
||||
(test-error "a type parameter does not unify with a type"
|
||||
(unify r (parse-type 'int) #f))
|
||||
(test-assert "an ordinary variable binds to it instead"
|
||||
(unify a r #f))
|
||||
;; An unsolved variable resolves to itself.
|
||||
(test-assert "it is still open" (tvar? (resolve r))))
|
||||
;; ...and the same the other way round: it is which side is free
|
||||
;; that decides, not which side was written first.
|
||||
(let ((r (fresh-rigid-tvar))
|
||||
(a (fresh-tvar)))
|
||||
(test-assert "rigid first binds the free one" (unify r a #f))
|
||||
(test-assert "to the parameter itself" (eq? r (resolve a))))
|
||||
(let ((r1 (fresh-rigid-tvar))
|
||||
(r2 (fresh-rigid-tvar)))
|
||||
(test-error "two parameters do not unify with each other"
|
||||
(unify r1 r2 #f)))))
|
||||
|
||||
(test-group "constraints"
|
||||
(test #t (entails? 'numeric (parse-type 'int)))
|
||||
(test #t (entails? 'numeric (parse-type '(unsigned long))))
|
||||
(test #t (entails? 'integral (parse-type 'char)))
|
||||
(test #f (entails? 'integral (parse-type 'double)))
|
||||
(test #t (entails? 'floating (parse-type 'double)))
|
||||
(test #f (entails? 'floating (parse-type 'int)))
|
||||
(test #f (entails? 'numeric (parse-type '(* char))))
|
||||
(test #t (entails? 'scalar (parse-type '(* char))))
|
||||
(test #f (entails? 'numeric (parse-type 'void)))
|
||||
|
||||
(add-enum 'i-mood '(enum i-mood (glad sad)))
|
||||
(test "an enum is an integer" #t (entails? 'integral (parse-type '(enum i-mood))))
|
||||
|
||||
;; The third answer, and the important one. A name from a header
|
||||
;; might well be numeric; #f would reject working programs and #t
|
||||
;; would invent knowledge.
|
||||
(test "an unparsed C name is not known either way"
|
||||
'unknown (entails? 'numeric (parse-type 'size-t)))
|
||||
(test "nor is an open variable" 'unknown (entails? 'numeric (fresh-tvar)))
|
||||
(test "? entails nothing, but says so quietly"
|
||||
'unknown (entails? 'numeric the-unknown-type))
|
||||
|
||||
;; A typedef is entailed by what it resolves to, so an alias cannot
|
||||
;; sneak past a constraint its target would fail.
|
||||
(add-typedef 'i-len '(typedef i-len int))
|
||||
(test "a typedef is judged by its target" #t (entails? 'numeric (parse-type 'i-len)))
|
||||
|
||||
;; A constrained variable checks its classes at the moment it is
|
||||
;; solved, not at the end.
|
||||
(let ((a (fresh-tvar '(numeric))))
|
||||
(test-error "solving to a type that fails the class is an error"
|
||||
(unify a (parse-type '(* char)) #f)))
|
||||
(let ((a (fresh-tvar '(numeric))))
|
||||
(test-assert "and to one that satisfies it is not"
|
||||
(unify a (parse-type 'double) #f)))
|
||||
;; ...but an unparsed name is not a failure, it is an absence of
|
||||
;; knowledge, and must stay silent.
|
||||
(let ((a (fresh-tvar '(numeric))))
|
||||
(test-assert "an unparsed C name does not trip a constraint"
|
||||
(unify a (parse-type 'GLuint) #f)))
|
||||
|
||||
;; Joining two variables joins what is known about both.
|
||||
(let ((a (fresh-tvar '(numeric)))
|
||||
(b (fresh-tvar '(integral))))
|
||||
(unify a b #f)
|
||||
(test "constraints merge when variables do"
|
||||
'("integral" "numeric")
|
||||
(sort (map symbol->string (tvar-classes b)) string<?)))
|
||||
|
||||
(test-group "defaulting"
|
||||
(let ((a (fresh-tvar '(numeric))))
|
||||
(default-type-variables! a #f)
|
||||
(test "an open numeric is an int" 'int (unparse-type a)))
|
||||
(let ((a (fresh-tvar '(floating))))
|
||||
(default-type-variables! a #f)
|
||||
(test "an open floating is a double" 'double (unparse-type a)))
|
||||
(let ((a (fresh-tvar)))
|
||||
(test "a variable with nothing known about it cannot be defaulted"
|
||||
#f (default-type-variables! a #f))
|
||||
(test "and stays a hole, for the caller to complain about"
|
||||
'_ (unparse-type a)))
|
||||
;; Defaulting reaches into the structure, since the hole may be
|
||||
;; anywhere: `(var p (* _) ...)'.
|
||||
(let ((t (parse-type '(* _))))
|
||||
(unify (ptr-target t) (fresh-tvar '(numeric)) #f)
|
||||
(default-type-variables! t #f)
|
||||
(test "and it reaches inside a type" '(* int) (unparse-type t))))
|
||||
|
||||
(test-group "user classes"
|
||||
;; A trait bound is the same shape as `numeric', discharged by
|
||||
;; the same procedure -- that is the point of one representation.
|
||||
;; It differs in two rules, and both are stated while there is
|
||||
;; still only one kind of constraint to state them about.
|
||||
(register-class! 'i-ord #f #f #t)
|
||||
(add-struct 'i-circle '(struct i-circle ((r int))))
|
||||
(test "no instance, no entailment" #f (entails? 'i-ord (parse-type '(struct i-circle))))
|
||||
(add-instance! 'i-ord (parse-type '(struct i-circle)))
|
||||
(test "and with one, entailment" #t (entails? 'i-ord (parse-type '(struct i-circle))))
|
||||
;; Instances key on the resolved type, so an alias cannot be
|
||||
;; registered twice under two names.
|
||||
(add-typedef 'i-circle-alias '(typedef i-circle-alias (struct i-circle)))
|
||||
(test "an alias of an instance is the same instance"
|
||||
#t (entails? 'i-ord (parse-type 'i-circle-alias)))
|
||||
;; Decision 2: static dispatch cannot select an instance for a
|
||||
;; type it does not know, so a strict class says no to ? rather
|
||||
;; than shrugging the way `numeric' does.
|
||||
(test "a strict class refuses ?" #f (entails? 'i-ord the-unknown-type))
|
||||
(test "where a lenient one abstains" 'unknown (entails? 'numeric the-unknown-type))
|
||||
;; ...and it has no default to fall back on.
|
||||
(let ((a (fresh-tvar '(i-ord))))
|
||||
(test-error "an unresolved user constraint is an error, not a guess"
|
||||
(default-type-variables! a #f)))))
|
||||
|
||||
(test-group "schemes"
|
||||
;; Nothing generalizes yet. The shape is here because a scheme
|
||||
;; without a constraint list is the wrong shape for every bounded
|
||||
;; generic, and because instantiation is how a generic body gets
|
||||
;; fresh cells instead of a second type for the same one.
|
||||
(let* ((a (fresh-tvar '(numeric)))
|
||||
(id (make-fn-type a (list a) #f))
|
||||
(s (generalize id (list))))
|
||||
(test "the free variable is quantified" 1 (length (scheme-vars s)))
|
||||
(test "carrying its class as the scheme's context"
|
||||
'((numeric)) (list (map car (scheme-constraints s))))
|
||||
|
||||
(let ((one (instantiate s))
|
||||
(two (instantiate s)))
|
||||
(test "an instantiation is still open" '(fn ((_)) _) (unparse-type one))
|
||||
(unify (fn-ret one) (parse-type 'int) #f)
|
||||
(test "solving one instantiation" '(fn ((int)) int) (unparse-type one))
|
||||
(test "leaves the other alone" '(fn ((_)) _) (unparse-type two))
|
||||
(test "and the scheme itself untouched" '(fn ((_)) _) (unparse-type id))))
|
||||
|
||||
(let* ((a (fresh-tvar))
|
||||
(b (fresh-tvar))
|
||||
(s (generalize (make-fn-type a (list b) #f) (list b))))
|
||||
(test "a variable free in the environment is not quantified"
|
||||
1 (length (scheme-vars s)))
|
||||
(let ((inst (instantiate s)))
|
||||
(unify (car (fn-args inst)) (parse-type 'char) #f)
|
||||
(test "so instantiating solves it for everyone" 'char (unparse-type b))))))
|
||||
'i-count (unparse-type a)))))
|
||||
|
||||
19
utils.scm
19
utils.scm
@@ -10,7 +10,6 @@
|
||||
|
||||
(export
|
||||
get-env-var
|
||||
set-working-directory
|
||||
to-absolute-pathname
|
||||
comment-form?
|
||||
strip-header-comments
|
||||
@@ -25,12 +24,9 @@
|
||||
form-end-line
|
||||
copy-form-source!
|
||||
stamp-form-source!
|
||||
form-location
|
||||
set-form-type!
|
||||
form-type
|
||||
sex-error
|
||||
sex-warning
|
||||
with-directory)
|
||||
(with-directory set-working-directory))
|
||||
|
||||
(define-syntax prog1
|
||||
(syntax-rules ()
|
||||
@@ -129,19 +125,6 @@
|
||||
;;; The file `parse-all' is currently reading. Bound by the reader
|
||||
(define current-source-file (make-parameter "<unknown>"))
|
||||
|
||||
;;; What type a form has, once something has worked it out. Keyed by
|
||||
;;; cons cell like the sources above, so one form has one type: a body
|
||||
;;; typed at two instantiations has to be copied before the second.
|
||||
(define +form-types+ (make-hash-table eq?))
|
||||
|
||||
(define (set-form-type! form type)
|
||||
(when (pair? form)
|
||||
(hash-table-set! +form-types+ form type))
|
||||
type)
|
||||
|
||||
(define (form-type form)
|
||||
(hash-table-ref/default +form-types+ form #f))
|
||||
|
||||
(define (set-form-source! form file line #!optional (end-line line))
|
||||
(hash-table-set! +form-sources+ form (list file line end-line)))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user