Skip to content

Instantly share code, notes, and snippets.

-- represent base smt types as strings
type Variable = String
type Refinement = String
-- add a list of free vars to refvars
data RefVar = RefVar String [Variable] deriving (Show)
-- ignore var list for equality
instance Eq RefVar where
// List representation with lambdas.
// https://en.wikipedia.org/wiki/Church_encoding#Represent_the_list_using_Scott_encoding
import java.util.*;
import java.util.function.*;
public abstract class ConsList<E> extends AbstractSequentialList<E> {
public abstract <C> C match(C n, BiFunction<E, ConsList<E>, C> c);
public static <E> ConsList<E> nil() {
;; https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
(defn topsort [g]
(let [l (atom ())
marks (atom (zipmap (keys g) (repeat nil)))
visit (fn visit [n]
(case (@marks n)
:perm nil
:temp (throw (IllegalArgumentException. "Not a DAG"))
(do
(swap! marks assoc n :temp)
@K9-guardian
K9-guardian / parity.pl
Last active February 22, 2023 09:34
Cursed probabilistic parity checker based on the length of the number in English. Grammar currently supports numbers up to 10^64 trillion. Run with scryer-prolog.
:- use_module(library(clpz)).
:- use_module(library(dcgs)).
:- use_module(library(lambda)).
:- use_module(library(lists)).
:- use_module(library(reif)).
num_to_parity(N, odds_evens(Os, Es)) :-
num_word(N, W),
same_length(W, Ls),
findall(M, word_to_num(Ls, M), Ms),
@K9-guardian
K9-guardian / brainfuck.pl
Last active December 5, 2022 20:27
Brainfuck interpreter in SWI-Prolog.
:- use_module(library(apply)).
:- use_module(library(assoc)).
:- use_module(library(clpfd)).
:- use_module(library(dcg/basics)).
:- use_module(library(dcg/high_order)).
:- use_module(library(edcg)).
:- use_module(library(pio)).
:- use_module(library(portray_text)).
% I'd prefer to use characters but this is a compromise to make the code simpler.
@K9-guardian
K9-guardian / earley.clj
Last active January 22, 2023 06:57
Earley parser in Clojure.
(ns earley
(:refer-clojure :exclude [==])
(:require [clojure.set :as set])
(:use clojure.core.logic))
(declare nullables single-parse all-parses)
;; An Earley set is a tuple of a vector and a set.
;; This lets us iterate over them as well as keep track of seen values.
(defn- set-conj [{:keys [seen?] :as all} & xs]
@K9-guardian
K9-guardian / priority_queue.clj
Last active January 26, 2022 10:16
Persistent Priority Queue
;; Persistent priority queue implementation using a binary heap. Min heap by default.
;; O(1) lookup, O(log n) insertion and deletion.
(ns priority-queue
(:import clojure.lang.IPersistentCollection
clojure.lang.IPersistentStack
clojure.lang.Seqable))
(defn- left [idx] (inc (* 2 idx)))
(defn- right [idx] (+ 2 (* 2 idx)))
use std::cmp::Ordering;
use std::collections::BinaryHeap;
// I can't come up with a better name for this :P
#[derive(Debug, PartialEq, Eq)]
struct Pair {
prime: u128,
multiple: u128,
}
@K9-guardian
K9-guardian / minify-lsts.bat
Last active October 22, 2021 02:16
Simple racket minifier. Only works for proper lists currently, plan to add improper lists and other datum as well.
; @echo off
; Racket.exe "%~f0" %*
; exit /b
#lang racket/base
(require racket/port)
(define (file->datums filepath)
(define fileport (open-input-file filepath))
(read-language fileport void)
(for/list ([datum (in-producer read eof fileport)]) datum))
@K9-guardian
K9-guardian / life.clj
Last active November 29, 2021 06:56
Game of life in Clojure (with wraparound)
(defn rotate [s l]
(case s
0 l
1 (cons (last l) (drop-last l))
-1 (concat (rest l) (take 1 l))))
(defn neighbors [b]
(let [rels (for [x (range -1 2)
y (range -1 2)
:when (not= 0 x y)]