diff options
author | Son HO | 2024-04-26 10:55:47 +0200 |
---|---|---|
committer | GitHub | 2024-04-26 10:55:47 +0200 |
commit | 5da597289c1723aa59bf87ad5075675820c18f73 (patch) | |
tree | d63e2f44c75634674a3e4eabb57ff08c0ae441ac /backends/lean/Base | |
parent | 2df6dd65b84a2fd9aad4f716f323bf3f85bf82db (diff) | |
parent | fe8d14cd8b7ba907d5248d574619e93e6d1d253a (diff) |
Merge pull request #125 from zhassan-aws/core-option-unwrap
Add `core::option::unwrap` builtin
Diffstat (limited to '')
-rw-r--r-- | backends/lean/Base/Primitives/Base.lean | 5 | ||||
-rw-r--r-- | backends/lean/Base/Primitives/Core.lean | 8 | ||||
-rw-r--r-- | backends/lean/Base/Primitives/Scalar.lean | 5 |
3 files changed, 13 insertions, 5 deletions
diff --git a/backends/lean/Base/Primitives/Base.lean b/backends/lean/Base/Primitives/Base.lean index 94134d86..d682e926 100644 --- a/backends/lean/Base/Primitives/Base.lean +++ b/backends/lean/Base/Primitives/Base.lean @@ -76,6 +76,11 @@ def eval_global {α: Type u} (x: Result α) (_: ok? x := by prove_eval_global) : | fail _ | div => by contradiction | ok x => x +def Result.ofOption {a : Type u} (x : Option a) (e : Error) : Result a := + match x with + | some x => ok x + | none => fail e + /- DO-DSL SUPPORT -/ def bind {α : Type u} {β : Type v} (x: Result α) (f: α → Result β) : Result β := diff --git a/backends/lean/Base/Primitives/Core.lean b/backends/lean/Base/Primitives/Core.lean index 99f65985..14a51bc1 100644 --- a/backends/lean/Base/Primitives/Core.lean +++ b/backends/lean/Base/Primitives/Core.lean @@ -51,4 +51,12 @@ def clone.CloneBool : clone.Clone Bool := { clone := fun b => ok (clone.impls.CloneBool.clone b) } +namespace option -- core.option + +/- [core::option::{core::option::Option<T>}::unwrap] -/ +def Option.unwrap (T : Type) (x : Option T) : Result T := + Result.ofOption x Error.panic + +end option -- core.option + end core diff --git a/backends/lean/Base/Primitives/Scalar.lean b/backends/lean/Base/Primitives/Scalar.lean index 014decb1..2bee8a2f 100644 --- a/backends/lean/Base/Primitives/Scalar.lean +++ b/backends/lean/Base/Primitives/Scalar.lean @@ -364,11 +364,6 @@ def Scalar.tryMkOpt (ty : ScalarTy) (x : Int) : Option (Scalar ty) := some (Scalar.ofIntCore x (Scalar.check_bounds_imp_in_bounds h)) else none -def Result.ofOption {a : Type u} (x : Option a) (e : Error) : Result a := - match x with - | some x => ok x - | none => fail e - def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := Result.ofOption (tryMkOpt ty x) integerOverflow |