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

Towards polymorphic sorts #542

Merged
merged 47 commits into from
Nov 2, 2023
Merged

Towards polymorphic sorts #542

merged 47 commits into from
Nov 2, 2023

Conversation

ranjitjhala
Copy link
Contributor

@ranjitjhala ranjitjhala commented Oct 17, 2023

Allow polymorphic sorts, see rset.rs, rmap.rs and rmapk.rs and clients.

@ranjitjhala
Copy link
Contributor Author

The rset00.rs tests work as expected. I'll probably add some more tests, and perhaps some WF/error message tests too, but I think you can take a look at this PR.

@ranjitjhala
Copy link
Contributor Author

Currently, we have to explicitly write the generic <Tiger> { elems : Set<Tiger>} in the refined_by clause.

Should we require this? (Without it, you get a weird crash later.) But OTOH we could plausibly automatically just "scrape" them from the actual sorts appearing in the refined_by ? e.g. Set<Tiger> I suppose the trouble is how do you know which one is supposed to be a generic and which one is just some other rust type?

#[flux::refined_by(<Tiger> { elems: Set<Tiger> })]
// #[flux::refined_by( elems: Set<Tiger> )] //~ ERROR unbound generic `Tiger`
pub struct RSet<Tiger> {
    pub inner: std::collections::HashSet<Tiger>,
}

@ranjitjhala
Copy link
Contributor Author

Maybe for now, better to be explicit and throw a WF error if there is an unbound generic.

@ranjitjhala
Copy link
Contributor Author

I decided to go the other way -- I removed the explicit params and instead just gather them from the sorts themselves.
The principle here -- perhaps inspired by the excessive bureaucracy at banks in India -- is we shouldn't be asking users to write down stuff that we can extract from other stuff they've already written down!

@ranjitjhala
Copy link
Contributor Author

@nilehmann - I think I'm done with this, ready for review.

@nilehmann
Copy link
Member

I'm going to take a look at this now

Copy link
Member

@nilehmann nilehmann left a comment

Choose a reason for hiding this comment

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

First batch of comments.

I've only looked until desugaring. Most comments are related to how we use the SortResolver. I think we could reuse it instead of creating a new one every time.

Also as a general comment about SortResolver. I think we never have a case where both generics and sort_vars are Some so we could expose that as two construction methods (not sure about the names)

impl<'a> SortResolver<'a> {
    fn with_sort_vars(sess: &'a FluxSession, sort_decls: &'a SortDecls, sort_vars: &[surface::Ident]) -> Self {
        ... 
    }
    
    // we grab `sort_decls` from `genv` and fetch generics of `owner_id` inside 
    fn with_generics(genv: &'a GlobalEnv<'a, '_>, owner_id: DefId) -> Self {
        ...
    }
}

edit: Just noticed that for qualifiers both generics and sort_vars are None, but the SortResolver for qualifier should morally be a SortResolver::with_sort_vars(sess, sort_decls, &[]).

crates/flux-syntax/src/grammar.lalrpop Outdated Show resolved Hide resolved
crates/flux-syntax/src/grammar.lalrpop Show resolved Hide resolved
crates/flux-desugar/src/lib.rs Show resolved Hide resolved
crates/flux-desugar/src/lib.rs Outdated Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Outdated Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Outdated Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Outdated Show resolved Hide resolved
Copy link
Member

@nilehmann nilehmann left a comment

Choose a reason for hiding this comment

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

@ranjitjhala Another batch of comments. Looking better!

I still haven't gone through the changes in flux-fhir-analysis and wf.

README.md Outdated Show resolved Hide resolved
crates/flux-driver/src/callbacks.rs Outdated Show resolved Hide resolved
crates/flux-middle/src/fhir.rs Outdated Show resolved Hide resolved
crates/flux-middle/src/fhir.rs Show resolved Hide resolved
crates/flux-middle/src/fhir.rs Outdated Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Outdated Show resolved Hide resolved
crates/flux-desugar/src/desugar.rs Outdated Show resolved Hide resolved
crates/flux-refineck/src/constraint_gen.rs Outdated Show resolved Hide resolved
crates/flux-middle/src/global_env.rs Outdated Show resolved Hide resolved
crates/flux-middle/src/global_env.rs Show resolved Hide resolved
Copy link
Member

@nilehmann nilehmann left a comment

Choose a reason for hiding this comment

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

Comments about fhir_analysis and wf

crates/flux-fhir-analysis/src/wf/mod.rs Outdated Show resolved Hide resolved
crates/flux-fhir-analysis/src/wf/sortck.rs Outdated Show resolved Hide resolved
crates/flux-fhir-analysis/src/wf/mod.rs Outdated Show resolved Hide resolved
crates/flux-fhir-analysis/src/wf/mod.rs Outdated Show resolved Hide resolved
crates/flux-fhir-analysis/src/wf/mod.rs Outdated Show resolved Hide resolved
crates/flux-fhir-analysis/src/wf/mod.rs Outdated Show resolved Hide resolved
Co-authored-by: Nico Lehmann <[email protected]>
@ranjitjhala
Copy link
Contributor Author

Ok I think I addressed the various comments; including representing the T inside refined_by with Var.
One unpleasant thing is that to do so, I'm now requiring an explicit list of sort_params i.e.

#[flux::refined_by(<K,V>{keys: Set<K>, vals: Map<K, V>})]

instead of automatically finding the K,V from the generics (its a bit tedious as you have to first "gather"
the relevant Vec<Ident> using the generics -- still, not impossible by any means...)

Not clear if we want to write this explicitly -- more the user has to write, the more opportunity for error!

Any thoughts?

Copy link
Member

@nilehmann nilehmann left a comment

Choose a reason for hiding this comment

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

Ok, this looks good. There are still many details I'm not happy with, but I'm ok with merging so we can move forward.

About the syntax, as always, I'm not too worried about it, we can always change it later. I'm more concerned about having enough pieces of the puzzle in place.

That being said, I do think is a bit redundant to annotate the parameters. It also makes it that you need to check more things. For example, this line will crash if you don't use the same names.

crates/flux-refineck/src/constraint_gen.rs Show resolved Hide resolved
crates/flux-middle/src/fhir.rs Outdated Show resolved Hide resolved
crates/flux-middle/src/fhir.rs Show resolved Hide resolved
crates/flux-middle/src/fhir.rs Outdated Show resolved Hide resolved
@ranjitjhala ranjitjhala merged commit a2612f5 into main Nov 2, 2023
4 checks passed
@ranjitjhala ranjitjhala deleted the rset branch November 2, 2023 18:43
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.

2 participants