Skip to content

Commit 4094b10

Browse files
Automated Resyntax fixes (#1452)
* Fix 14 occurrences of `single-clause-match-to-match-define` This `match` expression can be simplified using `match-define`. * Fix 1 occurrence of `define-simple-macro-to-define-syntax-parse-rule` The `define-simple-macro` form has been renamed to `define-syntax-parse-rule`. * Fix 1 occurrence of `equal-null-list-to-null-predicate` The `null?` predicate can be used to test for the empty list. * Fix 4 occurrences of `cond-let-to-cond-define` Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Fix 5 occurrences of `define-lambda-to-define` The `define` form supports a shorthand for defining functions. * Fix 8 occurrences of `let-to-define` Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Fix 3 occurrences of `let-values-then-call-to-call-with-values` This `let-values` expression can be replaced with a simpler, equivalent `call-with-values` expression. * Fix 1 occurrence of `inverted-when` This negated `when` expression can be replaced by an `unless` expression. * Fix 2 occurrences of `syntax-disarm-migration` The `syntax-disarm` function is a legacy function that does nothing. * Fix 2 occurrences of `if-else-false-to-and` This `if` expression can be refactored to an equivalent expression using `and`. * Fix 2 occurrences of `map-to-for` This `map` operation can be replaced with a `for/list` loop. * Fix 1 occurrence of `for/fold-result-keyword` Only one of the `for/fold` expression's result values is used. Use the `#:result` keyword to return just that result. * Fix 1 occurrence of `append*-and-map-to-append-map` The `append-map` function can be used to map each element into multiple elements in a single pass. * Fix 2 occurrences of `cond-else-if-to-cond` The `else`-`if` branch of this `cond` expression can be collapsed into the `cond` expression. * Fix 1 occurrence of `always-throwing-if-to-when` Using `when` and `unless` is simpler than a conditional with an always-throwing branch. * Fix 1 occurrence of `quasiquote-to-list` This quasiquotation is equialent to a simple `list` call. * Fix 1 occurrence of `let-to-define` Internal definitions are recommended instead of `let` expressions, to reduce nesting. --------- Co-authored-by: resyntax-ci[bot] <181813515+resyntax-ci[bot]@users.noreply.github.com>
1 parent 824a5d1 commit 4094b10

File tree

20 files changed

+296
-351
lines changed

20 files changed

+296
-351
lines changed

typed-racket-lib/typed-racket/infer/constraints.rkt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525

2626
;; add the constraints S <: var <: T to every map in cs
2727
(define (insert cs var S T)
28-
(match cs
29-
[(struct cset (maps))
30-
(make-cset (for/list ([map-entry (in-list maps)])
31-
(match-define (cons map dmap) map-entry)
32-
(cons (hash-set map var (make-c S T))
33-
dmap)))]))
28+
(match-define (struct cset (maps)) cs)
29+
(make-cset (for/list ([map-entry (in-list maps)])
30+
(match-define (cons map dmap) map-entry)
31+
(cons (hash-set map var (make-c S T)) dmap))))
3432

3533
;; meet: Type Type -> Type
3634
;; intersect the given types, producing the greatest lower bound
@@ -86,8 +84,8 @@
8684
;; produces a cset of all of the maps in all of the given csets
8785
;; FIXME: should this call `remove-duplicates`?
8886
(define (cset-join l)
89-
(let ([mapss (map cset-maps l)])
90-
(make-cset (apply stream-append mapss))))
87+
(define mapss (map cset-maps l))
88+
(make-cset (apply stream-append mapss)))
9189

9290
(define (stream-remove-duplicates st)
9391
(define seen (mutable-set))

typed-racket-lib/typed-racket/infer/infer-unit.rkt

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -65,34 +65,28 @@
6565
[indices (listof symbol?)]) #:transparent)
6666

6767
(define (context-add-vars ctx vars)
68-
(match ctx
69-
[(context V X Y)
70-
(context V (append vars X) Y)]))
68+
(match-define (context V X Y) ctx)
69+
(context V (append vars X) Y))
7170

7271
(define (context-add-var ctx var)
73-
(match ctx
74-
[(context V X Y)
75-
(context V (cons var X) Y)]))
72+
(match-define (context V X Y) ctx)
73+
(context V (cons var X) Y))
7674

7775
(define (context-add ctx #:bounds [bounds empty] #:vars [vars empty] #:indices [indices empty])
78-
(match ctx
79-
[(context V X Y)
80-
(context (append bounds V) (append vars X) (append indices Y))]))
76+
(match-define (context V X Y) ctx)
77+
(context (append bounds V) (append vars X) (append indices Y)))
8178

