-
Notifications
You must be signed in to change notification settings - Fork 108
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
alexfmpe
wants to merge
2
commits into
obsidiansystems:develop
Choose a base branch
from
alexfmpe:bifunctor
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,6 +172,7 @@ import qualified Control.Monad.State.Strict as State | |
import Control.Monad.Writer (execWriter, tell) | ||
import Data.Aeson (FromJSON, ToJSON) | ||
import qualified Data.Aeson as Aeson | ||
import Data.Bitraversable | ||
import qualified Data.ByteString as BS | ||
import qualified Data.ByteString.Lazy as BSL | ||
import Data.Dependent.Map (DMap) | ||
|
@@ -398,25 +399,24 @@ instance Monad parse => Category (EncoderImpl parse) where | |
, _encoderImpl_encode = _encoderImpl_encode f . _encoderImpl_encode g | ||
} | ||
|
||
instance Monad parse => PFunctor (,) (EncoderImpl parse) (EncoderImpl parse) where | ||
instance (Monad parse, Bitraversable p, Bifunctor p (->) (->) (->)) => PFunctor p (EncoderImpl parse) (EncoderImpl parse) where | ||
first f = bimap f id | ||
instance Monad parse => QFunctor (,) (EncoderImpl parse) (EncoderImpl parse) where | ||
instance (Monad parse, Bitraversable p, Bifunctor p (->) (->) (->)) => QFunctor p (EncoderImpl parse) (EncoderImpl parse) where | ||
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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
} | ||
|
||
instance (Monad parse, Applicative check) => Braided (Encoder check parse) (,) where | ||
instance (Monad parse, Applicative check, Bitraversable p, Symmetric (->) p) => Braided (Encoder check parse) p where | ||
braid = viewEncoder (iso swap swap) | ||
|
||
|
||
instance (Applicative check, Monad parse) => PFunctor (,) (Encoder check parse) (Encoder check parse) where | ||
instance (Applicative check, Monad parse, Bitraversable p, Bifunctor p (->) (->) (->)) => PFunctor p (Encoder check parse) (Encoder check parse) where | ||
first f = bimap f id | ||
instance (Applicative check, Monad parse) => QFunctor (,) (Encoder check parse) (Encoder check parse) where | ||
instance (Applicative check, Monad parse, Bitraversable p, Bifunctor p (->) (->) (->)) => QFunctor p (Encoder check parse) (Encoder check parse) where | ||
second g = bimap id g | ||
instance (Applicative check, Monad parse) => Bifunctor (,) (Encoder check parse) (Encoder check parse) (Encoder check parse) where | ||
instance (Applicative check, Monad parse, Bitraversable p, Bifunctor p (->) (->) (->)) => Bifunctor p (Encoder check parse) (Encoder check parse) (Encoder check parse) where | ||
bimap f g = Encoder $ liftA2 bimap (unEncoder f) (unEncoder g) | ||
|
||
instance (Traversable f, Monad parse) => Cat.Functor f (EncoderImpl parse) (EncoderImpl parse) where | ||
|
@@ -425,40 +425,14 @@ instance (Traversable f, Monad parse) => Cat.Functor f (EncoderImpl parse) (Enco | |
, _encoderImpl_decode = traverse $ _encoderImpl_decode ve | ||
} | ||
|
||
instance Monad parse => PFunctor Either (EncoderImpl parse) (EncoderImpl parse) where | ||
first f = bimap f id | ||
instance Monad parse => QFunctor Either (EncoderImpl parse) (EncoderImpl parse) where | ||
second g = bimap id g | ||
instance Monad parse => Bifunctor Either (EncoderImpl parse) (EncoderImpl parse) (EncoderImpl parse) where | ||
bimap f g = EncoderImpl | ||
{ _encoderImpl_encode = bimap (_encoderImpl_encode f) (_encoderImpl_encode g) | ||
, _encoderImpl_decode = \case | ||
Left a -> Left <$> _encoderImpl_decode f a | ||
Right b -> Right <$> _encoderImpl_decode g b | ||
} | ||
|
||
instance (Monad parse, Applicative check) => QFunctor Either (Encoder check parse) (Encoder check parse) where | ||
second g = bimap id g | ||
instance (Monad parse, Applicative check) => PFunctor Either (Encoder check parse) (Encoder check parse) where | ||
first f = bimap f id | ||
instance (Monad parse, Applicative check) => Bifunctor Either (Encoder check parse) (Encoder check parse) (Encoder check parse) where | ||
bimap f g = Encoder $ liftA2 bimap (unEncoder f) (unEncoder g) | ||
|
||
instance (Applicative check, Monad parse) => Associative (Encoder check parse) Either where | ||
associate = viewEncoder (iso (associate @(->) @Either) disassociate) | ||
disassociate = viewEncoder (iso disassociate associate) | ||
|
||
instance (Monad parse, Applicative check) => Braided (Encoder check parse) Either where | ||
braid = viewEncoder (iso swap swap) | ||
|
||
|
||
instance (Monad parse, Applicative check, Bitraversable p, Symmetric (->) p) => Symmetric (Encoder check parse) p | ||
|
||
instance (Traversable f, Monad check, Monad parse) => Cat.Functor f (Encoder check parse) (Encoder check parse) where | ||
fmap e = Encoder $ do | ||
ve <- unEncoder e | ||
pure $ Cat.fmap ve | ||
|
||
instance Monad parse => Associative (EncoderImpl parse) (,) where | ||
instance (Monad parse, Bitraversable p, Associative (->) p) => Associative (EncoderImpl parse) p where | ||
associate = EncoderImpl | ||
{ _encoderImpl_encode = associate | ||
, _encoderImpl_decode = pure . disassociate | ||
|
@@ -468,8 +442,8 @@ instance Monad parse => Associative (EncoderImpl parse) (,) where | |
, _encoderImpl_decode = pure . associate | ||
} | ||
|
||
instance Monad parse => Monoidal (EncoderImpl parse) (,) where | ||
type Id (EncoderImpl parse) (,) = () | ||
instance (Monad parse, Bitraversable p, Monoidal (->) p) => Monoidal (EncoderImpl parse) p where | ||
type Id (EncoderImpl parse) p = Id (->) p | ||
idl = EncoderImpl | ||
{ _encoderImpl_encode = idl | ||
, _encoderImpl_decode = pure . coidl | ||
|
@@ -487,12 +461,12 @@ instance Monad parse => Monoidal (EncoderImpl parse) (,) where | |
, _encoderImpl_decode = pure . idr | ||
} | ||
|
||
instance (Applicative check, Monad parse) => Associative (Encoder check parse) (,) where | ||
instance (Applicative check, Monad parse, Bitraversable p, Associative (->) p) => Associative (Encoder check parse) p where | ||
associate = Encoder $ pure associate | ||
disassociate = Encoder $ pure disassociate | ||
|
||
instance (Applicative check, Monad parse) => Monoidal (Encoder check parse) (,) where | ||
type Id (Encoder check parse) (,) = () | ||
instance (Applicative check, Monad parse, Bitraversable p, Associative (->) p, Monoidal (EncoderImpl parse) p) => Monoidal (Encoder check parse) p where | ||
type Id (Encoder check parse) p = Id (EncoderImpl parse) p | ||
idl = Encoder $ pure idl | ||
idr = Encoder $ pure idr | ||
coidl = Encoder $ pure coidl | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thanbimap
overp a b
in the forward direction since it must be able to encodeforall a b
As for whether it can do anything besides
bitraverse
in the backward direction, that equally applies to theFunctor (EncoderImpl parse) (EncoderImpl parse)
instance