Skip to content

Instantly share code, notes, and snippets.

@GeoffChurch
GeoffChurch / mi_dcg.pl
Last active January 26, 2024 15:18
SWI-Prolog-compatible DCG-ification of Power of Prolog's 'mi_list3' (www.metalevel.at/acomip/)
% The following is a 3-line meta-interpreter for pure Prolog (no
% cuts/negation). It reifies conjunction and dispatch, leaving
% unification and backtracking implicit. It is written for brevity,
% leveraging DCG notation and using lists to denote conjunctions of
% goals. It implements the semantics of a program (in the sense of a
% set of rules or "immediate consequence operator") as the program's
% least fixed point, obtained by "reflexive transitive closure" /
% exponential / "Kleene iteration".
%%%%%
:- op(700, xfx, .=).
A:AFs .= B:BFs =>
gets(AFs, A, Val),
gets(BFs, B, Val).
X:Fs .= Val => gets(Fs, X, Val).
Val .= X:Fs => gets(Fs, X, Val).
A .= B => A = B.
colon_list(X:Xs0, Out) => colon_list(Xs0, Xs), Out = [X|Xs].
@GeoffChurch
GeoffChurch / speculative_if.py
Created May 7, 2024 01:53
Speculative execution on both branches of a conditional
"""
Provides speculative_if(cond, branch1, branch2), which runs branch1 followed by
branch2 while cond is running.
"""
from dataclasses import dataclass
import itertools
import multiprocessing as mp
import time
@GeoffChurch
GeoffChurch / derive_accessors.pl
Last active February 27, 2025 12:28
Prolog getters, setters, mutators using term_expansion
user:term_expansion((:- derive_accessors(ClassTemplate)), Clauses) :-
ClassTemplate =.. [ClassName|Fields],
functor(ClassTemplate, ClassName, Arity),
functor(Skel, ClassName, Arity),
term_variables(Skel, Vars),
foldl(build_accessors(Skel), Fields, Vars, [], Clauses).
return(G), [Ret] --> { call(G, Ret) }.
build_accessors(Skel, Field, Var) -->
@GeoffChurch
GeoffChurch / stache.pl
Created February 7, 2025 19:33
STACHE: Stash That Actually Can Handle Existentials
:- module(stache, [
staching/2,
stachings/2,
rummage/1,
rummage_lazy/1
]).
:- meta_predicate staching(?, 0), stachings(?, 0).
:- use_module(library(chr)).