drop what infer had for types nothing builds
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m19s
Sex CI / build-linux (push) Successful in 5m19s

No source makes an unknown type, a rigid variable or a variable with a
class to satisfy, so the branches unify, bind-tvar! and entails? kept for
them never ran, and the classes registered for them were never asked.
This commit was merged in pull request #67.
This commit is contained in:
2026-10-20 12:20:40 +03:00
parent d2f36d35c6
commit 635cc48aa3

132
infer.scm
View File

@@ -28,7 +28,6 @@
(chicken module) (chicken module)
matchable matchable
srfi-1 srfi-1
srfi-69
types types
utils) utils)
@@ -54,25 +53,16 @@
;;; A type variable is a mutable cell. `ref' is #f while unsolved and ;;; 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 ;;; the type it stands for once bound -- union-find, with the path
;;; compression done in `resolve'. ;;; 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> (define-record-type <tvar>
(%make-tvar ref classes rigid?) (%make-tvar ref)
tvar? tvar?
(ref tvar-ref tvar-ref-set!) (ref tvar-ref tvar-ref-set!))
(classes tvar-classes tvar-classes-set!)
(rigid? tvar-rigid?))
;;; A primitive or otherwise nominal type. `name' is the list of words ;;; A primitive or otherwise nominal type. `name' is the list of words
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all ;;; 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 ;;; one node, and so is a name we have never parsed a declaration for
;;; (`size-t', `GLuint'). The two cases are told apart by ;;; (`size-t', `GLuint'). The two cases are told apart by
;;; `c-primitive?', which is what keeps a constraint over an unparsed C ;;; `c-primitive?'.
;;; typedef from being an error.
(define-record-type <prim> (define-record-type <prim>
(make-prim name quals) (make-prim name quals)
prim-type? prim-type?
@@ -122,16 +112,8 @@
(expansion alias-expansion) (expansion alias-expansion)
(quals alias-quals)) (quals alias-quals))
;;; `?'. Sex has full C interop, so `printf', `SDL-CreateWindow' and (define (fresh-tvar)
;;; `size-t' arrive from headers nobody parsed. Rather than reject (%make-tvar #f))
;;; 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 (fresh-tvar . classes)
(%make-tvar #f (if (null? classes) (list) (car classes)) #f))
;;; Follow a bound variable to what it stands for, compressing the path ;;; 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 ;;; on the way out. Every procedure that looks at a type's shape starts
@@ -307,7 +289,6 @@
(let ((t (resolve type))) (let ((t (resolve type)))
(cond (cond
((tvar? t) '_) ((tvar? t) '_)
((unknown-type? t) '?)
((alias-type? t) (qualify (alias-quals t) (list (alias-name t)))) ((alias-type? t) (qualify (alias-quals t) (list (alias-name t))))
((prim-type? t) (qualify (prim-quals t) (prim-name t))) ((prim-type? t) (qualify (prim-quals t) (prim-name t)))
((agg-type? t) (qualify (agg-quals t) (agg-spelling t))) ((agg-type? t) (qualify (agg-quals t) (agg-spelling t)))
@@ -347,42 +328,9 @@
(else surface))) (else surface)))
;;; --------------------------------------------------------------- ;;; ---------------------------------------------------------------
;;; Constraints ;;; C's own types
;;; --------------------------------------------------------------- ;;; ---------------------------------------------------------------
;;; `(numeric a)' is a type class: one representation, one table, one
;;; entailment check. `test' is how a class says "every arithmetic
;;; type" without enumerating twenty spellings.
(define-record-type <type-class>
(%make-type-class name test)
type-class?
(name type-class-name)
(test type-class-test))
(define +classes+ (make-hash-table))
(define (register-class! name test)
(hash-table-set! +classes+ name (%make-type-class name test)))
(define (get-class name)
(or (hash-table-ref/default +classes+ name #f)
(error "no such type class" name)))
;;; #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, and says nothing of one
((unknown-type? t) 'unknown)
((type-class-test cls) => (lambda (test) (test t)))
(else #f))))
(define +integer-words+ '(char short int long signed unsigned bool _Bool)) (define +integer-words+ '(char short int long signed unsigned bool _Bool))
(define +float-words+ '(float double)) (define +float-words+ '(float double))
(define +known-words+ (append '(void) +integer-words+ +float-words+)) (define +known-words+ (append '(void) +integer-words+ +float-words+))
@@ -393,71 +341,18 @@
(and (prim-type? t) (and (prim-type? t)
(every (lambda (word) (memq word +known-words+)) (prim-name 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))))
(register-class! 'numeric arithmetic-type?)
(register-class! 'integral integral-type?)
(register-class! 'floating floating-type?)
(register-class! 'scalar scalar-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 ;;; 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. ;;; FORM is carried only so a failure can say where it was written.
(define (unify t1 t2 form) (define (unify t1 t2 form)
(let ((a (resolve t1)) (let ((a (resolve t1))
(b (resolve t2))) (b (resolve t2)))
(cond (cond
((eq? a b) #t) ((eq? a b) #t)
((unknown-type? a) #t) ((tvar? a) (bind-tvar! a b form))
((unknown-type? b) #t) ((tvar? b) (bind-tvar! b a form))
;; 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))
;; A typedef unifies as what it stands for. Its name survives in ;; A typedef unifies as what it stands for. Its name survives in
;; whichever side is printed later, since neither side is rebuilt. ;; whichever side is printed later, since neither side is rebuilt.
((alias-type? a) (unify (alias-expansion a) b form)) ((alias-type? a) (unify (alias-expansion a) b form))
@@ -511,16 +406,7 @@
;; Without recursive types this cannot trigger. It is four lines, ;; Without recursive types this cannot trigger. It is four lines,
;; and the alternative to having it is a hang. ;; and the alternative to having it is a hang.
((occurs? v t) (sex-error form "recursive type" (unparse-type v))) ((occurs? v t) (sex-error form "recursive type" (unparse-type v)))
;; A rigid variable is a type *parameter*: inside a generic body it (else (tvar-ref-set! v t) #t)))
;; 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)))
(define (occurs? v type) (define (occurs? v type)
(let ((t (resolve type))) (let ((t (resolve type)))