Skip to content

Commit

Permalink
add list instances
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Jul 22, 2024
1 parent 378f6e9 commit 3bb34f3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Stdlib/Data/List.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Stdlib.Data.String.Base open;
import Stdlib.Trait.Eq open;
import Stdlib.Trait.Ord open;
import Stdlib.Trait.Show open;
import Stdlib.Trait.Functor as Functorr open;
import Stdlib.Trait.Partial open;

--- 𝒪(1). Partial function that returns the first element of a ;List;.
Expand Down Expand Up @@ -49,3 +50,12 @@ showListI {A} {{Show A}} : Show (List A) :=
| nil := "nil"
| s := "(" ++str go s ++str ")"
};

instance
functorListI : Functor List :=
mkFunctor@{
map := listMap
};

instance
monomorphicFunctorListI {A} : Monomorphic.Functor (List A) A := fromPolymorphicFunctor;
10 changes: 5 additions & 5 deletions Stdlib/Data/List/Base.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ syntax iterator for {init := 1; range := 1};
{-# inline: 0, isabelle-function: {name: "foldl"} #-}
for : {A B : Type} → (B → A → B) → B → List A → B := foldl;

syntax iterator map {init := 0; range := 1};
syntax iterator listMap {init := 0; range := 1};

--- 𝒪(𝓃). Maps a function over each element of a ;List;.
{-# specialize: [1] #-}
map {A B} (f : A → B) : List A → List B
listMap {A B} (f : A → B) : List A → List B
| nil := nil
| (h :: hs) := f h :: map f hs;
| (h :: hs) := f h :: listMap f hs;

syntax iterator filter {init := 0; range := 1};

Expand Down Expand Up @@ -221,10 +221,10 @@ syntax iterator concatMap {init := 0; range := 1};

--- Applies a function to every item on a ;List; and concatenates the result.
--- 𝒪(𝓃), where 𝓃 is the number of items in the resulting list.
concatMap {A B} (f : A -> List B) : List A -> List B := map f >> flatten;
concatMap {A B} (f : A -> List B) : List A -> List B := listMap f >> flatten;

--- 𝒪(𝓃 * 𝓂). Transposes a ;List; of ;List;s interpreted as a matrix.
transpose {A} : List (List A) -> List (List A)
| nil := nil
| (xs :: nil) := map λ {x := x :: nil} xs
| (xs :: nil) := listMap λ {x := x :: nil} xs
| (xs :: xss) := zipWith (::) xs (transpose xss);
6 changes: 5 additions & 1 deletion Stdlib/Trait/Functor/Monomorphic.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import Stdlib.Data.Unit open;
import Stdlib.Trait.Functor.Polymorphic as Poly;

trait
type Functor (container elem : Type) := mkFunctor {map : (elem -> elem) -> container -> container};
type Functor (container elem : Type) :=
mkFunctor {
syntax iterator map {init := 0; range := 1};
map : (elem -> elem) -> container -> container
};

open Functor public;

Expand Down
8 changes: 6 additions & 2 deletions Stdlib/Trait/Functor/Polymorphic.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import Stdlib.Data.Fixity open;
import Stdlib.Data.Unit open;

trait
type Functor (f : Type -> Type) := mkFunctor {map : {A B : Type} -> (A -> B) -> f A -> f B};
type Functor (f : Type -> Type) :=
mkFunctor {
syntax iterator map {init := 0; range := 1};
map : {A B : Type} -> (A -> B) -> f A -> f B
};

open Functor public;

open Functor public;

syntax operator <$> lapp;
syntax alias <$> := map;
<$> {f : Type -> Type} {{Functor f}} {A B} (fun : A -> B) (l : f A) : f B := map fun l;

syntax operator $> lapp;
$> {f : Type → Type} {A B : Type} {{Functor f}} (fa : f A) (b : B) : f B := λ {_ := b} <$> fa;
Expand Down

0 comments on commit 3bb34f3

Please sign in to comment.