Support for fixed-size integers #134
Replies: 5 comments 5 replies
-
Hi! I would suggest viewing the OCaml type The predicate
|
Beta Was this translation helpful? Give feedback.
-
On 17 February 2022 13:47:16 GMT, "Clément Pascutto" ***@***.***> wrote:
`max_int` and `min_int` already exist in the Gospel standard library ([here](https://github.com/ocaml-gospel/gospel/blob/main/src/stdlib/gospelstdlib.mli#L108-L109)).
What's wrong with using those directly?
I can use those definitions, but will likely need to add some range constraints separately (otherwise a solver can pick something like max_int=1 and min_int=2 in theory that could cause some pre/postcondition to fail).
Perhaps the gospel stdlib could have just some very minimal constraints: `min_int < 0 < max_int).
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
Beta Was this translation helpful? Give feedback.
-
On 17 February 2022 13:55:12 GMT, "Clément Pascutto" ***@***.***> wrote:
Maybe I misunderstand the issue, but I think it's cameleer's responsibility (or any other tool) to give the correct meaning to these constants (/cc @mariojppereira), just like for the rest of the standard library; Gospel is not aware of (and not specifically designed for) the further usage that you make of these specs (in particular when it comes to solvers).
Indeed, `ortac` would probably not need these, since it can just read the values at runtime. Perhaps as you say the additional constraints should be provided by cameleer's implementation of the gospel stdlib, and gospel should just ensure that the needed constants and predicates are declared.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
|
Beta Was this translation helpful? Give feedback.
-
I think it might be good to introduce a predicate Also, as Edwin notes, at the level of Cameleer, it might be good to not specify the exact values of |
Beta Was this translation helpful? Give feedback.
-
As you said, the integers that come from the program have type |
Beta Was this translation helpful? Give feedback.
-
Hi,
Sorry for raising so many different discussions, but I thought it'd be best to separate it into 3 different topics as they are quite orthogonal. I've recently started looking at the possibility of using Gospel to prove some OCaml code.
I'm aware this is quite a young project with some rough edges around the tooling, but I think it already provides quite a lot of value by being able to write down function behaviour in a machine parseable form. In fact writing down the specifications alone in gospel helps in thinking and reviewing code correctness (previously all these constraints were just in my head, and would have to "swap in" when coming back and looking at an old piece of code).
int32, int64 would be good to have, including 2's complement arithmetic or a form where any overflow means an error.
I think the latter is what Why3's stdlib provides, but isn't quite yet easily available in Gospel, but see https://github.com/ocaml-gospel/cameleer/blob/master/examples/cameleer.drv should be possible to add.
It might even be definable in pure Gospel syntax, although it'd duplicate the definitions in Why3.
There is also a question on what to do about
int
. Cameleer currently defines it the same asinteger
which is not quite right: it might prove that a function never throws an exception, whereas in practice it would due to integer overflow causing some invariants to not be upheld.I see the value of defining specifications with arbitrary sized integers, but then verification should ensure no overflow happens (and treat that as a spec failure if it does).
But which integer size should
int
be? If you're targeting verification only for one platform, e.g. x86-64 then choosing statically the size would make sense.OTOH for fully portable programs it'd be great if we could prove that they work for both 31-bit and 63-bit sized
int
s. E.g. one might focusortac
fuzzing on just one build, which would leave the other one less well tested, and this is exactly where formal tools could help prove it works everywhere.I don't see how to define a type depending on another predicate though, e.g. ideally something like this (IIUC this can't be expressed in Gospel):
Or perhaps just the various arithmetic operations would define their limits based on word_size, but that means it couldn't reuse the types from Why3...
Alternatively verification tools like Cameleer could build 2 formulas: one for word_size =32 and one for word_size =64 and then merge the 2 formulas with && (perhaps simplifying and dropping duplicate entries along the way to make the proofs simpler).
(Or run 2 separate proofs, once for 32-bit and once for 64-bit wordsizes, which might be the easiest with the addition of a cmdline flag to cameleer)
What do you think the best solution would be here?
I'd be happy to help in adding support for these (either in Gospel or Cameleer, although Cameleer says no support is provided yet, so not sure if it accepts PRs yet)
Beta Was this translation helpful? Give feedback.
All reactions