←back to thread

Learn Prolog Now

(lpn.swi-prolog.org)
205 points rramadass | 10 comments | | HN request time: 0.662s | source | bottom
1. jackallis ◴[] No.45901786[source]
is prolog a use-case language or is it as versatile as python?
replies(4): >>45901913 #>>45901945 #>>45902960 #>>45902978 #
2. zimpenfish ◴[] No.45901913[source]
In theory, it's as versatile as Python et al[0] but if you're using it for, e.g., serving bog-standard static pages over HTTP, you're very much using an industrial power hammer to apply screws to glass - you can probably make it work but people will look at you funny.

[0] Modulo that Python et al almost certainly have order(s) of magnitude more external libraries etc.

replies(1): >>45902061 #
3. qsort ◴[] No.45901945[source]
Python wins out in the versatility conversation because of its ecosystem, I'm still kinda convinced that the language itself is mid.

Prolog has many implementations and you don't have the same wealth of libraries, but yes, it's Turing complete and not of the "Turing tarpit" variety, you could reasonably write entire applications in SWI-Prolog.

replies(1): >>45902068 #
4. bigfishrunning ◴[] No.45902061[source]
> you can probably make it work but people will look at you funny

Don't threaten me with a good time

5. WillAdams ◴[] No.45902068[source]
Right, Python is usually the second-best choice for a language for any problem --- arguably the one thing it is best at is learning to program (in Python) --- it wins based on ease-of-learning/familiarity/widespread usage/library availability.
replies(3): >>45902474 #>>45902667 #>>45902769 #
6. aeonik ◴[] No.45902474{3}[source]
More like 3rd to 5th best is most categories. There's just a lot of categories.

Its ease of use and deployment give it a lot more staying power.

The syntax is also pretty nice.

7. ecshafer ◴[] No.45902667{3}[source]
I don't know if I would say its second-best. It just happened to get really popular because it has relatively easy syntax, and Numpy is a really great library making all of those scientific packages that people were using Fortran and C++ for before available in an easier language. This boosted the language, right when data science became a thing, right when dynamic programming became popular, right when there was a boost in Learn 2 Code forget about learning fundamentals was a thing. Its an okay language I guess, but I really think it was lucky that Numpy exists and Numby or Numphp.
8. ux266478 ◴[] No.45902769{3}[source]
Personally I find Python more towards the bottom of the list with me, despite being the language I learned on. Especially if the code involved is "pythonic". Just doesn't jive with my neurochemistry. All the problems of C++ with much greater ambiguity, and I've never really been impressed with the library ecosystem. Yeah there's a lot, but just like with node it's just a mountain of unusably bad crap.

I think lua is the much better language for a wide variety of reasons (Most of the good Python libraries are just wrappers around C libraries, which is necessary because Python's FFI is really substandard), but I wouldn't reach for python or lua if I'm expecting to write more than 1000 lines of code. They both scale horribly.

9. rramadass ◴[] No.45902960[source]
FWIK; You can't compare the two. Python is far more general and larger than Prolog which is more specialized. However there have been various extensions to Prolog to make it more general. See Extensions section in Prolog wikipedia page - https://en.wikipedia.org/wiki/Prolog#Extensions Eg. Prolog++ - https://en.wikipedia.org/wiki/Prolog%2B%2B to allow one to do large-scale OO programming with Prolog.

Earlier, Prolog was used in AI/Expert Systems domains. Interestingly it was also used to model Requirements/Structured Analysis/Structured Design and in Prototyping. These usages seems interesting to me since there might be a way to use these techniques today with LLMs to have them generate "correct" code/answers.

For Prolog and LLMs see - https://news.ycombinator.com/item?id=45712934

Some old papers/books that i dug up and seem relevant;

Prototyping analysis, structured analysis, Prolog and prototypes - https://dl.acm.org/doi/10.1145/57216.57230

Prolog and Natural Language Analysis by Fernando C. N. Pereira and Stuart M. Shieber (free digital edition) - http://www.mtome.com/Publications/PNLA/pnla.html

The Application of Prolog to Structured Design - https://www.researchgate.net/publication/220281904_The_Appli...

10. jodrellblank ◴[] No.45902978[source]
Do you mean Northern Conservative Baptist Great Lakes Region Council of 1879 standard Prolog?[2]

SWI Prolog (specifically, see [2] again) is a high level interpreted language implemented in C, with an FFI to use libraries written in C[1], shipping with a standard library for HTTP, threading, ODBC, desktop GUI, and so on. In that sense it's very close to Python. You can do everyday ordinary things with it, like compute stuff, take input and output, serve HTML pages, process data. It starts up quickly, and is decently performant within its peers of high level GC languages - not v8 fast but not classic Java sluggish.

In other senses, it's not. The normal Algol-derivative things you are used to (arithmetic, text, loops) are clunky and weird. It's got the same problem as other declarative languages - writing what you want is not as easy as it seemed like it was going to be, and performance involves contorting your code into forms that the interpreter/compiler is good with.

It's got the problems of functional languages - everything must be recursion. Having to pass the whole world state in and out of things. Immutable variables and datastructures are not great for performance. Not great for naming either, temporary variable names all over.

It's got some features I've never seen in other languages - the way the constraint logic engine just works with normal variables is cool. Code-is-data-is-code is cool. Code/data is metaprogrammable in a LISP macro sort of way. New operators are just another predicate. Declarative Grammars are pretty unique.

The way the interpreter will try to find any valid path through your code - the thing which makes it so great for "write a little code, find a solution" - makes it tough to debug why things aren't working. And hard to name things, code doesn't do things it describes the relation of states to each other. That's hard to name on its own, but it's worse when you have to pass the world state and the temporary state through a load of recursive calls and try to name that clearly, too.

This is fun:

    countdown(0) :-
      write("finished!").   

    countdown(X) :-
      writeln(X),
      countdown(X-1).
    
It's a recursive countdown. There's no deliberate typos in it, but it won't work. The reason why is subtle - that code is doing something you can't do as easily in Python. It's passing a Prolog source code expression of X-1 into the recursive call, not the result of evaluating X-1 at runtime. That's how easy metaprogramming and code-generation is! That's why it's a fun language! That's also how easy it is to trip over "the basics" you expect from other languages.

It's full of legacy, even more than Python is. It has a global state - the Prolog database - but it's shunned. It has two or three different ways of thinking about strings, and it has atoms. ISO Prolog doesn't have modules, but different implementations of Prolog do have different implementations of modules. Literals for hashtables are contentious (see [2] again). Same for object orientation, standard library predicates, and more.

[1] https://www.swi-prolog.org/pldoc/man?section=foreign

[2] https://news.ycombinator.com/item?id=26624442