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

Cryptol-saw-core translator produces ill-typed terms from polymorphic recursive functions #1318

Closed
brianhuffman opened this issue Jun 4, 2021 · 3 comments · Fixed by #1319
Assignees

Comments

@brianhuffman
Copy link
Contributor

brianhuffman commented Jun 4, 2021

This problem was discovered as a test suite failure in #1317, which bumps cryptol to include the new sortBy function in the Cryptol prelude. Here's a self-contained example that you can do from the REPL:

sawscript> let {{ funpow f (n : Integer) x = if n <= 0 then x else funpow f (n - 1) (f x) }}
sawscript> type {{ funpow }}
[18:30:50.891] {a} (a -> a) -> Integer -> a -> a
sawscript> check_term {{ funpow }}

Stack trace:
"check_term" (<stdin>:1:1-1:11):
Inferred type
  sort 1
Not a subtype of expected type
  sort 0
For term
  (u957 : sort 0)
-> (u957
    -> u957)
-> Prelude.Integer
-> u957
-> u957
@brianhuffman brianhuffman self-assigned this Jun 4, 2021
@robdockins
Copy link
Contributor

It looks well-typed to me. I think the problem is just expecting it to have sort 0.

@brianhuffman
Copy link
Contributor Author

I believe that the primitive fix (which is used to translate recursive functions) just has a too-restrictive type:

primitive fix : (a : sort 0) -> (a -> a) -> a;

I think the type error message is really complaining about the argument to fix having the wrong type (sort 1 vs the expected sort 0 for argument a). By the way, we should try to include more location information on such error messages so that I don't have to guess about things like that.

@brianhuffman
Copy link
Contributor Author

brianhuffman commented Jun 4, 2021

Indeed, changing the type of fix to (a : sort 1) -> (a -> a) -> a fixes the problem:

sawscript> let {{ funpow f (n : Integer) x = if n <= 0 then x else funpow f (n - 1) (f x) }}
sawscript> check_term {{ funpow }}
[18:40:25.702] (u957 : sort 0)
-> (u957
    -> u957)
-> Prelude.Integer
-> u957
-> u957

The more general type is absolutely necessary if we want to be able to handle polymorphic recursion.

brianhuffman pushed a commit that referenced this issue Jun 4, 2021
Old type:
  primitive fix : (a : sort 0) -> (a -> a) -> a;

New type:
  primitive fix : (a : sort 1) -> (a -> a) -> a;

Fixes #1318.
@mergify mergify bot closed this as completed in #1319 Jun 4, 2021
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 a pull request may close this issue.

2 participants