Silly me, should have seen this.
The syntax error
ERROR: Full stop in clause-body? Cannot redefine ,/2
is from :=
which should be :-
This code works
term(asset).
term(liability).
term(equity).
structure(balanceSheet).
entity(microsoft).
fact(term(asset), entity(microsoft), 5000).
fact(term(liabilities), entity(microsoft), 1000).
fact(term(equity), entity(microsoft), 4000).
% rule Assets = Liabilities + Equity %
does_balance_sheet_balance(Entity) :-
fact(term(asset),entity(Entity),Asset),
fact(term(liabilities),entity(Entity),Liabilities),
fact(term(equity),entity(Entity),Equity),
Asset is Liabilities + Equity.
Example run:
?- does_balance_sheet_balance(microsoft).
true.
For the final code I would use something similar to
asset(microsoft,5_000).
liability(microsoft,1_000).
equity(microsoft,4_000).
does_balance_sheet_balance_01(Entity) :-
asset(Entity,Asset),
liability(Entity,Liabilities),
equity(Entity,Equity),
Asset is Liabilities + Equity.
but I do understand that you are learning and having the extra term names is quite helpful and what I also do.
Also note the Prolog can use _
in integers which makes it nicer when viewing large numbers.
Since Prolog is a logic language, if the predicate fails, the answer will be false
.
asset(microsoft,5_000).
liability(microsoft,2_000).
equity(microsoft,4_000).
does_balance_sheet_balance(Entity) :-
asset(Entity,Asset),
liability(Entity,Liabilities),
equity(Entity,Equity),
Asset is Liabilities + Equity.
?- does_balance_sheet_balance(microsoft).
false.
If you want more meaningful error messages then using must_be/2 might be of use, e.g.
does_balance_sheet_balance(Entity) :-
must_be(bound,Entity),
asset(Entity,Asset),
liability(Entity,Liabilities),
equity(Entity,Equity),
Asset is Liabilities + Equity.
?- does_balance_sheet_balance(Variable).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [14] throw(error(instantiation_error,_11210))
ERROR: [10] does_balance_sheet_balance(_11234) at c:/users/eric/documents/projects/prolog/swi-discourse_016.pl:37
ERROR: [9] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.