Generic `implicit` operators for monads? - scala

This is part programming exercise and part practical.
I'd like to build an operator | in this example such that:
val x: Option[Int] = _
def fail: Nothing = _
val result = x | fail
I have this so far and it doesn't compile:
import language.implicitConversions
import scala.util.{Try, Success, Failure}
trait OrAbleFacilitator[T] {
def apply[U](t: T, u: => U): U
}
implicit val OptionOrAbleFacilitator = new OrAbleFacilitator[Option[T]] {
def apply[U, U >: T](t: Option[T], u: => U): U = {
t match {
case Some(v) => v
case _ => u
}
}
}
implicit val TryOrAbleFacilitator = new OrAbleFacilitator[Try[T]] {
def apply[U, U >: T](t: Try[T], u: => U): U = {
t match {
case Success(v) => v
case _ => u
}
}
}
implicit class OrAble[T](t: T) {
def |[U](u: => U)(implicit orFacilitator: OrAbleFacilitator[T, U]): U = {
orFacilitator(t, u)
}
}
What am I doing wrong?

I moved a bunch of little things to get things to work. I tried to comment most places I made changes to get it running.
import language.implicitConversions
import scala.util.{Try, Success, Failure}
// you need to wrap this all in an object, since you can't have vals at the top level
object Main extends App {
// you need to capture in the trait that you are abstracting of a
// * → * kind, that is to say it needs to be a type which takes a type
// as input and returns you a type, such as Option or Try or List.. The thing you are creating an
// orable for is going to have to be in the shape F[A], not just F,
// and there is no reason to fix the inner type
trait OrAbleFacilitator[F[_]] {
// the first parameter to apply needs to be F[T], and the return type must be a supertype of T
def apply[T, U >: T](t: F[T], u: => U): U
}
implicit val OptionOrAbleFacilitator = new OrAbleFacilitator[Option] {
// here we need to accept the types for the input and output, adn they must be realted
def apply[T, U >: T](t: Option[T], u: => U): U = {
t match {
case Some(v) => v
case _ => u
}
}
}
implicit val TryOrAbleFacilitator = new OrAbleFacilitator[Try] {
def apply[T, U >: T](t: Try[T], u: => U): U = {
t match {
case Success(v) => v
case _ => u
}
}
}
// we can't just wrap any old T, it has to be something in the shape
// F[T] moved the implcit up to where we create the class so that it
// doesn't cuase us to have an additional parameter list on our |
// function ( which would prevent us from using it inline )
implicit class OrAble[F[_], T](t: F[T])(implicit or: OrAbleFacilitator[F]) {
// the vertical bar | is already a keyword in scala, I used the formal "disjunction" operatior (|) instead.
def |[U >: T](u: => U): U = {
or(t, u)
}
}
// and it works:
val noneint: Option[Int] = None
val failint: Try[Int] = Failure(new Exception())
assert((Some(0) : Option[Int]) .|(Some(1)) == 0)
assert((noneint | 2) == 2)
assert(((Success(0) : Try[Int]) | 3) == 0)
assert((failint | 4) == 4)
}

Related

Phantom existential types in Scala

