Skip to content

Instantly share code, notes, and snippets.

@rntz
rntz / kanren-1.hs
Last active April 21, 2025 16:45
lambda join implementations via minikanren-style search
module Kanren where
import Control.Applicative
import Control.Monad
import Data.Monoid
import Data.List (intercalate)
-- The microkanren search monad. The MonadPlus instance for this implements a
-- *complete* search strategy, even over infinite search spaces -- unlike eg the
-- List monad -- AS LONG AS you use `Later` to guard any potentially infinite
@rntz
rntz / deps.py
Created April 6, 2025 20:55
Simple dependency-based incremental computation system
# a simple dependency tracking system
# pull-based, like Make
# ---------- INTERFACE ----------
class MakelikeInterface:
# A graph is a dictionary mapping keys to (callback, dependencies...)
# `callback` takes one argument per dependency and returns the computed
# value.
#
@rntz
rntz / gist:afd42848c82c074f870d9b1028ee5804
Created January 22, 2025 19:59
minikanren program for generating balanced ordered trees
(define (treeo x)
(conde
((== x 'n))
((fresh (a y z) (== x `(,y ,a ,z)) (into a) (treeo y) (treeo z)))
))
(define (lto x y)
(conde
((== x 0) (== y 1))
((== x 0) (== y 2))
@rntz
rntz / fix-eval-eager.ml
Last active December 7, 2024 20:58
Eager, lazy, & small-step "correct" fixed points
type var = string
type term = Var of var
| Lam of var * term
| App of term * term
| Fix of var * term (* only used for functions *)
| Lit of int
type value = Int of int
| Fun of (value -> value)
@rntz
rntz / mapgen.hs
Created November 13, 2024 03:04
generating maps
import System.Environment (getArgs)
import Control.Monad (guard, forM_)
import System.Random (RandomGen)
import qualified System.Random as Random
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
@rntz
rntz / sudoku.hs
Last active December 13, 2024 14:33
sudoku generation in Haskell
module Main where
import Data.List (minimumBy)
import Data.Ord (comparing)
import Data.Maybe (fromJust)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
-- Unknown: a list of ((x,y), options) for each cell (x,y).
type Unknown = [((Int, Int), [Int])]
@rntz
rntz / sudoku.py
Last active September 11, 2024 06:45
sudoku generator
import copy, random
n = 9
values = list(range(n))
def to_matrix(d):
return [[d[(x,y)] for y in range(n)] for x in range(n)]
def sudoku():
known = dict()
unknown = {(x,y): set(values)
@rntz
rntz / not-sudoku.py
Created September 11, 2024 06:09
not a sudoku solver
import copy, random
n = 4
values = list(range(n))
def to_matrix(d):
return [[d[(x,y)] for y in range(n)] for x in range(n)]
def sudoku():
known = dict()
unknown = {(x,y): set(values)
@rntz
rntz / GuardedKanren.hs
Created September 3, 2024 23:14
minikanren search strategy in Haskell
-- see https://gist.github.com/rntz/c2996e06301d9ae95ee0c7a9b43f5e0d for comparison
module GuardedKanren where
import Control.Monad (liftM2, guard)
import Control.Applicative
data Stream a = Nil | Cons a (Stream a) | Later (Stream a)
deriving (Show)
instance Functor Stream where
@rntz
rntz / GuardedKanren.agda
Last active September 4, 2024 14:49
implementation of muKanren's search strategy in Agda with --guarded
{-# OPTIONS --guarded #-}
module GuardedKanren where
open import Agda.Primitive using (Level)
private variable
l : Level
A B : Set l
data Bool : Set where true false : Bool
if_then_else_ : ∀{A : Set} → Bool → A → A → A