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

Solver losing assumptions in parameterized modules #883

Closed
robdockins opened this issue Sep 8, 2020 · 4 comments
Closed

Solver losing assumptions in parameterized modules #883

robdockins opened this issue Sep 8, 2020 · 4 comments
Assignees
Labels
parameterized modules Related to Cryptol's parameterized modules typechecker Issues related to type-checking Cryptol code.
Milestone

Comments

@robdockins
Copy link
Contributor

Attempting to load the following file results in an error. It seems like the constraint solver is somehow losing track of some assumptions, because I believe this should be trivial. I can't seem to reproduce this bug without involving module parameters, so there seems to be some interaction there.

module Main where
parameter
    type c : #
    type constraint ( fin c, c >= 1, 64 >= width (c-1) )

dumbHash : {n} (fin n, 64 >= width n) => [n] -> [128]
dumbHash msg = foldl (^) 0 xs
  where
    xs : [(n + 64) /^ 128][128]
    xs = groupBy`{128} (msg # zero # len)

    len = `n : [64]

asdf : {n} (64 >= width (n+192), 64 >= width (128 * c), fin n) => [n] -> [128]
asdf msg = dumbHash (join (drop`{1} ys))
  where
    ys : [c+1][128]
    ys = [ fromInteger 42 ] # [ dumbHash (msg#x#y) | x <- [ 0 .. (c-1):[64] ] | y <- ys ]
[error] at widthbug.cry:1:1--18:90:
  Failed to validate user-specified signature.
    when checking the module's parameters,
    we need to show that
      for any type Main::c
      assuming
        • fin Main::c
        • Main::c >= 1
        • 64 >= width (Main::c - 1)
      the following constraints hold:
        • 64 >= width (128 * Main::c)
            arising from
            use of expression Main::dumbHash
            at widthbug.cry:15:12--15:20
@robdockins robdockins added typechecker Issues related to type-checking Cryptol code. parameterized modules Related to Cryptol's parameterized modules labels Sep 8, 2020
@bboston7
Copy link
Contributor

Here's another simple case for whoever tackles this issue. Take the parameterized module:

module parameterized where

parameter
  type w : #
  type constraint (fin w)

getZeros : [w/8][8]
getZeros = split`{each=8} zero

the instantiated module:

module instantiated = parameterized where

type w = 32

and the importing module:

module importer where

import instantiated

zeros : [4][8]
zeros = getZeros

Crypol will error out on the importing module:

[error] at ./importer.cry:6:9--6:17:
  Unsolved constraints:
    • 4 == instantiated::w / 8
        arising from
        matching types
        at ./importer.cry:6:9--6:17

@atomb atomb added this to the 2.10.0 milestone Sep 22, 2020
@yav yav self-assigned this Sep 22, 2020
yav added a commit that referenced this issue Nov 11, 2020
This fixes #796.
It also fixes the @bboston7's example on #883, but not the original
example in #883.   The issue there is that `asdf` function generates
a constraint only involving the module parameter.

Normally we don't try to solve constraint that don't mention a schema's
parameters, because they can always be solved "later", and hopefully with
more things instantiated.

The weird thing in this case is that the schema adds *local" constraint to
the module parameters, but we end up ignoring these.
@yav
Copy link
Member

yav commented Nov 11, 2020

I have a pull request that fixes @bboston7's example, but the issue in the original example is only somewhat related to the module system. Here is an example of the same sort of issue without the module system:

f : {a} a
f = g
  where
  g :  (Literal 1 a) => a
  g = 1

This fails for much the same reason as @robdockins original example:

[error] at test.cry:3:1--6:8:
  Failed to validate user-specified signature.
    in the definition of 'Main::f', at test.cry:3:1--3:2,
    we need to show that
      for any type a
      the following constraints hold:
        • Literal 1 a
            arising from
            use of expression Main::g
            at test.cry:3:5--3:6

Basically the problem is when a schema introduces local assumptions that do not mention any of the schema's variables. The
constraint 64 >= width (128 * c) is just like that in the original example. The confusion arises because, normally, if the definition of a function gives rise to a constraint that does not mention any of the schema's variables we don't try to solve the constraint, but instead "delay" it, in the hopes of more unification variables being instantiated by the time we get to it.

I can see two solutions here:

  1. Modify the algorithm to be more aggressive and try to solve goals even if they don't mention any quantified variables, if they do mention some of the "local" constraints. This kind of make sense, especially if the goal doesn't actually have any free unification variables so delaying it doesn't buy us anything.
  2. Disallow local constraints that do not mention any of the quantified variables. Generally, such constraints are pretty suspect, I think. For example, in the original example one could instantiate the module with parameters that would make asdf unusable...

@robdockins @brianhuffman thoughts?

@yav
Copy link
Member

yav commented Nov 12, 2020

I ended up doing the more aggressive checking---it was a small change---and now the original example works.

By the way, the example I gave above is not exactly equivalent to Rob's original one, as of course the use of g gives rise to another Literal constraint. A more precise version would be something like this:

f : {a : *} ()
f = ()
  where
  g : (Literal 1 a) => a
  g = 1

@yav
Copy link
Member

yav commented Nov 12, 2020

Should be fixed via #959

@yav yav closed this as completed Nov 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parameterized modules Related to Cryptol's parameterized modules typechecker Issues related to type-checking Cryptol code.
Projects
None yet
Development

No branches or pull requests

4 participants