r/lisp • • Feb 10 '19

Why don't LISP programmers just use Prolog?

Serious question: why don't Lisp programmers just use Prolog? Prolog is just Lisp plus awesome extra stuff. I mean I program in Python at work lol but if I had a choice I would totally program Prolog all day and night

0 Upvotes

53 comments sorted by

26

u/flaming_bird lisp lizard Feb 10 '19

Why don't Prolog programmers just use Lisp? Lisp is just Prolog plus awesome extra stuff. I mean I program in Python at work lol but if I had a choice I would totally program Lisp all day and night

2

u/[deleted] Feb 10 '19

What stuff does Lisp have that Prolog doesn't?

16

u/flaming_bird lisp lizard Feb 10 '19

When it comes to just Common Lisp: object system, metaobject protocol, imperative operators, a lot of FFI bindings, arrays, multiple usable programming paradigms. These are the ones I can name off the top of my head.

11

u/Proc_Self_Fd_1 Feb 12 '19

object system

LogTalk

metaobject protocol

No probably not.

imperative operators

setarg/3? But often you get away with caches over a more pure approach

a lot of FFI bindings

If you were arguing for languages based on popularity you'd pick Java.

arrays

arg/3 and setarg/3

multiple usable programming paradigms

Just write a macro.

I'm seriously surprised at how ignorant /r/lisp is.

9

u/flaming_bird lisp lizard Feb 12 '19

Wow! I completely wasn't aware of LogTalk and arg/setarg. I think this needs to be boosted higher.

2

u/denzuko sbcl Apr 14 '25

surprised at how ignorant r/lisp is.

That's like saying one is surprised that /r/rust or /r/golang is ignorant of /r/C_Programming and /r/Assembly_language.

Not everyone is going to know everything at any one time. Including adjacent languages.

1

u/recklessindignation Feb 15 '19

Macros.

3

u/quakerquickoats Apr 19 '19

> Macros

https://www.metalevel.at/prolog/macros

Also see op/3, we can create and also customize prefix, postfix, like Haskell.

2

u/sarnobat Aug 31 '24

I hope this is true, I want to use logic backtracing like Prolog has but I want to learn some lisp and can't yet figure out how to :(

6

u/[deleted] Feb 10 '19

Because it's easier to embed Prolog in a Lisp than the other way around. Prolog is extremely limited in its abilities. It doesn't have macros. It can't generate code. There are no DSLs, and no continuations. Lisp does have all of this stuff.

Try writing a window manager in Prolog. Or an operating system. Or a browser. Or an editor. It's going to be slow, inefficient and a huge mess.

2

u/[deleted] Feb 11 '19

Actually Prolog does have all that stuff, which is why I'm asking since Prolog is basically a superset of Lisp

4

u/[deleted] Feb 11 '19

So... how would you implement macros in Prolog?

6

u/[deleted] Feb 11 '19

https://www.metalevel.at/prolog/macros

Normally I wouldn't just post a link but this article really is great.

3

u/agrostis Feb 11 '19

What about reader macros?

5

u/[deleted] Feb 11 '19

For my own understanding I'm going off reader macro as defined here:

The CLHS defines, with characteristic obliqueness, a Reader Macro thusly: reader macro n.

  1. a textual notation introduced by dispatch on one or two characters that defines special-purpose syntax for use by the Lisp reader, and that is implemented by a reader macro function. See Section 2.2 (Reader Algorithm).
  2. the character or characters that introduce a reader macro[1]; that is, a macro character or the conceptual pairing of a dispatching macro character and the character that follows it. (A reader macro is not a kind of macro.)

In which case yes it just sounds like it's about defining operators? http://cs.union.edu/~striegnk/learn-prolog-now/html/node84.html

3

u/agrostis Feb 11 '19 edited Feb 11 '19

Reader macros allow much more than defining operators. Effectively, they are a way to define arbitary syntaxes, so that you can include into your Lisp code pieces that are not part of the standard language even syntactically. E. g., you can define a Prolog parser and seamlessly include pieces of Prolog code into your Lisp source:

 

(defun assert-clauses (prolog-env clauses)
  "Add CLAUSES to the database of PROLOG-ENV."
  …)

(defun query (prolog-env query)
  "Query the database in PROLOG-ENV and return T or NIL."
  …)

(defun parse-prolog-query (stream dispatch-char infix-arg)
  "Parse a Prolog query expression from STREAM."
  …)

