Skip to content

Instantly share code, notes, and snippets.

@msullivan
msullivan / stlc-tuples.elf
Last active November 3, 2021 07:42
twelf formalization and type safety for simply typed lambda calculus with n-ary tuples
%%%% Formalization of a simply typed lambda-calculus with n-ary tuples.
% I decided to do this because it seemed like something small and
% interesting that was definitely not on the twelf wiki as a case
% study or a tutorial. n-ary tuples are a great feature in that they
% add no expressive power over pairs but are a much bigger pain to
% machine formalize!
%
% Doing it on paper is mostly just a matter of sprinkling "subscript i"
% and "..." around, but is otherwise pretty straightforward.
@msullivan
msullivan / full_outer_join_1.edgeql
Last active July 14, 2021 22:12
simulate a FULL OUTER JOIN in edgeql
# Do a FULL OUTER JOIN on User.name[0] = Card.name[0]
# (the seed computable is to demonstrate that it works with computed data)
WITH
L := User { seed := random() },
R := Card,
joined := DISTINCT {
(FOR l IN {L} UNION ([l.id], [(SELECT R FILTER l.name[0] = R.name[0]).id] ?? <array<uuid>>[])),
(FOR r IN {R} UNION (([(SELECT L FILTER L.name[0] = r.name[0]).id] ?? <array<uuid>>[]), [r.id])),
},
FOR row in {joined} UNION (
@msullivan
msullivan / scopetree-test-fixup.py
Last active March 17, 2021 20:33
fixup test_edgeql_ir_scopetree.py from test output log
#!/usr/bin/env python3
# Run with:
# edb test tests/test_edgeql_ir_scopetree.py 2>&1 | python3 scopetree-test-fixup.py -i
import textwrap
import sys
from typing import *
@msullivan
msullivan / knot_solve.py
Created January 3, 2021 05:07
not really working black knot solver for 2006 icfp
#!/usr/bin/env python3.8
import sys
import re
def extract(s):
return [int(x) for x in re.findall(r'-?\d+', s)]
from z3 import Bool, Int, If, Not, Solver, And
"""Fixer for six.iterkeys() -> .keys(), and similarly for iteritems and itervalues."""
from lib2to3 import fixer_util, fixer_base
from lib2to3 import pytree
from lib2to3.fixes import fix_dict
from lib2to3.fixer_util import Call, Name
import libpostmodernize
class FixDict(fix_dict.FixDict):
@msullivan
msullivan / unlambda_selfify.sml
Created August 20, 2019 04:19
lulz, mankangulisk, cps, nub manufacturer
signature UNLAMBDA_REPR =
sig
type F
val ap : F * F -> F
val ul_I : F
val ul_K : F
val ul_S : F
val ul_V : F
val ul_Dot : char -> F
val ul_C : F
@msullivan
msullivan / callable_module.py
Created April 3, 2019 21:15
making a python module callable
# ... don't do this
import sys
from types import ModuleType
def callme():
print("lol I'm a module")
class CallableModule(ModuleType):
def __call__(self, *args, **kwargs):
@msullivan
msullivan / 110
Created December 15, 2018 20:34
some day 12 inputs
initial state: #
..... => .
....# => .
#.... => .
#...# => .
...#. => #
...## => #
#..#. => #
#..## => #
@msullivan
msullivan / static.py
Created October 25, 2018 00:53
python static methods that can't be called on objects
from types import MethodType
class strict_staticmethod:
def __init__(self, f):
self.f = f
def __get__(self, obj, type=None):
if obj is not None:
raise AttributeError("strict_staticmethod can't be accessed through obj")
return self.f
@msullivan
msullivan / microbench.py
Created June 8, 2018 01:07
microbenchmark
from typing import cast
class Tree:
def accept(self, v: 'TreeVisitor') -> object:
pass
class Leaf(Tree):
def accept(self, v: 'TreeVisitor') -> object:
return v.visit_leaf(self)
class Node(Tree):
def __init__(self, value: int, left: Tree, right: Tree) -> None:
self.value = value