added (disabled) optimization
svn: r18567
This commit is contained in:
commit
d58bf1521b
51
collects/typed-scheme/private/optimize.ss
Normal file
51
collects/typed-scheme/private/optimize.ss
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
#lang scheme/base
|
||||||
|
|
||||||
|
(require syntax/parse (for-template scheme/base scheme/unsafe/ops)
|
||||||
|
"../utils/utils.ss" unstable/match scheme/match unstable/syntax
|
||||||
|
(rep type-rep)
|
||||||
|
(types abbrev type-table utils))
|
||||||
|
(provide optimize)
|
||||||
|
|
||||||
|
(define-syntax-class float-opt-expr
|
||||||
|
(pattern e:opt-expr
|
||||||
|
#:when (match (type-of #'e)
|
||||||
|
[(tc-result1: (== -Flonum type-equal?)) #t] [_ #f])
|
||||||
|
#:with opt #'e.opt))
|
||||||
|
|
||||||
|
(define-syntax-class float-binary-op
|
||||||
|
#:literals (+ - * / = <= < > >= min max)
|
||||||
|
(pattern (~and i:id (~or + - * / = <= < > >= min max))
|
||||||
|
#:with unsafe (format-id #'here "unsafe-fl~a" #'i)))
|
||||||
|
|
||||||
|
(define-syntax-class float-unary-op
|
||||||
|
#:literals (abs sin cos tan asin acos atan log exp)
|
||||||
|
(pattern (~and i:id (~or abs sin cos tan asin acos atan log exp))
|
||||||
|
#:with unsafe (format-id #'here "unsafe-fl~a" #'i)))
|
||||||
|
|
||||||
|
(define-syntax-class opt-expr
|
||||||
|
(pattern e:opt-expr*
|
||||||
|
#:with opt (syntax-recertify #'e.opt this-syntax (current-code-inspector) #f)))
|
||||||
|
|
||||||
|
(define-syntax-class opt-expr*
|
||||||
|
#:literal-sets (kernel-literals)
|
||||||
|
#:local-conventions ([#rx"^e" opt-expr]
|
||||||
|
[#rx"^f" float-opt-expr])
|
||||||
|
(pattern (let-values ([ids e-rhs] ...) e-body ...)
|
||||||
|
#:with opt #'(let-values ([ids e-rhs.opt] ...) e-body.opt ...))
|
||||||
|
(pattern (#%plain-app op:float-unary-op f)
|
||||||
|
#:with opt #'(op.unsafe f.opt))
|
||||||
|
(pattern (#%plain-app op:float-binary-op f fs ...)
|
||||||
|
#:with opt
|
||||||
|
(for/fold ([o #'f.opt])
|
||||||
|
([e (syntax->list #'(fs.opt ...))])
|
||||||
|
#`(op.unsafe #,o #,e)))
|
||||||
|
(pattern (#%plain-app e ...)
|
||||||
|
#:with opt #'(#%plain-app e.opt ...))
|
||||||
|
(pattern other:expr
|
||||||
|
#:with opt #'other))
|
||||||
|
|
||||||
|
(define (optimize stx)
|
||||||
|
(syntax-parse stx #:literal-sets (kernel-literals)
|
||||||
|
[(define-values ~! ids e:opt-expr)
|
||||||
|
(syntax/loc stx (define-values ids e.opt))]
|
||||||
|
[_ (printf "nothing happened") stx]))
|
|
@ -5,7 +5,7 @@
|
||||||
(require syntax/kerncase mzlib/trace
|
(require syntax/kerncase mzlib/trace
|
||||||
scheme/match (prefix-in - scheme/contract)
|
scheme/match (prefix-in - scheme/contract)
|
||||||
"signatures.ss" "tc-envops.ss" "tc-metafunctions.ss"
|
"signatures.ss" "tc-envops.ss" "tc-metafunctions.ss"
|
||||||
(types utils convenience union subtype remove-intersect)
|
(types utils convenience union subtype remove-intersect type-table)
|
||||||
(private-in parse-type type-annotation)
|
(private-in parse-type type-annotation)
|
||||||
(rep type-rep)
|
(rep type-rep)
|
||||||
(only-in (infer infer) restrict)
|
(only-in (infer infer) restrict)
|
||||||
|
@ -231,6 +231,7 @@
|
||||||
(lambda (ann)
|
(lambda (ann)
|
||||||
(let* ([r (tc-expr/check/internal form ann)]
|
(let* ([r (tc-expr/check/internal form ann)]
|
||||||
[r* (check-below r expected)])
|
[r* (check-below r expected)])
|
||||||
|
(add-typeof-expr form expected)
|
||||||
;; around again in case there is an instantiation
|
;; around again in case there is an instantiation
|
||||||
;; remove the ascription so we don't loop infinitely
|
;; remove the ascription so we don't loop infinitely
|
||||||
(loop (remove-ascription form) r* #t)))]
|
(loop (remove-ascription form) r* #t)))]
|
||||||
|
@ -242,13 +243,16 @@
|
||||||
;; do the instantiation on the old type
|
;; do the instantiation on the old type
|
||||||
(let* ([ts* (do-inst form ts)]
|
(let* ([ts* (do-inst form ts)]
|
||||||
[ts** (ret ts* fs os)])
|
[ts** (ret ts* fs os)])
|
||||||
|
(add-typeof-expr form ts**)
|
||||||
;; make sure the new type is ok
|
;; make sure the new type is ok
|
||||||
(check-below ts** expected))]
|
(check-below ts** expected))]
|
||||||
;; no annotations possible on dotted results
|
;; no annotations possible on dotted results
|
||||||
[ty ty])]
|
[ty (add-typeof-expr form ty) ty])]
|
||||||
;; nothing to see here
|
;; nothing to see here
|
||||||
[checked? expected]
|
[checked? expected]
|
||||||
[else (tc-expr/check/internal form expected)]))))
|
[else (let ([t (tc-expr/check/internal form expected)])
|
||||||
|
(add-typeof-expr form t)
|
||||||
|
t)]))))
|
||||||
|
|
||||||
(define (tc-or e1 e2 or-part [expected #f])
|
(define (tc-or e1 e2 or-part [expected #f])
|
||||||
(match (single-value e1)
|
(match (single-value e1)
|
||||||
|
@ -469,8 +473,10 @@
|
||||||
[else (internal-tc-expr form)])])
|
[else (internal-tc-expr form)])])
|
||||||
(match ty
|
(match ty
|
||||||
[(tc-results: ts fs os)
|
[(tc-results: ts fs os)
|
||||||
(let ([ts* (do-inst form ts)])
|
(let* ([ts* (do-inst form ts)]
|
||||||
(ret ts* fs os))]))))
|
[r (ret ts* fs os)])
|
||||||
|
(add-typeof-expr form r)
|
||||||
|
r)]))))
|
||||||
|
|
||||||
(define (tc/send rcvr method args [expected #f])
|
(define (tc/send rcvr method args [expected #f])
|
||||||
(match (tc-expr rcvr)
|
(match (tc-expr rcvr)
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
(for-syntax
|
(for-syntax
|
||||||
(except-in syntax/parse id)
|
(except-in syntax/parse id)
|
||||||
scheme/base
|
scheme/base
|
||||||
(private type-contract)
|
(private type-contract optimize)
|
||||||
(types utils convenience)
|
(types utils convenience)
|
||||||
(typecheck typechecker provide-handling)
|
(typecheck typechecker provide-handling)
|
||||||
(env type-environments type-name-env type-alias-env)
|
(env type-environments type-name-env type-alias-env)
|
||||||
|
@ -77,7 +77,12 @@
|
||||||
(type-check #'(body2 ...)))]
|
(type-check #'(body2 ...)))]
|
||||||
[check-syntax-help (syntax-property #'(void) 'disappeared-use (type-name-references))]
|
[check-syntax-help (syntax-property #'(void) 'disappeared-use (type-name-references))]
|
||||||
[(transformed-body ...) (remove-provides #'(body2 ...))])]
|
[(transformed-body ...) (remove-provides #'(body2 ...))])]
|
||||||
[with-syntax ([(transformed-body ...) (change-contract-fixups #'(transformed-body ...))])])
|
[with-syntax ([(transformed-body ...) (change-contract-fixups #'(transformed-body ...))])]
|
||||||
|
|
||||||
|
[with-syntax ([(transformed-body ...)
|
||||||
|
(if (optimize?)
|
||||||
|
(map optimize (syntax->list #'(transformed-body ...)))
|
||||||
|
#'(transformed-body ...))])])
|
||||||
(do-time "Typechecked")
|
(do-time "Typechecked")
|
||||||
#;(printf "checked ~a~n" module-name)
|
#;(printf "checked ~a~n" module-name)
|
||||||
#;(printf "created ~a types~n" (count!))
|
#;(printf "created ~a types~n" (count!))
|
||||||
|
|
17
collects/typed-scheme/types/type-table.ss
Normal file
17
collects/typed-scheme/types/type-table.ss
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#lang scheme/base
|
||||||
|
|
||||||
|
(require unstable/debug "../utils/utils.ss" (rep type-rep) (only-in (types abbrev utils) tc-results?) scheme/contract)
|
||||||
|
|
||||||
|
(define table (make-hasheq))
|
||||||
|
|
||||||
|
(define (reset-type-table) (set! table (make-hasheq)))
|
||||||
|
|
||||||
|
(define (add-typeof-expr e t)
|
||||||
|
(when (optimize?)
|
||||||
|
(hash-set! table e t)))
|
||||||
|
|
||||||
|
(define (type-of e) (hash-ref table e))
|
||||||
|
|
||||||
|
(p/c [add-typeof-expr (syntax? tc-results? . -> . any/c)]
|
||||||
|
[type-of (syntax? . -> . tc-results?)]
|
||||||
|
[reset-type-table (-> any/c)])
|
|
@ -14,6 +14,8 @@ at least theoretically.
|
||||||
(provide reverse-begin)
|
(provide reverse-begin)
|
||||||
|
|
||||||
(provide
|
(provide
|
||||||
|
;; optimization
|
||||||
|
optimize?
|
||||||
;; timing
|
;; timing
|
||||||
start-timing do-time
|
start-timing do-time
|
||||||
;; logging
|
;; logging
|
||||||
|
@ -23,6 +25,8 @@ at least theoretically.
|
||||||
;; provide macros
|
;; provide macros
|
||||||
rep utils typecheck infer env private)
|
rep utils typecheck infer env private)
|
||||||
|
|
||||||
|
(define optimize? (make-parameter #f))
|
||||||
|
|
||||||
;; fancy require syntax
|
;; fancy require syntax
|
||||||
(define-syntax (define-requirer stx)
|
(define-syntax (define-requirer stx)
|
||||||
(syntax-parse stx
|
(syntax-parse stx
|
||||||
|
|
Loading…
Reference in New Issue
Block a user