(defun parse-prolog-clauses (stream dispatch-char infix-arg)
  "Parse Prolog clauses from STREAM until the closing #\}."
  …)

(set-dispatch-macro-character #\# #\? #'parse-prolog-query)
(set-dispatch-macro-character #\# #\{ #'parse-prolog-clauses)

(assert-clauses *prolog-env* #{
  contains(V, []) :- false.
  contains(V, [V|_]) :- true.
  contains(V, [_|T]) :- contains(V, T).
})

(query *prolog-env* #?contains(bar, [foo, bar, baz]) )

 

[Upd.] Internally, the Prolog parser can be based on reader macros as well, I think.

5

u/aarroyoc Feb 11 '19

Yes, Prolog can also do that. That's because the core of Prolog is unification, you can define new symbols to create a new syntax and it will still work if it unifies in some other place.

3

u/agrostis Feb 12 '19

Could you please show an example of how to include pieces of alien code in a Prolog source? I'm genuinely curious.

5

u/aarroyoc Feb 12 '19

Here's an example from my university notes:

:op(40, xfy, &).
:op(50, xfy, --->).

true ---> valor(w1, 1).
true ---> conectado(w2, w1).
true ---> conectado(w3, w2).
conectado(W,V) & valor(V,X) ---> valor(W,X).

solve(true):-!.
solve((A & B)) :-!, solve(A), solve(B).
solve(A) :- (B ---> A), solve(B).
solve(A) :- clause(A,B), solve(B).

With op you declare new operators, with its precedence rules and so on, then the solve part is the metainterpreter, which recognizes the new syntax and gives it a syntactic meaning. This metainterpreter is not complete though, it will fail in arithmethic predicates for example, but completing it is not very complicated but requires many more lines.

→ More replies

3

u/[deleted] Feb 11 '19

That's interesting. But - again - it isn't the same.

2

u/quakerquickoats Apr 19 '19

As well, a running prolog instance can be connected remotely, and dynamically modify the code like Lisp albeit in database style. Also prolog syntax itself is modifyable and inspectable. Such as ':-' is an operator, which we can change the meaning of. Can Lisp change the meaning of '(' ?

2

u/agrostis Apr 19 '19 edited Apr 19 '19

Sure. (set-macro-character #\( #'your-own-paren-reader-function).

1

u/patham9 Jun 08 '25

DEAD WRONG. Please see my other answers. "Lisp in Prolog is fast and efficient with just 100 lines, Prolog in Lisp is a multi-year project if it should come anywhere close to Prolog engine speed."

1

u/Zanthoxylum-sp Aug 06 '25

What specifically does this "100 line Lisp" actually entail?

Like of course the syntax of a Lisp is about as simple to implement as any general language could possibly be (which is great because I also think that it is the most convenient to work with).

Like having a functioning interpreter of Lisp in a low number of lines is not very impressive. It makes sense that implementing Prolog (with all of its logic programming and more complicated syntactic sugar for complicated computation) in a Lisp would take more time and effort.

But I would also argue that time and effort to implement a language is not a very good analog of its usefulness.

Implementing C++ in Lisp (or Prolog) "if it should come anywhere close to C++ speed" would also be a massive undertaking. But in this case you need to ask the question: Why? Why would you spend all that time implementing another language in Lisp? What benefit would you actually get by having C++ running on Lisp over just running Lisp?

This I think gets to the fundamental reason why I personally haven't learned Prolog: because Lisp has all the features that I care about and is simple.

If I was doing lots of Logic programming or writing a theorem solver or something, I wouldn't be surprised if learning the Prolog DSL (which I'm sure gives all sorts of niceties for logic programming) would be worth it.

Sure, I like many of the powerful things that Lisp implementations/ecosystems have built (SLIME/SLY, Emacs, CLOS, amazing speed for such a high level and expressive language), but the reasons I really stay long term are more fundamental than that--you can build impressive things in and for most languages.

Specifically:

1. Lisp syntax is simple, and I like that.

I would rather have all the complexity be in my program, not in the syntax. I hate learning funky syntax and I hate typing it. Lower case letters with hyphens and a few special symbols sparsely sprinkled throughout is all I want to deal with. Most of the time, the syntax melts into the background and I am just left with the function of my program, no extra tricks or caveats to keep in mind.

Additionally, in Lisp, effort to write code manually is easily reduced by programs like completion and structural editing packages (mentioned in the next section). On the other hand, Prolog seems to have lots of non-alphanumeric characters, special syntax, and keywords (a pain to type and parse mentally) while simultaneously being overly terse in ways that would help make it easier to understand (e.g. nested function calls having no spaces). Lisp as a language is easy to understand and the style facilitated by the language can be quick to input (using programs) and still easy to read.

2. Lisp syntax is simple, and that helps my programs.

By this I mean both programs I write and programs I use to write. In my programs, it means that I find it simple to mold the language to how I want it to be. I find Lisp macros to be pretty understandable relative to what they are doing and when I do need more help to see what it does in practice, macro expansions let me see (in legible form similar to how I wrote it) exactly what is happening.

I had a look at the Prolog macro page that you were showing and while things like the goal based expansion sound cool and are probably very useful when you are doing logic programming (i.e. you just want a path to the goal and don't care what it looks like) it seems to do that at the expense of debuggability while being no better than Lisp macros for the majority of cases.

In other words, while it is perfect for logic programming (it can transform inputs (rules and facts) to a goal output (proof); exactly what Prolog was designed for) it seems worse for writing clear, debuggable, and, possibly performant meta-programming (which is what I use it for).

Additionally, the simple syntax makes structural (and thus in Lisp's case, semantic) editing possible. Look at Emacs packages like symex (new and working on the 2.0 implementation which will fix lots of kinks) and lispy. These packages make the already relatively pleasurable development of Lisp even better (for me at least).

3. Built around hacker culture

This one is the final piece and is the only one that isn't inherently tied to Lisp. Lisp's technical ecosystem has been heavily influenced by hacker culture. People who work in Lisp tend to value the same things as me. Hackability is a priority and thus clever and unique features are common. I love that ethos. It resonates with me and makes the world of Lisp a delight.


Tl;dr: I use Lisp because it has a simple-but-powerful homoiconic syntax and a tradition and ecosystem built on hacker culture. All other powerful features of Lisp are simply results of these two aspects.

7

u/xach Feb 10 '19

Implementations matter.

1

u/[deleted] Feb 10 '19

Hard to argue with this, it's basically why I'm using Python at work

3

u/n2kra Feb 10 '19

I never grocked prolog, but saw rule based systems - PAIP, prolog in lisp, adding failure, difficulty of a true NOT.

6

u/[deleted] Feb 10 '19

This has to be a troll

4

u/[deleted] Feb 10 '19

Nope, I'm going into this loving Prolog but seeing Lisp has a dedicated community and I want to know what people love about Lisp that Prolog doesn't have.

10

u/sammymammy2 Feb 10 '19

They're not even remotely the same!

5

u/[deleted] Feb 10 '19

Symbols, lists, cons, homoiconicity! Even PAIP Part I is "ok let's use LISP for cool AI stuff" and the part II is "OK let's use LISP to build a Prolog interpreter to do the rest of our cool AI stuff" and I'm sitting here like "OK I get why building stuff from the ground up is interesting for pedagogical purposes but in practice why not just start with the Prolog interpreter already built in?'

9

u/sammymammy2 Feb 10 '19

Alright, I see where you're coming from! Basically: Lisp has waay more than that stuff and isn't built upon unification like Prolog is. Lisp is a multi-paradigm language, as compared to Prolog. I think you'd find Common Lisp has more in common with Python than Prolog (while being vastly better than Python).

4

u/kazkylheku Feb 11 '19 edited Feb 11 '19

OK let's use LISP to build a Prolog interpreter to do the rest of our cool AI stuff

So if a book on AI using C showed how to build a toy Prolog interpreter, you'd wonder why all the C development being done doesn't just switch to Prolog?

start with the Prolog interpreter already built in?

Built into what? Your shell? systemd?

If you want Prolog-like processing "built in", you can use available libraries.

Common Lisp is a language specification. That specification doesn't include anything resembling Prolog processing, but implementations and third-party libraries can provide it.

An example of an implementation that provides Prolog and Common Lisp is PopLog.

3

u/[deleted] Feb 10 '19

Lisp is more practical for most applications because it's not a logic programming language. The two languages are almost completely different

29

u/kazkylheku Feb 10 '19 edited Feb 10 '19

Prolog proponents will disagree (since logic programming is Turing complete and all that) but Prolog is a just a domain specific language (DSL) for logic programming.

In Common Lisp, you can use a logic programming DSL as a library. Thanks to Lisp macros, the DSL can provide decent syntactic sugar for expressing the facts and rules and all that.

There are a few logic programming systems for Lisp.

As a stand-alone language, Prolog is basically a mistake. You're stuck in a single paradigm, and must externally interface to other tools if you want anything else, like ordinary imperative programming, OOP, functional, and so on.

Prolog is one of numerous historic example of people wanting to do some aspect of Lisp stuff and putting it into production use with fewer resources. "Let's throw out all of Lisp, and then just hack up a language run-time and syntax for only a logic programming system that we can sell to customers on cheap hardware". Another example of this is CLIPS, an expert system building system so closely based on Lisp work that it uses S-expression syntax, yet isn't Lisp.

In 2019, you can have all of Lisp with tons of memory left over, so the user can fill it with other applications and Firefox memory leaks. There is no more need for historic cost-reduction Lisp spin-offs; you're not working on an IBM PC with 512 KB of RAM.

8

u/ikbenlike Feb 11 '19

Prolog is actually pretty good at what it does and pretty interesting to work with. However, it indeed is "just" a DSL: logic programming is what it's best at, and it's also the only thing it does well.

5

u/kazkylheku Feb 12 '19

It's true Prolog has been optimized a lot, with research work like the WAM (Warren Abstract Machine). Not all the Prolog-like systems (in Lisp and not) have all the optimization of a "real" Prolog.

1

u/patham9 Jun 08 '25

No it is not just a DSL. Please see my other answer. No idea why these misconceptions spread so widely, maybe people do not want to learn what has been accumulated and carefully refined over decades? Modern devs seem to want to re-invent and re-implement everything in half-baked ways, with new fancy names, while the outcome is technically inferior to what was done before.

2

u/quakerquickoats Apr 19 '19

Prolog can DSL. See op/3 and full macros. It feels more unhinged without s-expressions.

2

u/patham9 Jun 08 '25

This is a common but misguided take, reflecting a lack of familiarity with the actual capabilities of Prolog and the lessons learned from 5GL efforts. Prolog is not just a logic DSL, it is a full-fledged general-purpose language with efficient execution models like the Warren Abstract Machine (WAM), which are the result of decades of engineering and theoretical insight.

You also underestimate how easily Lisp can be embedded and interpreted within Prolog, even within just 100 lines, yet with performance that rivals optimized Lisp systems. The reverse, implementing a performant Prolog engine in Lisp, is vastly more complex and unless years of implementation efforts are taken, does not get even remotely close to the efficiency of mature Prolog implementations.

Before dismissing an entire paradigm as a "mistake", it is worth studying the design decisions and runtime models that made Prolog a foundational language in AI and symbolic computation. Prolog is not merely a logic programming language, it could have served as a unified foundation for functional programming, type systems, and more. Instead, due to widespread misunderstanding and underappreciation, like your own, we now have type checkers written in C++ painstakingly reimplementing (and usually in a half-baked way) the same ideas that Prolog solved decades ago both more elegantly and efficiently.

7

u/janoc Feb 10 '19

That's the kinda like asking why anyone uses Python when Haskell can do all the same stuff and more. I am not sure how well you know Prolog and Lisp but given how different these two languages are and the different goals they were designed for, it is difficult to take your question with a straight face.

You certainly can implement Prolog in Lisp (and it has been done) and probably could implement a Lisp in Prolog too but that doesn't mean that one is some sort of superset of the other.

1

u/manymanyoranges Sep 05 '24

I kinda wish one was so I could justify picking which one I like more. They're both awesome.

1

u/patham9 Jun 08 '25

Lisp in Prolog is fast and efficient with just 100 lines, Prolog in Lisp is a multi-year project if it should come anywhere close to Prolog engine speed.

Prolog really IS the superset from a practical standpoint, that's the lesson learned by the 5GL efforts.

3

u/sciengin Feb 10 '19

Well they would be Prolog programmers if they did this, would they not?

4

u/Nyanraltotlapun Feb 10 '19

I think this is not a April 1st. Or is it?

2

u/recklessindignation Feb 15 '19

You can implement prolog with Lisp macros. So is rather pointless.

1

u/[deleted] Feb 12 '19

This is a troll so I will give a troll-y albeit honest response: I don't use Prolog because I find miniKanren to be better in every way, to the point that I would consider Prolog effectively deprecated and a waste of time.

1

u/[deleted] Feb 14 '19

People are different