Skip to content

Instantly share code, notes, and snippets.

%This is a basic English-to-Prolog translator. It uses a
%pathfinding algorithm to find the meaning of each sentence.
%For example, output([a,is,greater,than,B],X) means X = (a > b).
%The input is an English phrase. The output is a Prolog expression.
output(Input,Output) :-
is_output(Output),
means(Input,Output).
@gbmele
gbmele / gist:c85ece7f10e8b6b7d04c9d402b9e7b59
Created March 18, 2024 11:17
extensible records in prolog
attr(A, [N-V | R]):- memberchk( N-X, A), X = V, attr(A, R).
attr(_, []).
color(A, B):- attr( A, [color-B]).
pigs( Pigs):-
length( Pigs,N),
N rem 2 =:= 1, Middle is N div 2, /* there _is_ a middle - list length is odd */
nth0( Middle,Pigs,P1), attr( P1, [color-brown]),
member( P2, Pigs), attr( P2, [color-brown, eats-carrots]),
@gbmele
gbmele / database.lgt
Created March 1, 2024 05:17 — forked from anonymous/database.lgt
Meta-Programming in Prolog - Part 1
:- object(database).
:- public(rule/2).
nat(zero) :- true.
nat(s(X)) :- nat(X).
add(zero, Y, Y) :- true.
add(s(X), Y, s(Z)) :-
add(X, Y, Z).
@gbmele
gbmele / closure_example.pl
Last active February 28, 2024 23:32 — forked from jarble/closure_example.pl
An example of closures (or "nested predicates") in Prolog
%Since SWI-Prolog does not have a built-in implementation of closures, I wrote my own implementation here.
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
main :- predicate_with_nested(1,C),writeln(C).
call_local(Definition,Params) :-
copy_term(Definition,(Params :- Body)),
call(Body).
% test predicate
test(Summary, Predicate) :-
string_concat('\n ', Summary, Buf),
string_concat(Buf, ' is running', Message),
write(Message),
call(Predicate),
write('\ndone.').
% example
force(anakin, 9).
@gbmele
gbmele / basic.sh
Created July 19, 2023 02:22 — forked from myzsyweb/basic.sh
a simple interpreter for BASIC written in AWK
#!/usr/bin/gawk -f
# a simple interpreter for GWBASIC(subset) written in AWK
# following 'The AWK Programming Language' Chapter 6
# @date June 20,2012
# @link basic.sh https://gist.github.com/2959816
# @link test.sh https://gist.github.com/2959877
# TODO:
# gosub/return...ok
# if/else/elseif/endif...ok
# while/wend...ok
@gbmele
gbmele / ifdef.awk
Created July 19, 2023 02:18 — forked from simon-brooke/ifdef.awk
I needed a little tool to do ifdefs, for conditional inclusion of text; I didn't want the full panoply of the C preprocessor. I like this. Test-driven development in awk.
#!/usr/bin/awk -f
# Experimental implementation of ifdef in awk
BEGIN {
depth = 0;
defined = "";
printing = 1;
}
$1 ~ /#DEFINE/ {
@gbmele
gbmele / defines_to_cases.awk
Created July 19, 2023 02:15 — forked from MLKrisJohnson/defines_to_cases.awk
AWK script for generating switch/case statements to stringify #define'd macro names
#!/usr/bin/env awk -f
# Given a C-like header file, this AWK script will generate a sequence
# of case statements for stringifying macro names.
#
# For example, if the input file looks like this:
#
# #define kIOReturnSuccess KERN_SUCCESS // OK
# #define kIOReturnError iokit_common_err(0x2bc) // general error
# #define kIOReturnNoMemory iokit_common_err(0x2bd) // can't allocate memory
@gbmele
gbmele / json-to-csv.awk
Created July 19, 2023 02:06 — forked from HunterGerlach/json-to-csv.awk
How to convert a JSON file to CSV with AWK
# Usage: awk -f json-to-csv.awk values.json > output.csv
# Note: You may encounter issues w/ esacping values or missing/empty fields
BEGIN{ OFS=""; u=f=l=a=e=""; }
/"username"/{ u=$2 }
/"first_name"/{ f=$2 }
/"last_name"/{ l=$2 }
/"asset_type"/{ a=$2 }
/"email"/{ e=$2 }
{ if(e!="") { print u,f,l,a,e; e="" }} # add for any potentially empty lines
@gbmele
gbmele / awk-expense-calculator.awk
Created July 18, 2023 13:56 — forked from samask/awk-expense-calculator.awk
Awk Expense Calculator, by Ward Cunningham
#!/usr/bin/awk -f
# Source: http://c2.com/doc/expense/
/^[A-Z]+[A-Z0-9]*$/ {
if (sums[$1] == "" || $1 == "SUM") {
sums[$1] = sum # Define Symbol
$1 = sum
sum = 0
}