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

View File

@@ -197,15 +197,23 @@ Then a `pkg` directory will be created, containing everything you need for a web
```html ```html
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>Scryer Prolog - Sudoku Solver Example</title> <title>Scryer Prolog - Sudoku Solver Example</title>
<script type="module"> <script type="module">
import init, { eval_code } from './pkg/scryer_prolog.js'; import initScryer, { MachineBuilder } from "./pkg/scryer_prolog.js";
const run = async () => { // Initialize Scryer Prolog with WASM
await init("./pkg/scryer_prolog_bg.wasm"); const wasm = await fetch("./pkg/scryer_prolog_bg.wasm");
let code = ` const module = await WebAssembly.compile(await wasm.arrayBuffer());
await initScryer(module);
// Set up the Prolog machine
const machine = new MachineBuilder().build();
// Knowledge base: Sudoku rules and problem definition
const kb = `
:- use_module(library(format)). :- use_module(library(format)).
:- use_module(library(clpz)). :- use_module(library(clpz)).
:- use_module(library(lists)). :- use_module(library(lists)).
@@ -216,15 +224,15 @@ Then a `pkg` directory will be created, containing everything you need for a web
maplist(all_distinct, Rows), maplist(all_distinct, Rows),
transpose(Rows, Columns), transpose(Rows, Columns),
maplist(all_distinct, Columns), maplist(all_distinct, Columns),
Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], Rows = [A,B,C,D,E,F,G,H,I],
blocks(As, Bs, Cs), blocks(A, B, C),
blocks(Ds, Es, Fs), blocks(D, E, F),
blocks(Gs, Hs, Is). blocks(G, H, I).
blocks([], [], []). blocks([], [], []).
blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :- blocks([A,B,C|T1], [D,E,F|T2], [G,H,I|T3]) :-
all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]), all_distinct([A,B,C,D,E,F,G,H,I]),
blocks(Ns1, Ns2, Ns3). blocks(T1, T2, T3).
problem(1, [[_,_,_,_,_,_,_,_,_], problem(1, [[_,_,_,_,_,_,_,_,_],
[_,_,_,_,_,3,_,8,5], [_,_,_,_,_,3,_,8,5],
@@ -235,19 +243,40 @@ Then a `pkg` directory will be created, containing everything you need for a web
[5,_,_,_,_,_,_,7,3], [5,_,_,_,_,_,_,7,3],
[_,_,2,_,1,_,_,_,_], [_,_,2,_,1,_,_,_,_],
[_,_,_,_,4,_,_,_,9]]). [_,_,_,_,4,_,_,_,9]]).
main :-
problem(1, Rows), sudoku(Rows), maplist(portray_clause, Rows).
:- initialization(main).
`; `;
const result = eval_code(code);
document.write(`<p>Sudoku solver returns:</p><pre>${result}</pre>`); 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();
// Output results
document.write(`<p>Sudoku solver returns:</p>`);
for (const solution of formattedSolutions) {
document.write(`<pre>${solution}</pre>`);
}
</script> </script>
</head> </head>
<body></body> <body></body>
</html> </html>
``` ```