Skip to content

Commit bcd5f80

Browse files
authored
Merge branch 'main' into int32
2 parents 1ab3042 + ece20ee commit bcd5f80

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

README.md

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# VMC: a Library for Verified Monte Carlo Algorithms
22

3-
The `DafnyVMC` module introduces utils for probabilistic reasoning in Dafny. At the moment, the API is intentionally limited in scope, and only supports compilation to Java. For the future, we plan to extend both the functionality and the range of supported languages.
3+
The `DafnyVMC` module introduces utils for probabilistic reasoning in Dafny. At the moment, the API is intentionally limited in scope, and only supports compilation to Java and Python. For the future, we plan to extend both the functionality and the range of supported languages.
44

5-
## Java API Example
5+
## Java
6+
7+
### Java API
68

79
```java
810
import DafnyVMC.Random;
@@ -26,31 +28,71 @@ class Test {
2628
}
2729
```
2830

29-
## Dafny Examples
31+
### Java Examples
3032

31-
To run the examples in the `docs/dafny` directory, use the following commands:
33+
To run the examples in the `docs/java` directory, use the following commands:
34+
35+
```bash
36+
$ export TARGET_LANG=java
37+
$ bash scripts/build.sh
38+
$ bash build/java/run_samplers.sh
39+
$ bash build/java/run_shuffling.sh
40+
```
41+
42+
To run the tests in the `docs/dafny` directory, use the following commands:
3243

3344
```bash
3445
$ dafny build docs/dafny/ExamplesRandom.dfy --target:java src/interop/java/Full/Random.java src/interop/java/Part/Random.java dfyconfig.toml --no-verify
3546
$ java -jar docs/dafny/ExamplesRandom.jar
3647
```
3748

38-
## Java Examples
49+
To run the statistical tests in the `tests` directory, use the following commands:
50+
51+
```bash
52+
$ dafny test --target:java src/interop/java/Full/Random.java src/interop/java/Part/Random.java tests/TestsRandom.dfy tests/Tests.dfy dfyconfig.toml --no-verify
53+
```
54+
55+
## Python
56+
57+
### Python API
58+
59+
```py
60+
import DafnyVMC
61+
62+
def main():
63+
r = DafnyVMC.Random()
64+
65+
print("Example of Fisher-Yates shuffling")
66+
arr = ['a', 'b', 'c']
67+
arr = r.Shuffle(arr)
68+
print(arr)
69+
70+
print("Example of Bernoulli sampling")
71+
print(r.BernoulliSample(3, 5))
72+
```
73+
74+
### Python Examples
3975

4076
To run the examples in the `docs/java` directory, use the following commands:
4177

4278
```bash
43-
$ export TARGET_LANG=java
79+
$ export TARGET_LANG=py
4480
$ bash scripts/build.sh
45-
$ bash build/java/run.sh
81+
$ bash build/py/run_samplers.sh
82+
$ bash build/py/run_shuffling.sh
4683
```
4784

48-
## Dafny Testing
85+
To run the tests in the `docs/dafny` directory, use the following commands:
86+
87+
```bash
88+
$ dafny build docs/dafny/ExamplesRandom.dfy --target:py src/interop/py/Full/Random.py src/interop/py/Part/Random.py dfyconfig.toml --no-verify
89+
$ python3 docs/dafny/ExamplesRandom-py/__main__.py
90+
```
4991

5092
To run the statistical tests in the `tests` directory, use the following commands:
5193

5294
```bash
53-
$ dafny test --target:java src/interop/java/Full/Random.java src/interop/java/Part/Random.java tests/TestsRandom.dfy tests/Tests.dfy dfyconfig.toml --no-verify
95+
$ dafny test --target:py src/interop/py/Full/Random.py src/interop/py/Part/Random.py tests/TestsRandom.dfy tests/Tests.dfy dfyconfig.toml --no-verify
5496
```
5597

5698

docs/dafny/ExamplesRandom.dfy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module Examples {
7373
}
7474
}
7575

76-
print "Estimated parameter for BernoulliSample(5, 5): ", (t as real) / (n as real), " (should be around 1.0\n";
76+
print "Estimated parameter for BernoulliSample(5, 5): ", (t as real) / (n as real), " (should be around 1.0)\n";
7777

