Skip to content

Commit

Permalink
fix: refactor Writer monad
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 13, 2024
1 parent d0e4984 commit 01786a0
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 149 deletions.
11 changes: 7 additions & 4 deletions writer/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ package writer

import (
M "github.com/IBM/fp-go/monoid"
SG "github.com/IBM/fp-go/semigroup"
G "github.com/IBM/fp-go/writer/generic"
)

// Bind creates an empty context of type [S] to be used with the [Bind] operation
func Do[S, W any](m M.Monoid[W]) func(S) Writer[W, S] {
return G.Do[Writer[W, S], W, S](m)
func Do[S, W any](m M.Monoid[W], s S) Writer[W, S] {
return G.Do[Writer[W, S], W, S](m, s)
}

// Bind attaches the result of a computation to a context [S1] to produce a context [S2]
func Bind[S1, S2, T, W any](
s SG.Semigroup[W],
setter func(T) func(S1) S2,
f func(S1) Writer[W, T],
) func(Writer[W, S1]) Writer[W, S2] {
return G.Bind[Writer[W, S1], Writer[W, S2], Writer[W, T], W, S1, S2, T](setter, f)
return G.Bind[Writer[W, S1], Writer[W, S2], Writer[W, T], W, S1, S2, T](s, setter, f)
}

// Let attaches the result of a computation to a context [S1] to produce a context [S2]
Expand Down Expand Up @@ -58,8 +60,9 @@ func BindTo[W, S1, T any](

// ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently
func ApS[S1, S2, T, W any](
s SG.Semigroup[W],
setter func(T) func(S1) S2,
fa Writer[W, T],
) func(Writer[W, S1]) Writer[W, S2] {
return G.ApS[Writer[W, S1], Writer[W, S2], Writer[W, T], W, S1, S2, T](setter, fa)
return G.ApS[Writer[W, S1], Writer[W, S2], Writer[W, T], W, S1, S2, T](s, setter, fa)
}
20 changes: 10 additions & 10 deletions writer/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ var (
)

func getLastName(s utils.Initial) Writer[[]string, string] {
return Of[string](monoid)("Doe")
return Of[string](monoid, "Doe")
}

func getGivenName(s utils.WithLastName) Writer[[]string, string] {
return Of[string](monoid)("John")
return Of[string](monoid, "John")
}

func TestBind(t *testing.T) {

res := F.Pipe3(
Do[utils.Initial](monoid)(utils.Empty),
Bind(utils.SetLastName, getLastName),
Bind(utils.SetGivenName, getGivenName),
Do[utils.Initial](monoid, utils.Empty),
Bind(sg, utils.SetLastName, getLastName),
Bind(sg, utils.SetGivenName, getGivenName),
Map[[]string](utils.GetFullName),
)

assert.True(t, eq.Equals(res, Of[string](monoid)("John Doe")))
assert.True(t, eq.Equals(res, Of[string](monoid, "John Doe")))
}

func TestApS(t *testing.T) {

res := F.Pipe3(
Do[utils.Initial](monoid)(utils.Empty),
ApS(utils.SetLastName, Of[string](monoid)("Doe")),
ApS(utils.SetGivenName, Of[string](monoid)("John")),
Do[utils.Initial](monoid, utils.Empty),
ApS(sg, utils.SetLastName, Of[string](monoid, "Doe")),
ApS(sg, utils.SetGivenName, Of[string](monoid, "John")),
Map[[]string](utils.GetFullName),
)

assert.True(t, eq.Equals(res, Of[string](monoid)("John Doe")))
assert.True(t, eq.Equals(res, Of[string](monoid, "John Doe")))
}
25 changes: 14 additions & 11 deletions writer/generic/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,36 @@
package generic

import (
FCT "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/apply"
C "github.com/IBM/fp-go/internal/chain"
F "github.com/IBM/fp-go/internal/functor"
M "github.com/IBM/fp-go/monoid"
P "github.com/IBM/fp-go/pair"
SG "github.com/IBM/fp-go/semigroup"
T "github.com/IBM/fp-go/tuple"
)

// Bind creates an empty context of type [S] to be used with the [Bind] operation
func Do[GS ~func() T.Tuple3[S, W, SG.Semigroup[W]], W, S any](m M.Monoid[W]) func(S) GS {
return Of[GS, W, S](m)
func Do[GS ~func() P.Pair[S, W], W, S any](m M.Monoid[W], s S) GS {
return Of[GS, W, S](m, s)
}

// Bind attaches the result of a computation to a context [S1] to produce a context [S2]
func Bind[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GS2 ~func() T.Tuple3[S2, W, SG.Semigroup[W]], GT ~func() T.Tuple3[A, W, SG.Semigroup[W]], W, S1, S2, A any](
func Bind[GS1 ~func() P.Pair[S1, W], GS2 ~func() P.Pair[S2, W], GT ~func() P.Pair[A, W], W, S1, S2, A any](
s SG.Semigroup[W],
setter func(A) func(S1) S2,
f func(S1) GT,
) func(GS1) GS2 {
return C.Bind(
Chain[GS2, GS1, func(S1) GS2, W, S1, S2],
FCT.Bind1st(Chain[GS2, GS1, func(S1) GS2, W, S1, S2], s),
Map[GS2, GT, func(A) S2, W, A, S2],
setter,
f,
)
}

// Let attaches the result of a computation to a context [S1] to produce a context [S2]
func Let[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GS2 ~func() T.Tuple3[S2, W, SG.Semigroup[W]], W, S1, S2, A any](
func Let[GS1 ~func() P.Pair[S1, W], GS2 ~func() P.Pair[S2, W], W, S1, S2, A any](
key func(A) func(S1) S2,
f func(S1) A,
) func(GS1) GS2 {
Expand All @@ -55,7 +57,7 @@ func Let[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GS2 ~func() T.Tuple3[S2,
}

// LetTo attaches the a value to a context [S1] to produce a context [S2]
func LetTo[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GS2 ~func() T.Tuple3[S2, W, SG.Semigroup[W]], W, S1, S2, B any](
func LetTo[GS1 ~func() P.Pair[S1, W], GS2 ~func() P.Pair[S2, W], W, S1, S2, B any](
key func(B) func(S1) S2,
b B,
) func(GS1) GS2 {
Expand All @@ -67,7 +69,7 @@ func LetTo[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GS2 ~func() T.Tuple3[S2
}

// BindTo initializes a new state [S1] from a value [T]
func BindTo[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GT ~func() T.Tuple3[A, W, SG.Semigroup[W]], W, S1, A any](
func BindTo[GS1 ~func() P.Pair[S1, W], GT ~func() P.Pair[A, W], W, S1, A any](
setter func(A) S1,
) func(GT) GS1 {
return C.BindTo(
Expand All @@ -77,13 +79,14 @@ func BindTo[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GT ~func() T.Tuple3[A,
}

// ApS attaches a value to a context [S1] to produce a context [S2] by considering the context and the value concurrently
func ApS[GS1 ~func() T.Tuple3[S1, W, SG.Semigroup[W]], GS2 ~func() T.Tuple3[S2, W, SG.Semigroup[W]], GT ~func() T.Tuple3[A, W, SG.Semigroup[W]], W, S1, S2, A any](
func ApS[GS1 ~func() P.Pair[S1, W], GS2 ~func() P.Pair[S2, W], GT ~func() P.Pair[A, W], W, S1, S2, A any](
s SG.Semigroup[W],
setter func(A) func(S1) S2,
fa GT,
) func(GS1) GS2 {
return apply.ApS(
Ap[GS2, func() T.Tuple3[func(A) S2, W, SG.Semigroup[W]], GT, W, A, S2],
Map[func() T.Tuple3[func(A) S2, W, SG.Semigroup[W]], GS1, func(S1) func(A) S2],
FCT.Bind1st(Ap[GS2, func() P.Pair[func(A) S2, W], GT, W, A, S2], s),
Map[func() P.Pair[func(A) S2, W], GS1, func(S1) func(A) S2],
setter,
fa,
)
Expand Down
13 changes: 5 additions & 8 deletions writer/generic/eq.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@ package generic

import (
EQ "github.com/IBM/fp-go/eq"
SG "github.com/IBM/fp-go/semigroup"
T "github.com/IBM/fp-go/tuple"
P "github.com/IBM/fp-go/pair"
)

// Constructs an equal predicate for a [Writer]
func Eq[GA ~func() T.Tuple3[A, W, SG.Semigroup[W]], W, A any](w EQ.Eq[W], a EQ.Eq[A]) EQ.Eq[GA] {
func Eq[GA ~func() P.Pair[A, W], W, A any](w EQ.Eq[W], a EQ.Eq[A]) EQ.Eq[GA] {
eqp := P.Eq(a, w)
return EQ.FromEquals(func(l, r GA) bool {
ll := l()
rr := r()

return a.Equals(ll.F1, rr.F1) && w.Equals(ll.F2, rr.F2)
return eqp.Equals(l(), r())
})
}

// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func FromStrictEquals[GA ~func() T.Tuple3[A, W, SG.Semigroup[W]], W, A comparable]() EQ.Eq[GA] {
func FromStrictEquals[GA ~func() P.Pair[A, W], W, A comparable]() EQ.Eq[GA] {
return Eq[GA](EQ.FromStrictEquals[W](), EQ.FromStrictEquals[A]())
}
106 changes: 106 additions & 0 deletions writer/generic/monad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2024 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package generic

import (
"github.com/IBM/fp-go/internal/applicative"
"github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/monad"
"github.com/IBM/fp-go/internal/pointed"
M "github.com/IBM/fp-go/monoid"
P "github.com/IBM/fp-go/pair"
SG "github.com/IBM/fp-go/semigroup"
)

type writerPointed[GA ~func() P.Pair[A, W], W, A any] struct {
m M.Monoid[W]
}

type writerFunctor[GB ~func() P.Pair[B, W], GA ~func() P.Pair[A, W], W, A, B any] struct{}

type writerApplicative[GB ~func() P.Pair[B, W], GAB ~func() P.Pair[func(A) B, W], GA ~func() P.Pair[A, W], W, A, B any] struct {
s SG.Semigroup[W]
m M.Monoid[W]
}

type writerMonad[GB ~func() P.Pair[B, W], GAB ~func() P.Pair[func(A) B, W], GA ~func() P.Pair[A, W], W, A, B any] struct {
s SG.Semigroup[W]
m M.Monoid[W]
}

func (o *writerPointed[GA, W, A]) Of(a A) GA {
return Of[GA](o.m, a)
}

func (o *writerApplicative[GB, GAB, GA, W, A, B]) Of(a A) GA {
return Of[GA](o.m, a)
}

func (o *writerMonad[GB, GAB, GA, W, A, B]) Of(a A) GA {
return Of[GA](o.m, a)
}

func (o *writerFunctor[GB, GA, W, A, B]) Map(f func(A) B) func(GA) GB {
return Map[GB, GA](f)
}

func (o *writerApplicative[GB, GAB, GA, W, A, B]) Map(f func(A) B) func(GA) GB {
return Map[GB, GA](f)
}

func (o *writerMonad[GB, GAB, GA, W, A, B]) Map(f func(A) B) func(GA) GB {
return Map[GB, GA](f)
}

func (o *writerMonad[GB, GAB, GA, W, A, B]) Chain(f func(A) GB) func(GA) GB {
return Chain[GB, GA](o.s, f)
}

func (o *writerApplicative[GB, GAB, GA, W, A, B]) Ap(fa GA) func(GAB) GB {
return Ap[GB, GAB, GA](o.s, fa)
}

func (o *writerMonad[GB, GAB, GA, W, A, B]) Ap(fa GA) func(GAB) GB {
return Ap[GB, GAB, GA](o.s, fa)
}

// Pointed implements the pointed operations for [Writer]
func Pointed[GA ~func() P.Pair[A, W], W, A any](m M.Monoid[W]) pointed.Pointed[A, GA] {
return &writerPointed[GA, W, A]{
m: m,
}
}

// Functor implements the functor operations for [Writer]
func Functor[GB ~func() P.Pair[B, W], GA ~func() P.Pair[A, W], W, A, B any]() functor.Functor[A, B, GA, GB] {
return &writerFunctor[GB, GA, W, A, B]{}
}

// Applicative implements the applicative operations for [Writer]
func Applicative[GB ~func() P.Pair[B, W], GAB ~func() P.Pair[func(A) B, W], GA ~func() P.Pair[A, W], W, A, B any](m M.Monoid[W]) applicative.Applicative[A, B, GA, GB, GAB] {
return &writerApplicative[GB, GAB, GA, W, A, B]{
s: M.ToSemigroup(m),
m: m,
}
}

// Monad implements the monadic operations for [Writer]
func Monad[GB ~func() P.Pair[B, W], GAB ~func() P.Pair[func(A) B, W], GA ~func() P.Pair[A, W], W, A, B any](m M.Monoid[W]) monad.Monad[A, B, GA, GB, GAB] {
return &writerMonad[GB, GAB, GA, W, A, B]{
s: M.ToSemigroup(m),
m: m,
}
}

0 comments on commit 01786a0

Please sign in to comment.