Skip to content
New issue

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

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

Already on GitHub? # to your account

Support lifting polymorphic functions to FunctionK in Scala 3 #3895

Merged
merged 6 commits into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,18 @@
package cats
package arrow

private[arrow] class FunctionKMacroMethods
private[arrow] class FunctionKMacroMethods {

/**
* Lifts function `f` of `[X] => F[X] => G[X]` into a `FunctionK[F, G]`.
*
* {{{
* val headOptionK = FunctionK.lift[List, Option]([X] => (_: List[X]).headOption)
* }}}
*/
def lift[F[_], G[_]](f: [X] => F[X] => G[X]): FunctionK[F, G] =
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't a macro is it? Why the name?

I wonder if there could be a Conversion here so you could use a literal in scala 3 without having to call lift.

Copy link
Member

@armanbilge armanbilge May 17, 2022

Choose a reason for hiding this comment

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

I think it's convenient to put it in this file/class, because it was already present (the Scala 2 side uses it for macros). Ideally it could be renamed something more appropriate.

FTR I personally haven't had a good experiencing using Conversion on Scala 3, as it needs a feature import to enable implicit conversions. That makes me tend to stay away.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe I'm wrong but it shouldn't need a feature import if the conversion is on the companion object.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, I tried slapping an implicit in front of this but actually it doesn't seem to work 🤔

Suggested change
def lift[F[_], G[_]](f: [X] => F[X] => G[X]): FunctionK[F, G] =
implicit def lift[F[_], G[_]](f: [X] => F[X] => G[X]): FunctionK[F, G] =
[error] -- [E007] Type Mismatch Error: /workspace/cats/tests/shared/src/test/scala-3/cats/tests/FunctionKLiftSuite.scala:40:77 
[error] 40 |    val fHeadOption: FunctionK[List, Option] = [X] => (_: List[X]).headOption
[error]    |                                                                             ^
[error]    |    Found:    [X] => (List[X]) => Option[X]
[error]    |    Required: cats.arrow.FunctionK[List, Option]

I also tried using given Conversion[...] but the compiler didn't like that with our current scalacOptions.

Copy link
Member

@armanbilge armanbilge May 19, 2022

Choose a reason for hiding this comment

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

Yeah, it seems this just doesn't work. Maybe worth opening a dotty issue?

//> using scala "3.1.2"
//> using lib "org.typelevel::cats-core::2.7.0"

import cats.arrow.FunctionK

def lift[F[_], G[_]](f: [X] => F[X] => G[X]): FunctionK[F, G] =
  new FunctionK[F, G] {
    def apply[A](fa: F[A]): G[A] = f(fa)
  }

given [F[_], G[_]]: Conversion[[X] => F[X] => G[X], FunctionK[F, G]] = lift(_)

@main def main =
  val fHeadOption: FunctionK[List, Option] = [X] => (_: List[X]).headOption
  val a = List(1, 2, 3)
  assert(fHeadOption(a) == a.headOption)
[error] ./functionk.scala:14:76: Found:    [X] => (List[X]) => Option[X]
[error] Required: cats.arrow.FunctionK[List, Option]
[error]   val fHeadOption: FunctionK[List, Option] = [X] => (_: List[X]).headOption
[error]

Copy link
Member

Choose a reason for hiding this comment

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

I think just type inference fails here:

scala> summon[Conversion[[X] => List[X] => Option[X], FunctionK[List, Option]]](using given_Conversion_X_FunctionK)
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |summon[Conversion[[X] => List[X] => Option[X], FunctionK[List, Option]]](using given_Conversion_X_FunctionK)
  |                                                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |Found:    Conversion[[X] => (Nothing) => Any, FunctionK[F, G]]
  |Required: Conversion[[X] => (List[X]) => Option[X], FunctionK[List, Option]]
  |
  |where:    F is a type variable with constraint <: [_] =>> Any
  |          G is a type variable with constraint <: [_] =>> Any

longer explanation available when compiling with `-explain`
1 error found

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, this seems like maybe it is a bug that could be filed with scala.

Copy link
Member

Choose a reason for hiding this comment

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

I opened scala/scala3#15276.

new FunctionK[F, G] {
def apply[A](fa: F[A]): G[A] = f(fa)
}

}
37 changes: 37 additions & 0 deletions tests/shared/src/test/scala-3/cats/tests/FunctionKLiftSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2015 Typelevel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package cats.tests

import cats.arrow.FunctionK
import cats.implicits._
import org.scalacheck.Prop._
import cats.laws.discipline.arbitrary._

class FunctionKLiftSuite extends CatsSuite {

test("lift a polymorphic function directly") {
val fHeadOption = FunctionK.lift[List, Option]([X] => (_: List[X]).headOption)
forAll { (a: List[Int]) =>
assert(fHeadOption(a) === a.headOption)
}
}
}