Magic squares in Prolog?

A question for prologista if there are any.

I two years ago I tried software to look for "magic squares", like this:


P O L O S

O C K A

L O T O K

ON AND

W A K A T


In the Prologue it should be, the idea is trivial. To record facts of the form:

word('P','O','L','O','Z'). And to make a request:


word(A1,A2,A3,A4,A5),word(A2,B1,B2,B3,B4),word(A3, B2,C1,C2,C3),word(A4,B3,C2,D1,D2),word(A5,B4,C3,D2 ,E1).


I wanted these queries to generate a Python for squares and rectangles of various sizes, words to find the Prologue and pass the Python.

Tried to swi to run the query for square 5*5 and broken off. Don't remember the ending, whether it's frozen, or an error of memory, or just I've waited and not waited. In the end, just in Python rewrote, it turned out not so beautiful, of course, but it worked.

Then I do swi do not know how to cook, then if this task is too big and is worth taking another implementation of Prolog (what?), whether such a method is not suitable for the solution. In General, I will be glad to listen to smart people.


Here, if anything, a list of five-letter Russian nouns — dl.dropboxusercontent.com/u/20473637/n.txt
October 3rd 19 at 04:03
1 answer
October 3rd 19 at 04:05
Solution
If to take for a basis the code shown in the link.

:- use_module(library(error)).
:- use_module(library(clpfd)).
:- use_module(library(lists)).

solve :-
 load_file('n.txt', Words),
 member([A1, A2, A3, A4, A5], Words),
 member([A2, A6, A7, A8, A9], Words),
 member([A3, A7, A10, A11, A12], Words),
 member([A4, A8, A11, A13, A14], Words),
 member([A5, A9, A12, A14, A15], Words),
 writef("%s\n", [[A1, A2, A3, A4, A5]]),
 writef("%s\n", [[A2, A6, A7, A8, A9]]),
 writef("%s\n", [[A3, A7, A10, A11, A12]]),
 writef("%s\n", [[A4, A8, A11, A13, A14]]),
 writef("%s\n", [[A5, A9, A12, A14, A15]]).

load_file(File, Words) :-
 open(File, read, Stream, []),
 call_cleanup(load_strings(Stream,Words), close(Stream)).

load_strings(Stream, Words) :-
 read_line_to_codes(Stream, T0),
 load_strings(T0, Stream, Words).

load_strings(end_of_file, _Stream, []) :- !.
load_strings([], _Stream, []) :- !.
load_strings(Line, Stream, [Line|Rest]) :-
 read_line_to_codes(Stream, NextLine),
 load_strings(NextLine, Stream, Rest).


To compile:
swipl -O-c words.pl

And run...

Abaka
Badan
adept
Kaper
antre

Is instantly!
And if you call findall([A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14],solve R).
We will continuously obtain new solutions, and also quite quickly. - Lawrence12 commented on October 3rd 19 at 04:08
Thank you! - carlos_Rice commented on October 3rd 19 at 04:11
Final version: swipl -g true -O-q-t main -o words -c words.pl

words.pl
:- use_module(library(error)).
:- use_module(library(clpfd)).
:- use_module(library(lists)).

main :-
findall([A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15],solve R).

solve :-
 load_file('n.txt', Words),
 member([A1, A2, A3, A4, A5], Words),
 member([A2, A6, A7, A8, A9], Words),
 member([A3, A7, A10, A11, A12], Words),
 member([A4, A8, A11, A13, A14], Words),
 member([A5, A9, A12, A14, A15], Words),
 writef("%s\n", [[A1, A2, A3, A4, A5]]),
 writef("%s\n", [[A2, A6, A7, A8, A9]]),
 writef("%s\n", [[A3, A7, A10, A11, A12]]),
 writef("%s\n", [[A4, A8, A11, A13, A14]]),
 writef("%s\n\n", [[A5, A9, A12, A14, A15]]).

load_file(File, Words) :-
 open(File, read, Stream, []),
 call_cleanup(load_strings(Stream,Words), close(Stream)).

load_strings(Stream, Words) :-
 read_line_to_codes(Stream, T0),
 load_strings(T0, Stream, Words).

load_strings(end_of_file, _Stream, []) :- !.
load_strings([], _Stream, []) :- !.
load_strings(Line, Stream, [Line|Rest]) :-
 read_line_to_codes(Stream, NextLine),
 load_strings(NextLine, Stream, Rest).

- Lawrence12 commented on October 3rd 19 at 04:14

Find more questions by tags Abnormal programmingProlog