1use super::super::prelude::*;
2use core::convert::Infallible;
3use core::ops::ControlFlow;
4use core::ops::FromResidual;
5use core::ops::Try;
6
7verus! {
8
9#[verifier::external_type_specification]
10#[verifier::accept_recursive_types(B)]
11#[verifier::reject_recursive_types_in_ground_variants(C)]
12pub struct ExControlFlow<B, C>(ControlFlow<B, C>);
13
14#[verifier::external_trait_specification]
15pub trait ExResidual<O>: Sized {
16 type ExternalTraitSpecificationFor: core::ops::Residual<O>;
17}
18
19#[verifier::external_trait_specification]
21#[verifier::external_trait_extension(TrySpec via TrySpecImpl)]
22pub trait ExTry: FromResidual<Self::Residual> {
23 type ExternalTraitSpecificationFor: Try;
24
25 type Output;
26
27 type Residual: core::ops::Residual<Self::Output>;
28
29 spec fn may_unwind(&self) -> bool;
31
32 spec fn branch_model_is_valid() -> bool;
34
35 spec fn branch_model(self) -> ControlFlow<Self::Residual, Self::Output> where Self: Sized;
36
37 fn branch(self) -> (ret: ControlFlow<Self::Residual, Self::Output>) where Self: Sized
38 ensures
39 Self::branch_model_is_valid() ==> ret == Self::branch_model(self),
40 no_unwind when !self.may_unwind()
41 ;
42
43 fn from_output(output: Self::Output) -> Self where Self: Sized;
44}
45
46#[verifier::external_trait_specification]
47#[verifier::external_trait_extension(FromResidualSpec via FromResidualSpecImpl)]
48pub trait ExFromResidual<R> {
49 type ExternalTraitSpecificationFor: FromResidual<R>;
50
51 spec fn may_unwind(residual: &R) -> bool;
53
54 fn from_residual(residual: R) -> Self where Self: Sized
55 no_unwind when !Self::may_unwind(&residual)
56 ;
57}
58
59pub open spec fn try_laws<R: Try>() -> bool {
61 &&& R::branch_model_is_valid()
62 &&& forall|output: R::Output, result: R| #[trigger]
63 R::from_output.ensures((output,), result) ==> R::branch_model(result)
64 == ControlFlow::Continue(output)
65 &&& forall|residual: R::Residual, result: R| #[trigger]
66 <R as FromResidual<R::Residual>>::from_residual.ensures((residual,), result)
67 ==> R::branch_model(result) == ControlFlow::Break(residual)
68}
69
70impl<T> TrySpecImpl for Option<T> {
71 open spec fn may_unwind(&self) -> bool {
72 false
73 }
74
75 open spec fn branch_model_is_valid() -> bool {
76 true
77 }
78
79 open spec fn branch_model(self) -> ControlFlow<Self::Residual, Self::Output> {
80 match self {
81 Some(v) => ControlFlow::Continue(v),
82 None => ControlFlow::Break(None),
83 }
84 }
85}
86
87impl<T, E> TrySpecImpl for Result<T, E> {
88 open spec fn may_unwind(&self) -> bool {
89 false
90 }
91
92 open spec fn branch_model_is_valid() -> bool {
93 true
94 }
95
96 open spec fn branch_model(self) -> ControlFlow<Self::Residual, Self::Output> {
97 match self {
98 Ok(v) => ControlFlow::Continue(v),
99 Err(e) => ControlFlow::Break(Err(e)),
100 }
101 }
102}
103
104impl<B, C> TrySpecImpl for ControlFlow<B, C> {
105 open spec fn may_unwind(&self) -> bool {
106 false
107 }
108
109 open spec fn branch_model_is_valid() -> bool {
110 true
111 }
112
113 open spec fn branch_model(self) -> ControlFlow<Self::Residual, Self::Output> {
114 match self {
115 ControlFlow::Continue(v) => ControlFlow::Continue(v),
116 ControlFlow::Break(e) => ControlFlow::Break(ControlFlow::Break(e)),
117 }
118 }
119}
120
121impl<T> FromResidualSpecImpl<Option<Infallible>> for Option<T> {
122 open spec fn may_unwind(residual: &Option<Infallible>) -> bool {
123 false
124 }
125}
126
127impl<B, C> FromResidualSpecImpl<ControlFlow<B, Infallible>> for ControlFlow<B, C> {
128 open spec fn may_unwind(residual: &ControlFlow<B, Infallible>) -> bool {
129 false
130 }
131}
132
133impl<T, E, F: From<E>> FromResidualSpecImpl<Result<Infallible, E>> for Result<T, F> {
134 open spec fn may_unwind(residual: &Result<Infallible, E>) -> bool {
135 true
137 }
138}
139
140pub broadcast proof fn lemma_option_try_laws<T>()
141 ensures
142 #[trigger] try_laws::<Option<T>>(),
143{
144}
145
146pub broadcast proof fn lemma_result_try_laws<T, E>()
147 ensures
148 #[trigger] try_laws::<Result<T, E>>(),
149{
150 broadcast use spec_from_blanket_identity;
151
152}
153
154pub broadcast proof fn lemma_control_flow_try_laws<B, C>()
155 ensures
156 #[trigger] try_laws::<ControlFlow<B, C>>(),
157{
158}
159
160pub assume_specification<T, E>[ Result::<T, E>::branch ](result: Result<T, E>) -> (cf: ControlFlow<
161 <Result<T, E> as Try>::Residual,
162 <Result<T, E> as Try>::Output,
163>)
164 ensures
165 cf == match result {
166 Ok(v) => ControlFlow::Continue(v),
167 Err(e) => ControlFlow::Break(Err(e)),
168 },
169;
170
171pub assume_specification<T, E>[ Result::<T, E>::from_output ](
172 output: <Result<T, E> as Try>::Output,
173) -> (ret: Result<T, E>)
174 ensures
175 ret == Ok(output),
176;
177
178pub assume_specification<T>[ Option::<T>::from_output ](output: <Option<T> as Try>::Output) -> (ret:
179 Option<T>)
180 ensures
181 ret == Some(output),
182;
183
184pub assume_specification<B, C>[ ControlFlow::<B, C>::from_output ](
185 output: <ControlFlow<B, C> as Try>::Output,
186) -> (ret: ControlFlow<B, C>)
187 ensures
188 ret == ControlFlow::Continue(output),
189;
190
191pub assume_specification<B, C>[ ControlFlow::<B, C>::branch ](cf: ControlFlow<B, C>) -> (ret:
192 ControlFlow<<ControlFlow<B, C> as Try>::Residual, <ControlFlow<B, C> as Try>::Output>)
193 ensures
194 ret == match cf {
195 ControlFlow::Continue(output) => ControlFlow::Continue(output),
196 ControlFlow::Break(residual) => ControlFlow::Break(ControlFlow::Break(residual)),
197 },
198;
199
200pub assume_specification<T>[ Option::<T>::branch ](option: Option<T>) -> (cf: ControlFlow<
201 <Option<T> as Try>::Residual,
202 <Option<T> as Try>::Output,
203>)
204 ensures
205 cf == match option {
206 Some(v) => ControlFlow::Continue(v),
207 None => ControlFlow::Break(None),
208 },
209;
210
211pub assume_specification<T>[ Option::<T>::from_residual ](option: Option<Infallible>) -> (option2:
212 Option<T>)
213 ensures
214 option.is_none(),
215 option2.is_none(),
216;
217
218pub assume_specification<B, C>[ ControlFlow::<B, C>::from_residual ](
219 residual: ControlFlow<B, Infallible>,
220) -> (ret: ControlFlow<B, C>)
221 ensures
222 match residual {
223 ControlFlow::Break(value) => ret == ControlFlow::Break(value),
224 ControlFlow::Continue(_) => false,
225 },
226;
227
228pub uninterp spec fn spec_from<S, T>(value: T, ret: S) -> bool;
229
230pub broadcast axiom fn spec_from_blanket_identity<T>(t: T, s: T)
231 ensures
232 #[trigger] spec_from::<T, T>(t, s) ==> t == s,
233;
234
235pub assume_specification<T, E, F: From<E>>[ Result::<T, F>::from_residual ](
236 result: Result<Infallible, E>,
237) -> (result2: Result<T, F>)
238 ensures
239 match (result, result2) {
240 (Err(e), Err(e2)) => spec_from::<F, E>(e, e2) && call_ensures(F::from, (e,), e2),
241 _ => false,
242 },
243;
244
245pub broadcast group group_control_flow_axioms {
246 spec_from_blanket_identity,
247 lemma_option_try_laws,
248 lemma_result_try_laws,
249 lemma_control_flow_try_laws,
250}
251
252}