Skip to content

Commit 64d0f1d

Browse files
authored
Merge pull request #1768 from cryspen/alex/rustm
refactor(lean): rename Result to RustM
2 parents 9c12500 + c50b1ac commit 64d0f1d

File tree

15 files changed

+224
-222
lines changed

15 files changed

+224
-222
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Changes to the Lean backend:
2929
- Add support for base-expressions of structs (#1736)
3030
- Use the explicit monadic phase to insert `pure` and `` only on demand, and
3131
not introduce extra `do` block (#1746)
32+
- Rename `Result` monad to `RustM` to avoid confusion with Rust `Result` type (#1768)
3233

3334
Miscellaneous:
3435
- Reserve extraction folder for auto-generated files in Lean examples (#1754)

docs/manual/lean/internals.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ inductive Error where
3939
| panic: Error
4040
| undef: Error
4141
42-
inductive Result.{u} (α : Type u) where
43-
| ok (v: α): Result α
44-
| fail (e: Error): Result α
42+
inductive RustM.{u} (α : Type u) where
43+
| ok (v: α): RustM α
44+
| fail (e: Error): RustM α
4545
| div
4646
```
4747

4848
This monadic encoding shows for simple expressions: the result of the lean
49-
extracted function is not `u32` but `Result u32`.
49+
extracted function is not `u32` but `RustM u32`.
5050

5151
/// html | div[style='float: left; width: 48%;']
5252
```rust
@@ -58,7 +58,7 @@ fn f (x: u32) -> u32 {
5858

5959
/// html | div[style='float: right;width: 48%;']
6060
```lean
61-
def f (x : u32) : Result u32
61+
def f (x : u32) : RustM u32
6262
:= do (← x +? (1 : u32))
6363
```
6464
///
@@ -73,9 +73,9 @@ actually be understood as bindings in the monad, propagating potential
7373
errors to the top.
7474

7575
The `do` keywords enables the lifting `` and the `pure` operators. Intuitively,
76-
lifting turns a value of type `Result T` into a value of type `T` by turning the
76+
lifting turns a value of type `RustM T` into a value of type `T` by turning the
7777
rest of the program into a use of `bind`. Conversely, `pure` turns a value of
78-
type `T` into a value of type `Result T`. This shows also for let-bindings :
78+
type `T` into a value of type `RustM T`. This shows also for let-bindings :
7979

8080

8181
/// html | div[style='float: left; width: 48%;']
@@ -90,7 +90,7 @@ fn f (x: u32) -> u32 {
9090

9191
/// html | div[style='float: right;width: 48%;']
9292
```lean
93-
def f (x : u32) : Result u32 := do
93+
def f (x : u32) : RustM u32 := do
9494
let y : u32 ← (pure
9595
(← x +? (1 : u32)));
9696
let z : u32 ← (pure
@@ -327,7 +327,7 @@ match e_v1 {
327327
/// html | div[style='float: right;width: 48%;']
328328
```lean
329329
def enums (_ : Tuple0)
330-
: Result Tuple0
330+
: RustM Tuple0
331331
:= do
332332
let e_v1 : E ← (pure E.V1);
333333
let e_v2 : E ← (pure E.V2);
@@ -413,8 +413,8 @@ fn f<T: T1>(x: T) -> usize {
413413
/// html | div[style='float: right;width: 48%;']
414414
```lean
415415
class T1 (Self : Type) where
416-
f1 : Self -> Result usize
417-
f2 : Self -> Self -> Result usize
416+
f1 : Self -> RustM usize
417+
f2 : Self -> Self -> RustM usize
418418
419419
structure S where
420420
@@ -425,7 +425,7 @@ instance Impl : T1 S where
425425
:= do (43 : usize)
426426
427427
def f (T : Type) [(T1 T)] (x : T)
428-
: Result usize
428+
: RustM usize
429429
:= do
430430
(← (← T1.f1 x) +? (← T1.f2 x x))
431431
```
@@ -459,7 +459,7 @@ class Test
459459
[_constr_7570495343596639253 :
460460
(T1 T)]
461461
f_test :
462-
Self -> T -> Result usize
462+
Self -> T -> RustM usize
463463
```
464464
///
465465

@@ -507,7 +507,7 @@ class Bar (Self : Type) where
507507
508508
class T1 (Self : Type) where
509509
T : Type
510-
f : Self -> T -> Result T
510+
f : Self -> T -> RustM T
511511
512512
class T3 (Self : Type) where
513513
T : Type
@@ -516,12 +516,12 @@ class T3 (Self : Type) where
516516
Tp : Type
517517
[_constr_15450263461214744089 : (Foo Tp T)]
518518
f (A : Type) [(Bar A)] :
519-
Self -> T -> Tp -> Result usize
519+
Self -> T -> Tp -> RustM usize
520520
521521
class T2 (Self : Type) where
522522
T : Type
523523
[_constr_18277713886489441014 : (T1 T)]
524-
f : Self -> T -> Result usize
524+
f : Self -> T -> RustM usize
525525
526526
```
527527
///

examples/lean_chacha20/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ theorem Lean_chacha20.chacha20_quarter_round_spec a b c d state:
5050
intros
5151
mvcgen [Lean_chacha20.chacha20_quarter_round,
5252
Lean_chacha20.chacha20_line,
53-
Result.ofOption, ]
53+
RustM.ofOption, ]
5454
<;> try omega
5555
"
5656
)]

hax-lib/proof-libs/lean/Hax/Core/Clone.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Core.Clone
1212

1313
class Clone (Self : Type) where
1414

15-
def Clone.clone {Self: Type} : Self -> Result Self :=
15+
def Clone.clone {Self: Type} : Self -> RustM Self :=
1616
fun x => pure x
1717

1818
end Core.Clone

hax-lib/proof-libs/lean/Hax/Core/Convert.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set_option mvcgen.warning false
1414

1515
/- Warning : this function has been specialized, it should be turned into a typeclass -/
1616
def Core.Convert.TryInto.try_into {α n} (a: Array α) :
17-
Result (Core.Result.Result (Vector α n) Core.Array.TryFromSliceError) :=
17+
RustM (Core.Result.Result (Vector α n) Core.Array.TryFromSliceError) :=
1818
pure (
1919
if h: a.size = n then
2020
Core.Result.Result.Ok (a.toVector.cast h)

hax-lib/proof-libs/lean/Hax/Core/Default.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ open Rust_primitives.Hax
1212
namespace Core.Default
1313

1414
class Default (Self : Type) where
15-
default : Tuple0 -> Result Self
15+
default : Tuple0 -> RustM Self
1616

1717
end Core.Default

hax-lib/proof-libs/lean/Hax/Core/Ops/Function.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@ open Rust_primitives.Hax
1111
namespace Core.Ops.Function
1212

1313
class FnOnce (Self Args : Type) {Output: Type} where
14-
call_once : Self -> Args -> Result Output
14+
call_once : Self -> Args -> RustM Output
1515
Output : Type := Output
1616

1717
class Fn (Self Args : Type) {Output: Type} where
1818
[_constr_10219838627318688691 : (FnOnce Self Args (Output := Output))]
19-
call : Self -> Args -> Result Output
19+
call : Self -> Args -> RustM Output
2020

21-
instance {α β} : FnOnce (α → Result β) α (Output := β) where
21+
instance {α β} : FnOnce (α → RustM β) α (Output := β) where
2222
Output := β
2323
call_once f x := f x
2424

25-
instance {α β} : FnOnce (α → Result β) (Tuple1 α) (Output := β) where
25+
instance {α β} : FnOnce (α → RustM β) (Tuple1 α) (Output := β) where
2626
Output := β
2727
call_once f x := f x._0
2828

29-
instance {α β γ : Type} : FnOnce (α → β → Result γ) (Tuple2 α β) (Output := γ) where
29+
instance {α β γ : Type} : FnOnce (α → β → RustM γ) (Tuple2 α β) (Output := γ) where
3030
Output := γ
3131
call_once f x := f x._0 x._1
3232

33-
instance {α β} : Fn (α → Result β) α (Output := β) where
33+
instance {α β} : Fn (α → RustM β) α (Output := β) where
3434
call f x := (FnOnce.call_once) f x
3535

36-
instance {α β} : Fn (α → Result β) (Tuple1 α) (Output := β) where
36+
instance {α β} : Fn (α → RustM β) (Tuple1 α) (Output := β) where
3737
call f x := (FnOnce.call_once) f x
3838

39-
instance {α β γ} : Fn (α → β → Result γ) (Tuple2 α β) (Output := γ) where
39+
instance {α β γ} : Fn (α → β → RustM γ) (Tuple2 α β) (Output := γ) where
4040
call f x := (FnOnce.call_once) f x

hax-lib/proof-libs/lean/Hax/Core/Option.lean

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Hax.Core.Panicking
1313
import Hax.Core.Result
1414
open Rust_primitives.Hax
1515
open Core.Ops
16+
open Core.Result
1617
open Std.Do
1718
set_option mvcgen.warning false
1819

@@ -26,7 +27,7 @@ def Impl.is_some_and
2627
(T : Type) (F : Type) [(Function.FnOnce (Output := Bool) F T)] (self :
2728
(Option T))
2829
(f : F)
29-
: Result Bool
30+
: RustM Bool
3031
:= do
3132
match self with
3233
| (Option.None ) => (pure false)
@@ -37,7 +38,7 @@ def Impl.is_none_or
3738
(T : Type) (F : Type) [(Function.FnOnce (Output := Bool) F T)] (self :
3839
(Option T))
3940
(f : F)
40-
: Result Bool
41+
: RustM Bool
4142
:= do
4243
match self with
4344
| (Option.None ) => (pure true)
@@ -46,7 +47,7 @@ def Impl.is_none_or
4647

4748
def Impl.as_ref
4849
(T : Type) (self : (Option T))
49-
: Result (Option T)
50+
: RustM (Option T)
5051
:= do
5152
match self with
5253
| (Option.Some x)
@@ -56,7 +57,7 @@ def Impl.as_ref
5657
def Impl.unwrap_or
5758
(T : Type) (self : (Option T))
5859
(default : T)
59-
: Result T
60+
: RustM T
6061
:= do
6162
match self with
6263
| (Option.Some x) => (pure x)
@@ -68,7 +69,7 @@ def Impl.unwrap_or_else
6869
[(Function.FnOnce F Rust_primitives.Hax.Tuple0 (Output := T))]
6970
(self : (Option T))
7071
(f : F)
71-
: Result T
72+
: RustM T
7273
:= do
7374
match self with
7475
| (Option.Some x) => (pure x)
@@ -77,7 +78,7 @@ def Impl.unwrap_or_else
7778
def Impl.unwrap_or_default
7879
(T : Type) [(Core.Default.Default T)] (self :
7980
(Option T))
80-
: Result T
81+
: RustM T
8182
:= do
8283
match self with
8384
| (Option.Some x) => (pure x)
@@ -90,7 +91,7 @@ def Impl.map
9091
[(Function.FnOnce F T (Output := U))]
9192
(self : (Option T))
9293
(f : F)
93-
: Result (Option U)
94+
: RustM (Option U)
9495
:= do
9596
match self with
9697
| (Option.Some x) => (pure (Option.Some (← (Function.FnOnce.call_once f x))))
@@ -104,7 +105,7 @@ def Impl.map_or
104105
(self : (Option T))
105106
(default : U)
106107
(f : F)
107-
: Result U
108+
: RustM U
108109
:= do
109110
match self with
110111
| (Option.Some t) => (Function.FnOnce.call_once f t)
@@ -120,7 +121,7 @@ def Impl.map_or_else
120121
(self : (Option T))
121122
(default : D)
122123
(f : F)
123-
: Result U
124+
: RustM U
124125
:= do
125126
match self with
126127
| (Option.Some t) => (Function.FnOnce.call_once f t)
@@ -134,7 +135,7 @@ def Impl.map_or_default
134135
[(Core.Default.Default U)]
135136
(self : (Option T))
136137
(f : F)
137-
: Result U
138+
: RustM U
138139
:= do
139140
match self with
140141
| (Option.Some t)
@@ -146,13 +147,13 @@ def Impl.map_or_default
146147
def Impl.ok_or
147148
(T : Type) (E : Type) (self : (Option T))
148149
(err : E)
149-
: Result (Core.Result.Result T E)
150+
: RustM (Result T E)
150151
:= do
151152
match self with
152153
| (Option.Some v)
153-
=> (pure (Core.Result.Result.Ok v))
154+
=> (pure (Result.Ok v))
154155
| (Option.None )
155-
=> (pure (Core.Result.Result.Err err))
156+
=> (pure (Result.Err err))
156157

157158
def Impl.ok_or_else
158159
(T : Type)
@@ -161,14 +162,14 @@ def Impl.ok_or_else
161162
[(Function.FnOnce F Rust_primitives.Hax.Tuple0 (Output := E))]
162163
(self : (Option T))
163164
(err : F)
164-
: Result (Core.Result.Result T E)
165+
: RustM (Result T E)
165166
:= do
166167
match self with
167168
| (Option.Some v)
168-
=> (pure (Core.Result.Result.Ok v))
169+
=> (pure (Result.Ok v))
169170
| (Option.None )
170171
=>
171-
(pure (Core.Result.Result.Err
172+
(pure (Result.Err
172173
(← (Function.FnOnce.call_once
173174
err
174175
Rust_primitives.Hax.Tuple0.mk))))
@@ -180,7 +181,7 @@ def Impl.and_then
180181
[(Function.FnOnce F T (Output := Option U))]
181182
(self : (Option T))
182183
(f : F)
183-
: Result (Option U)
184+
: RustM (Option U)
184185
:= do
185186
match self with
186187
| (Option.Some x)
@@ -189,7 +190,7 @@ def Impl.and_then
189190

190191
def Impl.take
191192
(T : Type) (self : (Option T))
192-
: Result
193+
: RustM
193194
(Rust_primitives.Hax.Tuple2
194195
(Option T)
195196
(Option T))
@@ -198,7 +199,7 @@ def Impl.take
198199

199200
def Impl.is_some
200201
(T : Type) (self : (Option T))
201-
: Result Bool
202+
: RustM Bool
202203
:= do
203204
match self with
204205
| (Option.Some _) => (pure true)
@@ -217,7 +218,7 @@ def Impl.is_some.spec
217218

218219
def Impl.is_none
219220
(T : Type) (self : (Option T))
220-
: Result Bool
221+
: RustM Bool
221222
:= do
222223
match self with
223224
| (Option.Some _) => (pure false)
@@ -226,7 +227,7 @@ def Impl.is_none
226227
def Impl.expect
227228
(T : Type) (self : (Option T))
228229
(_msg : String)
229-
: Result T
230+
: RustM T
230231
:= do
231232
match self with
232233
| (Option.Some val) => (pure val)
@@ -235,13 +236,13 @@ def Impl.expect
235236

236237
def Impl.unwrap._.requires
237238
(T : Type) (self_ : (Option T))
238-
: Result Bool
239+
: RustM Bool
239240
:= do
240241
(Impl.is_some T self_)
241242

242243
def Impl.unwrap
243244
(T : Type) (self : (Option T))
244-
: Result T
245+
: RustM T
245246
:= do
246247
match self with
247248
| (Option.Some val) => (pure val)

hax-lib/proof-libs/lean/Hax/Core/Panicking.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ open Core.Ops
99
namespace Core.Panicking.Internal
1010

1111
def panic (T : Type) (_ : Rust_primitives.Hax.Tuple0)
12-
: Result T
12+
: RustM T
1313
:= do .fail .panic
1414

1515
end Core.Panicking.Internal

0 commit comments

Comments
 (0)