Skip to content

Commit

Permalink
add maybe instance
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jul 22, 2024
1 parent 52ca2e5 commit 378f6e9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Stdlib/Data/Maybe.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Stdlib.Data.Maybe.Base open public;
import Stdlib.Trait.Eq open;
import Stdlib.Trait.Ord open;
import Stdlib.Trait.Show open;
import Stdlib.Trait.Functor open;

import Stdlib.Data.Bool.Base open;
import Stdlib.Data.String.Base open;
Expand Down Expand Up @@ -35,3 +36,14 @@ ordMaybeI {A} {{Ord A}} : Ord (Maybe A) :=
| nothing (just _) := LT
| (just _) nothing := GT
};

instance
functorMaybeI : Functor Maybe :=
mkFunctor@{
map {A B} (f : A -> B) : Maybe A -> Maybe B
| nothing := nothing
| (just a) := just (f a)
};

instance
monomorphicFunctorMaybeI {A} : Monomorphic.Functor (Maybe A) A := fromPolymorphicFunctor;
1 change: 1 addition & 0 deletions Stdlib/Trait.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Stdlib.Trait;
import Stdlib.Trait.Eq as Eq open using {Eq; module Eq} public;
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.Partial open public;
import Stdlib.Trait.Natural open public;
import Stdlib.Trait.Integral open public;
Expand Down
1 change: 1 addition & 0 deletions Stdlib/Trait/Functor.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ module Stdlib.Trait.Functor;

import Stdlib.Trait.Functor.Polymorphic open public;
import Stdlib.Trait.Functor.Monomorphic as Monomorphic public;
import Stdlib.Trait.Functor.Monomorphic open using {fromPolymorphicFunctor} public;
9 changes: 8 additions & 1 deletion Stdlib/Trait/Functor/Monomorphic.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Stdlib.Trait.Functor.Monomorphic;

import Stdlib.Data.Fixity open;
import Stdlib.Data.Unit open;
import Stdlib.Trait.Functor.Polymorphic as Poly;

trait
type Functor (container elem : Type) := mkFunctor {map : (elem -> elem) -> container -> container};
Expand All @@ -10,8 +11,14 @@ open Functor public;

open Functor public;

fromPolymorphicFunctor {f : Type -> Type} {{Poly.Functor f}} {elem} : Functor (f elem) elem :=
mkFunctor@{
map := Poly.map
};

syntax operator <$> lapp;
syntax alias <$> := map;

syntax operator $> lapp;
$> {container elem : Type} {{Functor container elem}} (fa : container) (b : elem) : container := λ {_ := b} <$> fa;
$> {container elem : Type} {{Functor container elem}} (fa : container) (b : elem) : container :=
λ {_ := b} <$> fa;

0 comments on commit 378f6e9

Please sign in to comment.