|
1 | 1 | import os
|
2 | 2 | import atexit
|
3 |
| -import tempfile |
4 |
| -import collections.abc |
5 |
| -from hyperon import * |
6 |
| -from hyperon.ext import register_atoms |
7 |
| -from hyperon.atoms import OperationAtom |
8 |
| -import janus_swi as janus |
9 | 3 |
|
| 4 | +import janus_swi as janus |
10 | 5 |
|
11 |
| -PROLOG_BOOTSTRAP = r""" |
| 6 | +def _initialize_prolog_driver(): |
| 7 | + janus.consult("hyperlog", |
| 8 | +r""" |
12 | 9 |
|
13 | 10 | :- use_module(library(debug)).
|
14 | 11 | :- nodebug(hyperlog).
|
15 | 12 |
|
16 | 13 | ensure_mettalog_modules :-
|
17 |
| - ( current_predicate(eval_args/3) -> true ; ensure_loaded(library(metta_rt)) ). |
| 14 | + ensure_loaded(library(metta_rt)). |
18 | 15 |
|
19 | 16 | :- ensure_mettalog_modules.
|
20 | 17 |
|
21 |
| -""" |
22 |
| - |
23 |
| -def _write_temp_prolog_file(code: str): |
24 |
| - tmp = tempfile.NamedTemporaryFile("w+", suffix=".pl", delete=False) |
25 |
| - tmp.write(code) |
26 |
| - tmp.flush() |
27 |
| - return tmp.name |
28 |
| - |
29 |
| - |
30 |
| -def _initialize_prolog_driver(): |
31 |
| - temp_file = _write_temp_prolog_file(PROLOG_BOOTSTRAP) |
32 |
| - janus.consult(temp_file) |
| 18 | +""") |
33 | 19 |
|
34 | 20 |
|
35 | 21 | # ✅ Prolog bootstrap happens at module load (similar to __init__)
|
36 | 22 | _initialize_prolog_driver()
|
37 | 23 |
|
38 | 24 |
|
39 | 25 | def pretty_print_result(result, prefix=" => "):
|
40 |
| - if isinstance(result, collections.abc.Iterator) and not isinstance(result, (str, bytes, list, tuple, dict, set)): |
| 26 | + if not isinstance(result, (str, bytes, list, tuple, set)): |
41 | 27 | try:
|
| 28 | + # Attempt indexed iteration (like for generators or custom iterables) |
42 | 29 | for i, item in enumerate(result):
|
43 | 30 | print(f"{prefix}[{i}] {item}")
|
44 |
| - except Exception as e: |
45 |
| - print(f"Error iterating result: {e}") |
46 |
| - |
47 |
| - print(f"{prefix}{result}") |
48 |
| - elif isinstance(result, (list, tuple, set)): |
49 |
| - for i, item in enumerate(result): |
50 |
| - print(f"{prefix}[{i}] {item}") |
51 |
| - else: |
52 |
| - print(f"{prefix}{result}") |
| 31 | + return |
| 32 | + except Exception: pass |
| 33 | + |
| 34 | + try: |
| 35 | + # Attempt flat iteration (like for sets, dict keys, etc.) |
| 36 | + for item in result: |
| 37 | + print(f"{prefix}{item}") |
| 38 | + return |
| 39 | + |
| 40 | + except Exception: pass |
| 41 | + |
| 42 | + # Final fallback: just print the object |
| 43 | + print(f"{prefix}{result}") |
| 44 | + |
| 45 | + |
53 | 46 |
|
54 | 47 |
|
55 | 48 | class MeTTaLogImpl:
|
|
0 commit comments