r/prolog • • Mar 24 '26

Did I accidentally discovered that GPT reasons like a Prolog interpreter — while using Prolog?

1 Upvotes

While reading the answers GPT-5.4 (Codex) gave while working on my Prolog-based transpiler (UnifyWeaver), I noticed GPT appeared to be doing adaptive programming. Compared to Claude this was frustratingly slow, but also seemed very powerful! After discussing this with Perplexity; here is what we concluded. Does this seem accurate, or did the AI lead me astray?

Title: GPT's chain-of-thought reasoning is structurally isomorphic to Prolog proof search — and it shows most clearly when you're using Prolog

I've been building a multi-language compiler called UnifyWeaver — principally written in Prolog — and recently had a moment of clarity while watching GPT-5.4 work through a hard generalization problem.

The model was auditing the compiler's handling of mutually recursive predicates (SCCs — Strongly Connected Components in the call graph). Instead of immediately proposing a general solution, it:

Confirmed which SCC shapes already worked

Probed the next narrowest candidate

Only moved to a new shape after confirming the current one passed

Stopped the moment it found the first real failing case

This is exactly how Prolog search works. A Prolog interpreter commits to the most constrained clause first, only generalizing (backtracking) when a guard fails. GPT's audit loop was doing the same thing — systematically falsifying the frontier rather than guessing at a global fix.

But here's where it gets interesting.

Prolog isn't just an analogy for GPT's methodology. In this case, GPT was using Prolog to represent the solution state space itself. Each test case was a Prolog clause. Each pass/fail was a unification succeeding or failing. The boundary it was searching for — whether the compiler could handle a computed context-step between mutual recursive calls — was expressed as a Prolog predicate, tested by a Prolog harness, inside a Prolog compiler.

The audit was a meta-level Prolog query over an object-level Prolog system.

Why does the language choice matter?

Prolog's information density is uniquely suited to this kind of work. A single clause can simultaneously encode:

The recursion structure (mutual, arity-2, tree-shaped)

The argument invariants (one context arg, forwarded unchanged)

The branch logic (guarded, with inter-call state)

The termination shape (base case by pattern match)

The same specification in Python would require 5–10× more code. If error probability scales even loosely with code length, Prolog's conciseness isn't just aesthetic — it materially reduces the surface area for bugs at each generalization step.

The deeper connection: Tarjan's algorithm

Tarjan's SCC algorithm works by DFS-exploring a graph, processing sink components first (those with no outgoing edges to unprocessed nodes), and only widening the search when a component is fully confirmed. GPT's audit loop is structurally identical — it processed the simplest mutual-tree shapes (call_guard, call_alias, post_call_guard) before identifying ctx_step as the true frontier, exactly as Tarjan would process a sink SCC before its callers.

This isn't a coincidence. LLM chain-of-thought on graph-structured problems has been formalized as Graph-CoT (Graph Chain-of-Thought), where each reasoning step corresponds to a single node traversal. GPT's session was a Graph-CoT traversal of the SCC frontier of my compiler's call graph.

The contrast with Claude

Claude tends toward immediate synthesis — it infers a general solution and applies it directly. That's faster for problems with recognizable structure. GPT's approach is frustratingly slower but produces a falsified boundary map: you know exactly which shapes are handled and which aren't, rather than a single generalization that might silently break adjacent cases.

For hard, structurally novel problems — especially ones where the failure space is subtle — the slower approach wins.


r/prolog • • Jun 30 '22

What is Wrong with Prolog People

0 Upvotes

This is a reply from SO comment, I don't know what it meant, maybe someone can clarify:

> You are still forcefully inserting in your code a lot of irrelevant and useless stuff, the

> double_quotes

> flag,

> library(reif)

> , assertz and retracts. I don't think you are ready to be asking good questions. I would strongly recommend going to a different forum (reddit or maybe the swi-prolog discourse group) where it is easier to get advice. SO is not well suited for such questions. – TA_intern 59 mins ago

>

>

Here's the accompaniyting question>

Program enters a repl that accepts user input, prints state, and returns back to repl.

`:| go , prints the position.`<br/>

`:| position <fen> , sets the position to <fen>.`<br/>

`:| quit , exits the program.`<br/>

`:| any other input , ignores.`<br/>

I tried to do the quit part, congratulations Prolog you win. You achieved your goal.

