Skip to content

Instantly share code, notes, and snippets.

@petekneller
petekneller / gadt.md
Created July 18, 2019 09:34 — 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
@petekneller
petekneller / type-bounds.scala
Created February 27, 2019 16:39 — forked from tjweir/type-bounds.scala
Tour of Scala Type Bounds
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x
object Playground extends App {
sealed trait IExp
final case class Lit(i: Int) extends IExp
final case class Neg(e: IExp) extends IExp
final case class Add(r: IExp, l: IExp) extends IExp
sealed trait Tree
final case class Leaf(s: String) extends Tree
final case class Node(s: String, ts: List[Tree]) extends Tree

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

@petekneller
petekneller / ns-inet.sh
Created July 21, 2018 00:27 — forked from dpino/ns-inet.sh
Setup a network namespace with Internet access
#!/usr/bin/env bash
set -x
NS="ns1"
VETH="veth1"
VPEER="vpeer1"
VETH_ADDR="10.200.1.1"
VPEER_ADDR="10.200.1.2"
@petekneller
petekneller / svg-display.ipynb
Created May 15, 2018 22:33 — forked from rpmuller/svg-display.ipynb
IPython's SVG display functionality, in conjunction with the ease of making SVG strings using ElementTrees, makes it really easy to have a nice drawing canvas inside of IPython.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@petekneller
petekneller / svg-schematics.html
Last active April 17, 2018 21:58
SVG schematics
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Some experiments in how I might do multipart diagrams/schematics with SVG</title>
</head>
<body>
<h1>The problem</h1>
<p>I have a need/want to knock up schematic-like diagrams on the web with which I can: display either the whole or only parts of the diagram; zoom/scale or transform the part(s) being displayed. SVG seems to make sense a basis for this, so the question becomes one of: how do I do this with SVG?</p>
@petekneller
petekneller / gist:1a6ecd2500d5c7cbd32cbc4866559a58
Created January 4, 2017 14:31 — forked from milessabin/gist:cadd73b7756fe4097ca0
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 }
}
@petekneller
petekneller / install-haskell-stack-arm.sh
Created January 2, 2017 20:30 — forked from tmspzz/install-haskell-stack-arm.sh
A scrip to install Haskell Stack and set up things properly on Raspbian
#!/bin/sh
# Install stack
curl -sSL https://get.haskellstack.org/ | sh
# Install LLVM
sudo apt-get install llvm-3.7
# Add symbolic links to opt and llc
ln -s /usr/bin/opt-3.7 /usr/bin/opt
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Prelude hiding (log)
--------------------------------------------------------------------------------
-- The API for cloud files.
class Monad m => MonadCloud m where
saveFile :: Path -> Bytes -> m ()