Live data from Hacker News

The Power of Prolog

metalevel.at

51–60 of 164 posts

Re: The Power of Prolog

#51
post #4

Never understood why natural numbers (or some set of N) is not in the search space: fib(X,X):- X fib(X,Y1+Y2):- fib(X-1,Y1),fib(X-2,Y2). I tried to get answer to this kwestion in reddit, but all I got was personal insults.

You can do this in SWI Prolog using the between/3 predicate for the inequality, and the is/2 predicate for the arithmetic.

When I tried out Prolog, I was struck by the inelegance of is/2. There has to be something like it, because no one knows how to write an interpreter for

    arcsin(X,Y) :- sin(Y,X).
However, MetaPost can do that for linear expressions. You can say (in Prolog syntax)

    midpoint(X,Y,Z) :- Z == (X+Y)/2.
    two(X) :- midpoint(0,X,1).
And the MetaPost interpreter will find the solution two(2).

Does anyone know of Prolog extensions that can solve linear algebra problems like that?

Re: The Power of Prolog

#52
post #2

Roughly half the 'power of prolog' comes from the 'power of logic programming' and prolog is by far not the only logic programming language, e.g., - You can do logic programming using minikanren in scheme. (you can also extend the minikanren system if you find a feature missing). - Minikanren was implemented in clojure and called core.logic. - It was also ported to python by Matthew Rocklin I think, called logpy. - T…

Not really. I use SWI Prolog for a lot of personal projects (that actually see QPS no less) and there's a lot more to it than that. SWI gives you: good debugging support (with trace and spy), hooks into the Prolog database (with asserta/z and retract), optimized implementations of difference lists, online help, and so much more. Don't even get me started on its amazing DCG support that makes Regex feel like a Neolith…

How can you use DCG in a productive way? If haven't found a good way when there is left recursion. Memonization(tabbling) only sometimes helps and refactoring the grammar into non-left-recursive takes a lot of time and is error prone.

What's your solution?

Re: The Power of Prolog

#54
post #51

Earlier quoted context omitted.

You can do this in SWI Prolog using the between/3 predicate for the inequality, and the is/2 predicate for the arithmetic.

When I tried out Prolog, I was struck by the inelegance of is/2. There has to be something like it, because no one knows how to write an interpreter for arcsin(X,Y) :- sin(Y,X). However, MetaPost can do that for linear expressions. You can say (in Prolog syntax) midpoint(X,Y,Z) :- Z == (X+Y)/2. two(X) :- midpoint(0,X,1). And the MetaPost interpreter will find the solution two(2). Does anyone know of Prolog extensions…

SWI-Prolog has CLP extensions to do exactly that: http://www.swi-prolog.org/pldoc/man?section=clp

    ?- use_module(library(clpq)).
    midpoint(X, Y, Z) :- {Z =:= (X+Y)/2}.
    two(X) :- midpoint(0, X, 1).
does what you want.

Re: The Power of Prolog

#55
post #53

The Japanese government spent US $400 million in the '80's (a lot in those days) to try to jump ahead of "western" computer technology via its "5th Generation Project". https://news.ycombinator.com/item?id=14047780 The basis for it all ... Prolog.

> Japanese Government said this week that it was willing to give away the software developed by the project to anyone who wanted it, even foreigners.

Was wondering what they released as a result. Surely after a $400 in 80's money they had some lines of code written.

Apparently they focused on concurrent logic programming and wasted a lot of money on exotic hardware in https://en.wikipedia.org/wiki/Fifth_generation_computer by the time they were done off-the-shelf CPUs could run circles around the fancy workstations.

Re: The Power of Prolog

#56
post #5

erlang syntax is prolog inspired, not sure if that's a good thing though :)

I like it. There are some annoyances to do with minor syntactical warts, like commas and semicolons, but all in all, I find it expressive.

Re: The Power of Prolog

#57

i have always trouble understaing prolog, lot of guide online seem to just tackle the syntax or assume you already know a lot of logic programming, for example the first example in the link ( https://www.metalevel.at/prolog/facets ): list_length([], 0). list_length([_|Ls], N) :- N #> 0, N #= N0 + 1, list_length(Ls, N0). i don't undestand it, i read the segment describing it multiple time but i still don't get it, and…

Couple of weird things about Prolog: It's a pattern-matcher, in much the same way Haskell is -- but Prolog invented it first. Thus the first rule tries to match the args you give it, and if you give it the literals [] and 0, it just says "True" or "Yes" (depending on which Prolog you're using). If you give it something it cannot match to [] and 0, it will try the second clause and assume the first arg is a list with a head and a tail, and the second arg is an integer.

The other weird thing is that Prolog functions can often run backwards. If you ask Prolog

?- list_length(Foo, 4)

it will hand you back a list of 4 elements that it just made up on the spot. This is why Prolog rules are considered generalizations of functions: They don't have explicit inputs vs. outputs. They're relations, not functions.

This stuff blew my mind when I first encountered Prolog and it changed forever how I thought about programming.

Re: The Power of Prolog

#58

i have always trouble understaing prolog, lot of guide online seem to just tackle the syntax or assume you already know a lot of logic programming, for example the first example in the link ( https://www.metalevel.at/prolog/facets ): list_length([], 0). list_length([_|Ls], N) :- N #> 0, N #= N0 + 1, list_length(Ls, N0). i don't undestand it, i read the segment describing it multiple time but i still don't get it, and…

Couple of weird things about Prolog: It's a pattern-matcher, in much the same way Haskell is -- but Prolog invented it first. Thus the first rule tries to match the args you give it, and if you give it the literals [] and 0, it just says "True" or "Yes" (depending on which Prolog you're using). If you give it something it cannot match to [] and 0, it will try the second clause and assume the first arg is a list with…

The reversibility of Prolog computations was a big eye opener for me too back when I first encountered it. I recently wrote a post about relational programming using Prolog and Mozart/Oz as examples https://bluishcoder.co.nz/2017/03/20/relational-programming-...

Another interesting language in the same domain is Picat http://picat-lang.org/

Re: The Power of Prolog

#59
post #36
post #30

Earlier quoted context omitted.

It's no different than: public class LinkedList { ... public int size() { if (head == null) { return 0; } else { int n0 = 1 + head.size(); } } Actually, it is different. In prolog, you don't tell it how to compute an answer. You tell it what the answer is, and it figures out the rest. In the previous example, I'd phrase it more like this: I am sure [] has 0 length. I would be sure [_:Ls] has N length, if I was sure N…

Why on earth doesn't LinkedList already have a size() method? I'm thinking of typed languages, I guess, but surely any sane collection type knows how to perform addition/concatenation with other instances of its own type, right?

There is a built in function for it in Prolog. But when it's presented as an example of how to implement it in the same way that list classes are often used as examples in OO languages even if they language implementation includes one.

Re: The Power of Prolog

#60
Can anyone give examples of the kinds of problems prolog is ideally suited to? I took a course on it at university. It looked interesting but I didn't really "get it". It might be worth another look now I have a bit more experience under my belt. I've got a lingering feeling it would solve a certain kind of problem very easily.
Post reply on HN