Admin User, created Apr 23. 2025
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title editable="nocomment">Example 11</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<!-- Modern Albufeira Prolog Interpreter -->
<!-- Prolog sandbox -->
<!-- -->
<!-- Warranty & Liability -->
<!-- To the extent permitted by applicable law and unless explicitly -->
<!-- otherwise agreed upon, XLOG Technologies AG makes no warranties -->
<!-- regarding the provided information. XLOG Technologies AG assumes -->
<!-- no liability that any problems might be solved with the information -->
<!-- provided by XLOG Technologies AG. -->
<!-- -->
<!-- Rights & License -->
<!-- All industrial property rights regarding the information - copyright -->
<!-- and patent rights in particular - are the sole property of XLOG -->
<!-- Technologies AG. If the company was not the originator of some -->
<!-- excerpts, XLOG Technologies AG has at least obtained the right to -->
<!-- reproduce, change and translate the information. -->
<!-- -->
<!-- Reproduction is restricted to the whole unaltered document. -->
<!-- Reproduction of the information is only allowed for non-commercial -->
<!-- uses. Selling, giving away or letting of the execution of the -->
<!-- library is prohibited. The library can be distributed as part of -->
<!-- your applications and libraries for execution provided this comment -->
<!-- remains unchanged. -->
<!-- -->
<!-- Restrictions -->
<!-- Only to be distributed with programs that add significant and primary -->
<!-- functionality to the library. Not to be distributed with additional -->
<!-- software intended to replace any components of the library. -->
<!-- -->
<!-- Trademarks -->
<!-- Jekejeke is a registered trademark of XLOG Technologies AG. -->
<body>
<h1>Example 11: Random Shuffle</h1>
<h2>Overview</h2>
<p>The main structure of the program will be a) read lines, b)
random permutate lines, c) write lines. We can already define
the main program, without executing it:</p>
<pre class="code">
main :-
read_lines(L),
random_permutation(L, R),
write_lines(R).
</pre>
<h2>Random</h2>
<p>A simple method to produce a random permutation in Prolog,
is to first extend the given list by a uniform random number,
and then keysort and strip the list again. Dogelog Player provides
uniform random numbers in the interval [0,1) via library(util/math),
and a native implementation of keysort is found in library(compat).</p>
<p>Each time pressing the instrumentation button
will give a different result:</p>
<pre class="code">
:- ensure_loaded(library(compat)).
:- ensure_loaded(library(util/math)).
random_permutation(L, R) :-
add_key(L, H),
keysort(H, J),
remove_key(J, R).
add_key([], []).
add_key([X|L], [Y-X|R]) :- random(Y), add_key(L, R).
remove_key([], []).
remove_key([_-X|L], [X|R]) :- remove_key(L, R).
?- random_permutation([1,2,3,4,5], X).
</pre>
<h2>Input</h2>
<p>For input we rely on the Prolog term reading, whereas
we use the Prolog term end_of_file as a marker that the list
has been completely entered. We can explore reading directly in a
notebook, by adding the input after the reading query.
Because we read Prolog terms, we have to terminate each
input by a period. Feel free to edit the input and see what happens:</p>
<pre class="code">
read_lines(L) :- read(X), read_lines(X, L).
read_lines(end_of_file, []) :- !.
read_lines(X, [X|L]) :- read(Y), read_lines(Y, L).
?- read_lines(L).
foo.
bar.
baz.
end_of_file.
</pre>
<h2>Output</h2>
<p>For output we simple flesh out some Prolog terms in a
tail recursive loop. One might be tempted to use higher order
programming here and some maplist, but Dogelog Player doesn't
provide call/n for performance and didactical reasons, so we
have to do it more explicitly. The can explore writing
directly in a notebook, since it shows the output after the
writing query:</p>
<pre class="code">
write_lines([]).
write_lines([X|L]) :- write(X), nl, write_lines(L).
?- write_lines([foo,bar,baz]).
</pre>
<h2>Result</h2>
<p>We can now run the main program already sketched in the
first paragraph. We will provide a small list of Turing award
winners that were inclined with artificial intelligence.
The notebook allows combining reading and writing. Again each
time pressing the instrumention button will give a different result.
Feel also free to edit the input and see what happens:
</p>
<pre class="code">
?- main.
'Marvin Minsky'.
'John McCarthy'.
'Herbert A. Simon'.
'Allen Newell'.
end_of_file.
</pre>
<script type="module">
import {notebook_async
} from "../../../../dogelog/player/canned/dogelog.mjs";
await notebook_async();
</script>
</body>
</html>