Skip to content

Commit

Permalink
Merge pull request #3348 from joelberkeley/fromrightleft
Browse files Browse the repository at this point in the history
Add `fromRight` and `fromLeft` for extracting values out of `Either`
  • Loading branch information
andrevidela authored Jul 14, 2024
2 parents be2ec7d + 3a43c13 commit 182bcff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_NEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ This CHANGELOG describes the merged but unreleased changes. Please see [CHANGELO
* Quantity of the argument for the type being searched in the `search` function
from `Language.Reflection` was changed to be zero.

* Added `fromRight` and `fromLeft` for extracting values out of `Either`, equivalent to `fromJust` for `Just`.

#### Contrib

* `Data.List.Lazy` was moved from `contrib` to `base`.
Expand Down
10 changes: 10 additions & 0 deletions libs/base/Data/Either.idr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export
Uninhabited (IsRight (Left x)) where
uninhabited ItIsRight impossible

||| Returns the `r` value of an `Either l r` which is proved `Right`.
fromRight : (e : Either l r) -> {auto 0 isRight : IsRight e} -> r
fromRight (Right r) = r
fromRight (Left _) impossible

||| Proof that an `Either` is actually a Left value
public export
data IsLeft : Either a b -> Type where
Expand All @@ -50,6 +55,11 @@ export
Uninhabited (IsLeft (Right x)) where
uninhabited ItIsLeft impossible

||| Returns the `l` value of an `Either l r` which is proved `Left`.
fromLeft : (e : Either l r) -> {auto 0 isLeft : IsLeft e} -> l
fromLeft (Right _) impossible
fromLeft (Left l) = l

--------------------------------------------------------------------------------
-- Grouping values

Expand Down

0 comments on commit 182bcff

Please sign in to comment.