{"slug": "writing-prolog-with-chatgpt", "title": "Writing Prolog with ChatGPT", "summary": "ChatGPT successfully generated a SWI Prolog program to solve a chess puzzle placing five pieces on a 4x4 board without attacks, finding all 16 solutions. The experiment suggests LLMs can effectively handle Prolog's syntax and logic for real-world constraint problems.", "body_md": "[A few days ago](https://www.johndcook.com/blog/2026/06/11/prolog-claude/) I wrote about using Claude to solve a chess puzzle by writing Prolog code. This morning I tried a [similar chess puzzle](https://www.johndcook.com/blog/2025/05/13/a-crowded-little-chess-puzzle/) with ChatGPT. The task is to place a queen, king, rook, bishop, and knight on a 4 by 4 chessboard so no piece attacks another.\n\nOf course there’s not a lot of market demand for solving chess puzzles, but there is demand for solving real-world problems that amount to logic puzzles. Experiments with chess puzzles suggest writing Prolog with LLM help might be pleasant. The syntax of Prolog can be a little odd, but the LLM handles that. And because Prolog has been around so long without changing much, you’d expect LLMs to be good at generating code in it. [1]\n\nI gave ChatGPT this prompt:\n\nWrite a SWI Prolog program to solve the following chess puzzle. Place a queen, king, rook, bishop, and knight on a 4 by 4 chessboard so no piece attacks another. Print all solutions.\n\nIt produced a file `chess4.pl`\n\n(below) that I ran with\n\n```\nswipl -q -s chess4.pl -g solve -t halt\n```\n\nIt found all 16 solutions:\n\n```\nSolution 1:\n  queen=1-2  king=2-4  rook=3-1  bishop=4-3  knight=4-4\n. K . N\n. . . B\nQ . . .\n. . R .\n\nSolution 2:\n  queen=1-2  king=4-3  rook=2-4  bishop=3-1  knight=4-1\n. R . .\n. . . K\nQ . . .\n. . B N\n\n...\n```\n\nHere’s the code.\n\n```\n% chess4.pl\n% Place queen, king, rook, bishop, and knight on a 4x4 board\n% so that no piece attacks another.\n\n:- use_module(library(lists)).\n\nsquare(X-Y) :-\n    between(1, 4, X),\n    between(1, 4, Y).\n\nsolve :-\n    findall(Board, solution(Board), Boards),\n    print_solutions(Boards, 1),\n    length(Boards, Count),\n    format('Total solutions: ~d~n', [Count]).\n\nsolution(Board) :-\n    Pieces = [queen, king, rook, bishop, knight],\n    findall(S, square(S), Squares),\n    place_pieces(Pieces, Squares, Board),\n    safe(Board).\n\nplace_pieces([], _, []).\nplace_pieces([Piece | Pieces], Squares, [Piece-Square | Board]) :-\n    select(Square, Squares, RemainingSquares),\n    place_pieces(Pieces, RemainingSquares, Board).\n\nsafe(Board) :-\n    safe(Board, Board).\n\nsafe([], _).\nsafe([Piece-Square | Rest], Board) :-\n    forall(\n        member(OtherPiece-OtherSquare, Rest),\n        non_attacking(Piece-Square, OtherPiece-OtherSquare, Board)\n    ),\n    safe(Rest, Board).\n\nnon_attacking(P1-S1, P2-S2, Board) :-\n    \\+ attacks(P1, S1, S2, Board),\n    \\+ attacks(P2, S2, S1, Board).\n\n% Attack rules\n\nattacks(king, X1-Y1, X2-Y2, _) :-\n    X1-Y1 \\= X2-Y2,\n    DX is abs(X1 - X2),\n    DY is abs(Y1 - Y2),\n    DX =< 1,\n    DY =< 1. attacks(knight, X1-Y1, X2-Y2, _) :- DX is abs(X1 - X2), DY is abs(Y1 - Y2), ( DX =:= 1, DY =:= 2 ; DX =:= 2, DY =:= 1 ). attacks(rook, S1, S2, Board) :- same_rank_or_file(S1, S2), clear_line(S1, S2, Board). attacks(bishop, S1, S2, Board) :- same_diagonal(S1, S2), clear_line(S1, S2, Board). attacks(queen, S1, S2, Board) :- ( same_rank_or_file(S1, S2) ; same_diagonal(S1, S2) ), clear_line(S1, S2, Board). same_rank_or_file(X1-Y1, X2-Y2) :- ( X1 =:= X2 ; Y1 =:= Y2 ). same_diagonal(X1-Y1, X2-Y2) :- DX is abs(X1 - X2), DY is abs(Y1 - Y2), DX =:= DY. % For sliding pieces, make sure no piece blocks the path. clear_line(X1-Y1, X2-Y2, Board) :- step(X1, X2, SX), step(Y1, Y2, SY), NX is X1 + SX, NY is Y1 + SY, clear_path(NX-NY, X2-Y2, SX-SY, Board). clear_path(Target, Target, _, _) :- !. clear_path(X-Y, Target, SX-SY, Board) :- \\+ occupied(X-Y, Board), NX is X + SX, NY is Y + SY, clear_path(NX-NY, Target, SX-SY, Board). step(A, B, 0) :- A =:= B. step(A, B, 1) :- B > A.\nstep(A, B, -1) :-\n    B < A.\n\noccupied(Square, Board) :-\n    member(_-Square, Board).\n\n% Printing\n\nprint_solutions([], _).\nprint_solutions([Board | Rest], N) :-\n    format('Solution ~d:~n', [N]),\n    print_coordinates(Board),\n    print_board(Board),\n    nl,\n    N1 is N + 1,\n    print_solutions(Rest, N1).\n\nprint_coordinates(Board) :-\n    member(queen-Q, Board),\n    member(king-K, Board),\n    member(rook-R, Board),\n    member(bishop-B, Board),\n    member(knight-N, Board),\n    format('  queen=~w  king=~w  rook=~w  bishop=~w  knight=~w~n',\n           [Q, K, R, B, N]).\n\nprint_board(Board) :-\n    forall(\n        between(1, 4, Row0),\n        (\n            Y is 5 - Row0,\n            forall(\n                between(1, 4, X),\n                (\n                    piece_at(X-Y, Board, Symbol),\n                    format('~w ', [Symbol])\n                )\n            ),\n            nl\n        )\n    ).\n\npiece_at(Square, Board, Symbol) :-\n    member(Piece-Square, Board),\n    !,\n    piece_symbol(Piece, Symbol).\npiece_at(_, _, '.').\n\npiece_symbol(queen,  'Q').\npiece_symbol(king,   'K').\npiece_symbol(rook,   'R').\npiece_symbol(bishop, 'B').\npiece_symbol(knight, 'N').\n```\n\n[1] I’ve played around with generating Prolog and Lean this weekend, and I’ve had better results with Prolog. The problems with Lean haven’t been Lean per se but the Mathlib library. The library is frequently refactored, which makes sense for a young language, but this makes it harder to generate and debug code.", "url": "https://wpnews.pro/news/writing-prolog-with-chatgpt", "canonical_source": "https://www.johndcook.com/blog/2026/06/15/writing-prolog-with-chatgpt/", "published_at": "2026-06-15 17:22:43+00:00", "updated_at": "2026-06-16 18:57:09.295469+00:00", "lang": "en", "topics": ["large-language-models", "ai-tools", "generative-ai"], "entities": ["ChatGPT", "SWI Prolog", "Claude"], "alternates": {"html": "https://wpnews.pro/news/writing-prolog-with-chatgpt", "markdown": "https://wpnews.pro/news/writing-prolog-with-chatgpt.md", "text": "https://wpnews.pro/news/writing-prolog-with-chatgpt.txt", "jsonld": "https://wpnews.pro/news/writing-prolog-with-chatgpt.jsonld"}}