DEV Community

Tomohiro Endo
Tomohiro Endo

Posted on

Model business logic in Prolog

Prolog is a logic programming language that offers a concise approach to modeling systems with minimal lines of code. This has benefits that extend beyond engineers. Non-engineering professionals, such as sales teams, can leverage Prolog's modeling capabilities to understand and navigate complex systems.

Assume that you are going to design customers' discount rules. You first come up with a customer should possess a member's card to be eligible for discounts and your boss has specified that each customer should receive the discount only once.

You model the rules in Prolog.

have_discount(X) :- have_members_card(X, yes), discount_received(X, no).
Enter fullscreen mode Exit fullscreen mode

If your first customer, Tom, has member's card and hasn't received discounts ever, you can express it in Prolog,

have_discount(X) :- have_members_card(X, yes), discount_received(X, no).

have_members_card(tom, yes).
discount_received(tom, no).
Enter fullscreen mode Exit fullscreen mode

Try to run it in your console.

swipl business_logic.pl
?- have_discount(tom).
true.
Enter fullscreen mode Exit fullscreen mode

You finished the first rule implementation in Prolog.

If you need the rule to be expressed as natural sentences, ChatGPT helps you much.

You

have_discount(X) :- have_members_card(X, yes), discount_received(X, no).

Please translate the rule into English sentences.
Enter fullscreen mode Exit fullscreen mode

ChatGPT

The rule "have_discount(X) :- have_members_card(X, yes), discount_received(X, no)" can be translated into English as follows:

"If a person (represented by 'X') has a membership card and has not yet received a discount, then they have a discount."
Enter fullscreen mode Exit fullscreen mode

Hope you have a great time working with the Prolog programming language!

Top comments (0)