:- set_prolog_flag(double_quotes, chars).

:- use_module(library(reif)).

:- dynamic(brek/1).

take_user :-

assertz(brek(0)),

retract(brek(_)),

assertz(brek(0)),

read_line_to_string(user_input, L),

string_chars(L, Ls),

(phrase(uci, Ls); phrase(no_match(_), Ls)),

brek(B),

format('|~a|', B),

if_(dif(B, break), take_user, format('bye')).

uci --> quit; go; position.

quit --> "quit", {

format('wtf'),

retract(brek(_)),

assertz(brek(break))

}.

position --> {

}.

go --> "go", {

format('go fen ')

}.

no_match([]) --> [].

no_match([X|Xs]) --> [X], no_match(Xs).


r/prolog • • Jun 23 '21

Why my code doesn't work?

0 Upvotes

It returns me false every time I try.

table:-[[#,#,#,#,#,#,#,#,#],[#,@,' ',' ',$,' ',' ',.,#],[#,#,#,#,#,#,#,#,#]].

print_table([]).
print_table([A|B]):-format("~w~w~w~w~w~w~w~w~w",A),nl,print_table(B).

main:-
print_table(table).

r/prolog • • Mar 13 '20

how can i make resource allocation program using prolog

0 Upvotes

how can i make resource allocation program using prolog i am just new at prolog and want some help at the beginning cause i have tried so many times but every time i wont find anything and there is no thing in internet to help me?

i have coded the below code but the problem i want code that take 4 processes and 2 resources and print the execution order of the processes if there is no deadlock and if there is a deadlock print false thanks in advance


r/prolog • • Apr 07 '19

GNU Prolog

0 Upvotes

Hi, guys. I have problems with Prolog. Can anyone help me with this tasks? I'm really retard and can't solve this on GNU Prolog. I know that it is a blatant request but I have a deadline in university so I need some help.

  1. Create a predicate that calculates the number of numbers less than the arithmetic mean of all numbers from file

2)Create a predicate that forms from the text stored in the file a list of words that contain duplicate characters

3)Create a predicate supplementing all the lines stored in the file with the symbol "*" up to the longest line.

I will be very grateful if someone will help me. If you want I can present some game from steam like, for example,The Binding of isaac :)

Sorry for my English


r/prolog • • Mar 14 '16

homework help I'm still learning prolog, and this problem is hard for me, can u guys explain how to do it to me ELI5 style.

0 Upvotes

Create a rule to check for an exam grade.

100 = your grade is A. 80 = your grade is B. 60 = your grade is C.

Others = Your grade for the mark of {the mark} is not in the system.


r/prolog • • Dec 03 '20

Grouping list elements depending on values

0 Upvotes

Hi, i have a list of elements(tuples):
tuples(
[
(a,b,[1,3,5,7]),
(a,b,[9,11,13,15]),
(a,b,[17,19,21,23]),
(c,d,[0,2,4,6]),
(c,d,[8,10,12,14]),
(c,d,[16,18,20,22]),
(e,f,[100,200,300,400]),
(e,f,[500,600,700,800]),
(e,f,[900,1000,1100,1200])
]
).
How to group them so it becomes:
[
(a,b,[1,3,5,7,9,11,13,15,17,19,21,23]),
(c,d,[0,2,4,6,8,10,12,14,16,18,20,22]),
(e,f,[100,200,300,400,500,600,700,800,900,1000,1100,1200])
]
As we can see, we grouped the (a,b) (c,d) (e,f)

thank you for the help.


r/prolog • • Nov 24 '19

homework help [PROLOG] Need help is PEANO NUMERALS

0 Upvotes

Hey guys ! I am very new to Prolog and just started learning it. I need to understand peano numerals.

So here's what I need to accomplish:

 **lstlength([], SIZE) :- SIZE is 0.

lstlength([_|T], SIZE) :- lstlength(T, SMALLER), SIZE is SMALLER+1.**

asking Prolog to assemble a random list of a given size ("lstlength(X,3)") indicates more possible solutions, which runs forever. I need to solve the issue by counting the list's length using Peano Numerals.

And then I need to write a rule to relate integers to Peano Numerals.

Any help is appreciated. Thanks in advance :))))


r/prolog • • Feb 28 '22

help What is this query asking? X = rock, can_lose(Y)

0 Upvotes

