Skip to content

Commit 1ab8f95

Browse files
author
Ryan Ficklin
committed
Added TreeLists to Typed-Racket along with type-checking tests.
update Added treelis?/sc removed autorec files added treelist functions to base-env Added tests for treelists, missing identifier issues Added Treelist to type-printer Added a few functions and fixed all TreeList typechecking tests
1 parent 0420112 commit 1ab8f95

File tree

12 files changed

+136
-0
lines changed

12 files changed

+136
-0
lines changed

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? (-poly (a) (-> (-treelist a) B))]
1302+
[treelist-length (-poly (a) (-> (-treelist a) -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))]
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))]
1334+
[treelist-index-of
1335+
(-poly (a)
1336+
(cl->* ((-treelist a) a . -> . -Index)
1337+
((-treelist a) a (-> a a Univ) . -> . -Index)))]
1338+
[treelist-flatten (-poly (a) (-> a (-treelist a)))] ;; This type isn't any more or less pratical than Univ, but Univ might not be sound
1339+
[treelist-append* (-poly (a) (-> (-treelist (-treelist a)) (-treelist a)))] ;; Same type specificty issue as above
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@
353353
(def-structural Set ([elem #:covariant])
354354
[#:mask mask:set])
355355

356+
;;------
357+
;; Treelist
358+
;;------
359+
360+
;; TODO separate mutable/immutable treelist types
361+
(def-structural TreeList ([elem #:covariant])
362+
[#:mask mask:treelist])
363+
356364
;;------------
357365
;; HashTable
358366
;;------------

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

Lines changed: 5 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,10 @@
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+
;; TODO: Split TreeList into Mutable and Immutable
35+
(define treelist?/sc (flat/sc #'treelist?))
36+
(define empty-treelist/sc (and/sc treelist?/sc (flat/sc #'treelist-empty?)))
37+
3338
(define vector?/sc (flat/sc #'vector?))
3439
(define immutable-vector?/sc (and/sc vector?/sc
3540
(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)

typed-racket-lib/typed-racket/types/abbrev.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
(define -thread-cell make-ThreadCell)
5050
(define -Promise make-Promise)
5151
(define -set make-Set)
52+
(define -treelist make-TreeList)
5253
(define -mvec make-Mutable-Vector)
5354
(define -ivec make-Immutable-Vector)
5455

0 commit comments

Comments
 (0)