Skip to content

Commit

Permalink
add liftA2, mapA_, replicateA
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Aug 5, 2024
1 parent 51dc4d6 commit ea4faec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Stdlib/Data/Unit.juvix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- The unit type.
module Stdlib.Data.Unit;

import Stdlib.Data.Unit.Base open;
import Stdlib.Data.Unit.Base open public;
import Stdlib.Data.Bool.Base open;

import Stdlib.Trait.Eq open;
Expand Down
21 changes: 19 additions & 2 deletions Stdlib/Trait/Applicative.juvix
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
module Stdlib.Trait.Applicative;

import Stdlib.Data.Fixity open;
import Stdlib.Function open;
import Stdlib.Data.Bool.Base open;
import Stdlib.Data.Nat.Base open;
import Stdlib.Data.List.Base open;
import Stdlib.Data.Unit.Base open;
import Stdlib.Trait.Functor open;
import Stdlib.Trait.Foldable.Polymorphic open;
import Stdlib.Data.Unit.Base open;
Expand All @@ -20,8 +25,12 @@ open Applicative public;
applicativeSeq {f : Type -> Type} {{Applicative f}} {A B : Type} (fa : f A) (fb : f B) : f B :=
ap (map λ {_ b := b} fa) fb;

syntax iterator mapM_ {init := 0; range := 1};
mapM_
--- lifts a binary function to ;Applicative;
liftA2 {f : Type -> Type} {{Applicative f}} {A B C} (fun : A -> B -> C) : f A -> f B -> f C :=
map fun >> ap;

syntax iterator mapA_ {init := 0; range := 1};
mapA_
{t : Type -> Type}
{f : Type -> Type}
{A B}
Expand All @@ -30,3 +39,11 @@ mapM_
(fun : A -> f B)
(l : t A)
: f Unit := rfor (acc := pure unit) (x in l) {applicativeSeq (fun x) acc};

replicateA {f : Type -> Type} {A} {{Applicative f}} : Nat -> f A -> f (List A)
| zero _ := pure []
| (suc n) x := liftA2 (::) x (replicateA n x);

when {f : Type -> Type} {{Applicative f}} : Bool -> f Unit -> f Unit
| false _ := pure unit
| true a := a;

0 comments on commit ea4faec

Please sign in to comment.