Skip to content

Commit

Permalink
fix: add State 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 01786a0 commit 01d490b
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 1 deletion.
3 changes: 3 additions & 0 deletions scan.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off

busybox find . -type f -name "*\.go" | busybox xargs gopls check
31 changes: 31 additions & 0 deletions state/eq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2023 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 state

import (
EQ "github.com/IBM/fp-go/eq"
G "github.com/IBM/fp-go/state/generic"
)

// Constructs an equal predicate for a [State]
func Eq[S, A any](w EQ.Eq[S], a EQ.Eq[A]) func(S) EQ.Eq[State[S, A]] {
return G.Eq[State[S, A]](w, a)
}

// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func FromStrictEquals[S, A comparable]() func(S) EQ.Eq[State[S, A]] {
return G.FromStrictEquals[State[S, A]]()
}
36 changes: 36 additions & 0 deletions state/generic/eq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2023 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 (
EQ "github.com/IBM/fp-go/eq"
P "github.com/IBM/fp-go/pair"
)

// Constructs an equal predicate for a [State]
func Eq[GA ~func(S) P.Pair[A, S], S, A any](w EQ.Eq[S], a EQ.Eq[A]) func(S) EQ.Eq[GA] {
eqp := P.Eq(a, w)
return func(s S) EQ.Eq[GA] {
return EQ.FromEquals(func(l, r GA) bool {
return eqp.Equals(l(s), r(s))
})
}
}

// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
func FromStrictEquals[GA ~func(S) P.Pair[A, S], S, A comparable]() func(S) EQ.Eq[GA] {
return Eq[GA](EQ.FromStrictEquals[S](), EQ.FromStrictEquals[A]())
}
88 changes: 88 additions & 0 deletions state/generic/monad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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"
P "github.com/IBM/fp-go/pair"
)

type statePointed[GA ~func(S) P.Pair[A, S], S, A any] struct{}

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

type stateApplicative[GB ~func(S) P.Pair[B, S], GAB ~func(S) P.Pair[func(A) B, S], GA ~func(S) P.Pair[A, S], S, A, B any] struct{}

type stateMonad[GB ~func(S) P.Pair[B, S], GAB ~func(S) P.Pair[func(A) B, S], GA ~func(S) P.Pair[A, S], S, A, B any] struct{}

func (o *statePointed[GA, S, A]) Of(a A) GA {
return Of[GA](a)
}

func (o *stateApplicative[GB, GAB, GA, S, A, B]) Of(a A) GA {
return Of[GA](a)
}

func (o *stateMonad[GB, GAB, GA, S, A, B]) Of(a A) GA {
return Of[GA](a)
}

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

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

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

func (o *stateMonad[GB, GAB, GA, S, A, B]) Chain(f func(A) GB) func(GA) GB {
return Chain[GB, GA](f)
}

func (o *stateApplicative[GB, GAB, GA, S, A, B]) Ap(fa GA) func(GAB) GB {
return Ap[GB, GAB, GA](fa)
}

func (o *stateMonad[GB, GAB, GA, S, A, B]) Ap(fa GA) func(GAB) GB {
return Ap[GB, GAB, GA](fa)
}

// Pointed implements the pointed operations for [Writer]
func Pointed[GA ~func(S) P.Pair[A, S], S, A any]() pointed.Pointed[A, GA] {
return &statePointed[GA, S, A]{}
}

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

// Applicative implements the applicative operations for [Writer]
func Applicative[GB ~func(S) P.Pair[B, S], GAB ~func(S) P.Pair[func(A) B, S], GA ~func(S) P.Pair[A, S], S, A, B any]() applicative.Applicative[A, B, GA, GB, GAB] {
return &stateApplicative[GB, GAB, GA, S, A, B]{}
}

// Monad implements the monadic operations for [Writer]
func Monad[GB ~func(S) P.Pair[B, S], GAB ~func(S) P.Pair[func(A) B, S], GA ~func(S) P.Pair[A, S], S, A, B any]() monad.Monad[A, B, GA, GB, GAB] {
return &stateMonad[GB, GAB, GA, S, A, B]{}
}
131 changes: 131 additions & 0 deletions state/generic/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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 (
F "github.com/IBM/fp-go/function"
C "github.com/IBM/fp-go/internal/chain"
FC "github.com/IBM/fp-go/internal/functor"
P "github.com/IBM/fp-go/pair"
)

var (
undefined any = struct{}{}
)

func Get[GA ~func(S) P.Pair[S, S], S any]() GA {
return P.Of[S]
}

func Gets[GA ~func(S) P.Pair[A, S], FCT ~func(S) A, A, S any](f FCT) GA {
return func(s S) P.Pair[A, S] {
return P.MakePair(f(s), s)
}
}

func Put[GA ~func(S) P.Pair[any, S], S any]() GA {
return F.Bind1st(P.MakePair[any, S], undefined)
}

func Modify[GA ~func(S) P.Pair[any, S], FCT ~func(S) S, S any](f FCT) GA {
return F.Flow2(
f,
F.Bind1st(P.MakePair[any, S], undefined),
)
}

