Live data from Hacker News

Ask HN: What are some interesting examples of Prolog?

news.ycombinator.com

41–50 of 64 posts

Re: Ask HN: What are some interesting examples of Prolog?

#43

At work we modelled our role-based access control logic in prolog. Once you realise that your authorization is logic programming, you gain access to better tools, more formal ways to think about it, and a treasure trove of literature. :)

I would love to read more about this.

Re: Ask HN: What are some interesting examples of Prolog?

#46
Prolog is a programming language that was developed in the 1970s. It is a logic-based programming language, which is pretty different from your typical programming language. Instead of writing functions and then having the computer execute them, Prolog lets you write rules and it will try to deduce the answers to your questions.

It's a pretty wild concept, and there are tons of cool examples of what Prolog can do. Some examples are:

- A solver for Suduko puzzles: https://rosettacode.org/wiki/Sudoku#Prolog

- A simple game where you guess a person's age: https://rosettacode.org/wiki/Guess_the_number#Prolog

- A family tree program: https://rosettacode.org/wiki/Family_tree#Prolog

Re: Ask HN: What are some interesting examples of Prolog?

#47

At work we modelled our role-based access control logic in prolog. Once you realise that your authorization is logic programming, you gain access to better tools, more formal ways to think about it, and a treasure trove of literature. :)

I would love to read more about this.

Somewhat more widespread, the Open Policy Agent https://www.openpolicyagent.org/> uses essentially a subset of prolog written in form more familiar to most programmers - but while the code looks more familiar, the semantics are derived from Prolog.

Re: Ask HN: What are some interesting examples of Prolog?

#48
TerminusDB CTO here.

Echoing what triska said, CLP(ℤ) and friends are some of the most under-appreciated aspects of prolog implementations.

I'm amazed that programmers still don't have access to CLP when trying to do scheduling and planning solutions.

As an example in practice, what if you want to know about a transaction in which a number of entities transitively had holdings in one of the beneficiaries of the transaction at that particular time. The date window is not known, and the date windows are important in the ownership chain as well as the transactions that are being undertaken.

With CLP(FD) you can ask for a window of time, and the solution will zoom in on an appropriate time window which exists for the entire chain and match the time of the transaction.

Now try to do this query in SQL. It's almost impossibly hard.

I can't wait until I have the time to implement constraint variables for TerminusDB, but at the minute we are still working on more prosaic features.

Aside from that there are very interesting program correctness and optimisation systems which are based on prolog (usually a datalog). For instance Soufflé: https://souffle-lang.github.io

Re: Ask HN: What are some interesting examples of Prolog?

#49
I wrote commercial Prolog applications circa 1990 and it is the nicest programming language that I know (I used over 30 in my 50 year career); I recommend the book The Art of Prolog, by Ehud Shapiro. Also the idea that with Prolog, you can easily write programs that write programs (I wrote a few of those) and that it may be feasible to scale up a program to fill in the code to match input and output descriptions, and build possibly unlimited complex logic on top of simpler components, as I think Shapiro was working on this approach in the 1990's for the Israeli gov., before the project went under the covers, it seems.

Re: Ask HN: What are some interesting examples of Prolog?

#50
Here's an example for a problem that's moderately difficult to do recursively, hard to do iteratively, and trivial in Prolog:

Given that you have the following coins, 1¢ (i.e. 1 cent or $0.01), 5¢, 10¢, 25¢, 50¢, how many different ways are there to give change for a dollar? For example, one way would be to give 2x50c, another would be to give 100x1c. How many are there total?

In most languages this is not super easy to do unless you're very comfy with recursion.

In Prolog the solution is basically (pseudocode):

    100 = a*50 + b*25 + c*10 + d*5 + e*1
and Prolog will just figure out all possible combinations for you.

The challenge with Prolog as far as I understand is making things fast.

Post reply on HN