I'm trying to write a value class that wraps Scala collection's Map and provides an alternative get. I'm trying to use a phantom type in the value class and tag the Key with the same type using the method member. If the result of member is Some(k) then the user should be able to call get(k) and get a V instead of an Option[V].
import scala.collection.{Map => M}
class Key[PH, K] private (val k: K) extends AnyVal
object Key {
def apply[PH, K](k: K): Key[PH, K] = new Key(k)
}
class Map[PH, K, V] private (val m: M[K, V]) extends AnyVal {
def member(k: K): Option[Key[PH, K]] = m.get(k).map(_ => Key(k))
def get(key: Key[PH, K]): V = m.get(key.k).get
}
object Map {
def apply[PH, K, V](m: M[K, V]): Map[PH, K, V] = new Map(m)
}
def withMap[K, V, T](m: M[K, V])(cont: (Map[PH, K, V] forSome { type PH }) => T): T = cont(Map(m))
withMap(M("a" -> "a")){ m =>
m.member("a") match {
case Some(v) => println(m.get(v))
case None => println(":(")
}
}
But currently it fails compiling with the following error:
found : Key[PH(in value $anonfun),String] where type +PH(in value $anonfun)
required: Key[PH(in value cont),String]
case Some(v) => println(m.get(v))
How can I convince scalac that the PHs are the same?
Destructure the existential:
withMap(M("a" -> "a")) { case m =>
m.member("a") match {
case Some(v) => println(m.get(v))
case None => println(":(")
}
}
This abbreviates
withMap(M("a" -> "a")) { case m: Map[ph, String, String] => // name the phantom type ph, name the map m =>
m.member("a") match {
case Some(v) => println(m.get(v))
case None => println(":(")
}
}
The destructuring allows m to be given a non-existential type in terms of a newly introduced type variable. This means that every occurrence of m can now have the same type.
Phantom types are meaningless. You should say what you mean: every Key belongs to a certain Map:
import scala.collection.immutable.Map // it is not safe to use Maps in general!
class KeyedMap[K, V](val m: Map[K, V]) extends AnyVal {
import KeyedMap._
def member(k: K): Option[Key[K, V, m.type]] = m.get(k).map { _ => new Key[K, V, m.type](k) }
def fromKey(k: Key[K, V, m.type]): V = m(k.k)
}
object KeyedMap {
// vvvvvvvvvvvvvv requires this parameter to be <something>.type
class Key[K, +V, M <: Map[K, V] with Singleton] private[KeyedMap](val k: K) extends AnyVal
}
object Test {
def main(args: String*): Unit = {
val m = new KeyedMap(Map("a" -> "b"))
m.member("a") match {
case Some(v) => println(m.fromKey(v))
case None => println(":(")
}
}
}

When using HList with GADTs I am having to cast using asInstanceOf[H]. Is there a way to avoid the cast?

Given 2 GADT Algebras which know about each other and 2 interpreters that are mutually recursive, I am having issues having to cast from type A to type h <: HList even though in the context of the pattern match, it should be implied that type A is type h.
Is there a way to avoid the asInstanceOf[h] call in the interpreter?
abstract class KvpHList[H<:HList]
object KvpNil extends KvpHList[HNil]
case class KvpCons[H <: A :: T,A, T<:HList](head: KvpValue[A], tail: KvpHList[T])(implicit isHCons: IsHCons.Aux[H,A,T]) extends KvpHList[H] {
val hCons: IsHCons.Aux[H,A,T] = isHCons
}
abstract class KvpValue[A]
case object StringData extends KvpValue[String]
case class HListData[H <:HList](member: KvpHList[H]) extends KvpValue[H]
def hListInterpreter[H<:HList](hList: KvpHList[H]): H => String = {
hList match {
case KvpNil => (hNil: H) => "Nil"
case cons: KvpCons[H,a,t]=> {
implicit val hCons = cons.hCons
(input: H) => {
s"${kvpInterpreter(cons.head)(input.head)} :: ${hListInterpreter(cons.tail)(input.tail)}"
}
}
}
}
def kvpInterpreter[A](kvpValue: KvpValue[A]): A => String = {
kvpValue match {
case StringData => (str: String) => str
case h: HListData[h] => {
(input: A) => { // tried (input: h) as well
val toString: h => String = hListInterpreter(h.member)
toString(input.asInstanceOf[h]) // <--- CASTING :(
}
}
}
}
kvpInterpreter(HListData(KvpCons(StringData, KvpNil))).apply("Hello" :: HNil)
Since H in KvpCons is uniquely determined by A and T, KvpCons can be parametrized with two type parameters rather than three.
Type-level pattern matching is type classes
abstract class KvpHList[H <: HList]
object KvpNil extends KvpHList[HNil]
case class KvpCons[A, T <: HList](head: KvpValue[A], tail: KvpHList[T]) extends KvpHList[A :: T]
abstract class KvpValue[A]
case object StringData extends KvpValue[String]
case class HListData[H <: HList](member: KvpHList[H]) extends KvpValue[H]
trait HListInterpreter[H <: HList] {
def apply(hList: KvpHList[H]): H => String
}
object HListInterpreter {
implicit val nil: HListInterpreter[HNil] = new HListInterpreter[HNil] {
override def apply(hList: KvpHList[HNil]): HNil => String = _ => "Nil"
}
implicit def cons[A, T <: HList](implicit
headKvpInterpreter: KvpInterpreter[A],
tailHListInterpreter: HListInterpreter[T]
): HListInterpreter[A :: T] = new HListInterpreter[A :: T] {
override def apply(hList: KvpHList[A :: T]): A :: T => String = hList match {
case cons: KvpCons[_, _] => input => s"${headKvpInterpreter(cons.head)(input.head)} :: ${tailHListInterpreter(cons.tail)(input.tail)}"
}
}
}
def hListInterpreter[H <: HList](hList: KvpHList[H])(implicit hListInterp: HListInterpreter[H]): H => String = hListInterp(hList)
trait KvpInterpreter[A] {
def apply(kvpValue: KvpValue[A]): A => String
}
object KvpInterpreter {
implicit val string: KvpInterpreter[String] = new KvpInterpreter[String] {
override def apply(kvpValue: KvpValue[String]): String => String = str => str
}
implicit def hList[H <: HList : HListInterpreter]: KvpInterpreter[H] = new KvpInterpreter[H] {
override def apply(kvpValue: KvpValue[H]): H => String = kvpValue match {
case h: HListData[H] => input => {
val toString: H => String = hListInterpreter(h.member)
toString(input)
}
}
}
}
def kvpInterpreter[A](kvpValue: KvpValue[A])(a: A)(implicit kvpInterp: KvpInterpreter[A]): String = kvpInterp(kvpValue)(a)