8279
(define (inferable-index? ctx bound)
83-
(match ctx
84-
[(context _ _ Y)
85-
(memq bound Y)]))
80+
(match-define (context _ _ Y) ctx)
81+
(memq bound Y))
8682

8783
(define ((inferable-var? ctx) var)
88-
(match ctx
89-
[(context _ X _)
90-
(memq var X)]))
84+
(match-define (context _ X _) ctx)
85+
(memq var X))
9186

9287
(define (empty-cset/context ctx)
93-
(match ctx
94-
[(context _ X Y)
95-
(empty-cset X Y)]))
88+
(match-define (context _ X Y) ctx)
89+
(empty-cset X Y))
9690

9791

9892

@@ -766,9 +760,8 @@
766760
(list values -Nat)))
767761
(define type
768762
(for/or ([pred-type (in-list possibilities)])
769-
(match pred-type
770-
[(list pred? type)
771-
(and (pred? n) type)])))
763+
(match-define (list pred? type) pred-type)
764+
(and (pred? n) type)))
772765
(cgen/seq context (seq (list type) -null-end) ts*)]
773766
;; numeric? == #true
774767
[((Base-bits: #t _) (SequenceSeq: ts*))
@@ -917,16 +910,12 @@
917910
;; c : Constaint
918911
;; variance : Variance
919912
(define (constraint->type v variance)
920-
(match v
921-
[(c S T)
922-
(match variance
923-
[(? variance:const?) S]
924-
[(? variance:co?) S]
925-
[(? variance:contra?) T]
926-
[(? variance:inv?) (let ([gS (generalize S)])
927-
(if (subtype gS T)
928-
gS
929-
S))])]))
913+
(match-define (c S T) v)
914+
(match variance
915+
[(? variance:const?) S]
916+
[(? variance:co?) S]
917+
[(? variance:contra?) T]
918+
[(? variance:inv?) (let ([gS (generalize S)]) (if (subtype gS T) gS S))]))
930919

931920
;; Since we don't add entries to the empty cset for index variables (since there is no
932921
;; widest constraint, due to dcon-exacts), we must add substitutions here if no constraint
@@ -936,47 +925,40 @@
936925
(hash-union
937926
(for/hash ([v (in-list Y)]
938927
#:unless (hash-has-key? S v))
939-
(let ([var (hash-ref idx-hash v variance:const)])
940-
(values v
941-
(match var
942-
[(? variance:const?) (i-subst null)]
943-
[(? variance:co?) (i-subst null)]
944-
[(? variance:contra?) (i-subst/starred null Univ)]
945-
;; TODO figure out if there is a better subst here
946-
[(? variance:inv?) (i-subst null)]))))
928+
(define var (hash-ref idx-hash v variance:const))
929+
(values v
930+
(match var
931+
[(? variance:const?) (i-subst null)]
932+
[(? variance:co?) (i-subst null)]
933+
[(? variance:contra?) (i-subst/starred null Univ)]
934+
;; TODO figure out if there is a better subst here
935+
[(? variance:inv?) (i-subst null)])))
947936
S))
948937
(define (build-subst m)
949-
(match m
950-
[(cons cmap (dmap dm))
951-
(let* ([subst (hash-union
952-
(for/hash ([(k dc) (in-hash dm)])
953-
(define (c->t c) (constraint->type c (hash-ref idx-hash k variance:const)))
954-
(values
955-
k
956-
(match dc
957-
[(dcon fixed #f)
958-
(i-subst (map c->t fixed))]
959-
[(or (dcon fixed rest) (dcon-exact fixed rest))
960-
(i-subst/starred
961-
(map c->t fixed)
962-
(c->t rest))]
963-
[(dcon-dotted fixed dc dbound)
964-
(i-subst/dotted
965-
(map c->t fixed)
966-
(c->t dc)
967-
dbound)])))
968-
(for/hash ([(k v) (in-hash cmap)])
969-
(values k (t-subst (constraint->type v (hash-ref var-hash k variance:const))))))]
970-
[subst (for/fold ([subst subst]) ([v (in-list X)])
971-
(let ([entry (hash-ref subst v #f)])
972-
;; Make sure we got a subst entry for a type var
973-
;; (i.e. just a type to substitute)
974-
;; If we don't have one, there are no constraints on this variable
975-
(if (and entry (t-subst? entry))
976-
subst
977-
(hash-set subst v (t-subst Univ)))))])
978-
;; verify that we got all the important variables
979-
(extend-idxs subst))]))
938+
(match-define (cons cmap (dmap dm)) m)
939+
(let* ([subst (hash-union
940+
(for/hash ([(k dc) (in-hash dm)])
941+
(define (c->t c)
942+
(constraint->type c (hash-ref idx-hash k variance:const)))
943+
(values k
944+
(match dc
945+
[(dcon fixed #f) (i-subst (map c->t fixed))]
946+
[(or (dcon fixed rest) (dcon-exact fixed rest))
947+
(i-subst/starred (map c->t fixed) (c->t rest))]
948+
[(dcon-dotted fixed dc dbound)
949+
(i-subst/dotted (map c->t fixed) (c->t dc) dbound)])))
950+
(for/hash ([(k v) (in-hash cmap)])
951+
(values k (t-subst (constraint->type v (hash-ref var-hash k variance:const))))))]
952+
[subst (for/fold ([subst subst]) ([v (in-list X)])
953+
(define entry (hash-ref subst v #f))
954+
;; Make sure we got a subst entry for a type var
955+
;; (i.e. just a type to substitute)
956+
;; If we don't have one, there are no constraints on this variable
957+
(if (and entry (t-subst? entry))
958+
subst
959+
(hash-set subst v (t-subst Univ))))])
960+
;; verify that we got all the important variables
961+
(extend-idxs subst)))
980962
(if multiple-substitutions?
981963
(for/list ([md (in-stream (cset-maps C))])
982964
(build-subst md))

typed-racket-lib/typed-racket/infer/intersect.rkt

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,16 @@
116116
(nbits-intersect nbits1 nbits2))]
117117
[((BaseUnion: bbits nbits)
118118
(Base-bits: numeric? bits))
119-
(cond [numeric? (if (nbits-overlap? nbits bits)
120-
t2
121-
-Bottom)]
122-
[else (if (bbits-overlap? bbits bits)
123-
t2
124-
-Bottom)])]
119+
(cond
120+
[numeric? (if (nbits-overlap? nbits bits) t2 -Bottom)]
121+
[(bbits-overlap? bbits bits) t2]
122+
[else -Bottom])]
125123
[((Base-bits: numeric? bits)
126124
(BaseUnion: bbits nbits))
127-
(cond [numeric? (if (nbits-overlap? nbits bits)
128-
t1
129-
-Bottom)]
130-
[else (if (bbits-overlap? bbits bits)
131-
t1
132-
-Bottom)])]
125+
(cond
126+
[numeric? (if (nbits-overlap? nbits bits) t1 -Bottom)]
127+
[(bbits-overlap? bbits bits) t1]
128+
[else -Bottom])]
133129
[((BaseUnion-bases: bases1) t2)
134130
(apply Un (for/list ([b (in-list bases1)])
135131
(rec b t2 obj)))]
@@ -161,52 +157,58 @@
161157
;; If the back pointer is never used, we don't create a μ-type, we just
162158
;; return the result
163159
(define (resolvable-intersect initial-t1 initial-t2 seen obj additive?)
164-
(let ([t1 (if (resolvable? initial-t1)
165-
(resolve-once initial-t1)
166-
initial-t1)])
167-
(cond
168-
[(assoc (cons initial-t1 initial-t2) seen)
169-
;; we've seen these types before! -- use the stored symbol
170-
;; as a back pointer with an 'F' type (i.e. a type variable)
171-
=> (match-lambda
172-
[(cons _ record)
173-
;; record that we did indeed use the back
174-
;; pointer by set!-ing the flag
175-
(set-mcdr! record #t)
176-
(make-F (mcar record))])]
177-
;; if t1 is not a fully defined type, do the simple thing
178-
[(not t1) (if additive?
179-
(-unsafe-intersect initial-t1 initial-t2)
180-
initial-t1)]
181-
[else
182-
(let ([t2 (if (resolvable? initial-t2)
183-
(resolve-once initial-t2)
184-
initial-t2)])
185-
(cond
186-
;; if t2 is not a fully defined type, do the simple thing
187-
[(not t2) (if additive?
188-
(-unsafe-intersect t1 initial-t2)
189-
t1)]
190-
[else
191-
;; we've never seen these types together before! let's gensym a symbol
192-
;; so that if we do encounter them again, we can create a μ type.
193-
(define name (gensym 'rec))
194-
;; the 'record' contains the back pointer symbol we may or may not use in
195-
;; the car, and a flag for whether or not we actually used the back pointer
196-
;; in the cdr.
197-
(define record (mcons name #f))
198-
(define seen* (list* (cons (cons initial-t1 initial-t2) record)
199-
(cons (cons initial-t2 initial-t1) record)
200-
seen))
201-
(define t (cond
202-
[additive? (internal-intersect t1 t2 seen* obj)]
203-
[else (internal-restrict t1 t2 seen* obj)]))
160+
(define t1
161+
(if (resolvable? initial-t1)
162+
(resolve-once initial-t1)
163+
initial-t1))
164+
(cond
165+
[(assoc (cons initial-t1 initial-t2) seen)
166+
;; we've seen these types before! -- use the stored symbol
167+
;; as a back pointer with an 'F' type (i.e. a type variable)
168+
=>
169+
(match-lambda
170+
[(cons _ record)
171+
;; record that we did indeed use the back
172+
;; pointer by set!-ing the flag
173+
(set-mcdr! record #t)
174+
(make-F (mcar record))])]
175+
;; if t1 is not a fully defined type, do the simple thing
176+
[(not t1)
177+
(if additive?
178+
(-unsafe-intersect initial-t1 initial-t2)
179+
initial-t1)]
180+
[else
181+
(let ([t2 (if (resolvable? initial-t2)
182+
(resolve-once initial-t2)
183+
initial-t2)])
184+
(cond
185+
;; if t2 is not a fully defined type, do the simple thing
186+
[(not t2)
187+
(if additive?
188+
(-unsafe-intersect t1 initial-t2)
189+
t1)]
190+
[else
191+
;; we've never seen these types together before! let's gensym a symbol
192+
;; so that if we do encounter them again, we can create a μ type.
193+
(define name (gensym 'rec))
194+
;; the 'record' contains the back pointer symbol we may or may not use in
195+
;; the car, and a flag for whether or not we actually used the back pointer
196+
;; in the cdr.
197+
(define record (mcons name #f))
198+
(define seen*
199+
(list* (cons (cons initial-t1 initial-t2) record)
200+
(cons (cons initial-t2 initial-t1) record)
201+
seen))
202+
(define t
204203
(cond
205-
;; check if we used the backpointer, if so,
206-
;; make a recursive type using that name
207-
[(mcdr record) (make-Mu name t)]
208-
;; otherwise just return the result
209-
[else t])]))])))
204+
[additive? (internal-intersect t1 t2 seen* obj)]
205+
[else (internal-restrict t1 t2 seen* obj)]))
206+
(cond
207+
;; check if we used the backpointer, if so,
208+
;; make a recursive type using that name
209+
[(mcdr record) (make-Mu name t)]
210+
;; otherwise just return the result
211+
[else t])]))]))
210212

211213

212214
;; intersect

typed-racket-lib/typed-racket/infer/promote-demote.rkt

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
[var-demote (c:-> Type? (c:listof symbol?) Type?)])
1515

1616
(define (V-in? V . ts)
17-
(for/or ([e (in-list (append* (map fv ts)))])
17+
(for/or ([e (in-list (append-map fv ts))])
1818
(memq e V)))
1919

2020
;; get-propset : SomeValues -> PropSet
@@ -39,26 +39,12 @@
3939
;; arr? -> (or/c #f arr?)
4040
;; Returns the changed arr or #f if there is no arr above it
4141
(define (arr-change arr)
42-
(match arr
43-
[(Arrow: dom rst kws rng rng-T+)
44-
(cond
45-
[(apply V-in? V (get-propsets rng))
46-
#f]
47-
[(and (RestDots? rst)
48-
(memq (RestDots-nm rst) V))
49-
(make-Arrow
50-
(map contra dom)
51-
(contra (RestDots-ty rst))
52-
(map contra kws)
53-
(co rng)
54-
rng-T+)]
55-
[else
56-
(make-Arrow
57-
(map contra dom)
58-
(and rst (contra rst))
59-
(map contra kws)
60-
(co rng)
61-
rng-T+)])]))
42+
(match-define (Arrow: dom rst kws rng rng-T+) arr)
43+
(cond
44+
[(apply V-in? V (get-propsets rng)) #f]
45+
[(and (RestDots? rst) (memq (RestDots-nm rst) V))
46+
(make-Arrow (map contra dom) (contra (RestDots-ty rst)) (map contra kws) (co rng) rng-T+)]
47+
[else (make-Arrow (map contra dom) (and rst (contra rst)) (map contra kws) (co rng) rng-T+)]))
6248
(define (change-elems ts)
6349
(for/list ([t (in-list ts)])
6450
(if (V-in? V t)

0 commit comments

Comments
 (0)