Skip to content

Instantly share code, notes, and snippets.

@tfausak
tfausak / invertible-syntax-descriptions.markdown
Last active February 2, 2024 20:58
Survey of invertible syntax description libraries for Haskell.

Invertible syntax descriptions

An "invertible syntax description" is something that can be used to both parse and generate a syntax. For example, instead of defining separate toJson and fromJson functions for a given data type, an invertible syntax description could be used to provide both. There are many Haskell libraries that provide this functionality or something close to it. As far as I can tell most of them are at least inspired by Invertible syntax descriptions by Tillmann Rendel and Klaus Ostermann.

Personally I am interested in using these for HTTP routing. I frequently want to be able to define a route such as /episodes/:id.json, dispatch based on that route, and generate links to that route. Doing so manually is tedious and error prone.

@CodaFi
CodaFi / MBE.swift
Created July 8, 2018 06:33
Macro-by-Example: Deriving Syntactic Transformations from their Specifications, Re-Typeset
//: Foreword
//:
//: This playground is the culmination of a week of digging around
//: for the oldest papers I could find about (Lisp) macros and their evolution.
//: I found this paper particularly enlightening as it is the place where Scheme
//: and Rust-style "Macro-by-Example" macros first came into their own. In
//: addition, this paper discusses the implementation challenges faced by
//: previous macro systems and Kohlbecker's first go at MBEs.
//:
//: I have re-typeset this paper because [the one hosted by IU](ftp://www.cs.indiana.edu/pub/techreports/TR206.pdf)
// Swift's untyped errors are a goddam PiTA. Here's the pattern I use to try to work around this.
// The goal is basically to try to guarantee that every throwing function in the app throws an
// ApplicationError instead of some unknown error type. We can't actually enforce this statically
// But by following this convention we can simplify error handling
enum ApplicationError: Error, CustomStringConvertible {
// These are application-specific errors that may need special treatment
case specificError1
case specificError2(SomeType)
@Catfish-Man
Catfish-Man / lockcachecontention.m
Last active July 5, 2017 07:00
Benchmark showing how locks sharing a cache line will contend with each other
#import <Foundation/Foundation.h>
#import <time.h>
#import <os/lock.h>
#define ITERS 2000
#define NSEC_PER_ITER(time) (((double)time * (double)NSEC_PER_SEC) / (double)ITERS)
#define TEST(body, name) do {\
start = [NSDate date];\
for (int i = 0; i < ITERS; i++) {\
@CodaFi
CodaFi / update-swift-dev
Last active February 13, 2018 15:46 — forked from ddunbar/update-swift-dev
This is the script I currently use on OS X to get a working "swift-dev.xctoolchain" out of a built Swift. It isn't designed to work for anyone but me, but it should be easy to adapt. I always run this after every build, and then use `TOOLCHAINS=swift-dev swift build` (etc) to use the development compiler.
#!/bin/sh
set -e
if [ -z "${SWIFTPM_CONFIGURATION}" ]; then
SWIFTPM_CONFIGURATION=.bootstrap/lib/swift/pm/4
fi
if [ -z "${CONFIGURATION}" ]; then
CONFIGURATION=release
@beala
beala / TreeRotation.idr
Created March 5, 2017 05:24
Proving some properties of binary trees and rotation in idris.
{-
In this file I:
- Define a binary tree.
- Define a type level predicate for right rotation of a tree. That is, this type
can only be instantiated for trees that can be right rotated.
- Prove that it's decidable if a tree can be right-rotated or not.
- Prove that right-rotating a tree preserves in-order traversal.
-}
-- A binary tree.
@CodaFi
CodaFi / FileCheck.swift
Last active February 3, 2018 23:09
A self-contained port of FileCheck to Swift
/// Playground - noun: a place where people can play
/// FileCheck yourself before you wreck yourself
import Foundation
#if os(Linux)
import Glibc
typealias NSRegularExpression = RegularExpression
#else
import Darwin
@djspiewak
djspiewak / Free.hs
Created January 13, 2017 18:23
RoomConf Talk: Free as in Monads
data Free f a =
Return a |
forall e . Bind (Free f e) (e -> Free f a) |
LiftF (f a)
instance Monad (Free f) where
return = Return
(>>=) = Bind
liftF :: f a -> Free f a
@jdegoes
jdegoes / FreeMonad.scala
Created August 21, 2016 16:24
A pedagogical free(er) monad in 23 lines of Scala
sealed trait Free[F[_], A] { self =>
final def map[B](ab: A => B): Free[F, B] = Free.flatMap(self, ab andThen (Free.point[F, B](_)))
final def flatMap[B](afb: A => Free[F, B]): Free[F, B] = Free.flatMap(self, afb)
final def interpret[G[_]: Monad](fg: F ~> G): G[A] = self match {
case Free.Point(a0) => a0().point[G]
case Free.Effect(fa) => fg(fa)
case fm : Free.FlatMap[F, A] =>
val ga0 = fm.fa.interpret[G](fg)
ga0.flatMap(a0 => fm.afb(a0).interpret[G](fg))
}