Skip to content

Instantly share code, notes, and snippets.

{-|
- Example of using free constructions to build a flexible little compiler.
-
- The goal here is not necessarily efficiency but readability and flexibility.
-
- The language grammar is represented by an ADT; however, instead of
- recursively referring to itself it instead references a type variable.
-
- We derive instances of 'Functor' and 'Traversable' for this type.
-
@gatlin
gatlin / conway.hs
Last active December 12, 2021 01:53
Some conway experiments, will probably be updated often
-- | This can be run with
-- > cabal run conway.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{- cabal:
build-depends: base, Stream, JuicyPixels, comonad
-}
import Prelude hiding (head, tail, repeat, take)
@gatlin
gatlin / audio
Last active October 17, 2021 13:08
Shell script to make jackd and pulseaudio (and optionally fluidsynth) coexist with minimal interruption to my youtubing. Also it Works For Me™ on my laptop, will keep working on it
#!/bin/bash
###
# Script to launch jackd and reroute PulseAudio through it. And optionally
# start fluidsynth.
#
# READ THE NOTES PLEASE
###
# Usage (assuming this script is named `audio`, is in your PATH, and is
@gatlin
gatlin / pid.lhs
Created August 17, 2016 20:42
Streaming PID controller in Haskell
PID controller in Haskell
===
A major project I want to embark on at some point in the future is making a
quadrotor. I've made one before but I was at the mercy of a lot of off-the-shelf
software that I'm not altogether sure was entirely correct, either.
So I want to eventually program a quadrotor (or similarly awesome robot thing)
and I would really enjoy doing it in Haskell. It has a low footprint and is
already used in other real time settings.
@gatlin
gatlin / a.vm.ts
Created July 27, 2021 02:28
Interpreter for call-by-push-value language with side effects built with robot state machine and my precursor library.
import { readFile } from "fs/promises";
import { createInterface } from "readline";
import type { Interface } from "readline";
import { CESKM, parse_cbpv, scalar, continuation, topk } from "precursor-ts";
import type { State, Value, Store } from "precursor-ts";
import {
createMachine,
state,
transition,
reduce,
@gatlin
gatlin / ctx.ts
Last active May 10, 2021 22:42
something relating reactive UIs and spreadsheets
/**
* outputs:
* a { a: 3, b: 6, c: 12 }
* b { a: 5, b: 10, c: 18 }
* c { a: 8, b: 16, c: 27 }
* { a: '25', b: '196', c: '900' }
* c 900
*/
// todo: use type system to statically verify the key exists in the object.
@gatlin
gatlin / arith.ts
Last active December 16, 2020 00:47
Demonstrates a useful pattern of defining and (semi-)automatically deriving evaluators for domain-specific languages in TypeScript
import { _, Functor, Fix, _in, cata, Algebra } from './base';
import { strict as assert } from 'assert';
// The type parameter goes where the grammar would otherwise reference itself.
type ArithF<A>
= { tag: 'add' ; lhs: A ; rhs: A }
| { tag: 'mul' ; lhs: A ; rhs: A }
| { tag: 'num' ; n: number }
| { tag: 'paren' ; e: A } ;
@gatlin
gatlin / parser-combinator.rkt
Last active June 22, 2020 19:11
Simple parser combinator example in Typed Racket
#lang typed/racket
(provide None
Just
Opt
maybe
neq?
Parser
lit
try-parse
@gatlin
gatlin / psilo-typeclass.sl
Last active June 19, 2020 19:04
Hask-- I mean, psilo with zero real typeclasses and one virtual typeclass
;; Hask-- I mean, psilo with no real typeclasses and one virtual typeclass.
; Inspired by [1], this is an exploration of representing all typeclasses
; through one distinguished class.
;
; This version of psilo does not have "real" typeclasses. However faking them
; with explicit dictionary passing does seem to correctly infer and check the
; constraints.
;
; The goal then is to figure out how best to represent "real" typeclasses as a
; distinguished feature in the language implementation given that we are
@gatlin
gatlin / cont.hs
Last active May 22, 2020 17:42
Delimited continuation monad transformer
{- cabal:
build-depends: base
-}
module ContT
( ContT
, reset
, shift
, liftIO
)