Skip to content

Instantly share code, notes, and snippets.

View mndrix's full-sized avatar

Michael Hendricks mndrix

View GitHub Profile
@mndrix
mndrix / bar.prolog
Created May 16, 2014 21:55
Experiments with Prolog imports and exports from modules
:- module(bar, [ bar_a/0 ]).
do_import(TargetModule) :-
%TargetModule:use_module(bar, [bar_a/0]).
%TargetModule:import(bar:bar_a/0).
TargetModule:assert((bar_a :- writeln("Asserted bar_a/0"))).
bar_a :-
writeln("Inside bar_a/0").
@mndrix
mndrix / benchmark.prolog
Created May 7, 2014 15:25
Database predicate benchmarks (SWI Prolog)
:- dynamic asserted_a/1, asserted_z/1.
:- b_setval(global_variable, []).
count(1_000_000).
my_asserta(N) :- asserta(asserted_a(N)).
my_assertz(N) :- assertz(asserted_z(N)).
my_recorda(N) :- recorda(recorded_a, N).
my_recordz(N) :- recordz(recorded_z, N).
my_flag(N) :- flag(some_flag, _, N).
@mndrix
mndrix / focused-chrome-tab.scpt
Created April 22, 2014 18:50
AppleScript to find the currently focused Google Chrome tab
-- run with `osascript -s s focused-chrome-tab.scpt` to get machine-readable output
tell application "Google Chrome"
set active_window to first window
set i to active tab index of active_window
set active_tab to item i of tabs of active_window
set x to {URL:URL of active_tab, title:title of active_tab}
return x
end tell
@mndrix
mndrix / focused-iterm2-tab.scpt
Last active August 29, 2015 14:00
AppleScript to find the currently focused iTerm2 tab
tell application "iTerm"
return name of current session of current terminal
end tell
@mndrix
mndrix / under_camel.prolog
Last active August 29, 2015 13:55
Prolog relation between camel-case names and underscore-separated names
:- use_module(library(clpfd)).
:- use_module(library(delay)).
%% under_camel(?Underscore:atom, ?CamelCase:atom) is det.
%
% For example, `under_camel(hello_world, 'HelloWorld')`. Works in both
% directions.
under_camel(U,C) :-
when((ground(U0);ground(U)),prepend_underscore(U, U0)),
delay(atom_codes(U0,U0Codes)),
@mndrix
mndrix / trace.txt
Created December 13, 2013 16:55
SWI Prolog V7 listing a program with dicts
$ cat dicts.pl
person{name: john}.
$ swipl -f none -s dicts.pl
% /tmp/dicts.pl compiled 0.00 sec, 2 clauses
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.3)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
@mndrix
mndrix / stacktrace.txt
Created December 13, 2013 16:52
SWI Prolog V7.1.3 segfault
% The graphical front-end will be used for subsequent tracing
% .plrc compiled 0.28 sec, 4,557 clauses
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.3)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use ?- help(Topic). or ?- apropos(Word).
@mndrix
mndrix / answer.ama
Created August 8, 2013 20:50
Silly code snippet for Amalog spec
answer X
X is 41 + 1
@mndrix
mndrix / ack.ama
Last active December 20, 2015 04:29
Toying with Amalog concurrency
// ack +Dir +Language +Pattern -File -Line
//
// True if File is beneath the directory Dir, written in Language and contains
// a Line that matches Pattern. Similar to the command line tool ack.
ack Dir Language Pattern File Line
dir_entry Dir Entry
type Entry file
fan_out 1 // optional optimization. See Note_fan_out
language Entry Language
line Entry Line
@mndrix
mndrix / scrape.prolog
Last active October 23, 2016 02:20
speculative sketch of a web scraper in Prolog with XPath, regular expressions and constraints
% The numerous inline comments are explanatory. Normal code wouldn't have them.
% I'm using double quotes to delineate raw text content. Real code would use
% a quasiquotation mechanism.
amazon_condition_overview(ASIN, Condition, BestPrice, Inventory, Section) :-
% HTTP GET the URL and parse it into a DOM structure
get(url("amazon.com/gp/offer-listing/$ASIN/?ie=UTF8"), DOM),
% build an atom representing the tab's node ID
TabId = qq("olpTab$Condition"),