Simplify construction/matching on Name types

This commit is contained in:
Asumu Takikawa 2014-07-21 18:00:35 -04:00
parent 4b05d835f4
commit 49ba06b583
12 changed files with 37 additions and 21 deletions

View File

@ -35,11 +35,10 @@
...) ...)
...)])) ...)]))
(define -Srcloc (make-Name #'srcloc null #f #t)) (define -Srcloc (-struct-name #'srcloc))
(define -Date (make-Name #'date null #f #t)) (define -Date (-struct-name #'date))
(define -Arity-At-Least (define -Arity-At-Least (-struct-name #'arity-at-least))
(make-Name #'arity-at-least null #f #t)) (define -Exn (-struct-name #'exn))
(define -Exn (make-Name #'exn null #f #t))
(define (initialize-structs) (define (initialize-structs)

View File

@ -60,7 +60,7 @@
(define (check-type-alias-contractive id type) (define (check-type-alias-contractive id type)
(define/match (check type) (define/match (check type)
[((Union: elems)) (andmap check elems)] [((Union: elems)) (andmap check elems)]
[((Name: name-id _ _ _)) [((Name/simple: name-id))
(and (not (free-identifier=? name-id id)) (and (not (free-identifier=? name-id id))
(check (resolve-once type)))] (check (resolve-once type)))]
[((App: rator rands stx)) [((App: rator rands stx))

View File

@ -543,7 +543,7 @@
[args (parse-types #'(arg args ...))]) [args (parse-types #'(arg args ...))])
(resolve-app-check-error rator args stx) (resolve-app-check-error rator args stx)
(match rator (match rator
[(Name: _ _ _ _) (make-App rator args stx)] [(? Name?) (make-App rator args stx)]
[(Poly: _ _) (instantiate-poly rator args)] [(Poly: _ _) (instantiate-poly rator args)]
[(Mu: _ _) (loop (unfold rator) args)] [(Mu: _ _) (loop (unfold rator) args)]
[(Error:) Err] [(Error:) Err]

View File

@ -277,7 +277,7 @@
(λ () (loop resolved-name 'both rv))) (λ () (loop resolved-name 'both rv)))
(lookup-name-sc name-id typed-side)])] (lookup-name-sc name-id typed-side)])]
;; Ordinary type applications or struct type names, just resolve ;; Ordinary type applications or struct type names, just resolve
[(or (App: _ _ _) (Name: _ _ _ #t)) (t->sc (resolve-once type))] [(or (App: _ _ _) (Name/struct:)) (t->sc (resolve-once type))]
[(Univ:) (if (from-typed? typed-side) any-wrap/sc any/sc)] [(Univ:) (if (from-typed? typed-side) any-wrap/sc any/sc)]
[(Mu: var (Union: (list (Value: '()) (Pair: elem-ty (F: var))))) [(Mu: var (Union: (list (Value: '()) (Pair: elem-ty (F: var)))))
(listof/sc (t->sc elem-ty))] (listof/sc (t->sc elem-ty))]

View File

@ -32,6 +32,7 @@
type-equal? type-equal?
remove-dups remove-dups
sub-t sub-f sub-o sub-pe sub-t sub-f sub-o sub-pe
Name/simple: Name/struct:
(rename-out [Class:* Class:] (rename-out [Class:* Class:]
[Class* make-Class] [Class* make-Class]
[Row* make-Row] [Row* make-Row]
@ -1031,3 +1032,15 @@
(list row-pat inits-pat fields-pat (list row-pat inits-pat fields-pat
methods-pat augments-pat init-rest-pat)))]))) methods-pat augments-pat init-rest-pat)))])))
;; alternative to Name: that only matches the name part
(define-match-expander Name/simple:
(λ (stx)
(syntax-parse stx
[(_ name-pat) #'(Name: name-pat _ _ _)])))
;; alternative to Name: that only matches struct names
(define-match-expander Name/struct:
(λ (stx)
(syntax-parse stx
[(_) #'(Name: _ _ _ #t)]
[(_ name-pat) #'(Name: name-pat _ _ #t)])))

View File

@ -239,6 +239,10 @@
(define/decl -syntax-e (make-SyntaxPE)) (define/decl -syntax-e (make-SyntaxPE))
(define/decl -force (make-ForcePE)) (define/decl -force (make-ForcePE))
;; Type alias names
(define (-struct-name name)
(make-Name name null #f #t))
;; Structs ;; Structs
(define (-struct name parent flds [proc #f] [poly #f] [pred #'dummy]) (define (-struct name parent flds [proc #f] [poly #f] [pred #'dummy])
(make-Struct name parent flds proc poly pred)) (make-Struct name parent flds proc poly pred))

View File

@ -376,7 +376,7 @@
[(? Rep-stx a) (syntax->datum (Rep-stx a))] [(? Rep-stx a) (syntax->datum (Rep-stx a))]
[(Univ:) 'Any] [(Univ:) 'Any]
;; struct names are just printed as the original syntax ;; struct names are just printed as the original syntax
[(Name: id _ _ #t) (syntax-e id)] [(Name/struct: id) (syntax-e id)]
;; If a type has a name, then print it with that name. ;; If a type has a name, then print it with that name.
;; However, we expand the alias in some cases ;; However, we expand the alias in some cases
;; (i.e., the fuel is > 0) for the :type form. ;; (i.e., the fuel is > 0) for the :type form.

View File

@ -38,7 +38,7 @@
[(list _ (F: _)) #t] [(list _ (F: _)) #t]
[(list (Opaque: _) _) #t] [(list (Opaque: _) _) #t]
[(list _ (Opaque: _)) #t] [(list _ (Opaque: _)) #t]
[(list (Name: n _ _ _) (Name: n* _ _ _)) [(list (Name/simple: n) (Name/simple: n*))
(or (free-identifier=? n n*) (or (free-identifier=? n n*)
(overlap (resolve-once t1) (resolve-once t2)))] (overlap (resolve-once t1) (resolve-once t2)))]
[(list _ (Name: _ _ _ _)) [(list _ (Name: _ _ _ _))

View File

@ -29,8 +29,8 @@
(define (resolve-name t) (define (resolve-name t)
(match t (match t
[(Name: n _ _ _) (let ([t (lookup-type-name n)]) [(Name/simple: n) (let ([t (lookup-type-name n)])
(if (Type/c? t) t #f))] (if (Type/c? t) t #f))]
[_ (int-err "resolve-name: not a name ~a" t)])) [_ (int-err "resolve-name: not a name ~a" t)]))
(define already-resolving? (make-parameter #f)) (define already-resolving? (make-parameter #f))
@ -43,7 +43,7 @@
(unless (= n (length rands)) (unless (= n (length rands))
(tc-error "wrong number of arguments to polymorphic type: expected ~a and got ~a" (tc-error "wrong number of arguments to polymorphic type: expected ~a and got ~a"
n (length rands)))] n (length rands)))]
[(Name: n _ _ #t) [(Name/struct: n)
(when (and (current-poly-struct) (when (and (current-poly-struct)
(free-identifier=? n (poly-name (current-poly-struct)))) (free-identifier=? n (poly-name (current-poly-struct))))
(define num-rands (length rands)) (define num-rands (length rands))
@ -115,7 +115,7 @@
[already-resolving? #t]) [already-resolving? #t])
(resolve-app-check-error rator rands stx) (resolve-app-check-error rator rands stx)
(match rator (match rator
[(Name: _ _ _ _) [(? Name?)
(let ([r (resolve-name rator)]) (let ([r (resolve-name rator)])
(and r (resolve-app r rands stx)))] (and r (resolve-app r rands stx)))]
[(Poly: _ _) (instantiate-poly rator rands)] [(Poly: _ _) (instantiate-poly rator rands)]
@ -137,7 +137,7 @@
[(Mu: _ _) (unfold t)] [(Mu: _ _) (unfold t)]
[(App: r r* s) [(App: r r* s)
(resolve-app r r* s)] (resolve-app r r* s)]
[(Name: _ _ _ _) (resolve-name t)])]) [(? Name?) (resolve-name t)])])
(when (and r* (when (and r*
(not (currently-subtyping?))) (not (currently-subtyping?)))
(hash-set! resolver-cache seq r*)) (hash-set! resolver-cache seq r*))

View File

@ -178,9 +178,9 @@
(lambda (stx) (lambda (stx)
(syntax-case stx () (syntax-case stx ()
[(_ i) [(_ i)
#'(or (and (Name: _ _ _ #t) #'(or (and (Name/struct:)
(app resolve-once (? Struct? i))) (app resolve-once (? Struct? i)))
(App: (and (Name: _ _ _ #t) (App: (and (Name/struct:)
(app resolve-once (Poly: _ (? Struct? i)))) (app resolve-once (Poly: _ (? Struct? i))))
_ _))]))) _ _))])))
@ -210,7 +210,7 @@
(or (free-identifier=? s-name p-name) (or (free-identifier=? s-name p-name)
(match s (match s
[(Poly: _ (? Struct? s*)) (in-hierarchy? s* par)] [(Poly: _ (? Struct? s*)) (in-hierarchy? s* par)]
[(Struct: _ (and (Name: _ _ _ #t) p) _ _ _ _) [(Struct: _ (and (Name/struct:) p) _ _ _ _)
(in-hierarchy? (resolve-once p) par)] (in-hierarchy? (resolve-once p) par)]
[(Struct: _ (? Struct? p) _ _ _ _) (in-hierarchy? p par)] [(Struct: _ (? Struct? p) _ _ _ _) (in-hierarchy? p par)]
[(Struct: _ (Poly: _ p) _ _ _ _) (in-hierarchy? p par)] [(Struct: _ (Poly: _ p) _ _ _ _) (in-hierarchy? p par)]

View File

@ -8,10 +8,10 @@
make-Union))) make-Union)))
(begin-for-syntax (begin-for-syntax
(define -pict (make-Name #'pict null #f #t)) (define -pict (-struct-name #'pict))
(define -pict-path (define -pict-path
(make-Union (list (-val #f) -pict (-lst -pict)))) (make-Union (list (-val #f) -pict (-lst -pict))))
(define -child (make-Name #'child null #f #t)) (define -child (-struct-name #'child))
(define -linestyle (define -linestyle
(one-of/c 'transparent 'solid 'xor 'hilite (one-of/c 'transparent 'solid 'xor 'hilite
'dot 'long-dash 'short-dash 'dot-dash 'dot 'long-dash 'short-dash 'dot-dash

View File

@ -1907,7 +1907,7 @@
-Void) -Void)
[tc-e (raise (exn:fail:contract "1" (current-continuation-marks))) (t:Un)] [tc-e (raise (exn:fail:contract "1" (current-continuation-marks))) (t:Un)]
[tc-err (exn:fail:contract) [tc-err (exn:fail:contract)
#:ret (ret (resolve (make-Name #'exn:fail:contract null #f #t)))] #:ret (ret (resolve (-struct-name #'exn:fail:contract)))]
[tc-e (#%variable-reference) -Variable-Reference] [tc-e (#%variable-reference) -Variable-Reference]
[tc-e (#%variable-reference x) -Variable-Reference] [tc-e (#%variable-reference x) -Variable-Reference]
[tc-e (#%variable-reference +) -Variable-Reference] [tc-e (#%variable-reference +) -Variable-Reference]