-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
added TFunctor
#1815
added TFunctor
#1815
Conversation
Not sure about the laws but this could work for StateT as well, no?
Get Outlook for iOS<https://aka.ms/o0ukef>
…________________________________
From: Kai(luo) Wang <notifications@github.com>
Sent: Friday, August 11, 2017 12:41:08 AM
To: typelevel/cats
Cc: Subscribed
Subject: [typelevel/cats] added `TFunctor` (#1815)
added instances for WriterT, Kleisli, OptionT, EitherT and FreeT
Fixes #1812<#1812>
________________________________
You can view, comment on, or merge this pull request online at:
#1815
Commit Summary
* added TFunctor
File Changes
* A core/src/main/scala/cats/TFunctor.scala<https://github.com/typelevel/cats/pull/1815/files#diff-0> (31)
* M core/src/main/scala/cats/data/EitherT.scala<https://github.com/typelevel/cats/pull/1815/files#diff-1> (4)
* M core/src/main/scala/cats/data/IdT.scala<https://github.com/typelevel/cats/pull/1815/files#diff-2> (4)
* M core/src/main/scala/cats/data/Kleisli.scala<https://github.com/typelevel/cats/pull/1815/files#diff-3> (5)
* M core/src/main/scala/cats/data/OptionT.scala<https://github.com/typelevel/cats/pull/1815/files#diff-4> (5)
* M core/src/main/scala/cats/data/WriterT.scala<https://github.com/typelevel/cats/pull/1815/files#diff-5> (5)
* M core/src/main/scala/cats/syntax/all.scala<https://github.com/typelevel/cats/pull/1815/files#diff-6> (1)
* M core/src/main/scala/cats/syntax/package.scala<https://github.com/typelevel/cats/pull/1815/files#diff-7> (1)
* A core/src/main/scala/cats/syntax/tfunctor.scala<https://github.com/typelevel/cats/pull/1815/files#diff-8> (4)
* M free/src/main/scala/cats/free/FreeT.scala<https://github.com/typelevel/cats/pull/1815/files#diff-9> (6)
* M free/src/test/scala/cats/free/FreeTTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-10> (12)
* A laws/src/main/scala/cats/laws/TFunctorLaws.scala<https://github.com/typelevel/cats/pull/1815/files#diff-11> (24)
* M laws/src/main/scala/cats/laws/discipline/Arbitrary.scala<https://github.com/typelevel/cats/pull/1815/files#diff-12> (11)
* A laws/src/main/scala/cats/laws/discipline/TFunctorTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-13> (37)
* M tests/src/test/scala/cats/tests/EitherTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-14> (4)
* M tests/src/test/scala/cats/tests/IdTTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-15> (3)
* M tests/src/test/scala/cats/tests/KleisliTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-16> (5)
* M tests/src/test/scala/cats/tests/OptionTTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-17> (5)
* M tests/src/test/scala/cats/tests/WriterTTests.scala<https://github.com/typelevel/cats/pull/1815/files#diff-18> (3)
Patch Links:
* https://github.com/typelevel/cats/pull/1815.patch
* https://github.com/typelevel/cats/pull/1815.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub<#1815>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AARSxEqQZHPYP58wp27n6ak5a0UGJBlcks5sW3jzgaJpZM4O0A8c>.
|
@iravid |
added instances for WriterT, Kleisli, OptionT, EitherT and FreeT
Ah, you're right - I missed the part where |
Codecov Report
@@ Coverage Diff @@
## master #1815 +/- ##
==========================================
+ Coverage 94.88% 94.94% +0.05%
==========================================
Files 241 244 +3
Lines 4148 4193 +45
Branches 102 104 +2
==========================================
+ Hits 3936 3981 +45
Misses 212 212
Continue to review full report at Codecov.
|
@@ -31,6 +31,11 @@ object Func extends FuncInstances { | |||
} | |||
|
|||
private[data] abstract class FuncInstances extends FuncInstances0 { | |||
|
|||
implicit def catsDataTFunctorFoFunc[A]: TFunctor[Func[?[_], A, ?]] = new TFunctor[Func[?[_], A, ?]] { |
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.
Missing "r" in "foFunc", similarly in "foNested".
I don't think we need to provide this type class to have a uniform API. We can just have a uniform API. Type classes are abstractions; unless there's a useful way to abstract over this, I think we should not include it. This is the standard for all other type classes in cats if I'm not mistaken. |
@edmundnoble I think the type class might be able to provide reusable code other than |
* as `OptionT`, `EitherT` | ||
* | ||
*/ | ||
@typeclass trait TFunctor[T[_[_], _]] { |
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.
do we need T[_[_], _]
or would T[_[_]]
do? I'm not sure what the second parameter is buying us. Maybe I'm missing it. Can't we just ignore the inner value type? Also, wouldn't we have more flexibility only acting on T[_[_]]
?
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.
Agreed here. If we had T[_[_]]
I could come up with a lot more usecases. Maybe call it FunctorK
then. We'd have to wrap the transformers in Forall
though, which may mean our unified transform syntax fails to be found implicitly.
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.
The main reason I chose T[_[_], _]
here, is that given any Functor
F[_]
, T[F, _]
is also a Functor
. So T[_[_], _]
can be deemed as a functor between the F[_]
Functor
and the T[F, _]
Functor
. i.e. a endofunctor in the category of endofunctors, thus more like a higher kinded Functor
.
If you use T[_[_]]
, then given any Functor
F[_]
, T[F]
isn't a Functor
, it's a kind * type, not a type constructor. So T[_[_]]
is a functor between F[_]
Functor
and kind * type T[F]
s. That makes it less a high kinded Functor
. However, you can work around with the parametricity in instance definitions, e.g. for OptionT
,
def functorKInstance[A]: FunctorK[OptionT[?[_], A]]
With parametric A
, we can have a FunctorK
instance forall A
s. In some sense, we may choose to see this set of FunctorK
instances as a single truely higher kinded functor.
Practical use wise, I can't think of any downside of using T[_[_]]
. The only downside is its awkward encoding of the higher kinded Functor
- it relies on instance definition. It may have more usecases, one that immediately jumps out is FunctionK
. So maybe practically it's more useful.
Now, the fact that we aren't sure about this thing makes me think that maybe we shouldn't place this in cats-core
. I already have FunctorK[T[_]]
defined in mainecoon, I could add all these instances there. Or we can create a new cats-extra
module for this type of less commonly used type class.
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.
With the example of FunctionK
, my first thought is that that’s what’s defined incorrectly. We really want FunctionK[F[_], G[_], A]
and Forall[FunctionK[F, G, ?]]
for a natural transformation. Then you do have a FunctorK[FunctionK[F, ?[_], ?]]
. But that might just be crazy talk.
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'm not sure if "higher-kinded X" is a really rigid term. In my mind, higher-kinded functor could equally well mean a functor from (endofunctors in Scal) to Scal, or a functor from endofunctors in Scal to other endofunctors in Scal. This is my main problem with the -K
naming scheme; it gets even worse when type classes have multiple type parameters.
As well, the encoding proposed above does not reify that the A
can vary with the instance remaining valid. The consumer of the instance needs a Forall[Lambda[A =>FunctorK[OptionT[?[_], A]]]]
to have that fact in hand, and that makes it much more unwieldy, though a type alias could help.
To my mind if we're going to do a cats-extras project we'll want this there.
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 agree with @kailuowang and @edmundnoble, I don't think it's all that useful in cats-core. I'd like to see conformity in Monad Transformers for defining a mapK
method, but it doesn't need a type class in core IMO.
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'm not sure if "higher-kinded X" is a really rigid term.
Absolutely. I don’t think it helps at all. But if Functor
is really “endofunctor in the category of Scal”, I’d expect FunctorK
to mean the same thing as K
in other instances, which basically adds “endofunctors” to the description – i.e., “endofunctor in the category of endofunctors in Scal”.
I’d also be happy with Functor
being renamed to Endofunctor
and having EndofunctorK
and something like LowerFunctor
(for a functor from the category of endofunctors to the category of Skal).
Also agreed that none of this should be in cats-core. I would like to find some time to work with -Ykind-polymorphism
and to define these things as specializations of kind-polymorphic endofunctors, etc.
This sort of thing has been started in a few different libraries already – khats (which is too homophonically-named to be useful), Griffins (which never really got off the ground and mostly lives inside a Matryoshka branch), and I think one other that I have forgotten 😕
+1 for FunctorK. Seems more consistent with cats naming styles. |
Closing this for now since 1.0 already released |
added instances for
WriterT
Kleisli
OptionT
EitherT
IdT
EitherK
Tuple2K
Free
FreeT
Func
Nested
OneAnd
Fixes #1812, #1713 and #1492. also sort of a continuation of #1620
Any instances I miss?
Question, do we need an instance forFree
, not sure if there is a use case in the wild.Update: Added instance for
Free
,EitherK
,TupleK
,Func
,Nested
andOneAnd