Update README with working WebAssembly usage example.

This commit is contained in:
constraintAutomaton
2025-04-08 19:12:27 +02:00
parent 20525e5013
commit d2dfb7ccda

127
README.md
View File

@@ -197,57 +197,86 @@ Then a `pkg` directory will be created, containing everything you need for a web
```html ```html
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head>
<meta charset="UTF-8" />
<title>Scryer Prolog - Sudoku Solver Example</title>
<script type="module">
import init, { eval_code } from './pkg/scryer_prolog.js';
const run = async () => { <head>
await init("./pkg/scryer_prolog_bg.wasm"); <meta charset="UTF-8" />
let code = ` <title>Scryer Prolog - Sudoku Solver Example</title>
:- use_module(library(format)). <script type="module">
:- use_module(library(clpz)). import initScryer, { MachineBuilder } from "./pkg/scryer_prolog.js";
:- use_module(library(lists)).
// Initialize Scryer Prolog with WASM
sudoku(Rows) :- const wasm = await fetch("./pkg/scryer_prolog_bg.wasm");
length(Rows, 9), maplist(same_length(Rows), Rows), const module = await WebAssembly.compile(await wasm.arrayBuffer());
append(Rows, Vs), Vs ins 1..9, await initScryer(module);
maplist(all_distinct, Rows),
transpose(Rows, Columns), // Set up the Prolog machine
maplist(all_distinct, Columns), const machine = new MachineBuilder().build();
Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is],
blocks(As, Bs, Cs), // Knowledge base: Sudoku rules and problem definition
blocks(Ds, Es, Fs), const kb = `
blocks(Gs, Hs, Is). :- use_module(library(format)).
:- use_module(library(clpz)).
blocks([], [], []). :- use_module(library(lists)).
blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :-
all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]), sudoku(Rows) :-
blocks(Ns1, Ns2, Ns3). length(Rows, 9), maplist(same_length(Rows), Rows),
append(Rows, Vs), Vs ins 1..9,
problem(1, [[_,_,_,_,_,_,_,_,_], maplist(all_distinct, Rows),
[_,_,_,_,_,3,_,8,5], transpose(Rows, Columns),
[_,_,1,_,2,_,_,_,_], maplist(all_distinct, Columns),
[_,_,_,5,_,7,_,_,_], Rows = [A,B,C,D,E,F,G,H,I],
[_,_,4,_,_,_,1,_,_], blocks(A, B, C),
[_,9,_,_,_,_,_,_,_], blocks(D, E, F),
[5,_,_,_,_,_,_,7,3], blocks(G, H, I).
[_,_,2,_,1,_,_,_,_],
[_,_,_,_,4,_,_,_,9]]). blocks([], [], []).
blocks([A,B,C|T1], [D,E,F|T2], [G,H,I|T3]) :-
main :- all_distinct([A,B,C,D,E,F,G,H,I]),
problem(1, Rows), sudoku(Rows), maplist(portray_clause, Rows). blocks(T1, T2, T3).
:- initialization(main). problem(1, [[_,_,_,_,_,_,_,_,_],
`; [_,_,_,_,_,3,_,8,5],
const result = eval_code(code); [_,_,1,_,2,_,_,_,_],
document.write(`<p>Sudoku solver returns:</p><pre>${result}</pre>`); [_,_,_,5,_,7,_,_,_],
[_,_,4,_,_,_,1,_,_],
[_,9,_,_,_,_,_,_,_],
[5,_,_,_,_,_,_,7,3],
[_,_,2,_,1,_,_,_,_],
[_,_,_,_,4,_,_,_,9]]).
`;
machine.consultModuleString("user", kb);
// Run the query
const query = "problem(1, Rows), sudoku(Rows), maplist(portray_clause, Rows).";
const answers = machine.runQuery(query);
const formattedSolutions = [];
// Format the answers
for (let solution = answers.next(); !solution.done; solution = answers.next()) {
const rows = solution.value.bindings["Rows"].list;
const grid = rows.map(row =>
row.list.map(cell => cell.integer)
);
const formatted = grid.map(row => `[${row.join(", ")}]`).join("\n");
formattedSolutions.push(formatted);
console.log(formatted);
} }
run();
</script> // Output results
</head> document.write(`<p>Sudoku solver returns:</p>`);
<body></body> for (const solution of formattedSolutions) {
document.write(`<pre>${solution}</pre>`);
}
</script>
</head>
<body></body>
</html> </html>
``` ```