Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalise encoder bifunctor instances #1090

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

alexfmpe
Copy link
Contributor

I have:

  • Based work on latest develop branch
  • Followed the contribution guide
  • Looked for lint in my changes with hlint . (lint found code you did not write can be left alone)
  • Run the test suite: $(nix-build -A selftest --no-out-link)
  • Updated the changelog
  • (Optional) Run CI tests locally: nix-build release.nix -A build.x86_64-linux --no-out-link (or x86_64-darwin on macOS)

bimap f g = EncoderImpl
{ _encoderImpl_encode = bimap (_encoderImpl_encode f) (_encoderImpl_encode g)
, _encoderImpl_decode = \(a, b) -> liftA2 (,) (_encoderImpl_decode f a) (_encoderImpl_decode g b)
, _encoderImpl_decode = bitraverse (_encoderImpl_decode f) (_encoderImpl_decode g)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as

Naturality
  t . traverse f = traverse (t . f) for every applicative transformation t
Identity
  traverse Identity = Identity

give us

  traverse pure 
= traverse ((pure . runIdentity) . Identity)
= (pure . runIdentity) . traverse Identity
= pure

we also have bitraverse pure pure = pure and thus

  bitraverse df df . bimap ef eg
= bisequence . bimap df dg . bimap ef eg
= bisequence . bimap (df . ef) (dg . eg)
= bisequence . bimap pure pure 
= bitraverse pure pure 
= pure

second g = bimap id g
instance Monad parse => Bifunctor (,) (EncoderImpl parse) (EncoderImpl parse) (EncoderImpl parse) where
instance (Monad parse, Bitraversable p, Bifunctor p (->) (->) (->)) => Bifunctor p (EncoderImpl parse) (EncoderImpl parse) (EncoderImpl parse) where
Copy link
Contributor Author

@alexfmpe alexfmpe Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder whether there can be a Bifunctor p (EncoderImpl parse) (EncoderImpl parse) (EncoderImpl parse) that does anything different than bimap over p a b in the forward direction since it must be able to encode forall a b
As for whether it can do anything besides bitraverse in the backward direction, that equally applies to the Functor (EncoderImpl parse) (EncoderImpl parse) instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant