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!

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up