-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds implementations of the following, with corresponding tests: * An `Result` type with associated functions to the standard library. * A `find` function to `Stdlib.Data.List` Requirements for both arose when writing Anoma applications. NB: Adding the `{-# specialize: [1] #-}` pragma to `find` causes the compiler to go into an infinite loop when compiling a `find` call with predicate `const false`, e.g ``` find {Nat} (const false) [1;2;3] ``` So I've omitted that for now and ~~I'll raise a separate bug~~, I suspect I was using the `specialize` pragma incorrectly.
- Loading branch information
1 parent
89a5960
commit 216cb60
Showing
8 changed files
with
309 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Stdlib.Data.Result; | ||
|
||
import Stdlib.Data.Result.Base open public; | ||
import Stdlib.Data.Bool.Base open; | ||
|
||
import Stdlib.Trait.Eq open; | ||
import Stdlib.Trait.Ord open; | ||
|
||
instance | ||
ordResultI {A B} {{Ord A}} {{Ord B}} : Ord (Result A B) := | ||
mkOrd@{ | ||
cmp : Result A B -> Result A B -> Ordering | ||
| (error a1) (error a2) := Ord.cmp a1 a2 | ||
| (ok b1) (ok b2) := Ord.cmp b1 b2 | ||
| (error _) (ok _) := LT | ||
| (ok _) (error _) := GT | ||
}; | ||
|
||
instance | ||
eqResultI {A B} {{Eq A}} {{Eq B}} : Eq (Result A B) := | ||
mkEq@{ | ||
eq : Result A B -> Result A B -> Bool | ||
| (error a1) (error a2) := a1 == a2 | ||
| (ok b1) (ok b2) := b1 == b2 | ||
| _ _ := false | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Stdlib.Data.Result.Base; | ||
|
||
import Stdlib.Data.Bool.Base open; | ||
import Stdlib.Data.Maybe.Base open; | ||
import Stdlib.Function open; | ||
|
||
--- The Result type represents either a success with a value of `ok` or an error | ||
--- with value `error`. | ||
type Result E A := | ||
| error E | ||
| ok A; | ||
|
||
--- Apply the onError function if the value is ;error; or apply the | ||
--- onOk function if the value is ;ok;. | ||
handleResult | ||
{E A B} | ||
(onError : E -> B) | ||
(onOk : A -> B) | ||
: Result E A -> B | ||
| (error a) := onError a | ||
| (ok a) := onOk a; | ||
|
||
--- Apply a function to the ;error; value of a Result. | ||
mapError | ||
{A E1 E2} (f : E1 -> E2) : Result E1 A -> Result E2 A := | ||
handleResult (f >> error) ok; | ||
|
||
--- Apply a function to the ;ok; value of a Result. | ||
mapOk {E A C} (f : A -> C) : Result E A -> Result E C := | ||
handleResult error (f >> ok); | ||
|
||
--- Return ;true; if the value is an ;error;, otherwise ;false;. | ||
isError {E A} : Result E A -> Bool | ||
| (error _) := true | ||
| (ok _) := false; | ||
|
||
--- Return ;true; if the value is ;ok;, otherwise ;false;. | ||
isOk {E A} : Result E A -> Bool | ||
| (error _) := false | ||
| (ok _) := true; | ||
|
||
--- Return the contents of an ;error; value, otherwise return a default. | ||
fromError {E A} (default : E) : Result E A -> E | ||
| (error a) := a | ||
| (ok _) := default; | ||
|
||
--- Return the contents of an ;ok; value, otherwise return a default. | ||
fromOk {E A} (default : A) : Result E A -> A | ||
| (error _) := default | ||
| (ok b) := b; | ||
|
||
--- Convert a Result to a Maybe. An ;error; value becomes `nothing`. | ||
resultToMaybe {E A} : Result E A -> Maybe A := | ||
handleResult (const nothing) just; | ||
|
||
--- Convert a Maybe to a Result. A ;nothing; value becomes `error defaultError`. | ||
maybeToResult | ||
{E A} (defaultError : E) : Maybe A -> Result E A | ||
| nothing := error defaultError | ||
| (just x) := ok x; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Test.StdlibTestExtra; | ||
|
||
import Stdlib.Prelude open; | ||
|
||
instance | ||
eitherShow | ||
{A B} {{Show A}} {{Show B}} : Show (Result A B) := | ||
mkShow@{ | ||
show : Result A B -> String | ||
| (error x) := "Error (" ++str Show.show x ++str ")" | ||
| (ok x) := "Ok (" ++str Show.show x ++str ")" | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
# This file was autogenerated by Juvix version 0.6.2. | ||
# This file was autogenerated by Juvix version 0.6.3. | ||
# Do not edit this file manually. | ||
|
||
version: 2 | ||
checksum: 5de68b52002fccbfacd5df82593d631368f3303203d03dc72f126e74a5997283 | ||
checksum: 8013d01c2b6d46596d97438913b23987618efe1f7a6e54e7abd22dd5e0ae7f43 | ||
dependencies: | ||
- path: ../ | ||
dependencies: [] | ||
- git: | ||
name: anoma_juvix-quickcheck | ||
ref: 8068d58360d2adecba3674a31e031e7ad992e0e1 | ||
ref: 8e5d49682fb0b861fc0a1aed95cfebab03231d85 | ||
url: https://github.com/anoma/juvix-quickcheck | ||
dependencies: | ||
- git: | ||
name: anoma_juvix-stdlib | ||
ref: 290f67fc85b637ce4df886fc3cd1a5289b38db95 | ||
ref: 95f93e60e1033046d950414a805f7a4bc75adb62 | ||
url: https://github.com/anoma/juvix-stdlib | ||
dependencies: [] |