@@ -57,6 +57,22 @@ module Monad {
57
57
case Diverging => false
58
58
case Result (_, rest) => property (rest)
59
59
}
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
+ }
60
76
}
61
77
62
78
ghost function Values< A> (results: iset < Result< A>> ): iset < A> {
@@ -96,13 +112,30 @@ module Monad {
96
112
(s: Rand. Bitstream) => f (s). Bind (g)
97
113
}
98
114
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
+
99
123
// Equation (2.42)
100
124
const Coin: Hurd< bool > := s => Result (Rand.Head(s), Rand. Tail (s))
101
125
102
126
function Composition< A,B,C> (f: A - > Hurd< B> , g: B - > Hurd< C> ): A - > Hurd< C> {
103
127
(a: A) => Bind (f(a), g)
104
128
}
105
129
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
+
106
139
// Equation (3.3)
107
140
function Return< A> (a: A): Hurd< A> {
108
141
(s: Rand. Bitstream) => Result (a, s)
0 commit comments