Skip to content

Instantly share code, notes, and snippets.

View gvolpe's full-sized avatar

Gabriel Volpe gvolpe

View GitHub Profile
@gvolpe
gvolpe / Ex.scala
Created November 3, 2018 04:43 — forked from SystemFw/Ex.scala
Encoding existentials via abstract types (1) and higher-ranks (2)
// This is the traditional encoding, using abstract type members
// to encode existentials
object AbstractExistentials {
trait E {
type X
val x: X
def f(p: X): String
}
collectSuccessful :: [IO a] -> IO [a]
collectSuccessful xs = do
ref <- newIORef []
parTraverse (f ref) xs `onError` readIORef ref
where
f ref fa = fromEitherIO $ try fa >>= \a -> action ref a $> a
action ref a = case a of
(Right x) -> void $ atomicModifyIORef ref (\y -> (x : y, x))
(Left _) -> pure ()
def genericgeneric[[FF[_]: [_]: MonadErrorMonadError[?[_], [?[_], ThrowableThrowable], ], GG[_]: [_]: TraverseTraverse, , AA](
gfa: ]( gfa: GG[[FF[[AA]],
append: ]], append: GG[[AA] => ] => AA => => GG[[AA],
ref: ], ref: RefRef[[FF, , GG[[AA]]
): ]] ): FF[[GG[[AA]] =
gfa
.traverse(_.attempt.flatTap {
]] = gfa .traverse(_.attempt.flatTap { casecase RightRight(x) => ref.update(append(_)(x))
(x) => ref.update(append(_)(x)) casecase LeftLeft(_) => (_) => ApplicativeApplicative[[FF].unit

Explaining Miles's Magic

Miles Sabin recently opened a pull request fixing the infamous SI-2712. First off, this is remarkable and, if merged, will make everyone's life enormously easier. This is a bug that a lot of people hit often without even realizing it, and they just assume that either they did something wrong or the compiler is broken in some weird way. It is especially common for users of scalaz or cats.

But that's not what I wanted to write about. What I want to write about is the exact semantics of Miles's fix, because it does impose some very specific assumptions about the way that type constructors work, and understanding those assumptions is the key to getting the most of it his fix.

For starters, here is the sort of thing that SI-2712 affects:

def foo[F[_], A](fa: F[A]): String = fa.toString
import cats.effect.{ExitCode, IO, IOApp, Timer}
import cats.syntax.apply._
import cats.syntax.functor._
import scala.concurrent.duration._
object scheduler extends IOApp {
def repeatAtFixedRate(period: FiniteDuration, task: IO[Unit])
import cats.{ ApplicativeError, MonadError }
import cats.data.{ Kleisli, OptionT }
import cats.effect.Sync
import cats.effect.concurrent.Ref
import cats.syntax.all._
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s._
import org.http4s.circe.CirceEntityDecoder._
import org.http4s.circe._
import cats.MonadError
import cats.effect.{IO, LiftIO}
import cats.syntax.all._
import com.olegpy.meow.hierarchy._
sealed trait CustomError extends Throwable
case class ErrorOne(msg: String) extends CustomError
case class ErrorTwo(id: Int) extends CustomError
trait UsersAlgebra[F[_]] {
import cats.effect._
import cats.syntax.apply._
import fs2._
import scala.util.Random
val ioa: IO[Option[Int]] = IO(Random.nextInt(100)).flatMap { n =>
if (n % 2 == 0) IO(println(s"$n")) *> IO.pure(Some(n))
else IO(println(s"ERROR >> $n")) *> IO.raiseError(new Exception(s"Error $n"))
}
import cats.Monad
import cats.effect.{IO, Sync}
import cats.syntax.flatMap._
import cats.syntax.functor._
trait Command[F[_]] {
def getStrLn: F[String]
def putStrLn(string: String): F[Unit]
}

Revisiting Tagless Final Interpreters

Tageless Final interpreters are an alternative to the traditional Algebraic Data Type (and generalized ADT) based implementation of the interpreter pattern. This document presents the Tageless Final approach with Scala, and shows how Dotty with it's recently added implicits functions makes the approach even more appealing. All examples are direct translations of their Haskell version presented in the Typed Tagless Final Interpreters: Lecture Notes (section 2).

The interpreter pattern has recently received a lot of attention in the Scala community. A lot of efforts have been invested in trying to address the biggest shortcomings of ADT/GADT based solutions: extensibility. One can first look at cats' Inject typeclass for an implementation of [Data Type à la Carte](http://www.cs.ru.nl/~W.Swierstra/Publications/DataTypesA