From b3bdb40ab739fe315d89ce1c9e6373ae43e46e5c Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Tue, 24 Mar 2015 16:45:15 -0400 Subject: [PATCH] Optimize pair operations, even in the absence of syntactic pair types. --- .../typed-racket/optimizer/pair.rkt | 10 +++-- typed-racket-test/optimizer/tests/gh60.rkt | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 typed-racket-test/optimizer/tests/gh60.rkt diff --git a/typed-racket-lib/typed-racket/optimizer/pair.rkt b/typed-racket-lib/typed-racket/optimizer/pair.rkt index a022ce21..0f90ec81 100644 --- a/typed-racket-lib/typed-racket/optimizer/pair.rkt +++ b/typed-racket-lib/typed-racket/optimizer/pair.rkt @@ -6,7 +6,7 @@ (for-syntax racket/base syntax/parse racket/syntax) "../utils/utils.rkt" (rep type-rep) - (types type-table utils) + (types type-table utils base-abbrev) (typecheck typechecker) (optimizer utils logging)) @@ -25,9 +25,11 @@ (define (has-pair-type? e) - (match (type-of e) ; type of the operand - [(tc-result1: (Pair: _ _)) #t] - [_ #f])) + (and (subtypeof? e (-pair Univ Univ)) + ;; sometimes composite operations end up with Nothing as result type, + ;; not sure why. TODO investigate + (not (isoftype? e -Bottom)))) +;; can't do the above for mpairs, as they are invariant (define (has-mpair-type? e) (match (type-of e) ; type of the operand [(tc-result1: (MPair: _ _)) #t] diff --git a/typed-racket-test/optimizer/tests/gh60.rkt b/typed-racket-test/optimizer/tests/gh60.rkt new file mode 100644 index 00000000..9bed7945 --- /dev/null +++ b/typed-racket-test/optimizer/tests/gh60.rkt @@ -0,0 +1,41 @@ +#;#; +#< . Symbol)) +(define (foo-car x) + (car x)) + + +(define-type bar (List Symbol (U String bar))) + +(: bar-car (bar . -> . Symbol)) +(define (bar-car x) + (car x)) + + +(define-type zam (Rec z (List Symbol (U String z)))) + +(: zam-car (zam . -> . Symbol)) +(define (zam-car x) + (car x)) + + +(define-type bar2 (List Symbol (U String bar))) + +(: bar2-car (bar2 . -> . Symbol)) +(define (bar2-car x) + (if (not (null? x)) + (car x) + 'error))