Skip to content

Commit f715055

Browse files
stefan-awsjtristan
andauthored
Update with Failure (#124)
Turns `Result` into a [failure-compatible type](https://dafny.org/latest/DafnyRef/DafnyRef#sec-update-with-failure-statement). That is, allows us to write: `var (a, s') :- f(s);` for any `f: Hurd<A>` and `s: Rand.Bitstream`. By submitting this pull request, I confirm that my contribution is made under the terms of the [MIT license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt). Co-authored-by: John Tristan <trjohnb@amazon.com>
1 parent 86a2c9a commit f715055

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/ProbabilisticProgramming/Monad.dfy

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ module Monad {
5757
case Diverging => false
5858
case Result(_, rest) => property(rest)
5959
}
60+
61+
predicate IsFailure() {
62+
Diverging?
63+
}
64+
65+
function PropagateFailure<B>(): Result<B>
66+
requires Diverging?
67+
{
68+
Diverging
69+
}
70+
71+
function Extract(): (A, Rand.Bitstream)
72+
requires Result?
73+
{
74+
(this.value, this.rest)
75+
}
6076
}
6177

6278
ghost function Values<A>(results: iset<Result<A>>): iset<A> {
@@ -96,13 +112,30 @@ module Monad {
96112
(s: Rand.Bitstream) => f(s).Bind(g)
97113
}
98114

115+
function BindAlternative<A,B>(f: Hurd<A>, g: A -> Hurd<B>): (h: Hurd<B>)
116+
ensures forall s :: h(s) == Bind(f, g)(s)
117+
{
118+
(s: Rand.Bitstream) =>
119+
var (a, s') :- f(s);
120+
g(a)(s')
121+
}
122+
99123
// Equation (2.42)
100124
const Coin: Hurd<bool> := s => Result(Rand.Head(s), Rand.Tail(s))
101125

102126
function Composition<A,B,C>(f: A -> Hurd<B>, g: B -> Hurd<C>): A -> Hurd<C> {
103127
(a: A) => Bind(f(a), g)
104128
}
105129

130+
function CompositionAlternative<A(!new),B,C>(f: A -> Hurd<B>, g: B -> Hurd<C>): (h: A -> Hurd<C>)
131+
ensures forall a, s :: h(a)(s) == Composition(f, g)(a)(s)
132+
{
133+
(a: A) =>
134+
(s: Rand.Bitstream) =>
135+
var (b, s') :- f(a)(s);
136+
g(b)(s')
137+
}
138+
106139
// Equation (3.3)
107140
function Return<A>(a: A): Hurd<A> {
108141
(s: Rand.Bitstream) => Result(a, s)

0 commit comments

Comments
 (0)