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

Typeclasses implicit objects #264

Merged
merged 42 commits into from
Sep 13, 2017
Merged

Conversation

raulraja
Copy link
Member

@raulraja raulraja commented Sep 10, 2017

This allows Typeclasses instances of arbitrary arity to be discovered implicitly by providing a default factory based on naming conventions that can be used to lookup the instance.
It also introduces granularity in the typeclass definitions for better composition.

Fixes #260

  • Function0
  • Eval
  • ListKW
  • Option
  • Try
  • Const
  • Coproduct
  • Either
  • EitherT
  • FreeApplicative
  • Free
  • Function1
  • Ior
  • Iterable
  • Kleisli
  • NonEmptyList
  • Number
  • SequenceKW
  • StateT
  • String
  • Validated
  • WriterT
  • Yoneda

Example on EitherTFunctorInstance

interface EitherTFunctorInstance<F, L> : Functor<EitherTKindPartial<F, L>> {
    fun FF(): Functor<F>
    override fun <A, B> map(fa: EitherTKind<F, L, A>, f: (A) -> B): EitherT<F, L, B> = fa.ev().map({ f(it) }, FF())
}

object EitherTFunctorInstanceImplicits {
    @JvmStatic
    fun <F, L> instance(FF: Functor<F>): EitherTFunctorInstance<F, L> = object : EitherTFunctorInstance<F, L> {
        override fun FF(): Functor<F> = FF
    }
}

You can now safely invoke without prior registration:

functor<EitherTKindPartial<OptionHK, Throwable>>()

Note the EitherT Functor instance depends on the Functor<F>. Kategory can now automatically resolve constrains recursively as declared in the EitherTFunctorInstanceImplicits#instance arguments.

Once all instances are migrated I will introduce a new processor @typeclass which generates the implicit object automatically and also the companion helpers for the datatype that the instance is targeting in the same way now @deriving does.

As a final pass we will remove all calls that manually provide instances that can be resolved implicitly in reified contexts.

@raulraja raulraja requested a review from a team September 10, 2017 23:54
@codecov-io
Copy link

codecov-io commented Sep 11, 2017

Codecov Report

❗ No coverage uploaded for pull request base (master@9b47f64). Click here to learn what that means.
The diff coverage is 61.15%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #264   +/-   ##
=========================================
  Coverage          ?   45.57%           
  Complexity        ?      315           
=========================================
  Files             ?      143           
  Lines             ?     3721           
  Branches          ?      423           
=========================================
  Hits              ?     1696           
  Misses            ?     1891           
  Partials          ?      134
Impacted Files Coverage Δ Complexity Δ
...-test/src/main/kotlin/kategory/laws/FunctorLaws.kt 5.26% <ø> (ø) 1 <0> (?)
...e/src/main/kotlin/kategory/typeclasses/Composed.kt 67.64% <ø> (ø) 0 <0> (?)
...src/main/kotlin/kategory/laws/FunctorFilterLaws.kt 5.55% <ø> (ø) 1 <0> (?)
...y-core/src/main/kotlin/kategory/arrow/FunctionK.kt 60% <ø> (ø) 0 <0> (?)
...core/src/main/kotlin/kategory/typeclasses/Monad.kt 65.67% <0%> (ø) 0 <0> (?)
...n/kotlin/kategory/instances/SequenceKWInstances.kt 0% <0%> (ø) 0 <0> (?)
...ry-test/src/main/kotlin/kategory/laws/MonadLaws.kt 1.4% <0%> (ø) 2 <0> (?)
...ore/src/main/kotlin/kategory/typeclasses/Monoid.kt 0% <0%> (ø) 0 <0> (?)
...y-core/src/main/kotlin/kategory/data/SequenceKW.kt 76.31% <0%> (ø) 8 <0> (?)
...ore/src/main/kotlin/kategory/typeclasses/Inject.kt 0% <0%> (ø) 0 <0> (?)
... and 71 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b47f64...d66a279. Read the comment docs.

@@ -1,6 +1,6 @@
package kategory

interface FunctionK<in F, out G> {
interface FunctionK<F, G> {
Copy link
Member

Choose a reason for hiding this comment

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

Why have they lost their variance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Inferred types with the type-literal in java reflection not liking contravariant positions. Also these are usually mapped to root ADTs and typeclasses which are just invariant positions AFAIK.

@raulraja raulraja changed the title WIP. Typeclasses implicit objects Typeclasses implicit objects Sep 13, 2017
@raulraja raulraja requested a review from a team September 13, 2017 11:26
Copy link
Member

@pakoito pakoito left a comment

Choose a reason for hiding this comment

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

Magic!

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.

Add all possible implicit objects to manual instances
3 participants