Could you please state it in the terms, "Is there any X such that....."


r/prolog • • Sep 08 '11

SWI-Prolog

0 Upvotes

Can someone explain to me how to use SWI-Prolog on a mac? The main objective being, integrating SWI-Prolog to Aquamacs? Plus a link to a good tutorial would be much appreciated.

thanks!


r/prolog • • Jan 21 '23

discussion Help

0 Upvotes

How to recursively delete the elements of a list of the tail part only with the reoccurrences?


r/prolog • • Jan 17 '19

[Beginner] Predicate not called?

Post image
0 Upvotes

r/prolog • • Jan 16 '19

Need help with a Prolog insert into a list predicate

0 Upvotes

this predicate inserts an element is a list organized from small to big.

eg: insert 3 into [1,2,4] becomes [1,2,3,4]

is this correct:

insert(X,[],R,Z):-!.

insert(X,[Y|L],R,0):- X>=Y, insert(X,L,[R|Y],0).

insert(X,[Y|L],R,0):- X<Y, insert(X,L,[R|X,Y],1).

insert(X,[Y|L],R,1):- insert(X,L,[R|Y],1).

question:

?- insert(4,[1,2,5],R,_).

thanks in advance


r/prolog • • Mar 13 '15

Logic advice as I am struggling with c# programming

0 Upvotes

Hi yall I will get to the point so that I don't waste your time.

I'm bad at logic and at problem solving. I am learning c sharp and If it weren't for Google I would struggle with algorithms such as the bubble sort, finding prime numbers, reverse order, etc. Studio know what for loops are, methods, etc. It's just the logic and critical thinking that I struggle with. The problem is that once I really learn c sharp and get interviewed, I won't have Google to help me answer those questions. I'm aware that visual Studio already has stuff such as sum, sort array, etc. But I am interested in the logic behind it itself

I have taken pre calculus and brief calculus which were fairly easy subjects because formulas were given to me and all I had to do was plug in numbers.

Right now there are two books I'm considering to improve logic and problem solving :

How to prove it by vellmen Concrete mathematics by Donald Knuth

Would these be considered too advanced for someone like me considering my limited math background? I really am just a beginner.

I thank you in advance for providing any help you can give.

Thank you


r/prolog • • Mar 13 '20

how can i make resource allocation program using prolog

0 Upvotes

how can i make resource allocation program using prolog iam just new at prolog and iwant some help at the beginning cause i have tried so many times but every time i wont find anything and there is no thing in internet to help me


r/prolog • • Jun 11 '20

Brain Teaser

1 Upvotes

anyone know how to instantiate these:

[3,X] = [ ... , ...]. 

output:

X=3, Y=[3].

=====================================

g(f[a],X,b) = g(... , X).

output:

X=b, Y=[a].

=====================================

output:

X=h(b), Y=h(a,h(b)), Z=g(g,c).


r/prolog • • Jun 08 '21

Can somebody help me solve these errors?

Post image
4 Upvotes

r/prolog • • Jul 05 '26

I want to know if this Claude generated code resemble anything a human Prolog programmer would generate.

4 Upvotes
I am trying to learn Prolog by using Claude to complete a year of Advent of COde, working through various algorithms.  

The problem statements is bewlow along withthe Prolog solution.  Can anyone tell me if the is good code? 


## --- Day 3: Toboggan Trajectory ---


With the toboggan login problems resolved, you set off toward the airport. While travel by toboggan might be easy, it's certainly not safe: there's very minimal steering and the area is covered in trees. You'll need to see which angles will take you near the fewest trees.


Due to the local geology, trees in this area only grow on exact integer coordinates in a grid. You make a map (your puzzle input) of the open squares (`.`) and trees (`#`) you can see. For example:


```text
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#


```


These aren't the only trees, though; due to something you read about once involving arboreal genetics and biome stability, the same pattern repeats to the right many times:


```text
*..##.......*..##.........##.........##.........##.........##.......  --->
*#...#...#..*#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
*.#....#..#.*.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
*..#.#...#.#*..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
*.#...##..#.*.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
*..#.##.....*..#.##.......#.##.......#.##.......#.##.......#.##.....  --->
*.#.#.#....#*.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
*.#........#*.#........#.#........#.#........#.#........#.#........#
*#.##...#...*#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
*#...##....#*#...##....##...##....##...##....##...##....##...##....#
*.#..#...#.#*.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#  --->


```


