Live data from Hacker News

Use Prolog to improve LLM's reasoning

shchegrikovich.substack.com

81–90 of 158 posts

Re: Use Prolog to improve LLM's reasoning

#81
post #32
post #14

i've come to appreciate, over the past 2 years of heavy Prolog use, that all coding should be (eventually) be done in Prolog. It's one of few languages that is simultaneously a standalone logical formalism, and a standalone representation of computation. (With caveats and exceptions, I know). So a Prolog program can stand in as a document of all facts, rules and relations that a person/organization understands/declar…

Is it your thought that for the average programmer Prolog is easier to read and maintain than say Go, C#, or Java?

As someone that went through a degree where Prolog and LP was cherisched, I would say yes, however LP might be even weirder to start into than even FP.

Many folks on our degree couldn't be happier when they didn't had to see Prolog ever again, while me and others went on to take our chances on the national LP challenge across universities.

Tarski's World was a good way back then to dive into what LP is all about, without being programming language specific.

https://www.gradegrinder.net/Products/tw-index.html

Re: Use Prolog to improve LLM's reasoning

#82
post #57

Earlier quoted context omitted.

> over the past 2 years of heavy Prolog use Oh, cool. Mind if I pick your brain a bit? Recently, there was an HN post[0] of a paper that makes a case against pure logic languages in favor of "functional logic" ones, which they exhibit with Curry[1]. The setup argument is that Prolog's specs backtracking, which strongly downlimits it from full SLD resolution, causing fatally sharp edges in real world usage. Being fair…

So first, let's keep in mind that with no execution model, Prolog is still a "syntax" for Horn clauses. It's still a way to document knowledge. Add SLD resolution and we can compute. The paper (intentionally I presume) orders clauses of a simple predicate to illustrate (cause) a problem in Prolog. But what I actually find is the more time spent in Prolog, the more natural it is to express things in a way that is clea…

> The Art of Prolog is a classic "must have".

I figured it would be a good introduction to prolog, but to date there doesn't seem to be any prolog interpreter that lets me copy the things in the book to play with them?

Re: Use Prolog to improve LLM's reasoning

#83
post #45

Earlier quoted context omitted.

Prolog (and logic programming in general) is much older than you think. In fact, if we take modern functional programming to have been born with John Backus' Turing Award presentation[1], then it even predates it. Many advancements to functional programming were implemented on top of Prolog! Erlang's early versions were built on top of a Prolog-derived language who's name escapes me. It's the source of Erlang's unfam…

Did you just answer me with chatgpt?

> top of a Prolog-derived language who's name escapes me

are you saying we've made a huge leap in LLMs - that they can now admit when they don't know something?

Re: Use Prolog to improve LLM's reasoning

#84
post #36

Earlier quoted context omitted.

So why Prolog in particular and not another logic language like Mercury or Oz/Mozart etc?

"Prolog" is like Lisp, a wide array of superficially similar languages that actually are quite diverse. Mind you, in that sense, Java and C# are more or less the same language, which has Prolog programmers nodding their heads and Java and C# developers screaming.

Nope. Prolog is an ISO-standardized language since 1995 and the spec was updated in 2012. Where older "legacy" Prolog implementations such as SWI, YAP, and SICStus are deviating from the standard is generally pretty well-known to Prolog practitioners, and the convener of ISO 13211 actually can verify claims of ISO conformance; for example, [1] is a link to the ISO certification of Quantum Prolog (the web app at [2]).

It's true however that people are quick to conflate Prolog with constraint-logic programming libs, "expert systems" (RETE-style forward-chainging systems and other "rule engines"), or random "functional-logic" programming languages. The misunderstanding of Prolog and logic by Lisp programmers has been ongoing since the 1980s, probably because at one point Prolog and Lisp were seen as competing "languages for AI" for some reason even though they have very little in common.

[1]: http://www.complang.tuwien.ac.at/ulrich/quantum-prolog/

[2]: https://quantumprolog.sgml.net

Re: Use Prolog to improve LLM's reasoning

#85
post #20

Earlier quoted context omitted.

It's taken ages for anything from functional programming to penetrate general use. Do you think uptake of logic stuff will be any faster?

Prolog (and logic programming in general) is much older than you think. In fact, if we take modern functional programming to have been born with John Backus' Turing Award presentation[1], then it even predates it. Many advancements to functional programming were implemented on top of Prolog! Erlang's early versions were built on top of a Prolog-derived language who's name escapes me. It's the source of Erlang's unfam…

> Erlang's early versions were built on top of a Prolog-derived language who's name escapes me.

AFAIK, Erlang was originally implemented in Prolog and the original VM was inspired by the Warren Abstract Machine targeted by some Prolog implementations. It was also inspired by PLEX, but PLEX wasn't a Prolog derivative.

Re: Use Prolog to improve LLM's reasoning

#87
post #36

Earlier quoted context omitted.

"Prolog" is like Lisp, a wide array of superficially similar languages that actually are quite diverse. Mind you, in that sense, Java and C# are more or less the same language, which has Prolog programmers nodding their heads and Java and C# developers screaming.

Nope. Prolog is an ISO-standardized language since 1995 and the spec was updated in 2012. Where older "legacy" Prolog implementations such as SWI, YAP, and SICStus are deviating from the standard is generally pretty well-known to Prolog practitioners, and the convener of ISO 13211 actually can verify claims of ISO conformance; for example, [1] is a link to the ISO certification of Quantum Prolog (the web app at [2]).…

[deleted]

Re: Use Prolog to improve LLM's reasoning

#89
While the paper is about helping an LLM using Prolog, to give an idea for a realistic application for generating Prolog, here's the zero-shot prompt and response of a basic local Instruct LLM on a challenge to a classic 1998 AI Planning Competition "Logistics" problem re-formulated in English from PDDL, running on a very basic local 8B LLM quant'd to run on 12GB and containing even typos, but not showing the system prompt portion though. I mean, it's quite impressive and the generated state and action predicates can be run right away in the browser on eg. Quantum Prolog much like the container planning problem [1], with the main clause replaced by a generic STRIPS-like indeterministic/backtracking planning routine. Using additional training and/or in-context techniques and prompt reformulation can improve the result further.

Since the action predicates use retract/assert primitives to change state, Quantum Prolog's vardb feature [2] can also be put to good use by transforming the resulting program into an equivalent form that can be backtracked over, even in parallel, to perform combinatorical search (whereas assertz/retract in Prolog is destructive and cannot be "undone" automatically to search new states).

But as you also can see from the plan description text, a natural-language description of the problem that an LLM can comprehend only helps so much since it's highly repetitive in stating hundreds of facts about eg. current and desired package locations. This is where Prolog comes in since you can state those ground facts in clause format, or a DSL format, or a database (as in realistic applications where Prolog facts are generated or Prolog is deployed alongside a backend for integration with other services for eg. messaging in a a logistics app).

Still, the combination of an LLM (excelling at language tasks) and Prolog (for actually searching a combinatorical space spanned by clauses extracted in a natural way), just as a human reach out to a pocket calculator for certain tasks, sure is powerful and has been also explored recently in one form or another in academia eg [3] also referenced elsewhere in this thread. And this isn't even using tool calling and iterative re-prompting, etc.

[1]: https://quantumprolog.sgml.net/container-planning-demo/part1...

[2]: https://quantumprolog.sgml.net/container-planning-demo/part2...

[3]: "LLMs Can't Plan, But Can Help Planning in LLM-Modulo Frameworks" at https://arxiv.org/abs/2402.01817>

PROMPT (submitted as-is, including typos):

Write a Prolog program to solve the following logistics problem: Packages must be transported from and to locations inside the six cities named city1, city2, city3, city4, city5, and city6) using trucks, and between those citiesusing airplanes. Each city has two locations: city1 has locations city11 and city12, city2 has locations city21 and city22, city3 has locations city31 and city32, city4 has locations city41 and city42, city5 has locations city51 and city52, andcity6 has locations city61 and city62. Initially, package1 is located at city21, package2 is located at city12, package3 is located in city11, package4 is located in city11, package5 is located in city42, and package6 is located in city31. The aim is to move package1 to city21, package2 to city62, package3 to city61, package4 to city32, package5 to city62, and package6 to city12. In each city, at the locations city11, city21, city31, city41, city51, and city61, there's a truck to transport packages to other locations within the city. There are moreover two airplanes, named plane1 and plane2, located at the airport of city1 at location11 and at the airport of city2 at location22. The airports of the other cities have the locations location32 in city3, location42 in city4, location52 in city5, and location62 in city6. To move packages, a sequence of actions as follows must be performed until the packages have reached their destination. The action load_truck will place a package in a truck at a given location. The action unload_truck will unload a package from a truck at a given location. The action fly_airplane will fly an airplane from one airport to another. The action unload_airplane will unload a package from an airplane at a given location. The action load_airplane will load an airplane with a package at a given location.

RESPONSE:

    % State facts
    
    % Initial state of the packages
    package(package1, city21).
    package(package2, city12).
    package(package3, city11).
    package(package4, city11).
    package(package5, city42).
    package(package6, city31).
    
    % Initial state of the trucks
    truck(city11, city11).
    truck(city21, city21).
    truck(city31, city31).
    truck(city41, city41).
    truck(city51, city51).
    truck(city61, city61).
    
    % Initial state of the airplanes
    airplane(plane1, city11).
    airplane(plane2, city22).
    
    % Action clauses
    % load_truck :- load a package into a truck at a given location
    load_truck :- 
        % If the given location is valid for loading a truck
        location(Loc, City), 
        package(Pkg, Loc), 
        truck(T, Loc),
        % Remove the package from its current location
        retract(package(Pkg, Loc)), 
        % Add the package to the truck
        assertz(package(Pkg, T)), 
        % Assert the new state
        assertz(truck(T, Loc)).
    
    % unload_truck :- unload a package from a truck at a given location
    unload_truck :-
        % If the given location is valid for unloading a truck
        location(Loc, City), 
        package(Pkg, T), 
        truck(T, Loc),
        % Remove the package from the truck
        retract(package(Pkg, T)), 
        % Add the package to its destination location
        assertz(package(Pkg, Loc)),
        % Assert the new state
        assertz(truck(T, Loc)).
    
    % fly_airplane :- fly an airplane from one airport to another
    fly_airplane :-
        % If the airplane is at a valid airport
        airplane(plane, Loc),
        % If there is a valid destination airport
        airport(Dest, DestCity),
        % Remove the airplane from its current location
        retract(airplane(plane, Loc)), 
        % Add the airplane to the destination airport
        assertz(airplane(plane, Dest)).
    
    % unload_airplane :- unload a package from an airplane at a given location
    unload_airplane :-
        % If the given location is valid for unloading an airplane
        location(Loc, City), 
        package(Pkg, A),
        airplane(plane, A),
        % Remove the package from the airplane
        retract(package(Pkg, A)), 
        % Add the package to its destination location
        assertz(package(Pkg, Loc)), 
        % Assert the new state
        assertz(airplane(plane, Loc)).
    
    % load_airplane :- load a package into an airplane at a given location
    load_airplane :-
        % If the given location is valid for loading an airplane
        location(Loc, City), 
        package(Pkg, Loc),
        airplane(plane, A),
        % Remove the package from its current location
        retract(package(Pkg, Loc)),
        % Add the package to the airplane
        assertz(package(Pkg, A)),
        % Assert the new state
        assertz(airplane(plane, Loc)).
    
    % Target state
    target :-
        package(package1, city22),
        package(package2, city62),
        package(package3, city61),
        package(package4, city32),
        package(package5, city62),
        package(package6, city12).
    
    % Main predicate to solve the problem
    solve :- ...
    % BOGUS MAIN AND HELPER CLAUSES OMITTED

Re: Use Prolog to improve LLM's reasoning

#90

ChatGPT seems to do awful with Prolog. Do you guys have any experiences with using an LLM to write Prolog?

See my top-level comment for an example. Though different, there's also casual experimentation described in [1] (and additional posts linking to academic research on the SWI Prolog forum).

Do you mind sharing your experience with ChatGPT (which version)?

[1]: https://swi-prolog.discourse.group/t/chatgpt-prompts-prolog-...

Post reply on HN