Make top interaction commands lazily loaded.

This commit is contained in:
Eric Dobson 2013-12-15 13:21:07 -08:00
parent 8e51f2b5ac
commit 5d4cb8c08b

View File

@ -1,8 +1,37 @@
#lang racket/base
(require
"../utils/utils.rkt"
(for-syntax racket/base racket/lazy-require syntax/parse))
(begin-for-syntax
(lazy-require ['implementantion (:type-impl :print-type-impl :query-type/args-impl :query-type/result-impl)]))
(provide
(for-syntax
interactive-command?
interactive-command-procedure)
:type :print-type :query-type/args :query-type/result)
(define-for-syntax (fail _ stx)
(syntax-parse stx
[_:id
(raise-syntax-error #f "must be applied to arguments" stx)]
[_ (raise-syntax-error #f "only valid at the top-level of an interaction" stx)]))
(begin-for-syntax
(struct interactive-command (procedure)
#:property prop:procedure fail))
(define-syntax :type (interactive-command :type-impl))
(define-syntax :print-type (interactive-command :print-type-impl))
(define-syntax :query-type/args (interactive-command :query-type/args-impl))
(define-syntax :query-type/result (interactive-command :query-type/result-impl))
(module implementation racket/base
(require
"../utils/utils.rkt"
racket/base
racket/match
racket/format
@ -15,28 +44,11 @@
(types utils abbrev printer)
(typecheck tc-toplevel tc-app-helper)
(rep type-rep)
(utils tc-utils)))
(provide
(for-syntax
interactive-command?
interactive-command-procedure)
(utils tc-utils))
(provide
:type-impl :print-type-impl :query-type/args-impl :query-type/result-impl)
:type :print-type :query-type/args :query-type/result)
(define-for-syntax (fail _ stx)
(syntax-parse stx
[_:id
(raise-syntax-error #f "must be applied to arguments" stx)]
[_ (raise-syntax-error #f "only valid at the top-level of an interaction" stx)]))
(begin-for-syntax
(struct interactive-command (procedure)
#:property prop:procedure fail))
(define-syntax :type
(interactive-command
(λ (stx init)
(define (:type-impl stx init)
(syntax-parse stx
[(_ (~optional (~and #:verbose verbose-kw)) ty:expr)
(parameterize ([current-print-type-fuel
@ -57,12 +69,11 @@
(string-join (map ~a unexpanded)))))
#`(display #,(format "~a\n~a" type cue)))]
[form
(raise-syntax-error #f "must be applied to exactly one argument" #'form)]))))
(raise-syntax-error #f "must be applied to exactly one argument" #'form)]))
;; Prints the _entire_ type. May be quite large.
(define-syntax :print-type
(interactive-command
(λ (stx init)
;; TODO what should be done with stx
;; Prints the _entire_ type. May be quite large.
(define (:print-type-impl stx init)
(syntax-parse stx
[(_ e)
(tc-setup stx #'e 'top-level expanded init tc-toplevel-form before type
@ -73,12 +84,10 @@
[(tc-results: t) (-values t)]
[(tc-any-results:) ManyUniv])))))]
[form
(raise-syntax-error #f "must be applied to exactly one argument" #'form)]))))
(raise-syntax-error #f "must be applied to exactly one argument" #'form)]))
;; given a function and input types, display the result type
(define-syntax :query-type/args
(interactive-command
(λ (stx init)
;; given a function and input types, display the result type
(define (:query-type/args-impl stx init)
(syntax-parse stx
[(_ op arg-type ...)
(with-syntax ([(dummy-arg ...) (generate-temporaries #'(arg-type ...))])
@ -93,12 +102,10 @@
(match type
[(tc-result1: (and t (Function: _)) f o) t])))))]
[form
(raise-syntax-error #f "must be applied to at least one argument" #'form)]))))
(raise-syntax-error #f "must be applied to at least one argument" #'form)]))
;; given a function and a desired return type, fill in the blanks
(define-syntax :query-type/result
(interactive-command
(λ (stx init)
;; given a function and a desired return type, fill in the blanks
(define (:query-type/result-impl stx init)
(syntax-parse stx
[(_ op desired-type)
(init)
@ -115,4 +122,5 @@
(format "~a\n" cleaned)])))]
[_ (error (format "~a: not a function" (syntax->datum #'op) ))])))]
[form
(raise-syntax-error #f "must be applied to exactly two arguments" #'form)]))))
(raise-syntax-error #f "must be applied to exactly two arguments" #'form)])))