From d2dfb7ccda23782d0692f6aff76157ba876a436d Mon Sep 17 00:00:00 2001 From: constraintAutomaton Date: Tue, 8 Apr 2025 19:12:27 +0200 Subject: [PATCH 1/5] Update README with working WebAssembly usage example. --- README.md | 127 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 0e8438d4..cbd9ff51 100644 --- a/README.md +++ b/README.md @@ -197,57 +197,86 @@ Then a `pkg` directory will be created, containing everything you need for a web ```html - - - Scryer Prolog - Sudoku Solver Example - - - + + // Output results + document.write(`

Sudoku solver returns:

`); + for (const solution of formattedSolutions) { + document.write(`
${solution}
`); + } + + + + + + ``` From 1f82dafcb6296089aa4e5fc10ebc3ce809ae7bce Mon Sep 17 00:00:00 2001 From: constraintAutomaton Date: Tue, 8 Apr 2025 19:17:20 +0200 Subject: [PATCH 2/5] Useless console.log in the example deleted. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index cbd9ff51..ead2b06c 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,6 @@ Then a `pkg` directory will be created, containing everything you need for a web const formatted = grid.map(row => `[${row.join(", ")}]`).join("\n"); formattedSolutions.push(formatted); - console.log(formatted); } // Output results From 8c348a7160ae11fd86bf94ead16b9a26552b1713 Mon Sep 17 00:00:00 2001 From: constraintAutomaton Date: Tue, 8 Apr 2025 19:29:34 +0200 Subject: [PATCH 3/5] better identation of the knowledge base. --- README.md | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ead2b06c..2cd8be48 100644 --- a/README.md +++ b/README.md @@ -214,36 +214,36 @@ Then a `pkg` directory will be created, containing everything you need for a web // Knowledge base: Sudoku rules and problem definition const kb = ` - :- use_module(library(format)). - :- use_module(library(clpz)). - :- use_module(library(lists)). + :- use_module(library(format)). + :- use_module(library(clpz)). + :- use_module(library(lists)). - sudoku(Rows) :- - length(Rows, 9), maplist(same_length(Rows), Rows), - append(Rows, Vs), Vs ins 1..9, - maplist(all_distinct, Rows), - transpose(Rows, Columns), - maplist(all_distinct, Columns), - Rows = [A,B,C,D,E,F,G,H,I], - blocks(A, B, C), - blocks(D, E, F), - blocks(G, H, I). + sudoku(Rows) :- + length(Rows, 9), maplist(same_length(Rows), Rows), + append(Rows, Vs), Vs ins 1..9, + maplist(all_distinct, Rows), + transpose(Rows, Columns), + maplist(all_distinct, Columns), + Rows = [A,B,C,D,E,F,G,H,I], + blocks(A, B, C), + blocks(D, E, F), + blocks(G, H, I). - blocks([], [], []). - blocks([A,B,C|T1], [D,E,F|T2], [G,H,I|T3]) :- - all_distinct([A,B,C,D,E,F,G,H,I]), - blocks(T1, T2, T3). + blocks([], [], []). + blocks([A,B,C|T1], [D,E,F|T2], [G,H,I|T3]) :- + all_distinct([A,B,C,D,E,F,G,H,I]), + blocks(T1, T2, T3). - problem(1, [[_,_,_,_,_,_,_,_,_], - [_,_,_,_,_,3,_,8,5], - [_,_,1,_,2,_,_,_,_], - [_,_,_,5,_,7,_,_,_], - [_,_,4,_,_,_,1,_,_], - [_,9,_,_,_,_,_,_,_], - [5,_,_,_,_,_,_,7,3], - [_,_,2,_,1,_,_,_,_], - [_,_,_,_,4,_,_,_,9]]). -`; + problem(1, [[_,_,_,_,_,_,_,_,_], + [_,_,_,_,_,3,_,8,5], + [_,_,1,_,2,_,_,_,_], + [_,_,_,5,_,7,_,_,_], + [_,_,4,_,_,_,1,_,_], + [_,9,_,_,_,_,_,_,_], + [5,_,_,_,_,_,_,7,3], + [_,_,2,_,1,_,_,_,_], + [_,_,_,_,4,_,_,_,9]]). + `; machine.consultModuleString("user", kb); From 024c498beeb7d49dd65babbcb498abee03346c19 Mon Sep 17 00:00:00 2001 From: constraintAutomaton Date: Tue, 8 Apr 2025 20:13:02 +0200 Subject: [PATCH 4/5] Fix DOM manipulation to avoid deprecated functions in Sudoku solver example. --- README.md | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2cd8be48..68a4cec8 100644 --- a/README.md +++ b/README.md @@ -266,17 +266,24 @@ Then a `pkg` directory will be created, containing everything you need for a web } // Output results - document.write(`

Sudoku solver returns:

`); + const solutionDiv = document.querySelector("#soduku-solution"); for (const solution of formattedSolutions) { - document.write(`
${solution}
`); + const newPre = document.createElement("pre"); + newPre.textContent = solution; + solutionDiv.appendChild(newPre); } - - + +

Sudoku solver returns:

+
+ +
+ + ``` Then you can serve it with your favorite http server like `python -m http.server` or `npx serve`, and access the page with your browser. From 3090cb0084cde9b6be125c0011fda77e0e2ed344 Mon Sep 17 00:00:00 2001 From: constraintAutomaton Date: Sat, 12 Apr 2025 07:18:00 +0200 Subject: [PATCH 5/5] Example update to use iterable. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 68a4cec8..c1451221 100644 --- a/README.md +++ b/README.md @@ -254,8 +254,8 @@ Then a `pkg` directory will be created, containing everything you need for a web const formattedSolutions = []; // Format the answers - for (let solution = answers.next(); !solution.done; solution = answers.next()) { - const rows = solution.value.bindings["Rows"].list; + for (const solution of answers) { + const rows = solution.bindings["Rows"].list; const grid = rows.map(row => row.list.map(cell => cell.integer) @@ -283,7 +283,6 @@ Then a `pkg` directory will be created, containing everything you need for a web - ``` Then you can serve it with your favorite http server like `python -m http.server` or `npx serve`, and access the page with your browser.