typed-racket/typed-racket-test/unit-tests/check-below-tests.rkt
Andrew Kent 8e7f39025a remove interning for most Reps in TR
Prior to this change (which was Typed Racket PR 469) all internal TR
objects (Reps) were interned and kept around for the entire duration
of type checking.  Because of this, frequent operations that rebuilt
types were particularly costly (e.g. various forms of
substitution). To recoup some of this cost, caching was being used in
a lot of places. This PR sought to remove interning as the default
behavior for Reps and allow for more flexibility in how we approach
time/space performance needs going forward.

The following changes were included in this overhaul:

Interning: All Reps are no longer interned. Right now we only intern
unions and some propositions.

Rep generic operations: we now use racket/generic so we're not
reinventing this wheel.

Singletons: Reps (e.g. TrueProp, Univ, etc) can be declared singleton,
which creates a single instance of the rep that all visible operations
(even within the declaring module) are defined in terms of
(e.g. predicates are defined as (λ (x) (eq? x singleton-instance)),
etc).

Custom constructors: Custom constructors can be specified for Reps,
which allows for simple normalization, interning, or other invariants
to be enfored whenever a Rep is created.

Union: Unions used to try to ensure no obviously overlaping types
would inhabit the same Union (e.g. (U String (Pairof Any Any) (Pairof
Int Int)) would be simplified to (U String (Pairof Any Any))). This,
however, required frequent calls to subtyping every time a Union was
modified and working with Unions thus had a high cost (another thing
that caching was used to reduce). Instead of this, Unions now enforce
a much simpler set of invariants on their members: (1) No duplicates
(by virtue of using a hash-based set), (2) Any and Nothing do not
appear in unions, and (3) Nested unions are flattened. Also, using a
hashset as the internal data structure meant that we could easily
intern unions w.r.t. equal? equality. NOTE: we do reduce unions to not
contain obviously overlapping terms when printing to users and when
generating contracts (so obviously and avoidable inneficient contracts
are not generated – See union.rkt for 'normalize-type').

Subtyping changes: Subtyping has been designed to reduce dispatch time
w/ a switch since we no longer cache _all_ subtyping calls (we only
cache subtyping results for unions since they have some costly
subtyping).

prop-ops changes: AndProps now are careful to sort OrProps by length
before building the resulting proposition. This is done because
OrProp implication only checks if one Or is a subset of another Or.
By ordering Or props by size, we only ever check if an OrProp implies
another if its size is <= the other OrProp. This also makes the
smart constructor '-and' more robust, since the order the props
appear does not affect if some Ors are kept or not.

Testing: More subtype tests have been added (we are still probably
relying too much on typecheck-tests.rkt and not the more granular unit
tests in general).  Also, typecheck-tests.rkt has been changed to
check for type-equivalence (i.e. subtyping in both directions)
instead of equal? equivalence.
2016-12-16 15:18:50 -05:00

221 lines
7.9 KiB
Racket