You start on the open square (`.`) in the top-left corner and need to reach the bottom (below the bottom-most row on your map).


The toboggan can only follow a few specific slopes (you opted for a cheaper model that prefers rational numbers); start by *counting all the trees* you would encounter for the slope *right 3, down 1*:


From your starting position at the top-left, check the position that is right 3 and down 1. Then, check the position that is right 3 and down 1 from there, and so on until you go past the bottom of the map.


The locations you'd check in the above example are marked here with `*O*` where there was an open square and `*X*` where there was a tree:


```text
..##.........##.........##.........##.........##.........##.......  --->
#..*O*#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
.#....*X*..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
..#.#...#*O*#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
.#...##..#..*X*...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
..#.##.......#.*X*#.......#.##.......#.##.......#.##.......#.##.....  --->
.#.#.#....#.#.#.#.*O*..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
.#........#.#........*X*.#........#.#........#.#........#.#........#
#.##...#...#.##...#...#.*X*#...#...#.##...#...#.##...#...#.##...#...
#...##....##...##....##...#*X*....##...##....##...##....##...##....#
.#..#...#.#.#..#...#.#.#..#...*X*.#.#..#...#.#.#..#...#.#.#..#...#.#  --->


```


In this example, traversing the map using this slope would cause you to encounter `*7*` trees.


Starting at the top-left corner of your map and following a slope of right 3 and down 1, *how many trees would you encounter?*


--- Part Two ---
Time to check the rest of the slopes - you need to minimize the probability of a sudden arboreal stop, after all.


Determine the number of trees you would encounter if, for each of the following slopes, you start at the top-left corner and traverse the map all the way to the bottom:


- Right 1, down 1.
- Right 3, down 1. (This is the slope you already checked.)
- Right 5, down 1.
- Right 7, down 1.
- Right 1, down 2.


In the above example, these slopes would find `2`, `7`, `3`, `4`, and `2` tree(s) respectively; multiplied together, these produce the answer `336`.


What do you get if you multiply together the number of trees encountered on each of the listed slopes?

:- module(day03, [parse_input/2, part1/2, part2/2, solve/3,
                  slope_trees/4]).


