-
-
Save fogus/1337248 to your computer and use it in GitHub Desktop.
Meta-Programming in Prolog - Part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- initialization(( | |
logtalk_load(library(metapredicates_loader)), | |
logtalk_load(library(types_loader)), | |
logtalk_load(rule_expansion), | |
logtalk_load(database, [hook(rule_expansion)]), | |
logtalk_load(interpreter))). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- 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