#lang racket/base
(require "test-utils.rkt"
rackunit racket/list racket/match racket/format
syntax/srcloc syntax/location
(types abbrev tc-result)
(utils tc-utils)
(rep prop-rep object-rep type-rep)
(typecheck check-below)
(for-syntax racket/base syntax/parse))
(provide tests)
(gen-test-main)
;; Ensure that we never return a prop or object of #f.
(define (check-prop f)
(match f
[#f (fail-check "Result has no prop (instead of a top prop).")]
[_ (void)]))
(define (check-object o)
(match o
[#f (fail-check "Result has no object (instead of an empty object).")]
[_ (void)]))
(define (check-result result)
(match result
[(tc-results: ts fs os)
(for-each check-prop fs)
(for-each check-object os) ]
[(tc-results: ts fs os dty bound)
(for-each check-prop fs)
(for-each check-object os)]
[(tc-any-results: f)
(check-prop f)]
[(? Type?)
(void)]))
(define-syntax (test-below stx)
(syntax-parse stx
[(_ t1:expr t2:expr (~optional (~seq #:result expected-result:expr)
#:defaults [(expected-result #'t2)]))
#`(test-case (~a 't1 " <: " 't2)
(with-check-info (['location (build-source-location-list (quote-srcloc #,stx))]
['expected expected-result])
(define result (check-below t1 t2))
(with-check-info (['actual result])
(check-result result)
(unless (equal? expected-result result)
(fail-check "Check below did not return expected result.")))))]
[(_ #:fail (~optional message:expr #:defaults [(message #'#rx"type mismatch")])
t1:expr t2:expr
(~optional (~seq #:result expected-result:expr)
#:defaults [(expected-result #'t2)]))
#`(test-case (~a 't1 " !<: " 't2)
(with-check-info (['location (build-source-location-list (quote-srcloc #,stx))]
['expected expected-result])
(define result
(parameterize ([delay-errors? #t])
(check-below t1 t2)))
(with-check-info (['actual result])
(define exn
(let/ec exit
(with-handlers [(exn:fail? exit)]
(report-all-errors)
(fail-check "Check below did not fail."))))
(check-result result)
(unless (equal? expected-result result)
(fail-check "Check below did not return expected result."))
(check-regexp-match message (exn-message exn)))))]))
(define tests
(test-suite "Check Below"
(test-below -Bottom Univ)
(test-below #:fail -Symbol -String)
(test-below
(ret -Bottom)
(ret (list Univ Univ) (list -true-propset #f) (list #f -empty-obj))
#:result (ret (list Univ Univ) (list -true-propset -ff-propset) (list -empty-obj -empty-obj)))
(test-below
(ret -Bottom)
(ret (list Univ) (list #f) (list #f) Univ 'B)
#:result (ret (list Univ) (list -ff-propset) (list -empty-obj) Univ 'B))
;; Bottom is not below everything if the number of values doesn't match up.
(test-below #:fail
(ret (list -Bottom -Bottom))
(ret (list Univ) (list -true-propset) (list #f))
#:result (ret (list Univ) (list -true-propset) (list -empty-obj)))
(test-below #:fail
(ret (list))
(ret (list Univ) (list -true-propset) (list #f))
#:result (ret (list Univ) (list -true-propset) (list -empty-obj)))
(test-below
(ret (list -Symbol) (list -tt-propset) (list -empty-obj))
(ret (list Univ) (list #f) (list #f))
#:result (ret (list Univ) (list -tt-propset) (list -empty-obj)))
(test-below
(ret (list -Symbol) (list -true-propset) (list -empty-obj))
(ret (list Univ) (list -tt-propset) (list -empty-obj)))
(test-below #:fail
(ret (list -Symbol) (list -tt-propset) (list -empty-obj))
(ret (list Univ) (list -true-propset) (list #f))
#:result (ret (list Univ) (list -true-propset) (list -empty-obj)))
(test-below #:fail #rx"no object"
(ret (list -Symbol) (list -tt-propset) (list -empty-obj))
(ret (list Univ) (list -tt-propset) (list (make-Path empty #'x))))
(test-below #:fail #rx"no object"
(ret (list -Symbol) (list -tt-propset) (list -empty-obj))
(ret (list Univ) (list -true-propset) (list (make-Path empty #'x))))
(test-below (ret -Bottom) (tc-any-results #f) #:result (tc-any-results -ff))
(test-below (ret Univ) (tc-any-results -tt) #:result (tc-any-results -tt))
(test-below (tc-any-results -ff) (tc-any-results #f) #:result (tc-any-results -ff))
(test-below
(ret (list -Symbol -String) (list -true-propset -ff-propset))
(tc-any-results #f)
#:result (tc-any-results -ff))
(test-below (ret -Symbol -ff-propset) (tc-any-results #f) #:result (tc-any-results -ff))
(test-below (ret -Symbol -true-propset -empty-obj) (tc-any-results #f)
#:result (tc-any-results -tt))
(test-below (ret (list -Symbol -String)) (tc-any-results #f)
#:result (tc-any-results -tt))
(test-below
(ret (list -Symbol -String) (list -true-propset -false-propset) (list -empty-obj -empty-obj))
(tc-any-results #f)
#:result (tc-any-results -tt))
(test-below #:fail
(ret -Symbol)
(ret (list -Symbol -Symbol) (list -tt-propset #f) (list #f -empty-obj))
#:result (ret (list -Symbol -Symbol) (list -tt-propset -tt-propset) (list -empty-obj -empty-obj)))
(test-below #:fail
(tc-any-results -tt)
(ret -Symbol))
(test-below #:fail
(ret -Symbol -true-propset -empty-obj)
(ret -Symbol -true-propset -empty-obj Univ 'B))
(test-below #:fail
(ret -Symbol -true-propset -empty-obj Univ 'B)
(ret -Symbol -true-propset -empty-obj))
(test-below #:fail
(ret -Symbol)
(ret -Symbol #f -empty-obj Univ 'B)
#:result (ret -Symbol -tt-propset -empty-obj Univ 'B))
(test-below #:fail
(tc-any-results -tt)
(ret -Symbol #f -empty-obj Univ 'B)
#:result (ret (list -Symbol) (list -tt-propset) (list -empty-obj) Univ 'B))
(test-below #:fail
(ret -Symbol -tt-propset -empty-obj Univ 'B)
(ret (list -Symbol -Symbol) (list -tt-propset -tt-propset) (list -empty-obj -empty-obj) Univ 'B))
(test-below (ret -Symbol -true-propset -empty-obj Univ 'B)
(tc-any-results #f)
#:result (tc-any-results -tt))
(test-below
(ret -Symbol)
(ret -Symbol #f -empty-obj)
#:result (ret -Symbol -tt-propset -empty-obj))
(test-below
(ret -Symbol -true-propset)
(ret -Symbol #f -empty-obj)
#:result (ret -Symbol -true-propset -empty-obj))
(test-below #:fail
(ret -Symbol -true-propset)
(ret (list Univ -Symbol) (list #f -tt-propset))
#:result (ret (list Univ -Symbol) (list -tt-propset -tt-propset)))
(test-below
(ret (list Univ) (list -true-propset) (list -empty-obj))
(ret Univ #f)
#:result (ret (list Univ) (list -true-propset) (list -empty-obj)))
;; Enable these once check-below is fixed
;; Currently does not fail
#;
(test-below #:fail
(ret (list Univ) (list -tt-propset) (list -empty-obj) Univ 'B)
(ret (list Univ) (list -false-propset) (list #f) Univ 'B)
#:result (ret (list Univ) (list -false-propset) (list -empty-obj) Univ 'B))
;; Currently does not fail
#;
(test-below #:fail
(ret (list Univ) (list -tt-propset) (list -empty-obj))
(ret (list Univ) (list -false-propset) (list #f) Univ 'B)
#:result (ret (list Univ) (list -false-propset) (list -empty-obj) Univ 'B))
;; Currently does not fail
#;
(test-below #:fail
(ret (list Univ Univ) (list -tt-propset -tt-propset) (list -empty-obj -empty-obj))
(ret (list Univ Univ) (list -false-propset -false-propset) (list #f #f))
#:result (ret (list Univ Univ) (list -false-propset -false-propset) (list -empty-obj -empty-obj)))
))