% Day 3: Toboggan Trajectory.
% The map is a grid of open squares (.) and trees (#) whose pattern
% repeats infinitely to the right. Starting at the top-left and stepping
% Right columns / Down rows at a time, count the trees hit before
% falling off the bottom. Part 1: slope right 3, down 1. Part 2: product
% of the counts over five fixed slopes.


% parse_input(+Raw, -Rows)
% Rows is a list of rows, each a list of char atoms ('.' or '#').
parse_input(Raw, Rows) :-
    split_string(Raw, "\n", " \t\r", Lines0),
    exclude(=(""), Lines0, Lines),
    maplist(string_chars, Lines, Rows).


% slope_trees(+Rows, +Right, +Down, -Count)
% Count is the number of trees on the slope: starting at the top-left,
% visit column I*Right (mod row width — the pattern repeats rightward)
% of every Down-th row, and count the '#' cells.
slope_trees(Rows, Right, Down, Count) :-
    slope_trees_(Rows, Right, Down, 0, 0, Count).


% slope_trees_(+Rows, +Right, +Down, +Col, +Acc, -Count)
% Accumulator walk: check the head row at Col (wrapped), then step to
% the row Down below (drop Down-1 of the remaining rows) at Col+Right.
slope_trees_([], _, _, _, Count, Count).
slope_trees_([Row|Rest], Right, Down, Col, Acc0, Count) :-
    length(Row, Width),
    Index is Col mod Width,
    nth0(Index, Row, Cell),
    (   Cell == '#'
    ->  Acc1 is Acc0 + 1
    ;   Acc1 = Acc0
    ),
    Col1 is Col + Right,
    Skip is Down - 1,
    drop(Skip, Rest, Rest1),
    slope_trees_(Rest1, Right, Down, Col1, Acc1, Count).


% drop(+N, +List, -Rest)
% Rest is List without its first N elements ([] if List is shorter).
drop(0, List, List) :- !.
drop(_, [], []) :- !.
drop(N, [_|Xs], Rest) :-
    N > 0,
    N1 is N - 1,
    drop(N1, Xs, Rest).


% slope_trees_pair(+Rows, +Right-Down, -Count)
% slope_trees/4 with the slope packed as a Right-Down pair, so it can
% be partially applied under maplist/3.
slope_trees_pair(Rows, Right-Down, Count) :-
    slope_trees(Rows, Right, Down, Count).


% part1(+Rows, -Answer)
part1(Rows, Answer) :-
    slope_trees(Rows, 3, 1, Answer).


% part2(+Rows, -Answer)
% Product of the tree counts over the five slopes from the statement.
part2(Rows, Answer) :-
    Slopes = [1-1, 3-1, 5-1, 7-1, 1-2],
    maplist(slope_trees_pair(Rows), Slopes, Counts),
    foldl([X, Acc0, Acc]>>(Acc is Acc0 * X), Counts, 1, Answer).


% solve(+Raw, -Part1, -Part2)
solve(Raw, Part1, Part2) :-
    parse_input(Raw, Rows),
    part1(Rows, Part1),
    part2(Rows, Part2).

r/prolog • • Apr 23 '26

homework help Help me :0, removing every nth of a list in Prolog!

1 Upvotes

Hello once again! i am a college student so behind on work its crippling, however im slowly getting there.

I have a question for one of my computer labs and genuinely cannot figure it out and cannot find anything helpful online, so i came here to ask if anyone could help

The question is "Write a Prolog function, removeEveryNth/3 which removes every nth item from a list."

I have tried a few solutions, using removeEveryNth(List, 1, Ans). as a base case for retuning a empty list. If anyone can help me at all please do i appreciate it so much!


r/prolog • • Jan 02 '23

homework help SLD Resolution Theory vs Prolog

1 Upvotes

Hi everyone. I am preparing for an exam in logic programming, and I am struggling to come up with an answer to a preparatory question which seems simple but I can't seem to come up with an answer. I would appreciate if someone could help me out.

The question asks for a program and a query that generates different results described by theory (SLD-resolution) vs running in prolog. The results should be finite failure in one case and success in the other, using the same selection rule.


r/prolog • • Jun 11 '21

geometric prolog problem

1 Upvotes

Given n rectangular parallelograms with dimensions a1 x b1, a2 x b2, ..., an x bn (all dimensions are positive integers) and a positive integer A, an interesting problem is to decide whether it is possible to place the n rectangular parallelograms inside a square box A x A, without overlaps. The rectangular parallelograms are allowed to rotate. It is easy to see that if there is such a placement, then there is one in which the sides of all the rectangles and the square frame are either perpendicular to each other or parallel to each other. Therefore the rotations that may be required are only 90 degrees.

Write a program in Prolog to solve the above problem. In particular, define a predicate rectanglePacking(L,A), which will hold true if L is a list consisting of terms of the form ai * bi, where ai, bi are positive integers representing the dimensions of a rectangular parallelogram, A is also a positive integer representing the length of the side of the square frame, and the rectangular parallelograms corresponding to the elements of the list L can be placed in the square frame A x A without overlapping. For example, p([2*2, 1*2, 1*1, 1*2], 3) is true

(


r/prolog • • Dec 25 '23

homework help How to do if else but if has two conditions

Post image
0 Upvotes

So I am trying to do the two jugs problem but the jugs have Vx, Vy const size and we need to reach z by pour from y -> x and vice versa, has anyone encountered this problem before ?


r/prolog • • Jan 22 '19

[Beginner] append in a recursion.

0 Upvotes

I want a program that puts the last N items in a list,

So far I have;

returnN(S,[Head|Tail]):-

reverse([Head|Tail],[Head2|Tail2]),

delete_last([Head|Tail],X),

%append in new List Goes here

M is S-1,

returnN(M,X).

Head2 contains the element I want to concatenate with a list throughout each iteration of the recursion. I cant do append([Head2],[L],L). because L is empty in the first iteration. I would appreciate any help, thanks :)


r/prolog • • May 15 '18

For this solution how does findall get all the subsets and what is member used for?

0 Upvotes

subset(_,[]).

subset([H|T],[H|T2]):-

subset(T,T2). subset([_|T],T2):-

subset(T,T2). subset2(S,S2):-

findall(X, subset(S,X), L),

member(S2, L).

member(H,[H|_]).

member(X,[_|T]):-

member(X,T).