From 615a02c8107076ca9661c5234d41792be91a5104 Mon Sep 17 00:00:00 2001 From: Jan Mas Rovira Date: Thu, 29 Aug 2024 14:57:19 +0200 Subject: [PATCH] add Monad trait (#119) Co-authored-by: Paul Cadman --- Stdlib/Data/List.juvix | 6 ++++++ Stdlib/Data/Maybe.juvix | 9 +++++++++ Stdlib/Data/Result.juvix | 8 ++++++++ Stdlib/Trait.juvix | 1 + Stdlib/Trait/Monad.juvix | 20 ++++++++++++++++++++ test/Package.juvix | 2 +- test/juvix.lock.yaml | 6 +++--- 7 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 Stdlib/Trait/Monad.juvix diff --git a/Stdlib/Data/List.juvix b/Stdlib/Data/List.juvix index a4ff5b3a..e31a7b06 100644 --- a/Stdlib/Data/List.juvix +++ b/Stdlib/Data/List.juvix @@ -84,3 +84,9 @@ applicativeListI : Applicative List := | [] _ := [] | (f :: fs) l := map f l ++ ap fs l }; + +instance +monadListI : Monad List := + mkMonad@{ + bind := flip concatMap + }; diff --git a/Stdlib/Data/Maybe.juvix b/Stdlib/Data/Maybe.juvix index 1140a72a..095171ad 100644 --- a/Stdlib/Data/Maybe.juvix +++ b/Stdlib/Data/Maybe.juvix @@ -7,6 +7,7 @@ import Stdlib.Trait.Ord open; import Stdlib.Trait.Show open; import Stdlib.Trait.Functor open; import Stdlib.Trait.Applicative open; +import Stdlib.Trait.Monad open; import Stdlib.Data.Bool.Base open; import Stdlib.Data.String.Base open; @@ -61,3 +62,11 @@ applicativeMaybeI : Applicative Maybe := | (just f) (just x) := just (f x) | _ _ := nothing }; + +instance +monadMaybeI : Monad Maybe := + mkMonad@{ + bind {A B} : Maybe A -> (A -> Maybe B) -> Maybe B + | nothing _ := nothing + | (just a) f := f a + }; diff --git a/Stdlib/Data/Result.juvix b/Stdlib/Data/Result.juvix index 6c82211f..a46bb681 100644 --- a/Stdlib/Data/Result.juvix +++ b/Stdlib/Data/Result.juvix @@ -49,3 +49,11 @@ applicativeResultI {err} : Applicative (Result err) := | (ok _) (error e) := error e | (error e) _ := error e }; + +instance +monadResultI {err} : Monad (Result err) := + mkMonad@{ + bind {A B} : Result err A -> (A -> Result err B) -> Result err B + | (error e) _ := error e + | (ok a) f := f a + }; diff --git a/Stdlib/Trait.juvix b/Stdlib/Trait.juvix index 8e3a99aa..c255fc1f 100644 --- a/Stdlib/Trait.juvix +++ b/Stdlib/Trait.juvix @@ -5,6 +5,7 @@ import Stdlib.Trait.Show as Show open using {Show; module Show} public; import Stdlib.Trait.Ord as Ord open using {Ord; module Ord} public; import Stdlib.Trait.Functor open public; import Stdlib.Trait.Applicative open public; +import Stdlib.Trait.Monad open public; import Stdlib.Trait.Foldable open public; import Stdlib.Trait.Partial open public; import Stdlib.Trait.Natural open public; diff --git a/Stdlib/Trait/Monad.juvix b/Stdlib/Trait/Monad.juvix new file mode 100644 index 00000000..8197b4b4 --- /dev/null +++ b/Stdlib/Trait/Monad.juvix @@ -0,0 +1,20 @@ +module Stdlib.Trait.Monad; + +import Stdlib.Data.Fixity open; +import Stdlib.Trait.Applicative open; + +trait +type Monad (m : Type -> Type) := + mkMonad { + {{applicative}} : Applicative m; + builtin monad-bind + bind : {A B : Type} -> m A -> (A -> m B) -> m B + }; + +open Monad public; + +syntax operator >>= seq; +>>= {A B} {f : Type -> Type} {{Monad f}} (x : f A) (g : A -> f B) : f B := bind x g; + +syntax operator >=> seq; +>=> {A B C} {f : Type -> Type} {{Monad f}} (h : A -> f B) (g : B -> f C) (a : A) : f C := h a >>= g; diff --git a/test/Package.juvix b/test/Package.juvix index 2aa5c8a7..8ce4b428 100644 --- a/test/Package.juvix +++ b/test/Package.juvix @@ -6,5 +6,5 @@ package : Package := defaultPackage@?{ name := "stdlib-test"; dependencies := - [path "../"; github "anoma" "juvix-quickcheck" "104c749950480ed5dad42c7385a020ff31ca8875"] + [path "../"; github "anoma" "juvix-quickcheck" "b398d3cd58f0a7fb9be24d57fc5b3d82f31de555"] }; diff --git a/test/juvix.lock.yaml b/test/juvix.lock.yaml index 77e7f93b..886cfe41 100644 --- a/test/juvix.lock.yaml +++ b/test/juvix.lock.yaml @@ -2,17 +2,17 @@ # Do not edit this file manually. version: 2 -checksum: 74c7b4f7ff78859f15da9fa14ede6df5397c698d1efdcd5cc0f435d5cbb59e9d +checksum: d7a0456ff0284c1898071b9775fbcf63ba5d9d7257e9583b9573581c16f0ec89 dependencies: - path: ../ dependencies: [] - git: name: anoma_juvix-quickcheck - ref: 104c749950480ed5dad42c7385a020ff31ca8875 + ref: b398d3cd58f0a7fb9be24d57fc5b3d82f31de555 url: https://github.com/anoma/juvix-quickcheck dependencies: - git: name: anoma_juvix-stdlib - ref: 0ca2d5181e7c98eceace5c12bfd0a8cfb3d4d132 + ref: af72f25057217619a03b7a5114000d02d0abed31 url: https://github.com/anoma/juvix-stdlib dependencies: []