Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TreeLists to Typed-Racket along with type-checking tests. #1439

Merged
merged 5 commits into from
Mar 21, 2025

Conversation

Ryan-Ficklin
Copy link
Contributor

Added TreeList as a type corresponding to the data structure provided by racket/treelist. The only support currently is for Immutable TreeLists. Types were provided for all immutable Treelist functions as described in Section 4.16 of The Racket Reference, with the exception of for/treelist and for*/treelist, as no other similar structures (i.e. sets) supported typings for their respective "for/" functions. Functions treelist-append* and treelist-flatten return some undesirable/inelegant types, but I do not know how to add a proper level of specificity. Tests for all supported functions are provided in typed-racket-test/unit-tests/typecheck-tests.rkt.

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
@jbclements jbclements requested a review from samth March 3, 2025 19:12
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 10 files in this pull request and found no issues.

Copy link
Contributor

@bennn bennn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base-env comments

@jbclements
Copy link
Contributor

It now occurs to me that at some point in the future we might want fixed-length treelist types as we have for lists, e.g. (List Symbol Symbol Integer). I don't see a problem with those, but it sounds like we can just leave that for future work?

@jbclements
Copy link
Contributor

Also, I think this is the first time I've ever seen the "Resolve Conversation" button. Sorry to be such a n00b, do we ... use this mechanism?

@samth
Copy link
Member

samth commented Mar 4, 2025

Yes, I think deferring fixed-length treelists is fine; I don't think we really want to encourage that use of them anyway.

@jbclements
Copy link
Contributor

Oh, I see, "resolve conversation" essentially just collapses a subthread, with a note of who did it. (per https://github.blog/changelog/2018-08-31-resolvable-conversations/ )

…ToDos regarding Mutable/Immutable TreeLists, added shallow annotations
@samth
Copy link
Member

samth commented Mar 5, 2025 via email

@jbclements
Copy link
Contributor

I'm going to go ahead and merge this, tell me if I blew it!

@samth
Copy link
Member

samth commented Mar 7, 2025

Please do not merge changes if the tests have not passed yet.

@NoahStoryM
Copy link
Contributor

I think the types of treelist-empty? and treelist-length should be like this:

[treelist-empty? (-> (-treelist Univ) B : (-treelist (Un)))]
[treelist-length (-> (-treelist Univ) -Index)]

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 10 files in this pull request and found no issues.

@samth
Copy link
Member

samth commented Mar 7, 2025

The tests seem to be failing because it says TreeListof is not documented: https://github.com/racket/typed-racket/actions/runs/13665145959/job/38355185672?pr=1439#step:13:8

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 10 files in this pull request and found no issues.

@samth
Copy link
Member

samth commented Mar 11, 2025

The error message here is quite worrying, since that should not happen: https://github.com/racket/typed-racket/actions/runs/13796457815/job/38589419249#step:7:27

I think the problem is that you need to make changes in types/subtype.rkt.

@Ryan-Ficklin
Copy link
Contributor Author

I think the types of treelist-empty? and treelist-length should be like this:

[treelist-empty? (-> (-treelist Univ) B : (-treelist (Un)))]
[treelist-length (-> (-treelist Univ) -Index)]

I think this is right, but doing so causes:
(treelist-length (treelist "a" "b"))
to throw type error
Expected: TreeListof Any
Actual: TreeListof String
This is odd because TreeLists are marked as covariant in static-contracts/combinators/structural and in rep/type-rep, so I would assume that those types would work...

@samth
Copy link
Member

samth commented Mar 11, 2025

You should put some tests for subtyping in typed-racket-tests/unit-tests/subtype-tests and see if they work, and if not see about fixing that.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 10 files in this pull request and found no issues.

@Ryan-Ficklin
Copy link
Contributor Author

You should put some tests for subtyping in typed-racket-tests/unit-tests/subtype-tests and see if they work, and if not see about fixing that.

I see now. I will fix this and change the types for length and empty? back to Univ instead of the parametric polymorphic

@jbclements
Copy link
Contributor

Okay, so maybe I'm just going down a rabbit-hole here, but it appears that you can provide a slightly stronger negative condition for treelist-empty?. Specifically, @NoahStoryM you suggest

[treelist-empty? (-> (-treelist Univ) B : (-treelist (Un)))]

(To check here: (Un) is an empty union, meaning something like (TreeListof Nothing), right?)

But since treelist-empty? signals an error on non-treelists, it seems that in the negative case, you know not only that the result is not a (TreeListof Nothing), but also that the result is in fact a TreeList! This suggests to me the type

(: treelist-empty? ((TreeListof Any) -> Boolean : #:+ (TreeListof Nothing) #:- (TreeListof Any)))

(I'm writing this using TR surface syntax just because I'm more confident I can express what I mean.)

@samth
Copy link
Member

samth commented Mar 14, 2025

You are correct, but there's no point since we already know that the input is a (TreeListof Any). In general I don't think there's any value in having predicates in the type of treelist-empty?.

@NoahStoryM
Copy link
Contributor

NoahStoryM commented Mar 17, 2025

I think the type of treelist-empty? can indeed be further refined:

(: treelist-empty?
   (case-> (-> (TreeListof Any) Boolean : (TreeListof Nothing))
           (-> Any Boolean : #:+ (TreeListof Nothing) #:- (TreeListof Any))))

This way, it seems we might not need to use treelist? in many scenarios. However, I'm not sure if this is worthwhile: it means the type checker can't statically check expressions like (treelist-empty? 123).

@capfredf
Copy link
Member

capfredf commented Mar 18, 2025

In general I don't think there's any value in having predicates in the type of treelist-empty?.

I agree with Sam here, since it seems that there are no operations that only work for an empty treelist.

@Ryan-Ficklin
Copy link
Contributor Author

TreeList subtyping should be fixed now. I wrote a few tests for subtyping, mostly as sanity checks, which didn't pass at first, but do now.

I changed the base-env.rkt definitions of treelist-length and treelist-empty? to take (TreeListof Any) instead of using polymorphic type pattern. However, I did not add any predicates to these new definitions. I can add those if desired, but from what I can see no other empty?-like functions are given predicates in base-env.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 12 files in this pull request and found no issues.

@samth samth merged commit 8257245 into racket:master Mar 21, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants