Create a gist now

Instantly share code, notes, and snippets.

anonymous /database.lgt
Created Nov 1, 2011

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).
append([], Ys, Ys) :- true.
append([X|Xs], Ys, [X|Zs]) :-
append(Xs, Ys, Zs).
prove(G) :-
prove1([G]).
prove1([]) :- true.
prove1([G|Gs]) :-
rule(G, B),
prove1(B),
prove1(Gs).
length([], 0) :- true.
length([_|Xs], N) :-
length(Xs, N1),
N is N1 + 1.
:- end_object.
:- object(interpreter).
:- public(prove/1).
:- public(prove/2).
%Initialize the goal list with G.
prove(G) :-
prove1([G]).
prove1([]).
prove1([G|Gs]) :-
rule(G, B),
prove1(B),
prove1(Gs).
prove(G, T) :-
prove1([G], T).
prove1([], true).
prove1([G|Gs], ((G :- T1), T2)) :-
rule(G, B),
prove1(B, T1),
prove1(Gs, T2).
rule(rule(A, B), []) :- rule(A, B).
rule((X is Y), []) :- X is Y.
rule(G, B) :-
database::rule(G, B).
:- end_object.
:- initialization((
logtalk_load(library(metapredicates_loader)),
logtalk_load(library(types_loader)),
logtalk_load(rule_expansion),
logtalk_load(database, [hook(rule_expansion)]),
logtalk_load(interpreter))).
:- object(rule_expansion,
implements(expanding)).
term_expansion((Head :- Goals), rule(Head, List)) :-
flatten_goals(Goals, List).
flatten_goals((A, B), [A|Gs]) :-
!, % Used as implicit negation.
flatten_goals(B, Gs).
flatten_goals(true, []) :-
!. % Used as implicit negation.
flatten_goals(A, [A]).
:- end_object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment