Skip to content

Instantly share code, notes, and snippets.

View Astrac's full-sized avatar

Aldo Stracquadanio Astrac

View GitHub Profile
@monaddle
monaddle / recursion schemes.scala
Last active February 13, 2019 19:32
Recursion schemes implemented in Scala using Matroyshka. They don't all make sense or anything, but they compile and run. lo
package prestwood
// Examples of recursion scheme implementations using matroysha. Mostly they're not that useful,
// and I really only got three tricks down, but they're good tricks:
// 1. Collect information across an AST using a Writer monad
// 2. Modify AST nodes in place
// 3. Annotate AST nodes with arbitrary types

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

import play.api.libs.json._
/**
* IdT represents a typed unique identifier. The type of IdT is the type of the
* object that it points to. Thus an IdT[Blog] is the unique identifier of a blog.
* It is meant to be imported as import IdT => Id to replace old non-typed ids
* which are of type IdT[Nothing]. The covariance of IdT is only for backward
* compatibility.
*/
class IdT[+A] private (val key: Long) extends AnyVal with Ordered[IdT[_]] with Serializable {
@milessabin
milessabin / singleton-only.scala
Created June 6, 2016 11:04
Scala type which can only be extended by an object, not by a non-abstract type ...
scala> class Foo { self: Singleton => }
defined class Foo
scala> class Bar extends Foo
<console>:12: error: illegal inheritance;
self-type Bar does not conform to Foo's selftype Foo with Singleton
class Bar extends Foo
^
scala> object Bar extends Foo
@badboy
badboy / hkt.rs
Created March 26, 2016 11:33 — forked from 14427/hkt.rs
Higher-kinded type trait
use std::rc::Rc;
pub trait HKT<U> {
type C; // Current type
type T; // Type with C swapped with U
}
macro_rules! derive_hkt {
($t:ident) => {
impl<T, U> HKT<U> for $t<T> {
@milessabin
milessabin / gist:cadd73b7756fe4097ca0
Last active September 16, 2019 13:44
A new approach to encoding dependently-typed chained implicits, using singleton types ...
object Demo {
// A couple of type classes with type members ...
trait Foo[T] {
type A
}
object Foo {
implicit val fooIS = new Foo[Int] { type A = String }
}