7878
t := 0;
7979
for i := 0 to n {

src/Util/FisherYates/Correctness.dfy

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,23 @@ module FisherYates.Correctness {
576576
reveal DecomposeE;
577577
}
578578

579-
lemma {:vcs_split_on_every_assert} ProbabilityOfE<T(!new)>(xs: seq<T>, ys: seq<T>, p: seq<T>, i: nat, j: nat, h: Monad.Hurd<int>, A: iset<int>, e: iset<Rand.Bitstream>, e': iset<Rand.Bitstream>)
579+
lemma ProbabilityOfESimplifyFractions<T(!new)>(xs: seq<T>, ys: seq<T>, p: seq<T>, i: nat, j: nat, h: Monad.Hurd<int>, A: iset<int>, e: iset<Rand.Bitstream>, e': iset<Rand.Bitstream>)
580+
requires |xs| - i > 1
581+
ensures (1.0 / ((|xs|-i) as real)) * (1.0 / NatArith.FactorialTraditional((|xs|-i)-1) as real) == (1.0 * 1.0) / (((|xs|-i) as real) * (NatArith.FactorialTraditional(|xs|-(i+1)) as real))
582+
{
583+
var denom := NatArith.FactorialTraditional(|xs|-(i+1)) as real;
584+
RealArith.SimplifyFractionsMultiplication(1.0, (|xs|-i) as real, 1.0, denom);
585+
}
586+
587+
lemma ProbabilityOfEAsRealOfMult<T(!new)>(xs: seq<T>, ys: seq<T>, p: seq<T>, i: nat, j: nat, h: Monad.Hurd<int>, A: iset<int>, e: iset<Rand.Bitstream>, e': iset<Rand.Bitstream>)
588+
requires |xs| - i > 1
589+
ensures (((|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1)) as real) != 0.0
590+
ensures 1.0 / (((|xs|-i) as real) * NatArith.FactorialTraditional(|xs|-(i+1)) as real) == 1.0 / (((|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1)) as real)
591+
{
592+
RealArith.AsRealOfMultiplication(|xs|-i, NatArith.FactorialTraditional((|xs|-i)-1));
593+
}
594+
595+
lemma ProbabilityOfE<T(!new)>(xs: seq<T>, ys: seq<T>, p: seq<T>, i: nat, j: nat, h: Monad.Hurd<int>, A: iset<int>, e: iset<Rand.Bitstream>, e': iset<Rand.Bitstream>)
580596
requires i <= |xs|
581597
requires i <= |p|
582598
requires forall a, b | i <= a < b < |xs| :: xs[a] != xs[b]
@@ -626,26 +642,6 @@ module FisherYates.Correctness {
626642
RealArith.MultiplicationInvariance(1.0 / ((|xs|-i) as real), frac, frac2);
627643
}
628644

629-
assert SimplifyFractionsMultiplicationLifted: (1.0 / ((|xs|-i) as real)) * frac2 == (1.0 * 1.0) / (((|xs|-i) as real) * denom) by {
630-
assert |xs|-i > 1;
631-
RealArith.SimplifyFractionsMultiplication(1.0, (|xs|-i) as real, 1.0, denom);
632-
}
633-
634-
assert AsRealOfMultiplicationLifted: 1.0 / (((|xs|-i) as real) * denom) == 1.0 / (((|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1)) as real) by {
635-
assert ((|xs|-i) as real) * denom == ((|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1)) as real by {
636-
RealArith.AsRealOfMultiplication(|xs|-i, NatArith.FactorialTraditional((|xs|-i)-1));
637-
}
638-
}
639-
640-
assert NonZero: (((|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1)) as real) != 0.0 by {
641-
assert |xs|-i != 0;
642-
assert NatArith.FactorialTraditional((|xs|-i)-1) != 0;
643-
}
644-
645-
assert NonZeroDenom: denom != 0.0 by {
646-
assert NatArith.FactorialTraditional((|xs|-i)-1) != 0;
647-
}
648-
649645
calc {
650646
Rand.prob(e);
651647
{ reveal DecomposeE; }
@@ -660,11 +656,11 @@ module FisherYates.Correctness {
660656
(1.0 / ((|xs|-i) as real)) * frac;
661657
{ reveal FracLifted; }
662658
(1.0 / ((|xs|-i) as real)) * frac2;
663-
{ reveal SimplifyFractionsMultiplicationLifted; reveal NonZeroDenom; }
659+
{ ProbabilityOfESimplifyFractions(xs, ys, p, i, j, h, A, e, e'); }
664660
(1.0 * 1.0) / (((|xs|-i) as real) * denom);
665661
{ assert 1.0 * 1.0 == 1.0; }
666662
1.0 / (((|xs|-i) as real) * denom);
667-
{ reveal AsRealOfMultiplicationLifted; reveal NonZero; }
663+
{ ProbabilityOfEAsRealOfMult(xs, ys, p, i, j, h, A, e, e'); }
668664
1.0 / (((|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1)) as real);
669665
{ assert (|xs|-i) * NatArith.FactorialTraditional((|xs|-i)-1) == NatArith.FactorialTraditional(|xs|-i) by { reveal NatArith.FactorialTraditional(); } }
670666
1.0 / (NatArith.FactorialTraditional(|xs|-i) as real);

0 commit comments

Comments
 (0)