Skip to content

Instantly share code, notes, and snippets.

View DmytroMitin's full-sized avatar
😎
everything is ok

Dmytro Mitin DmytroMitin

😎
everything is ok
View GitHub Profile
@DmytroMitin
DmytroMitin / opaque.hs
Created May 23, 2020 08:09 — forked from luqui/opaque.hs
Natural transformations never need to type switch
{-# LANGUAGE RankNTypes, TypeApplications #-}
-- We give a constructive proof that any natural transformation that uses
-- a type switch in its implementation can also be implemented without the type switch,
-- as long as the domain functor is finitely Traversable (and the argument can easily
-- be modified to drop the finiteness property).
-- We do this by providing a function `opaquify` which takes an n.t. which may do
-- type switches, and returns an equivalent n.t. which does not.
import scala.reflect.ClassTag
object DefaultType extends App {
case class AttributeMeasure[T](name: String)(implicit ev: reflect.ClassTag[T]) {
def ct: ClassTag[T] = ev
}
/** This almost works, except for the case:
* {{{
import scala.reflect.ClassTag
object Bottom extends App {
class C[T](implicit ev: reflect.ClassTag[T]) {
def ct: reflect.ClassTag[T] = ev
}
class X[U, -T]
object X {
@DmytroMitin
DmytroMitin / TypeLevelBacktrack.scala
Created June 6, 2020 23:52 — forked from OlivierBlanvillain/TypeLevelBacktrack.scala
Example of a workaround for the absence of backtracking in implicit resolution. Original problem statement: https://gist.github.com/atamborrino/daa451aea542e912c2d6
import shapeless.{HList, HNil, ::}
import shapeless.ops.hlist.{Selector, Prepend}
import shapeless.test.illTyped
object TypeLevelBacktrack extends App {
/** [[Parent]] / [[Child]] relationship, father side. */
trait FatherOf[Parent, Child]
/** [[Parent]] / [[Child]] relationship, mother side */
trait MotherOf[Parent, Child]
@DmytroMitin
DmytroMitin / gadt.md
Created October 12, 2020 18:19 — forked from smarter/gadt.md
GADTs in Scala

Generalized Algebraic Data Types in Scala

Basic GADTs

Here's an ADT which is not a GADT, in Haskell:

data Expr = IntExpr Int | BoolExpr Bool
@DmytroMitin
DmytroMitin / Polymorphism.md
Created October 27, 2020 14:14 — forked from BalmungSan/Polymorphism.md
Polymorphism in Scala.

Polymorphism in Scala.

This document aims to show and compare three alternatives for achieving polymorphism in Scala.

  • Subtyping, common in object-oriented languages like Java.
  • Duck typing, common in dynamically typed languages like Python.
  • Typeclasses, common in functional languages like Haskell.

Additionally, when implementing the typeclass pattern in Scala,