scala variant type causes error type mismatch error

B is a super class of A , then as per scala variants and covariants . variant type can occur at parameter and covariant type can occur at function return type
My scala class make method is taking B type in paramters and returning subtype A as function type but as per function "make" it is correct but if i have companion class like case class for same class which is generic in A is giving error. I spent enough time to correct this error but not able to do so.
sealed class myList[+A] {
def apply[A](as: A*): myList[A] ={
if (as.isEmpty) Nil
else Cons(as.head, apply(as.tail: _*))
}
def head():A = this.head
def tail():myList[A] = this.tail
def isEmpty():Boolean ={
this match {
case Nil => true
case _: myList[A] => false
}
}
def preappend[B>:A](x: B): myList[A] ={
if (isEmpty) make(x)
else make(x)
}
def append[B>:A](x: B): myList[A] ={
if (this.isEmpty) make(x)
else Cons(this.head,this.tail.append(x))
}
def print()={
this.map(println)
}
def make[B>:A](x:B): myList[A] ={
this match {
case Nil => Cons(x,Nil)
case Cons(xh, xs) => Cons(xh, xs.make(x))
}
}
def map[A,B](f: (A) => B): myList[B] = {
this match {
case Nil => Nil
case Cons(xh:A, xs:myList[A]) => Cons(f(xh),xs.map(f ))
}
}
/**
* Combines all elements of this list into value.
*
* Time - O(n)
* Space - O(n)
*/
def fold[B](n: B)(op: (B, A) => B): B = {
def loop(l: myList[A], a: B): B =
if (l.isEmpty) a
else loop(l.tail, op(a, l.head))
loop(this, n)
}
def foldLeft[B](z: B)(f: (B, A) => B): B = {
var acc = z
var these = this
while (!these.isEmpty) {
acc = f(acc, these.head)
these = these.tail
}
acc
}
def foldRight[B,A](z: B)(f: (A, B) => B): B = this match {
case nil=> z
case Cons(x:A,xs:myList[A])=>f(x, foldRight(z)(f))
}
def length[B>:A](lst:myList[B]):Int={
this.foldRight(0) {( lst:myList[A],x:Int) => lst match{
case nil=>x
case _: myList[B] => x+1
}
}
}
def fail(m: String) = throw new NoSuchElementException(m)
}
case object Nil extends myList[Nothing] {
override def head: Nothing = fail("An empty list.")
override def tail: myList[Nothing] = fail("An empty list.")
override def isEmpty: Boolean = true
}
case class Cons[-A](head: A, tail: myList[A]) extends myList[A] {
override def isEmpty: Boolean = false
}
case class truck(
numberPlate:String
)
object Main {
def main(args: Array[String]) {
val a= new truck("1233bsd")
val b = new truck("dsads334")
val c = new myList[truck]
c.append(a)
c.print()
c.append(b)
c.print()
}
}
error i am getting:-
mylist-v2.scala:40: error: type mismatch;
found : x.type (with underlying type B)
required: A
case Nil => Cons(x,Nil)
^
mylist-v2.scala:50: warning: abstract type pattern A is unchecked since it is eliminated by erasure
case Cons(xh:A, xs:myList[A]) => Cons(f(xh),xs.map(f ))
^
mylist-v2.scala:50: warning: abstract type A in type pattern myList[A] is unchecked since it is eliminated by erasure
case Cons(xh:A, xs:myList[A]) => Cons(f(xh),xs.map(f ))
^
mylist-v2.scala:79: warning: abstract type pattern A is unchecked since it is eliminated by erasure
case Cons(x:A,xs:myList[A])=>f(x, foldRight(z)(f))
^
mylist-v2.scala:79: warning: abstract type A in type pattern myList[A] is unchecked since it is eliminated by erasure
case Cons(x:A,xs:myList[A])=>f(x, foldRight(z)(f))
^
four warnings found
one error found
I think the minimal changes to make this code compile is to change Cons to
case class Cons[+A](override val head: A, override val tail: myList[A]) extends myList[A] {
and signatures of prepend, append and make to
def preappend[B>:A](x: B): myList[B] ={
def append[B>:A](x: B): myList[B] ={
def make[B>:A](x:B): myList[B] ={
As you didn't describe your actual goal, it is hard to say whether this is what you really want or not.

Scalacheck issue with higher kinds : diverging implicit expansion for type Arbitrary

I've defined a monad type class and I'm trying to verify its law with scalacheck.
I have the following error :
diverging implicit expansion for type org.scalacheck.Arbitrary[(A, Box[B])]
My scalacheck code is the following :
class OptionMonadSpec extends MonadSpec[String, String, String, Option](Monad.optionMonad)
abstract class MonadSpec[A, B, C, Box[_] : ClassTag](monad: Monad[Box])
(implicit boxArb: Arbitrary[Box[A]], aArb: Arbitrary[A], bArb: Arbitrary[B], cArb: Arbitrary[C])
extends Properties(s"Monad for ${classTag[Box[_]]}") {
property("left identity") = forAll { (f: (A => Box[B]), a: A) =>
val boxA: Box[A] = monad.pure(a)
monad.flatMap(boxA)(f) == f(a)
}
property("right identity") = forAll { box: Box[A] =>
monad.flatMap(box)(monad.pure) == monad
}
property("associativity") = forAll { (f: (A => Box[B]), g: (B => Box[C]), box: Box[A]) =>
val boxB: Box[B] = monad.flatMap(box)(f)
monad.flatMap(boxB)(g) == monad.flatMap(box) { a =>
val boxB: Box[B] = f(a)
monad.flatMap(boxB)(g)
}
}
}
Did I miss soemthing in the implicit Arbitrary types ?
Here is my monad :
trait Monad[Box[_]] extends Functor[Box] {
def pure[A](a: A): Box[A]
def flatMap[A, B](boxA: Box[A])(f: A => Box[B]): Box[B]
}
object Monad {
implicit val optionMonad = new Monad[Option] {
override def pure[A](x: A): Option[A] = Some(x)
override def flatMap[A, B](boxA: Option[A])(f: A => Option[B]) = boxA.flatMap(f)
override def map[A, B](boxA: Option[A])(f: A => B) = boxA.map(f)
}
}
Thanks
You have an implicit Arbitrary[Box[A]] in scope, but you don't have one for Arbitrary[Box[B]] (which Scalacheck needs to create one for A => Box[B]) or for Arbitrary[Box[C]] (which it'll ask for afterwards).
A more principled approach would be to create something like
trait Arbitrary1[F[_]] {
def liftArb[A](arb: Arbitrary[A]): Arbitrary[F[A]]
}
and provide Arbitrary1[Box] but it would require being more explicit when calling forAll.

Scala: How to use Functors for multi type parameter trait

I have and ADT which is basically a wrapper over Function1:
case class Abstract[M[_], A, B](f:M[A] => M[B]) {
def fn: M[A] => M[B] = { case x: M[A] => f(x) }
}
I want to map over these, so I defined a Functor like so:
trait AbstractAPI[E] {
type AbsO[T] = Abstract[List, E, T]
// type AbsO[T] = Abstract[List, _, T] => does not work (?)
implicit val abstractO: Functor[AbsO] = new Functor[AbsO] {
def map[A, B](fa: AbsO[A])(f: A => B): AbsO[B] = {
new Abstract(fa.fn andThen { x: List[A] => x.map{ y => f(y) } })
}
}
}
Now, to actually map over an Abstract, I'd need AbstractAPI[Int], like
case object IntAbstractAPI extends AbstractAPI[Int]
object A {
import IntAbstractAPI._
val f:List[Int] => List[String] = { case x: List[Int] => x.map{ _.toString.toLowerCase } }
val hey = (new Abstract(f)).map{ x => x.toInt }
}
or
object A extends AbstractAPI[Int] {
val f:List[Int] => List[String] = { case x: List[Int] => x.map{ _.toString.toLowerCase } }
// FINALLY!
val res = (new Abstract(f)).map{ x => x.toInt }.map{ _.toFloat + 10f }
// Abstract[List, Int, Float] = Abstract(<function1>)
}
However, in this pattern, I'd have to define case objects for every possible E. Here are my questions:
Is this the correct way to use Functors?
How can I automate the creation of the case objects for every possible E (or make the compiler infer it?)
Edit 1:
Further clarification: The above implementation works, but this one does not:
object A extends AbstractAPI {
val f:List[Int] => List[String] = { case x: List[Int] => x.map{ _.toString.toLowerCase } }
val res = (new Abstract(f)).map{ x => x.toInt }.map{ _.toFloat + 10f }
// Abstract[List, Int, Float] = Abstract(<function1>)
}
gives compilation error:
value map is not a member of Abstract[List,Int,String]
I assume this is because the compiler is not able to derive a functor for Abstract[List,Int,String]?
You can derive a functor for type parameters that you don't care about.
import cats.Functor
import cats.syntax.functor._
And I'll rename second type parameter on Abstract to X, it'll help
case class Abstract[M[_], X, A](f: M[X] => M[A]) // forget the fn bit for now
You can create typeclass instances not only with a val, but also with a def. It is allowed to have type parameters and also take other implicit (but only implicit) parameters.
type Abs1[X] = ({ type L[A] = Abstract[List, X, A] })
/*implicit*/ def abstract1[X]: Functor[Abs1[X]#L] = new Functor[Abs1[X]#L] {
override def map[A, B](fa: Abstract[List, X, A])(f: A => B): Abstract[List, X, B] =
Abstract(mx => fa.f(mx).map(f))
}
If map is all you need from a List, you can generalize further for any M[_] that has a Functor instance. Also placing it into a companion object of Abstract enables it to be found without additional imports / inheritance / etc.
object Abstract {
// Abstract.MX[M, X]#L can be replaced with Abstract[M, X, ?] if you use kind-projector
type MX[M[_], X] = ({ type L[A] = Abstract[M, X, A] })
implicit def genericFunctor[M[_]: Functor, X] = new Functor[MX[M, X]#L] {
override def map[A, B](fa: Abstract[M, X, A])(f: A => B): Abstract[M, X, B] =
Abstract(mx => fa.f(mx).map(f)) // the implementation is the same
}
}
And it works, if you import instances for whatever your M[_] is
assert {
import cats.instances.list._ // get Functor[List]
// map is automatically picked up from Functor[Abstract[List, Int, ?]]
Abstract(identity[List[Int]])
.map(Vector.range(0, _))
.map(_.mkString(""))
.f(List(1, 2, 3)) == List("0", "01", "012")
}
assert {
import cats.instances.option._
Abstract(identity[Option[Int]])
.map(_ min 42)
.map(i => Range(i, i + 3))
.f(Some(11)) == Some(Range(11, 14))
}
You can try the code there
Answering your second question, you could try this implicit AbstractAPI[T] factory:
implicit def abstractAPI[T]: AbstractAPI[T] = new AbstractAPI[T] {}
Any required implicit evidence for AbstractAPI[T] should work, e.g:
def f[T : AbstractAPI]: Unit = ()
f