File: contract.rkt

package info (click to toggle)
racket 7.9%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 178,684 kB
  • sloc: ansic: 282,112; lisp: 234,887; pascal: 70,954; sh: 27,112; asm: 16,268; makefile: 4,613; cpp: 2,715; ada: 1,681; javascript: 1,244; cs: 879; exp: 499; csh: 422; python: 274; xml: 106; perl: 104
file content (214 lines) | stat: -rw-r--r-- 8,950 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#lang racket/base
(require racket/contract/base
         syntax/srcloc
         syntax/modcollapse
         racket/syntax
         syntax/location)

(provide/contract
 [wrap-expr/c
  (->* (syntax? syntax?)
       (#:arg? any/c
        #:positive (or/c syntax? string? module-path-index?
                         'from-macro 'use-site 'unknown)
        #:negative (or/c syntax? string? module-path-index?
                         'from-macro 'use-site 'unknown)
        #:name (or/c identifier? symbol? string? #f)
        #:macro (or/c identifier? symbol? string? #f)
        #:context (or/c syntax? #f)
        #:phase exact-integer?)
       syntax?)])

(module runtime racket/base
  (require (for-syntax racket/base
                       syntax/free-vars)
           racket/contract/base
           racket/contract/combinator
           (only-in racket/contract/private/base
                    make-apply-contract)
           syntax/location)
  (provide (all-from-out racket/base
                         syntax/location)
           expr/contract
           relative-source)

  (define (macro-expr/c arg? expr-name ctc0)
    (define ctc (coerce-contract 'wrap-expr/c ctc0))
    (define proj (get/build-late-neg-projection ctc))
    (make-contract
     #:name (unquoted-printing-string
             (format "macro ~a contract~a~a"
                     (if arg? "argument" "result")
                     (if expr-name " on " "")
                     (if expr-name expr-name "")))
     #:first-order (contract-first-order ctc)
     #:late-neg-projection
     (λ (blame)
       (define blame* (blame-add-context blame (format "~s" (contract-name ctc)) #:swap? arg?))
       (proj blame*))
     #:list-contract? (list-contract? ctc)))

  (define (macro-dep-expr/c arg? expr-name)
    (make-contract
     #:name (unquoted-printing-string
             (format "macro ~a contract~a~a"
                     (if arg? "argument" "result")
                     (if expr-name " on " "")
                     (if expr-name expr-name "")))
     #:late-neg-projection
     (lambda (blame)
       (lambda (_f neg)
         ;; Note: specialized to _f = return-second-arg.
         (lambda (c v)
           (define (slow-path)
             (define ctc (coerce-contract 'wrap-expr/c c))
             (define proj (get/build-late-neg-projection ctc))
             (define blame*
               (blame-add-context blame (format "~s" (contract-name ctc)) #:swap? arg?))
             ((proj blame*) v neg))
           (cond [(flat-contract? c)
                  (let ([c (if (procedure? c) c (coerce-contract 'wrap-expr/c c))])
                    (if (c v) v (slow-path)))]
                 [else (slow-path)]))))))

  (define (return-second-arg c v) v)

  (begin-for-syntax
    (define (okay-to-lift? ee)
      (and (identifier? ee) (not (local-free-vars? ee))))
    (define (self-module-path-index? mpi)
      (define-values (rel base) (module-path-index-split mpi))
      (and (eq? rel #f) (eq? (module-path-index-submodule mpi) #f)))
    (define (local-free-vars? ee)
      (for/or ([fv (in-list (free-vars ee #:module-bound? #t))])
        (define b (identifier-binding fv))
        (cond [(list? b) (self-module-path-index? (car b))]
              [else #t]))))

  (define-syntax (expr/contract stx)
    (cond
      [(eq? (syntax-local-context) 'expression)
       (syntax-case stx ()
         [(_ val-expr ctc-expr arg? expr-name [mac-arg ...])
          (let ([ctc-ee (local-expand #'ctc-expr 'expression null)])
            (cond [(okay-to-lift? ctc-ee)
                   #`(#,(syntax-local-lift-expression
                         #`(make-apply-contract
                            (macro-expr/c arg? expr-name #,ctc-ee)
                            mac-arg ...))
                      val-expr)]
                  [else
                   #`(#,(syntax-local-lift-expression
                         #`((make-apply-contract
                             (macro-dep-expr/c arg? expr-name)
                             mac-arg ...)
                            return-second-arg))
                      #,ctc-ee
                      val-expr)]))])]
      [else #`(#%expression #,stx)]))

  (define (relative-source base-mpi rel-mod-path)
    (define r
      (resolved-module-path-name
       (module-path-index-resolve
        (module-path-index-join rel-mod-path base-mpi))))
    (cond [(pair? r)
           (cons 'submod r)]
          [(symbol? r)
           (list 'quote r)]
          [else r])))

;; Allow phase shift of 0 or 1 without needing to lift requires
(require (for-template (submod "." runtime))
         ;; for phase +1 uses, only need to instantiate, since we’ll shift
         (only-in (submod "." runtime)))

(define (wrap-expr/c ctc-expr expr
                     #:arg? [arg? #t]
                     #:positive [pos-source 'from-macro]
                     #:negative [neg-source 'use-site]
                     #:name [expr-name #f]
                     #:macro [macro-name #f]
                     #:context [ctx (current-syntax-context)]
                     #:phase [phase (syntax-local-phase-level)])
  (let* ([pos-source-expr
          (get-source-expr pos-source
                           (if (identifier? macro-name) macro-name ctx))]
         [neg-source-expr
          (get-source-expr neg-source
                           (if (identifier? macro-name) macro-name ctx))]
         [expr-name
          (if (identifier? expr-name)
              (syntax-e expr-name)
              expr-name)]
         [macro-name
          (cond [(identifier? macro-name) (syntax-e macro-name)]
                [(or (string? macro-name) (symbol? macro-name)) macro-name]
                [(syntax? ctx)
                 (syntax-case ctx ()
                   [(x . _) (identifier? #'x) (syntax-e #'x)]
                   [x (identifier? #'x) (syntax-e #'x)]
                   [_ '?])]
                [else '?])]
         [introduce (make-syntax-introducer)]
         [phase-shift (- phase (syntax-local-phase-level))]
         [shift+introduce (lambda (stx) (introduce (syntax-shift-phase-level stx phase-shift)))]
         [unshift+introduce (lambda (stx) (introduce (syntax-shift-phase-level stx (- phase-shift))))]
         [expr+ctc (shift+introduce
                    #`(expr/contract #,(unshift+introduce expr) #,(unshift+introduce ctc-expr)
                                     '#,(and arg? #t) '#,expr-name
                                     [#,pos-source-expr
                                      #,neg-source-expr
                                      '#,macro-name
                                      (quote-syntax #,expr)
                                      #f]))])
    (cond
      ;; no need to lift for common phases, since we explicitly require them in this module
      [(memq phase-shift '(0 1))
       expr+ctc]
      [else
       (unless (syntax-transforming?)
         (raise-arguments-error 'wrap-expr/c "not currently expanding"))
       (define phased-require-spec
         (introduce (datum->syntax #'here `(for-meta ,phase-shift ,(quote-module-path runtime)))))
       (syntax-local-introduce (syntax-local-lift-require (syntax-local-introduce phased-require-spec)
                                                          (syntax-local-introduce expr+ctc)))])))

(define (get-source-expr source ctx)
  (cond [(eq? source 'use-site)
         #'(quote-module-path)]
        [(eq? source 'unknown)
         #'(quote "unknown")]
        [(eq? source 'from-macro)
         (get-source-expr (extract-source ctx) #f)]
        [(string? source) #`(quote #,source)]
        [(syntax? source) #`(quote #,(source-location->string source))]
        [(module-path-index? source)
         ;; FIXME: This assumes that if source is relative, it is relative to
         ;; the current self-index (the module currently being compiled). That
         ;; should usually be the case, but it's not necessarily true.
         (define collapsed (collapse-module-path-index source))
         (cond [(eq? collapsed #f)
                #'(quote-module-path)]
               [(relative-module-path? collapsed)
                #`(relative-source (variable-reference->module-path-index
                                    (#%variable-reference))
                                   '#,collapsed)]
               [else #`(quote #,collapsed)])]))

(define (relative-module-path? mp)
  (or (string? mp) (path? mp)
      (and (pair? mp) (eq? (car mp) 'submod)
           (let ([base (cadr mp)]) (or (string? base) (path? base))))))

;; extract-source : (U Syntax #f) -> (U ModulePathIndex 'use-site 'unknown)
(define (extract-source stx)
  (let ([id (syntax-case stx ()
              [(x . _) (identifier? #'x) #'x]
              [x (identifier? #'x) #'x]
              [_ #f])])
    (if id
        (let ([b (identifier-binding id)])
          (cond [(list? b) (car b)] ;; module-path-index
                [else 'use-site]))
        'unknown)))