Skip to content

Instantly share code, notes, and snippets.

View mndrix's full-sized avatar

Michael Hendricks mndrix

View GitHub Profile
@mndrix
mndrix / append.go
Last active November 2, 2016 21:49
Compiling Prolog to Go
package prolog
/*
%% append(list,list,list)
%% append(+,+,+) is semidet.
%% append(+,+,-) is det.
%% append(+,-,+) is semidet.
%% append(+,-,-) is det.
%% append(-,+,+) is multi.
%% append(-,+,-) is multi.
@mndrix
mndrix / currency.pl
Created October 7, 2016 14:54
PriceCharting's Prolog library for currency amounts
:- module(currency, [ atom_currency/2
, codes_currency/2
, currency//1
]).
:- use_module(library(clpfd)).
:- use_module(library(dcg/basics), [integer//1]).
:- use_module(library(delay)).
:- use_module(library(error)).
@mndrix
mndrix / base32-padding.go
Created November 2, 2015 21:54
Add padding characters to base32 encoded data
package main
import (
"encoding/base32"
"fmt"
"strings"
)
func main() {
data := "2246b2egzcc3ktvvoklo5cvzh4"
@mndrix
mndrix / roman.pl
Created July 10, 2015 20:49
Roman numerals in Prolog
:- use_module(library(clpfd)).
dcg(Arabic) -->
{ Arabic in 1..3999 },
roman(Arabic),
!.
roman(Total) -->
{ Rest #>= 0 },
{ Total #= Value + Rest },
@mndrix
mndrix / rules.pl
Last active August 29, 2015 14:20
Qualifications for foreign earned income exclusion
% Requirements, using Prolog syntax, to qualify for the foreign earned income exclusion
% See http://www.irs.gov/Individuals/International-Taxpayers/Foreign-Earned-Income-Exclusion
% facts which act as parameters to the loophole solver
:- dynamic
us_citizen/0,
us_resident_alien/0.
% See http://www.irs.gov/Individuals/International-Taxpayers/Foreign-Earned-Income-Exclusion-Can-I-Claim-the-Exclusion-or-Deduction
qualify :-
@mndrix
mndrix / dcg_util.pl
Last active September 29, 2018 17:41
Prolog DCG utility predicates
% at_least//2 is like at_least//3 but ignores the specific matches found.
:- meta_predicate at_least(+,3,*,*).
at_least(N,Goal) -->
at_least(N,Goal,_).
% at_least(N,Goal,Matches) consumes at least N matches of Goal.
% after that, it consumes as much as possible.
:- meta_predicate at_least(+,3,?,*,*).
at_least(N0,Goal,[X|Xs]) -->
@mndrix
mndrix / main.go
Created November 18, 2014 20:21
Golang abort HTTP request after header
package main
import (
"crypto/tls"
"fmt"
"net/http"
"time"
)
// Make a GET request for a large (24 MB) file. We only care about a single header field ("ETag")
@mndrix
mndrix / binary-simple.prolog
Last active August 29, 2015 14:06
Simplification and binarization of Prolog clauses
% Simplification and binarization of Prolog clauses.
%
% This code was my note paper while reading
% "The BinProlog Experience: Architecture and
% Implementation Choices for Continuation Passing
% Prolog and First-Class Logic Engines" by Paul Tarau.
%
% I find it especially pleasing that binary logic programs
% require only about half as many WAM instructions as
% traditional Prolog. This should make it easy to build
@mndrix
mndrix / tips.md
Last active August 14, 2018 04:38
Prolog macros tips

Tips for writing macros in SWI Prolog. This should eventually be expanded into a full blog post:

Only expand macros when requested

For each module that defines a macro, define a predicate like:

wants_my_fancy_macro :-
    prolog_load_context(module, Module),
 Module \== my_module, % don't expand macros in our own code
@mndrix
mndrix / bench.prolog
Last active August 29, 2015 14:01
Sketch of a benchmark library
:- module(bench, [call_ns/1,call_ns/2,compare/1]).
:- use_module(library(sweet)).
compare(Goals) :-
maplist(run_comparison,Goals,Timings),
keysort(Timings,Sorted),
format("name\tns/op~n",[]),
maplist(show_timing,Sorted).