Skip to content

Commit 8257245

Browse files
Ryan-FicklinRyan Ficklin
andauthored
Added TreeLists to Typed-Racket along with type-checking tests. (#1439)
* Added TreeLists to Typed-Racket along with type-checking tests. --------- Co-authored-by: Ryan Ficklin <[email protected]>
1 parent 0420112 commit 8257245

File tree

17 files changed

+224
-0
lines changed

17 files changed

+224
-0
lines changed

typed-racket-doc/typed-racket/scribblings/reference/types.scrbl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ corresponding to @racket[trest], where @racket[bound]
352352
@deftypeconstr[(MListof t)]{Returns the type of a homogeneous @rtech{mutable list} of @racket[t].}
353353
@deftypeconstr[(MPairof t u)]{Returns the type of a @rtech{Mutable pair} of @racket[t] and @racket[u].}
354354

355+
@deftypeconstr[(TreeListof t)]{Returns the type of @rtech{treelist} of @racket[t]}
356+
355357
@deftype[MPairTop]{Is the type of a @rtech{mutable pair} with unknown
356358
element types and is the supertype of all mutable pair types.
357359
This type typically appears in programs via the combination of

typed-racket-lib/typed-racket/base-env/base-env.rkt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
racket/unsafe/ops
99
racket/unsafe/undefined
1010
racket/hash
11+
racket/treelist
1112
(only-in racket/extflonum floating-point-bytes->extfl extfl->floating-point-bytes)
1213
;(only-in rnrs/lists-6 fold-left)
1314
'#%paramz
@@ -1294,6 +1295,56 @@
12941295

12951296
[unsafe-undefined -Unsafe-Undefined]
12961297

1298+
;; Section 4.21 (TreeLists)
1299+
1300+
[treelist (-poly (a) (->* (list) a (-treelist a)))]
1301+
[treelist-empty? (-> (-treelist Univ) B)]
1302+
[treelist-length (-> (-treelist Univ) -Index)]
1303+
[treelist-member?
1304+
(-poly (a)
1305+
(cl->* ((-treelist a) a . -> . Univ)
1306+
((-treelist a) a (-> a a Univ) . -> . B)))]
1307+
[treelist-first (-poly (a) (-> (-treelist a) a :T+ #f))]
1308+
[treelist-last (-poly (a) (-> (-treelist a) a :T+ #f))]
1309+
[treelist-rest (-poly (a) (-> (-treelist a) (-treelist a)))]
1310+
[treelist-add (-poly (a) (-> (-treelist a) a (-treelist a)))]
1311+
[treelist-cons (-poly (a) (-> (-treelist a) a (-treelist a)))]
1312+
[treelist-delete (-poly (a) (-> (-treelist a) -Index (-treelist a)))]
1313+
[make-treelist (-poly (a) (-> -Nat a (-treelist a)))]
1314+
[treelist-ref (-poly (a) (-> (-treelist a) -Index a :T+ #f))]
1315+
[treelist-insert (-poly (a) (-> (-treelist a) -Index a (-treelist a)))]
1316+
[treelist-set (-poly (a) (-> (-treelist a) -Index a (-treelist a)))]
1317+
[treelist-take (-poly (a) (-> (-treelist a) -Index (-treelist a)))]
1318+
[treelist-drop (-poly (a) (-> (-treelist a) -Index (-treelist a)))]
1319+
[treelist-take-right (-poly (a) (-> (-treelist a) -Index (-treelist a)))]
1320+
[treelist-drop-right (-poly (a) (-> (-treelist a) -Index (-treelist a)))]
1321+
[treelist-sublist (-poly (a) (-> (-treelist a) -Index -Index (-treelist a)))]
1322+
[treelist-reverse (-poly (a) (-> (-treelist a) (-treelist a)))]
1323+
[treelist->list (-poly (a) (-> (-treelist a) (-lst a)))]
1324+
[list->treelist (-poly (a) (-> (-lst a) (-treelist a)))]
1325+
[treelist->vector (-poly (a) (-> (-treelist a) (-vec a)))]
1326+
[vector->treelist (-poly (a) (-> (-vec a) (-treelist a)))]
1327+
[in-treelist (-poly (a) (-> (-treelist a) (-seq a)))]
1328+
[treelist? (unsafe-shallow:make-pred-ty (-treelist Univ))]
1329+
[treelist-append (-poly (a) (->* (list) (-treelist a) (-treelist a)))]
1330+
[treelist-map (-poly (a b) (-> (-treelist a) (-> a b) (-treelist b)))]
1331+
[treelist-for-each (-poly (a b) (-> (-treelist a) (-> a b) -Void))]
1332+
[treelist-filter (-poly (a) (-> (-> a Univ) (-treelist a) (-treelist a)))]
1333+
[treelist-find (-poly (a) (-> (-treelist a) (-> a Univ) a :T+ #f))]
1334+
[treelist-index-of
1335+
(-poly (a)
1336+
(cl->* ((-treelist a) a . -> . -Index)
1337+
((-treelist a) a (-> a a Univ) . -> . -Index)))]
1338+
[treelist-flatten (Univ . -> . (-treelist Univ))]
1339+
[treelist-append* (-poly (a) (-> (-treelist (-treelist a)) (-treelist a)))]
1340+
[treelist-sort
1341+
(-poly
1342+
(a b)
1343+
(cl->*
1344+
(->key (-treelist a) (-> a a -Boolean) #:key (-opt (-> a a :T+ #f)) #f #:cache-keys? -Boolean #f (-treelist a))
1345+
(->key (-treelist a) (-> b b -Boolean) #:key (-> a b :T+ #f) #t #:cache-keys? -Boolean #f (-treelist a))))]
1346+
1347+
12971348
;; Section 5.2 (Structure Types)
12981349
[make-struct-type
12991350
(->opt -Symbol

typed-racket-lib/typed-racket/base-env/base-types.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
[Async-Channelof -async-channel]
198198
[Ephemeronof -Ephemeron]
199199
[Setof -set]
200+
[TreeListof -treelist]
200201
[Evtof -evt]
201202
[Continuation-Mark-Set -Cont-Mark-Set]
202203
[False -False]

typed-racket-lib/typed-racket/env/init-envs.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@
155155
`(-CustodianBox ,(type->sexp ty))]
156156
[(Set: ty)
157157
`(make-Set ,(type->sexp ty))]
158+
[(TreeList: ty)
159+
`(make-TreeList ,(type->sexp ty))]
158160
[(Evt: ty)
159161
`(make-Evt ,(type->sexp ty))]
160162
[(Future: ty)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,8 @@
827827
(cg e e*)]
828828
[((Set: a) (Set: a*))
829829
(cg a a*)]
830+
[((TreeList: a) (TreeList: a*))
831+
(cg a a*)]
830832
[((Evt: a) (Evt: a*))
831833
(cg a a*)]
832834
[((? Base:Semaphore?) (Evt: t))

typed-racket-lib/typed-racket/private/type-contract.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
racket/format
4343
racket/string
4444
racket/set
45+
racket/treelist
4546
syntax/flatten-begin
4647
(only-in "../types/abbrev.rkt" -Bottom -Boolean)
4748
"../static-contracts/instantiate.rkt"
@@ -564,6 +565,7 @@
564565
[(? Fun? t) (t->sc/fun t)]
565566
[(? DepFun? t) (t->sc/fun t)]
566567
[(Set: t) (set/sc (t->sc t))]
568+
[(TreeList: t) (treelist/sc (t->sc t))]
567569
[(Sequence: (list t))
568570
#:when (subtype t:-Nat t)
569571
;; sequence/c is always a wrapper, so avoid it when we just have a number
@@ -930,6 +932,7 @@
930932
none/sc
931933
(make-procedure-arity-flat/sc num-mand-args '() '()))]
932934
[(Set: _) set?/sc]
935+
[(TreeList: _) treelist?/sc]
933936
[(or (Sequence: _)
934937
(SequenceTop:)
935938
(SequenceDots: _ _ _))

typed-racket-lib/typed-racket/rep/type-mask.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
mask:future
144144
mask:other-box
145145
mask:set
146+
mask:treelist
146147
mask:procedure
147148
mask:prompt-tag
148149
mask:continuation-mark-key

typed-racket-lib/typed-racket/rep/type-rep.rkt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@
353353
(def-structural Set ([elem #:covariant])
354354
[#:mask mask:set])
355355

356+
;;------
357+
;; Treelist (Immutable)
358+
;;------
359+
360+
(def-structural TreeList ([elem #:covariant])
361+
[#:mask mask:treelist])
362+
356363
;;------------
357364
;; HashTable
358365
;;------------

typed-racket-lib/typed-racket/static-contracts/combinators/derived.rkt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
racket/list
1212
racket/promise
1313
racket/set
14+
racket/treelist
1415
racket/unit)
1516
"simple.rkt"
1617
"structural.rkt")
@@ -30,6 +31,9 @@
3031
(define set?/sc (flat/sc #'(lambda (x) (or (set? x) (set-mutable? x) (set-weak? x)))))
3132
(define empty-set/sc (and/sc set?/sc (flat/sc #'set-empty?)))
3233

34+
(define treelist?/sc (flat/sc #'treelist?))
35+
(define empty-treelist/sc (and/sc treelist?/sc (flat/sc #'treelist-empty?)))
36+
3337
(define vector?/sc (flat/sc #'vector?))
3438
(define immutable-vector?/sc (and/sc vector?/sc
3539
(flat/sc #'immutable?)))

typed-racket-lib/typed-racket/static-contracts/combinators/structural.rkt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
racket/match
1010
(for-syntax racket/base racket/syntax syntax/stx syntax/parse)
1111
racket/set
12+
racket/treelist
1213
racket/sequence
1314
(for-template racket/base
1415
racket/contract/base
1516
racket/set
17+
racket/treelist
1618
racket/async-channel
1719
racket/sequence
1820
racket/promise
@@ -160,6 +162,7 @@
160162
((listof/sc (#:covariant)) listof #:flat)
161163
((cons/sc (#:covariant) (#:covariant)) cons/c #:flat)
162164
((set/sc (#:covariant #:chaperone)) set/c #:flat)
165+
((treelist/sc (#:covariant #:chaperone)) treelist/c #:flat)
163166
((struct-property/sc (#:invariant)) struct-type-property/c #:impersonator)
164167
((immutable-vectorof/sc (#:covariant)) immutable-vectorof/c #:flat)
165168
((mutable-vectorof/sc (#:invariant)) mutable-vectorof/c #:chaperone)

0 commit comments

Comments
 (0)