func Of[GA ~func(S) P.Pair[A, S], S, A any](a A) GA {
return F.Bind1st(P.MakePair[A, S], a)
}

func MonadMap[GB ~func(S) P.Pair[B, S], GA ~func(S) P.Pair[A, S], FCT ~func(A) B, S, A, B any](fa GA, f FCT) GB {
return func(s S) P.Pair[B, S] {
p2 := fa(s)
return P.MakePair(f(P.Head(p2)), P.Tail(p2))
}
}

func Map[GB ~func(S) P.Pair[B, S], GA ~func(S) P.Pair[A, S], FCT ~func(A) B, S, A, B any](f FCT) func(GA) GB {
return F.Bind2nd(MonadMap[GB, GA, FCT, S, A, B], f)
}

func MonadChain[GB ~func(S) P.Pair[B, S], GA ~func(S) P.Pair[A, S], FCT ~func(A) GB, S, A, B any](fa GA, f FCT) GB {
return func(s S) P.Pair[B, S] {
a := fa(s)
return f(P.Head(a))(P.Tail(a))
}
}

func Chain[GB ~func(S) P.Pair[B, S], GA ~func(S) P.Pair[A, S], FCT ~func(A) GB, S, A, B any](f FCT) func(GA) GB {
return F.Bind2nd(MonadChain[GB, GA, FCT, S, A, B], f)
}

func MonadAp[GB ~func(S) P.Pair[B, S], GAB ~func(S) P.Pair[func(A) B, S], GA ~func(S) P.Pair[A, S], S, A, B any](fab GAB, fa GA) GB {
return func(s S) P.Pair[B, S] {
f := fab(s)
a := fa(P.Tail(f))

return P.MakePair(P.Head(f)(P.Head(a)), P.Tail(a))
}
}

func Ap[GB ~func(S) P.Pair[B, S], GAB ~func(S) P.Pair[func(A) B, S], GA ~func(S) P.Pair[A, S], S, A, B any](ga GA) func(GAB) GB {
return F.Bind2nd(MonadAp[GB, GAB, GA, S, A, B], ga)
}

func MonadChainFirst[GB ~func(S) P.Pair[B, S], GA ~func(S) P.Pair[A, S], FCT ~func(A) GB, S, A, B any](ma GA, f FCT) GA {
return C.MonadChainFirst(
MonadChain[GA, GA, func(A) GA],
MonadMap[GA, GB, func(B) A],
ma,
f,
)
}

func ChainFirst[GB ~func(S) P.Pair[B, S], GA ~func(S) P.Pair[A, S], FCT ~func(A) GB, S, A, B any](f FCT) func(GA) GA {
return C.ChainFirst(
Chain[GA, GA, func(A) GA],
Map[GA, GB, func(B) A],
f,
)
}

func Flatten[GAA ~func(S) P.Pair[GA, S], GA ~func(S) P.Pair[A, S], S, A any](mma GAA) GA {
return MonadChain[GA, GAA, func(GA) GA](mma, F.Identity[GA])
}

func Execute[GA ~func(S) P.Pair[A, S], S, A any](s S) func(GA) S {
return func(fa GA) S {
return P.Tail(fa(s))
}
}

func Evaluate[GA ~func(S) P.Pair[A, S], S, A any](s S) func(GA) A {
return func(fa GA) A {
return P.Head(fa(s))
}
}

func MonadFlap[FAB ~func(A) B, GFAB ~func(S) P.Pair[FAB, S], GB ~func(S) P.Pair[B, S], S, A, B any](fab GFAB, a A) GB {
return FC.MonadFlap(
MonadMap[GB, GFAB, func(FAB) B],
fab,
a)
}

func Flap[FAB ~func(A) B, GFAB ~func(S) P.Pair[FAB, S], GB ~func(S) P.Pair[B, S], S, A, B any](a A) func(GFAB) GB {
return FC.Flap(Map[GB, GFAB, func(FAB) B], a)
}
44 changes: 44 additions & 0 deletions state/monad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 state

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"
G "github.com/IBM/fp-go/state/generic"
)

// Pointed implements the pointed operations for [State]
func Pointed[S, A any]() pointed.Pointed[A, State[S, A]] {
return G.Pointed[State[S, A], S, A]()
}

// Functor implements the pointed operations for [State]
func Functor[S, A, B any]() functor.Functor[A, B, State[S, A], State[S, B]] {
return G.Functor[State[S, B], State[S, A], S, A, B]()
}

// Applicative implements the applicative operations for [State]
func Applicative[S, A, B any]() applicative.Applicative[A, B, State[S, A], State[S, B], State[S, func(A) B]] {
return G.Applicative[State[S, B], State[S, func(A) B], State[S, A]]()
}

// Monad implements the monadic operations for [State]
func Monad[S, A, B any]() monad.Monad[A, B, State[S, A], State[S, B], State[S, func(A) B]] {
return G.Monad[State[S, B], State[S, func(A) B], State[S, A]]()
}

0 comments on commit 01d490b

Please sign in to comment.