I am new to prolog and need help on how to write a prolog program. Here is what i am trying to do. I have downloaded the dataset from this link - https://archive.ics.uci.edu/ml/datasets/Blood+Transfusion+Service+Center

My goal is

  1. Use Any of the decision tree algorithms to build a decision tree for the given data
  2. Create rules from the decision tree.
  3. Code the rules into a Prolog Knowledge base
  4. Get the data about the blood donor from the user and predict if the donor has donated in March 2007.

I need some guidance on how to start the work. I have developed algorithms in python and understand #1 and #2 but have no idea on work with #3 and #4.

  • $\begingroup$ Wow, I didn't think I would hear about prolog on DataScienceSE :) Why do you need to implement the decision tree in Prolog? This looks like a very cumbersome way to use the model. $\endgroup$
    – Erwan
    Mar 20, 2021 at 21:45
  • $\begingroup$ Assignment at school.We dont have a subject on prolog, not sure why i have been given this assignment. $\endgroup$
    – kumar
    Mar 21, 2021 at 17:13
  • $\begingroup$ It's strange, prolog is a programming paradigm which needs a good bit of practice to understand. I'd suggest you ask your professor about this: do they really want you to learn the language? This is usually taught as a full module. $\endgroup$
    – Erwan
    Mar 21, 2021 at 23:53

1 Answer 1


I can give you some basics but the proper way to do this is to learn the prolog language, and it's very different from any other programming language.

In Prolog you define predicates (similar to functions) which contain conditions for the predicate to be satisfied, for example:

dt(Feat1, Feat2, Feat3, labelA) :-
    Feat1 =< 3,
    Feat2 == 'green',
    Feat3 >= 0.54.

dt(Feat1, Feat2, Feat3, labelB) :-
    Feat1 =< 3,
    Feat2 == 'green',
    Feat3 < 0.54.

dt(Feat1, Feat2, _Feat3, labelC) :-
    Feat1 > 3,
    Feat2 == 'green'.

dt(_Feat1, Feat2, _Feat3, labelB) :-
    Feat2 == 'blue'.
  • Here the predictate is dt and it has 4 clauses. In order for dt to be satisfied at least one of the clauses needs to to be true (i.e. all the conditions it contains must be true).
  • Variables are represented with a capital, for instance Feat1, Feat2, Feat3. Prolog will try to find an instanciation of the variables which satisfies one of the clauses (it will actually try all the possibilities until one of them works, it's a solver).
  • In each clause above the label is the last parameter of the predicate. Basically each clause means "if all the conditions after :- are satisfied, then the label is labelX".

Assuming the 4 clauses above have been saved in a file dt.pl, one can use for instance the SWI Prolog interpreter like this:

?- consult('dt.pl').

Now the way to use this toy decision tree would be:

?- dt(4,'green',0.3, Label).
Label = labelC .

?- dt(4,'blue',0.3, Label).
Label = labelB.
  • Note the capital L for Label: it means that now we let prolog find a value of Label which satisfies the predicate dt given the values provided for the features.

This is the basic idea. A more advanced and more generic representation of the DT would be possible using recursion and unification, but this would require an advanced understanding of Prolog.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.