-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatchVampireInteractive.sh
More file actions
executable file
·465 lines (415 loc) · 13.6 KB
/
patchVampireInteractive.sh
File metadata and controls
executable file
·465 lines (415 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env bash
set -euo pipefail
# Apply interactive-IO tweaks to the Vampire source tree so that stdin
# prompts can round-trip through the browser (via Module.vampireReadline).
#
# Usage: ./patchVampireInteractive.sh [path/to/vampire]
ROOT_DIR=${1:-vampire}
if [[ ! -d "$ROOT_DIR" ]]; then
echo "error: '$ROOT_DIR' is not a directory" >&2
exit 1
fi
run_patch() {
local description="$1"
local patch_input="$2"
if patch -p1 -d "$ROOT_DIR" --forward --dry-run --quiet <<<"$patch_input" >/dev/null 2>&1; then
patch -p1 -d "$ROOT_DIR" --forward --quiet <<<"$patch_input" >/dev/null 2>&1
echo "Applied: $description"
else
echo "Skipped (already applied?): $description"
fi
}
install_header() {
local dest="$ROOT_DIR/Lib/WebInteractive.hpp"
mkdir -p "$(dirname "$dest")"
cat >"$dest" <<'EOF'
#pragma once
#include <string>
#include <iostream>
#ifdef __EMSCRIPTEN__
#include <cstdlib>
#include <emscripten.h>
extern "C" char* vampire_async_readline(const char* prompt);
#endif
namespace Lib {
namespace Web {
/**
* Read a line from either std::cin (native) or the web input hook (WASM).
* Returns false on EOF.
*/
inline bool readInteractiveLine(std::string& out, const std::string& prompt = "")
{
#ifdef __EMSCRIPTEN__
char* res = vampire_async_readline(prompt.c_str());
if (!res) { return false; }
out.assign(res);
std::free(res);
return true;
#else
if (!prompt.empty()) { std::cout << prompt; }
return static_cast<bool>(std::getline(std::cin, out));
#endif
}
} // namespace Web
} // namespace Lib
EOF
echo "Ensured header: ${dest#$ROOT_DIR/}"
}
install_header
ensure_webinteractive_cpp() {
local dest="$ROOT_DIR/Lib/WebInteractive.cpp"
if [[ -f "$dest" ]]; then
return
fi
cat >"$dest" <<'EOF'
#include "Lib/WebInteractive.hpp"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
extern "C" {
EM_ASYNC_JS(char*, vampire_async_readline, (const char* prompt), {
if (typeof Module.vampireReadline !== 'function') { return 0; }
const text = prompt ? UTF8ToString(prompt) : "";
const response = await Module.vampireReadline(text);
if (response === undefined || response === null) { return 0; }
const len = lengthBytesUTF8(response) + 1;
const buf = _malloc(len);
stringToUTF8(response, buf, len);
return buf;
});
}
#endif
EOF
echo "Ensured source: ${dest#$ROOT_DIR/}"
}
ensure_webinteractive_cpp
run_patch "Add WebInteractive.cpp implementation" '*** Add File: Lib/WebInteractive.cpp
#include "Lib/WebInteractive.hpp"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
extern "C" {
EM_ASYNC_JS(char*, vampire_async_readline, (const char* prompt), {
if (typeof Module.vampireReadline !== '\''function'\'') { return 0; }
const text = prompt ? UTF8ToString(prompt) : "";
const response = await Module.vampireReadline(text);
if (response === undefined || response === null) { return 0; }
const len = lengthBytesUTF8(response) + 1;
const buf = _malloc(len);
stringToUTF8(response, buf, len);
return buf;
});
}
#endif
'
run_patch "Wire interactive input helper into vampire.cpp" '--- a/vampire.cpp
+++ b/vampire.cpp
@@ -59,6 +59,7 @@
#include "Saturation/SaturationAlgorithm.hpp"
#include "FMB/ModelCheck.hpp"
+#include "Lib/WebInteractive.hpp"
using namespace std;
@@ -566,7 +567,7 @@ void interactiveMetamode()
while (true) {
std::string line;
- if (!getline(cin, line) || line.rfind("exit",0) == 0) {
+ if (!Lib::Web::readInteractiveLine(line) || line.rfind("exit",0) == 0) {
cout << "Bye." << endl;
break;
} else if (line.rfind("run",0) == 0) {
'
run_patch "Use web-aware input in ManCSPassiveClauseContainer" '--- a/Saturation/ManCSPassiveClauseContainer.cpp
+++ b/Saturation/ManCSPassiveClauseContainer.cpp
@@ -13,6 +13,8 @@
*/
#include <iostream>
#include <algorithm>
+#include <stdexcept>
+#include "Lib/WebInteractive.hpp"
#include "ManCSPassiveClauseContainer.hpp"
#include "Lib/VirtualIterator.hpp"
namespace Saturation
@@ -49,7 +51,9 @@ Clause* ManCSPassiveClauseContainer::popSelected()
// ask user to pick a clause id
std::cout << "Pick a clause:\n";
std::string id;
- std::cin >> id;
+ if (!Lib::Web::readInteractiveLine(id)) {
+ throw std::runtime_error("No input available for clause selection");
+ }
unsigned selectedId = 0;
try {
selectedId = std::stoul(id);
} catch (const std::exception&) {
std::cout << "User error: Invalid clause id '" << id << "'!\n";
continue;
}
'
# Fallback: ensure web-aware input is used in ManCSPassiveClauseContainer
MANCS_PATH="$ROOT_DIR/Saturation/ManCSPassiveClauseContainer.cpp"
if [[ -f "$MANCS_PATH" ]]; then
MANCS_PATH="$MANCS_PATH" python - <<'PY'
from pathlib import Path
import os
import re
path = Path(os.environ["MANCS_PATH"])
text = path.read_text()
if "Lib/WebInteractive.hpp" not in text:
text = text.replace("#include <algorithm>\n", "#include <algorithm>\n#include <stdexcept>\n#include \"Lib/WebInteractive.hpp\"\n", 1)
pattern = re.compile(r'^\s*std::cin\s*>>\s*id;\s*$', re.M)
if pattern.search(text):
text = pattern.sub(
" if (!Lib::Web::readInteractiveLine(id)) {\n"
" throw std::runtime_error(\"No input available for clause selection\");\n"
" }",
text,
count=1,
)
def ensure_validation(src: str) -> str:
if "std::stoul" in src and "Invalid clause id" in src:
return src
return src.replace(
" unsigned selectedId = std::stoi(id);\n",
" unsigned selectedId = 0;\n"
" try {\n"
" selectedId = std::stoul(id);\n"
" } catch (const std::exception&) {\n"
" std::cout << \"User error: Invalid clause id '\" << id << \"'!\" << std::endl;\n"
" continue;\n"
" }\n",
1,
)
new_text = ensure_validation(text)
if new_text != text:
text = new_text
path.write_text(text)
print("Applied: Use web-aware input in ManCSPassiveClauseContainer (fallback)")
PY
fi
run_patch "Relax TermOrderingDiagram asserts for Emscripten" '--- a/Kernel/TermOrderingDiagram.hpp
+++ b/Kernel/TermOrderingDiagram.hpp
@@
- static_assert(sizeof(uint64_t) == sizeof(Branch));
- static_assert(sizeof(uint64_t) == sizeof(TermList));
- static_assert(sizeof(uint64_t) == sizeof(void*));
- static_assert(sizeof(uint64_t) == sizeof(intptr_t));
+#ifndef __EMSCRIPTEN__
+ static_assert(sizeof(uint64_t) == sizeof(Branch));
+ static_assert(sizeof(uint64_t) == sizeof(TermList));
+ static_assert(sizeof(uint64_t) == sizeof(void*));
+ static_assert(sizeof(uint64_t) == sizeof(intptr_t));
+#endif
'
# Fallback: guard TermOrderingDiagram static_asserts if patch context doesn't match
TOD_PATH="$ROOT_DIR/Kernel/TermOrderingDiagram.hpp"
if [[ -f "$TOD_PATH" ]]; then
TOD_PATH="$TOD_PATH" python - <<'PY'
from pathlib import Path
import os
import re
path = Path(os.environ["TOD_PATH"])
lines = path.read_text().splitlines(True)
pattern = re.compile(r'^\s*static_assert\(sizeof\(uint64_t\)\s*==\s*sizeof\([^)]+\)\);\s*$')
groups = []
start = None
for i, line in enumerate(lines):
if pattern.match(line):
if start is None:
start = i
else:
if start is not None:
groups.append((start, i))
start = None
if start is not None:
groups.append((start, len(lines)))
changed = False
for start, end in reversed(groups):
prev = start - 1
while prev >= 0 and lines[prev].strip() == "":
prev -= 1
if prev >= 0 and lines[prev].strip() == "#ifndef __EMSCRIPTEN__":
continue
lines.insert(end, "#endif\n")
lines.insert(start, "#ifndef __EMSCRIPTEN__\n")
changed = True
if changed:
path.write_text("".join(lines))
print("Applied: Relax TermOrderingDiagram asserts for Emscripten (fallback)")
PY
fi
run_patch "Disable mutex usage in Timer for single-threaded Emscripten" '--- a/Lib/Timer.cpp
+++ b/Lib/Timer.cpp
@@
-#include <mutex>
+#include <mutex>
@@
-static std::recursive_mutex EXIT_LOCK;
+#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
+struct DummyMutex {
+ void lock() {}
+ void unlock() {}
+};
+static DummyMutex EXIT_LOCK;
+#else
+static std::recursive_mutex EXIT_LOCK;
+#endif
@@
- ::new (&EXIT_LOCK) std::recursive_mutex;
+#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)
+ ::new (&EXIT_LOCK) std::recursive_mutex;
+#endif
'
# Fallback: disable mutex usage in Timer for single-threaded Emscripten
TIMER_PATH="$ROOT_DIR/Lib/Timer.cpp"
if [[ -f "$TIMER_PATH" ]]; then
TIMER_PATH="$TIMER_PATH" python - <<'PY'
from pathlib import Path
import os
path = Path(os.environ["TIMER_PATH"])
text = path.read_text()
if "DummyMutex" not in text:
text = text.replace(
"static std::recursive_mutex EXIT_LOCK;",
"#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)\nstruct DummyMutex {\n void lock() {}\n void unlock() {}\n};\nstatic DummyMutex EXIT_LOCK;\n#else\nstatic std::recursive_mutex EXIT_LOCK;\n#endif",
1,
)
if "::new (&EXIT_LOCK) std::recursive_mutex;" in text:
text = text.replace("::new (&EXIT_LOCK) std::recursive_mutex;",
"#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__)\n ::new (&EXIT_LOCK) std::recursive_mutex;\n#endif")
path.write_text(text)
PY
echo "Applied: Disable mutex usage in Timer for single-threaded Emscripten (fallback)"
fi
run_patch "Avoid random_device failure in WASM" '--- a/Lib/Random.hpp
+++ b/Lib/Random.hpp
@@
+ inline static unsigned systemSeed()
+ {
+#ifdef __EMSCRIPTEN__
+ return static_cast<unsigned>(std::time(nullptr));
+#else
+ return std::random_device()();
+#endif
+ }
+
inline static void resetSeed ()
{
- setSeed(std::random_device()());
+ setSeed(systemSeed());
}
'
run_patch "Use Random::systemSeed in Options sampling" '--- a/Shell/Options.cpp
+++ b/Shell/Options.cpp
@@
- auto rng = _randomStrategySeed.actualValue == 0
- ? std::mt19937((std::random_device())())
+ auto rng = _randomStrategySeed.actualValue == 0
+ ? std::mt19937(Random::systemSeed())
: std::mt19937(_randomStrategySeed.actualValue);
'
run_patch "Use Random::systemSeed in PortfolioMode" '--- a/CASC/PortfolioMode.cpp
+++ b/CASC/PortfolioMode.cpp
@@
#include "Lib/ScopedLet.hpp"
#include "Debug/TimeProfiling.hpp"
#include "Lib/Timer.hpp"
#include "Lib/Sys/Multiprocessing.hpp"
+#include "Lib/Random.hpp"
@@
- opt.setRandomSeed(std::random_device()());
+ opt.setRandomSeed(Random::systemSeed());
'
# Fallback: guard random_device for Emscripten in Lib/Random.hpp
RAND_PATH="$ROOT_DIR/Lib/Random.hpp"
if [[ -f "$RAND_PATH" ]]; then
RAND_PATH="$RAND_PATH" python - <<'PY'
from pathlib import Path
import os
path = Path(os.environ["RAND_PATH"])
text = path.read_text()
insert = (
" inline static unsigned systemSeed()\n"
" {\n"
"#ifdef __EMSCRIPTEN__\n"
" return static_cast<unsigned>(std::time(nullptr));\n"
"#else\n"
" return std::random_device()();\n"
"#endif\n"
" }\n\n"
)
if "systemSeed()" not in text:
marker = "inline static void resetSeed"
if marker in text:
text = text.replace(marker, insert + marker, 1)
elif "public:" in text:
text = text.replace("public:\n", "public:\n" + insert, 1)
text = text.replace("setSeed(std::random_device()());", "setSeed(systemSeed());")
path.write_text(text)
PY
echo "Applied: Avoid random_device failure in WASM (fallback)"
fi
# Fallback: patch Options.cpp to use Random::systemSeed()
OPT_PATH="$ROOT_DIR/Shell/Options.cpp"
if [[ -f "$OPT_PATH" ]]; then
if grep -q "random_device" "$OPT_PATH" && grep -q "strategySamplerFilename" "$OPT_PATH"; then
OPT_PATH="$OPT_PATH" python - <<'PY'
from pathlib import Path
import os
path = Path(os.environ["OPT_PATH"])
text = path.read_text()
old = " auto rng = _randomStrategySeed.actualValue == 0\n ? std::mt19937((std::random_device())())\n : std::mt19937(_randomStrategySeed.actualValue);\n"
new = " auto rng = _randomStrategySeed.actualValue == 0\n ? std::mt19937(Random::systemSeed())\n : std::mt19937(_randomStrategySeed.actualValue);\n"
if old in text:
text = text.replace(old, new, 1)
path.write_text(text)
PY
echo "Applied: Use Random::systemSeed in Options sampling (fallback)"
fi
fi
# Fallback: patch PortfolioMode.cpp to use Random::systemSeed()
PORT_PATH="$ROOT_DIR/CASC/PortfolioMode.cpp"
if [[ -f "$PORT_PATH" ]]; then
if grep -q "randomizeSeedForPortfolioWorkers" "$PORT_PATH"; then
PORT_PATH="$PORT_PATH" python - <<'PY'
from pathlib import Path
import os
path = Path(os.environ["PORT_PATH"])
text = path.read_text()
if "Lib/Random.hpp" not in text:
text = text.replace('#include "Lib/Timer.hpp"\n', '#include "Lib/Timer.hpp"\n#include "Lib/Random.hpp"\n', 1)
text = text.replace("opt.setRandomSeed(std::random_device()());", "opt.setRandomSeed(Random::systemSeed());")
path.write_text(text)
PY
echo "Applied: Use Random::systemSeed in PortfolioMode (fallback)"
fi
fi
run_patch "List WebInteractive.cpp in sources" '--- a/cmake/sources.cmake
+++ b/cmake/sources.cmake
@@
Lib/System.cpp
Lib/System.hpp
Lib/Timer.cpp
Lib/Timer.hpp
+ Lib/WebInteractive.cpp
Lib/TriangularArray.hpp
Lib/TypeList.hpp
Lib/Vector.hpp
Lib/VirtualIterator.hpp
'
# Fallback: ensure WebInteractive.cpp is in sources.cmake
SRC_PATH="$ROOT_DIR/cmake/sources.cmake"
if [[ -f "$SRC_PATH" ]]; then
if ! grep -q "Lib/WebInteractive.cpp" "$SRC_PATH"; then
SRC_PATH="$SRC_PATH" python - <<'PY'
from pathlib import Path
import os
path = Path(os.environ["SRC_PATH"])
text = path.read_text()
needle = "Lib/Timer.hpp\n"
insert = "Lib/Timer.hpp\n Lib/WebInteractive.cpp\n"
if needle in text:
text = text.replace(needle, insert, 1)
path.write_text(text)
print("Applied: List WebInteractive.cpp in sources (fallback)")
PY
fi
fi
echo "Interactive patch completed."