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

Constraint Integral ([n]a) should imply a = Bit #760

Closed
brianhuffman opened this issue Jun 9, 2020 · 6 comments
Closed

Constraint Integral ([n]a) should imply a = Bit #760

brianhuffman opened this issue Jun 9, 2020 · 6 comments
Labels
typechecker Issues related to type-checking Cryptol code. wontfix For issues that we've reviewed and decided do not require changes.

Comments

@brianhuffman
Copy link
Contributor

Cryptol infers what I consider to be a too-general type for expressions involving shift/indexing operators:

Cryptol> :t \x -> 0b01 << [x]
(\x -> 0b01 << [x]) : {a} (Integral ([1]a)) => a -> [2]
Cryptol> :t \x -> 0b01 @ [x]
(\x -> 0b01 @ [x]) : {a} (Integral ([1]a)) => a -> Bit

I would expect that the constraint Integral ([n]a) should improve to the type equality a = Bool, and then be discharged.

@robdockins
Copy link
Contributor

This requires closed world assumptions about the class instances, which is pretty different from how I usually think about class inference. Do we do inference of this sort elsewhere in Cryptol already?

@brianhuffman
Copy link
Contributor Author

This seems similar to #265. Maybe there are reasons to infer such a general type, but it doesn't seem very user-friendly to me.

@brianhuffman
Copy link
Contributor Author

I don't think this actually requires a closed-world assumption. We just need to have a single Integral class instance rule for the list type, which is basically instance (fin n, a ~ Bool) => Integral ([n]a). So this situation is actually simpler than we had for #265, because when we see Integral ([n]a), there is exactly one instance rule that applies. (In #265, we couldn't simplify Arith ([n]a) because we have two overlapping instance rules Arith a => Arith ([n]a) and (fin n, a ~ Bool) => Arith ([n]a).)

@robdockins
Copy link
Contributor

Well, I guess that's true, you don't have any confluence, etc. issues if you phrase the instance that way.

However, I don't think we have a type equality constraint, even internally. It seems like it might be a pretty big change to add one.

@yav
Copy link
Member

yav commented Jun 9, 2020

We have equality on numeric types, and it wouldn't be hard to add it to value types too. It's not there because (1) we didn't really need it in the past, and (2) it allows you to write odd type schemes like:

{a} (a = Bool) => [8]a

which complicates typechecking.

My vote would be to add an equality predicate on value types, but to treat it as a bit of a 2nd class citizen, in that it should never end up in type schemes. In particular, users shouldn't be allowed to write them, and they should never end up in type schemes (i.e., trying to unify bound variables of kind * would result in an error just like it does now, rather than producing a delayed equality constraint).

@robdockins robdockins added the typechecker Issues related to type-checking Cryptol code. label Jun 19, 2020
@brianhuffman
Copy link
Contributor Author

Adding type equalities to the class system is probably too big of a change to be worth it just for this feature.

@brianhuffman brianhuffman added the wontfix For issues that we've reviewed and decided do not require changes. label Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typechecker Issues related to type-checking Cryptol code. wontfix For issues that we've reviewed and decided do not require changes.
Projects
None yet
Development

No branches or pull requests

3 participants