Skip to content

Commit

Permalink
add Monad trait (#119)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Cadman <[email protected]>
  • Loading branch information
janmasrovira and paulcadman authored Aug 29, 2024
1 parent f51043d commit 615a02c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Stdlib/Data/List.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ applicativeListI : Applicative List :=
| [] _ := []
| (f :: fs) l := map f l ++ ap fs l
};

instance
monadListI : Monad List :=
mkMonad@{
bind := flip concatMap
};
9 changes: 9 additions & 0 deletions Stdlib/Data/Maybe.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
};
8 changes: 8 additions & 0 deletions Stdlib/Data/Result.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
1 change: 1 addition & 0 deletions Stdlib/Trait.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions Stdlib/Trait/Monad.juvix
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion test/Package.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ package : Package :=
defaultPackage@?{
name := "stdlib-test";
dependencies :=
[path "../"; github "anoma" "juvix-quickcheck" "104c749950480ed5dad42c7385a020ff31ca8875"]
[path "../"; github "anoma" "juvix-quickcheck" "b398d3cd58f0a7fb9be24d57fc5b3d82f31de555"]
};
6 changes: 3 additions & 3 deletions test/juvix.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: []

0 comments on commit 615a02c

Please sign in to comment.