
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.
158 lines
6.7 KiB
Racket
158 lines
6.7 KiB
Racket
#lang racket
|
|
|
|
(require "test-utils.rkt"
|
|
"evaluator.rkt"
|
|
(for-syntax racket/base)
|
|
(for-template racket/base)
|
|
(rep type-rep prop-rep object-rep)
|
|
(for-syntax
|
|
(rename-in (types utils numeric-tower abbrev prop-ops)
|
|
[Un t:Un]
|
|
[-> t:->]))
|
|
(utils tc-utils utils)
|
|
(utils mutated-vars)
|
|
|
|
rackunit rackunit/text-ui
|
|
syntax/parse
|
|
racket/file racket/port
|
|
syntax/location
|
|
(for-syntax syntax/kerncase syntax/parse racket/syntax
|
|
(types abbrev numeric-tower utils)
|
|
(utils mutated-vars) (env mvar-env)
|
|
(utils tc-utils) (typecheck typechecker check-below))
|
|
typed-racket/base-env/prims
|
|
typed-racket/base-env/base-types
|
|
(for-syntax typed-racket/standard-inits))
|
|
(provide tests)
|
|
(gen-test-main)
|
|
|
|
(begin-for-syntax (do-standard-inits))
|
|
|
|
(define-syntax-rule (tc-e/t e t) (tc-e e #:ret (reduce-tc-results/subsumption (ret t -true-propset))))
|
|
|
|
(define-syntax (tc-e stx)
|
|
(syntax-parse stx
|
|
[(tc-e expr ty) (syntax/loc stx (tc-e expr #:ret (reduce-tc-results/subsumption (ret ty))))]
|
|
[(id a #:ret b)
|
|
(syntax/loc stx
|
|
(test-case
|
|
(format "~a ~a" (quote-line-number id) 'a)
|
|
(let*-values
|
|
([(res1 res2 equiv? expanded)
|
|
(phase1-phase0-eval
|
|
(let ([ex (local-expand #'a 'expression null)])
|
|
(find-mutated-vars ex mvar-env)
|
|
(let ([res1 (tc-expr ex)]
|
|
[res2 b])
|
|
(let ([equiv? (and (check-below res1 res2)
|
|
(check-below res2 res1))])
|
|
#`(values '#,res1 '#,res2 '#,equiv? '#,(syntax->datum ex))))))])
|
|
(with-check-info (['expanded expanded])
|
|
(unless equiv?
|
|
(fail-check (format "Expression didn't have expected type.\n Expected: ~a\n Actual: ~a\n"
|
|
(struct->vector res1)
|
|
(struct->vector res2))))))))]))
|
|
|
|
(define tests
|
|
(test-suite
|
|
"Special Typechecker tests"
|
|
;; should work but don't -- need expected type
|
|
#|
|
|
[tc-e (for/list ([(k v) (in-hash #hash((1 . 2)))]) 0) (-lst -Zero)]
|
|
[tc-e (in-list (list 1 2 3)) (-seq -Integer)]
|
|
[tc-e (in-vector (vector 1 2 3)) (-seq -Integer)]
|
|
|#
|
|
|
|
[tc-e (in-hash #hash((1 . 2))) (-seq -Integer -Integer)]
|
|
[tc-e (in-hash-keys #hash((1 . 2))) (-seq -Integer)]
|
|
[tc-e (in-hash-values #hash((1 . 2))) (-seq -Integer)]
|
|
[tc-e (in-hash-pairs #hash((1 . 2))) (-seq (-pair -Integer -Integer))]
|
|
|
|
(tc-e (file->string "tmp") -String)
|
|
(tc-e (file->string "tmp" #:mode 'binary) -String)
|
|
(tc-e (file->string "tmp" #:mode 'text) -String)
|
|
|
|
(tc-e (file->bytes "tmp") -Bytes)
|
|
(tc-e (file->bytes "tmp" #:mode 'binary) -Bytes)
|
|
(tc-e (file->bytes "tmp" #:mode 'text) -Bytes)
|
|
|
|
(tc-e (file->list "tmp") (-lst Univ))
|
|
(tc-e ((inst file->list Any) "tmp" #:mode 'binary) (-lst Univ))
|
|
(tc-e ((inst file->list Any) "tmp" #:mode 'text) (-lst Univ))
|
|
|
|
(tc-e (file->list "tmp" (lambda (x) "string")) (-lst -String))
|
|
(tc-e ((inst file->list String) "tmp" (lambda (x) "string") #:mode 'binary) (-lst -String))
|
|
(tc-e ((inst file->list String) "tmp" (lambda (x) "string") #:mode 'text) (-lst -String))
|
|
|
|
(tc-e (file->lines "tmp") (-lst -String))
|
|
(tc-e (file->lines "tmp" #:mode 'text) (-lst -String))
|
|
(tc-e (file->lines "tmp" #:line-mode (first (shuffle '(linefeed return return-linefeed any any-one)))
|
|
#:mode 'binary) (-lst -String))
|
|
|
|
|
|
(tc-e (file->bytes-lines "tmp") (-lst -Bytes))
|
|
(tc-e (file->bytes-lines "tmp" #:mode 'text) (-lst -Bytes))
|
|
(tc-e (file->bytes-lines "tmp" #:line-mode (first (shuffle '(linefeed return return-linefeed any any-one)))
|
|
#:mode 'binary) (-lst -Bytes))
|
|
|
|
(tc-e (display-to-file "a" "tmp" #:mode (if (= 1 2) 'binary 'text)
|
|
#:exists (first (shuffle '(error append update replace truncate truncate/replace))))
|
|
-Void)
|
|
|
|
(tc-e (write-to-file "a" "tmp" #:mode (if (= 1 2) 'binary 'text)
|
|
#:exists (first (shuffle '(error append update replace truncate truncate/replace))))
|
|
-Void)
|
|
|
|
|
|
(tc-e (display-lines-to-file (list 2 'esha "esht") "tmp" #:separator #f
|
|
#:mode (if (= 1 2) 'binary 'text)
|
|
#:exists (first (shuffle '(error append update replace truncate truncate/replace))))
|
|
-Void)
|
|
|
|
(tc-e (get-preference 'pref (lambda () 'error) 'timestamp #f #:use-lock? #t #:timeout-lock-there #f #:lock-there #f) Univ)
|
|
|
|
|
|
(tc-e (make-handle-get-preference-locked .3 'sym (lambda () 'eseh) 'timestamp #f #:lock-there #f #:max-delay .45)
|
|
(t:-> -Pathlike Univ))
|
|
|
|
(tc-e (call-with-file-lock/timeout #f 'exclusive (lambda () 'res) (lambda () 'err)
|
|
#:lock-file "lock"
|
|
#:delay .01
|
|
#:max-delay .2) (one-of/c 'res 'err))
|
|
|
|
(tc-e (make-derived-parameter current-input-port
|
|
(lambda: ((s : String)) (open-input-file s))
|
|
object-name) (-Param -String Univ))
|
|
|
|
;; exception handling
|
|
[tc-e (with-handlers ([void (λ (x) (values 0 0))]) (values "" ""))
|
|
#:ret (ret (list (t:Un -Zero -String) (t:Un -Zero -String))
|
|
(list -true-propset -true-propset))]
|
|
|
|
(tc-e (make-temporary-file) -Path)
|
|
(tc-e (make-temporary-file "ee~a") -Path)
|
|
(tc-e (make-temporary-file "ee~a" 'directory) -Path)
|
|
(tc-e (make-temporary-file "ee~a" "temp" "here") -Path)
|
|
|
|
;; more sequences
|
|
[tc-e (sequence-ref (in-directory) 0) -Path]
|
|
[tc-e (sequence-ref (in-directory "foo" (λ (p) #t)) 0) -Path]
|
|
[tc-e (in-mlist (ann (mcons 'a null) (MListof 'a))) (-seq (-val 'a))]
|
|
[tc-e (in-producer (λ () 'hi)) (-seq (-val 'hi))]
|
|
[tc-e (in-producer (λ: ([x : String]) 'hi) symbol? "foo")
|
|
(-seq (-val 'hi))]
|
|
[tc-e (in-value 'hi) (-seq (-val 'hi))]
|
|
[tc-e (in-indexed '(a b c)) (-seq (one-of/c 'a 'b 'c) -Nat)]
|
|
[tc-e (in-sequences '(a b) '(z y)) (-seq (one-of/c 'a 'b 'z 'y))]
|
|
[tc-e (in-cycle '(a b) '(z y)) (-seq (one-of/c 'a 'b 'z 'y))]
|
|
[tc-e (in-parallel '(a b) '(z y)) (-seq (one-of/c 'a 'b) (one-of/c 'z 'y))]
|
|
[tc-e (in-values-sequence (in-parallel '(a b) '(z y)))
|
|
(-seq (-lst* (one-of/c 'a 'b) (one-of/c 'z 'y)))]
|
|
[tc-e (in-values-sequence '(a b c))
|
|
(-seq (-lst* (one-of/c 'a 'b 'c)))]
|
|
[tc-e (in-values*-sequence (in-parallel '(a b) '(z y)))
|
|
(-seq (-lst* (one-of/c 'a 'b) (one-of/c 'z 'y)))]
|
|
[tc-e (in-values*-sequence '(a b c))
|
|
(-seq (one-of/c 'a 'b 'c))]
|
|
))
|