Skip to content

Instantly share code, notes, and snippets.

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

@dpiponi
dpiponi / Main.hs
Created February 15, 2016 23:38
Haskell heat engine simulation. (Not quite complete.)
{-# LANGUAGE FlexibleContexts #-}
import Data.Array
import Data.Array.IO
import Data.Foldable
import Data.List hiding (sum)
import System.Random
import Control.Monad.State
import Prelude hiding (sum)
--import Debug.Trace
@suhailshergill
suhailshergill / RDDasMonadPlus.scala
Last active February 24, 2019 16:09
having monad instances for RDD like things
object RDDasMonadPlus {
import org.apache.spark.{ SparkContext }
import org.apache.spark.rdd.RDD
import scalaz._
import Scalaz._
import scala.reflect.ClassTag
// RDDMPlus is the type for which we will define the Monad instance. it can be
// constructed from an RDD using the RDDClassTag constructor. this
// implementation is based on insights from
@tel
tel / gist:7ad4dafb6c39221fc773
Last active July 3, 2024 23:03
Lennart Augustsson's "Simpler, Easier!", copied (without permission) to get around the Great Fire Wall

Simpler, Easier!

Lennart Augustsson, Oct 25, 2007

In a recent paper, Simply Easy! (An Implementation of a Dependently Typed Lambda Calculus), the authors argue that type checking a dependently typed language is easy. I agree whole-heartedly, it doesn't have to be difficult at all. But I don't think the paper presents the easiest way to do it. So here is my take on how to write a simple dependent type checker. (There's nothing new here, and the authors of the paper are undoubtedly familiar with all of it.)

I'll start by implementing the untyped lambda calculus. It's a very simple language with just three constructs: variables, applications, and lambda expressions, i.e.,

@ldacosta
ldacosta / SparkFactoryNotSerializable.scala
Last active August 29, 2015 14:07
I am trying to narrow down an Exception thrown by Spark when using "factories" to create classes. See example below ====> only factory2 works!
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
/**
* I am trying to narrow down on an Exception thrown by Spark when using "Factories".
* The factories have parameters that are used in the classes' functions.
*
* To run this code: copy-paste this whole content in a Spark-Shell. Execute Test.theMain(sc)
*
*/
@ldacosta
ldacosta / betterCounting
Last active August 29, 2015 14:05
Counting Booleans
def isEven(x:Int) = (x % 2 == 0)
def isPositive(x: Int) = (x > 0)
def aList = List(1,2,-2,34,57, -91, -90)
// in this case I go from [Int] => [Bool] => Int. 2 times, because I have 2 functions.
val (numEven, numPos) = (aList.map(isEven).filter(_ == true).length, aList.map(isPositive).filter(_ == true).length)
@runarorama
runarorama / gist:a8fab38e473fafa0921d
Last active April 13, 2021 22:28
Compositional application architecture with reasonably priced monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@jdegoes
jdegoes / MonadImpliesFunctor.scala
Created April 16, 2014 16:51
Scalaz test bed
import scala.language.higherKinds
trait Monad[M[_]] {
implicit def `Monad ~> Functor`[M[_]: Monad]: Functor[M] = new Functor[M]{}
}
trait Functor[F[_]] {
}
object Functor {
@lelandbatey
lelandbatey / whiteboardCleaner.md
Last active November 11, 2024 22:46
Whiteboard Picture Cleaner - Shell one-liner/script to clean up and beautify photos of whiteboards!

Description

This simple script will take a picture of a whiteboard and use parts of the ImageMagick library with sane defaults to clean it up tremendously.

The script is here:

#!/bin/bash
convert "$1" -morphology Convolve DoG:15,100,0 -negate -normalize -blur 0x1 -channel RBG -level 60%,91%,0.1 "$2"

Results

@ldacosta
ldacosta / BunchOfTraits
Created January 16, 2014 18:47
Let's suppose that I have an abstract trait for which I have 'n' (> 2) concrete implementations. When I mix those traits, I want to have an elegant way to create concrete classes. See below.
// define mixable traits:
trait T
trait T1 extends T
trait T2 extends T
...
trait T100 extends T
// now let's mix them:
trait anotherT extends T
// and create concrete classes.