This repository was archived by the owner on Sep 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetta.pl
More file actions
2595 lines (2507 loc) · 137 KB
/
Copy pathmetta.pl
File metadata and controls
2595 lines (2507 loc) · 137 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Purpose: provide MeTTa's Prolog runtime, builtins, type system, evaluator,
% imports, function registration, and named-space execution context.
% Guarantees: metta_space_registered/1 exposes existing namespace owners
% [tested: run_tests(space_registration); commit=a8e3fc42306377adf7cae0a331f3d92fbf190304].
% Guarantees: both trailed context scopes are published host services
% [tested: reference_scopes:both_scope_doors_are_published_host_services;
% commit=9b0a084e534ddf7dd67980ad84c27c8279b877f1].
% Guarantees: metta_after_foreign/2 retains one reconciliation attempt through
% native parents and foreign completion before observation delivery
% [tested: transaction_completion; commit=37d417bd059b4636f3fe603863a2e738e1f9aeda].
% Guarantees: engine/host_transactions.pl supplies the documented host rollback
% workaround before runtime declarations load [tested:
% host_transactions, test_class_declaration_rollback; commit=9b0a084e534ddf7dd67980ad84c27c8279b877f1].
% Guarantees: occurrence-output writes and result-aware transactions are
% exported through their owning runtime modules
% [tested: spaces_tokens, classes_transaction_results; commit=9b0a084e534ddf7dd67980ad84c27c8279b877f1].
% Guarantees:
% - metta_operation_parameters/4 exposes the joint argument types and
% origins used by constructor compilation and runtime admission
% [tested: translator_constructors, engine_layering; commit=2398951d3272ad02b2c2d7b1e2b610c8e332c1f5].
% - broken extension entries raise through loading_loudly/1 with their
% diagnostic instead of allowing boot to report success
% [tested: tests/shell/test_packaged_cli.sh; commit=8ee8fcd4e43a932131909f7c58ad4fbe4dcf8d1d].
% - materialize.pl loads before source processing and shares the engine's
% runtime context [tested: function_free_materialization; commit=3c64e2e24787362a5a5081513bc24b880711a1d7].
% - The engine/metta/ units compile into metta_engine in source
% order. Engine and library definitions stay in their owning modules;
% designated SWI protocol hooks live in user.
% [tested: engine_modules; commit=b7866b4d874879ff0cb212eb1c6af60dddaa39c6].
% - A built-in call covered by the effects cluster whose declared operand
% types already conflict is refused before operand evaluation; shallow
% compile-time checks inspect literals and declared return types without
% binding source variables
% [tested: operation_answers, test_a_repeated_eval_does_not_recompile_and_the_effects_cluster_conforms; commit=8d0027a3942000c799daccb45bf0abe1b46b10aa].
% - repr/2, println!/2, format-args, test/3 and assert/2 presentation retain
% host display text through sdisplay/2 without weakening swrite/2's
% reader-inverse contract [tested: parser_display,
% a_value_prints_according_to_its_default_reading,
% a_partial_application_remains_visible_in_test_output; commit=0c1bd4c2faadc1c4fc97cc9d2caa084907d20072].
% - import! loads a MeTTa source that is new or that has been edited, and
% skips one that is neither, which is SWI's if(changed); a Python source
% keeps if(not_loaded) [tested 2026-08-19:
% test_an_unchanged_repeat_import_does_not_run_the_source_again,
% test_an_edited_import_is_not_skipped,
% filereader_source_reload:an_unchanged_file_is_not_loaded_again].
% - the builtin type surface and engine prelude are decoded as UTF-8 rather
% than through the process locale [tested:
% filereader_source_reload:a_source_is_utf8_independent_of_the_locale;
% commit=18b1135167d60396c41e63e42ded2f66d0eb1900].
% - metta_handles_route/5 routes a query by the most specific matching
% (handles ...) entry in &metta, where specificity is pattern
% subsumption first and adornment-set inclusion between renaming-equal
% patterns, disagreeing maximal ties throw metta_contract_conflict/4
% naming both entries and the query, and a context with no entries
% fails in one indexed probe [tested 2026-08-17: metta_handles_route]
% [measured 2026-08-17: 15 inferences per undeclared-context miss].
% - get-type/2 returns each derived type once, while has_type/2 uses one
% witness for a fixed expected type [tested 2026-08-15:
% metta_type_answers, translator_typed_checks].
% - a child execution module resolves parent equations before the shared
% &self and builtin tiers [tested:
% test_a_child_space_reads_through_its_parent_and_writes_locally;
% commit=755330de329ece49eddcfb7d6db3061c3350a0ca].
% - restricted modules resolve only their local equations and curated
% builtin surface [tested: spaces_restricted_modules;
% commit=6a08901f4125c2536f5b4032daac9937f793870f].
% - expression-named spaces are SpaceType values, select their own execution
% modules, and report their exact ground identifier through context-space
% [tested: test_two_instances_of_a_parametric_space_answer_independently;
% commit=3c7bcde6a0670ec5c563584b26977b41cc727580].
% - reporting observers type the empty expression as unit `(->)` while
% internal classifier paths retain their gradual empty-expression result
% [tested: test_the_empty_expressions_type_follows_the_arbiters_ruling; commit=0d90e628b1f90c4b4464a2907efcb357d74b13d3].
% - the include refusal's self/top pair records the arbiter-owned module
% bases explicitly, so the inventory's exemption remains checkable
% [tested: test_a_planted_closed_policy_list_is_reported_by_the_inventory_lane;
% commit=42b5d28232e75c32b20a1d5bf1f740fec134938d].
% - a hook handler whose call remains unreduced has not supplied a verdict;
% the hook door reports its existing stuck state instead of treating the
% residual call as a malformed verdict [tested:
% hooks:an_unclaimed_request_is_a_stuck_state_that_says_so,
% hooks:a_post_stuck_state_undoes_the_write; commit=0d90e628b1f90c4b4464a2907efcb357d74b13d3].
% - support_graph.pl loads before the specializer and file reader that publish
% derived artifact edges [tested:
% support_graph:test_a_derived_fact_is_invalidated_forward_from_what_it_supports;
% commit=7ade2b90e2631451fd6ffc23d22dd8c2d4a7a7aa].
% - lib_memo.pl is resident before user source compiles, explain reports its
% automatic decision, and the effect walk follows its transparent cache
% dispatcher to the underlying source function [tested:
% test_a_doubly_branching_recursion_is_tabled_automatically_and_a_tail_recursion_is_not,
% test_an_impure_function_is_never_cached_automatically; commit=ccad9f6d588270ec2f0810fc56c30e9e59207e7c].
% - Integers inside signed i64 report Number and integers outside it report
% BigInt; a Number parameter admits either while a BigInt parameter admits
% only BigInt, and arithmetic may cross the boundary in either direction
% without changing its exact SWI value [tested 2026-08-20:
% bigint_number, test_bigint_and_number_type_the_numeric_tower,
% test_integer_type_follows_the_signed_i64_boundary,
% test_number_parameters_accept_bigint_without_retyping_number].
% - A successful named-space import commits one receipt tying the source
% path and digest to its exact load and stored-output references. Reuse
% requires that receipt to remain current, so any public removal rebuilds
% the missing source contribution without duplicating survivors
% [tested: filereader_import_lifecycle,
% test_public_import_rebuilds_when_a_receipt_dependency_disappears,
% test_repeat_import_reuses_one_current_receipt_without_duplication;
% commit=b77e3ce5233e5f6032cfc8546ff83ecf4dc3de87].
% - Host failures from builtins retain their ISO error class and name the
% written MeTTa operation [tested 2026-08-15:
% metta_operation_errors, translator_evaluation_errors]. Integer
% arithmetic pays nothing for this and float arithmetic pays one
% inference per call, because only the integer pair takes the guarded
% fast path [measured 2026-08-15: 300,000 and 400,000 inferences per
% 100,000 calls, against 300,000 unguarded]; division's integer pair
% pays the catch too, because a non-divisible pair converts its result
% to float and can overflow doing it. Whole-corpus cost is
% +2.1% instructions on examples/ch18-performance/18-01-larger-workloads/01-scale.metta
% [measured 2026-08-15].
% - Python operation registration reaches the canonical `(effect Name Class)`
% atom consumed by operation reflection; exactly pureStructural projects
% to seam:pure_operation/1 [tested:
% test_structural_registration_reflects_an_effect_atom,
% effects_lattice:only_pure_structural_projects_to_the_cache_purity_seam;
% commit=3cfbe0d7417b1c453c2dc12d47e2e47e7de461f7].
% - the final boot pass materializes one catalog visibility row for every
% callable after the prelude has registered its equations [tested:
% catalog_self_description:every_shipped_callable_has_one_visibility;
% commit=8779452fed89853c3f77c3469f7a6ec7b12e9efa].
% - StateMonad cells use one process-shared non-backtrackable store, so main
% evaluation and held answer engines observe the same writes without
% losing their parameterized held-value type [tested:
% test_state_cells_are_shared_across_answer_engines,
% test_state_retires_three_state_function_strings; commit=18b1135167d60396c41e63e42ded2f66d0eb1900].
% - A result past binary64 saturates to the IEEE value on the engine's
% operations, agreeing with the reader's saturating literals, and an
% infinity a literal produced carries through further arithmetic; the
% same recovery answers the whole IEEE family when a float operand is
% present, a float zero divides to the signed infinity and the NaN
% class answers NaN, while integer division and remainder by zero answer
% a contained DivisionByZero Error atom; raw
% is/2 keeps every flag's error mode [tested 2026-08-20:
% engine_operations_saturate_where_raw_is_still_raises,
% a_read_infinity_survives_further_arithmetic,
% a_twice_faulting_compound_saturates_all_the_way,
% test_integer_division_by_zero_answers_what_d1_decides,
% test_arithmetic_overflow_agrees_with_the_literal_side,
% test_float_zero_division_and_nan_agree_with_the_arbiter;
% commit=ecd792eacbfe1810645434ce406f79be3a9e03d1].
% - is-alpha-member/3 tests unifiability without retaining bindings in its
% arguments [tested 2026-08-15: metta_alpha_membership].
% - alpha-unique-atom/2 confirms identity inside each term-hash bucket, so a
% hash collision cannot remove an inequivalent term [tested 2026-08-15:
% metta_alpha_unique].
% - get-metatype/2 classifies every Prolog term used as a MeTTa value, and
% classifies a NAME by the arbiter's grounded-token table gated on this
% engine holding the operation, so a token nothing here answers to reports
% Symbol as an unknown name does [tested 2026-08-20: metta_metatypes].
% - metta_transaction/1 answers everything its body answers, and every
% answer's writes commit or roll back together [tested 2026-08-19:
% extensions/python/tests/ch15_writing_transactions_and_worlds/test_atomic_forms.py::test_a_transaction_preserves_every_answer_of_its_body].
% - Every guarded_input_position/3 refuses an unbound argument and names the
% MeTTa operation, so no builtin binds the caller's variable, invents an
% answer, runs away or reports a host predicate [tested 2026-08-19:
% builtin_input_guards:every_builtin_refuses_an_unbound_input_by_name,
% extensions/python/tests/ch10_errors_and_refusals/test_builtin_inputs.py::test_a_raising_builtin_names_the_metta_operation_not_the_host_predicate].
% - ==/3 and !=/3 refuse two operands of known and different types and
% answer for every other pair, at no cost on two numbers [tested
% 2026-08-19:
% extensions/python/tests/ch03_atoms_and_expressions/test_equality.py::test_cross_kind_equality_answers_false]
% [measured 2026-08-19: 4487.45 inferences per thousand-iteration loop,
% unchanged].
% - %Undefined% is consistent with every type in both directions, so a call
% site refuses only a PROVEN conflict, while has_declared_type/2 demands a
% witness for a contract [tested 2026-08-19:
% extensions/python/tests/ch09_types/test_gradual_typing.py::test_an_unknown_type_is_consistent_with_every_declared_type,
% extensions/python/tests/ch04_spaces_and_matching/test_answer_protocol.py::test_admission_types_the_pool].
% - An expression no arrow types reads element-wise, and the tuple it reads
% is %Undefined% as soon as one member's type is [tested 2026-08-19:
% metta_type_answers:a_tuple_with_an_untyped_member_is_undefined].
% - get-type/2 and get-type-space/3 answer from declarations without running
% the inspected expression, so inspection has no effects of its own
% [tested 2026-08-19:
% extensions/python/tests/ch09_types/test_type_inspection.py::test_get_type_does_not_run_its_arguments_effects].
% - get-type-space/3 reads only the selected space, and the upstream doc
% family builds @doc-formal answers from that scoped type and prose
% [tested 2026-08-20:
% extensions/python/tests/repository/test_doc_family.py::test_the_doc_family_answers_what_upstream_answers].
% - seam:builtin_type_declaration/2 rows are the union of lib_builtin_types.metta
% and the prelude's, with each row written once and evicted only by the
% register that wrote it [tested 2026-08-19:
% metta_builtin_type_surface:a_shared_declaration_is_evicted_only_from_the_register_that_wrote_it].
% - External Prolog libraries extend seam:builtin_type_declaration/2 without
% replacing the engine's rows, and unloading retires only their own clauses
% [tested: test_a_library_types_its_own_blob_without_destroying_the_table;
% commit=6f06e918c8f3382e8e1c8ccd8d120c6d809999a5].
% - The prelude loads exactly three form shapes: a declaration, an equation,
% and `!(add-translator-rule! NAME)` for a name it defines itself, which
% is how a DERIVED form ships. A program that defines such a name in any
% execution module takes the whole form over, so the global registration
% is withdrawn with the clauses
% [tested: prelude_derived_forms; commit=d1318d20b5d89d33079c49d0e94aa29e12685664].
% - add-translator-rule! REFUSES a protected_core_head/1 name and puts that
% name in the error term, and records what an accepted registration took
% over from in translator_rule_override/2, so a rule going ahead of a
% special form or a builtin is stated rather than silent
% [tested: test_overriding_a_protected_name_is_refused_with_the_name;
% commit=9330b5d7ebf607e34a85be950bb226fce65f45c0].
% - Test assertions distinguish no answer from one empty-expression answer
% [tested 2026-08-14: translator_test_answers].
% - pragma! validates keys against the closed registry and values before
% they can replace a working setting: an unknown key is refused, max-time
% requires a positive number, max-inferences requires a positive integer,
% none explicitly disables either bound, max-stack-depth answers the
% arbiter's error atom for a non-count, and the HE spellings type-check
% and interpreter stay accepted, NOT enforced
% [tested: test_pragma_validates_values_and_refuses_only_unknown_keys,
% interpreter_pragmas; commit=e8270f8551083f236ce5134ca299adf5347d6898].
% - stack-limit scopes SWI's per-thread byte ceiling and restores the exact
% previous value after success, failure, exception, and nested scopes;
% max-stack-depth remains branch-local reduction fuel [tested:
% scoped_stack_limit,
% test_janus_stack_scope_restores_on_all_exits; commit=81c50d3ae4c03ddfd70ed3f1ff70e085cfee3978].
% - metta_assertion_failure/4 classifies the three assertion formals, so a
% harness tells a false claim from a broken engine by TYPE rather than by
% reading the message [tested 2026-08-19:
% extensions/python/tests/ch12_testing/test_assertion_failures.py::test_a_failing_assertion_is_a_different_exception_from_an_engine_fault].
% - Runtime builtins reject prebound outputs that they would not produce
% [tested 2026-08-14: metta_builtin_outputs].
% - Function registration performed by a source load participates in that
% load's rollback [tested 2026-08-14: filereader_source_rollback].
% - metta_host_function_generation/1 exposes the sum of the process-global
% SWI database generations of fun/1, fun_in/2, fun_scoped/1 and
% metta_exec_module_parent/2, which advances on committed catalogue
% changes, including a second space defining an already-registered name,
% and on no ordinary evaluation or data write
% [tested: function_catalogue_generation; commit=1f32a7c85d5c3bcbd8797218694ae5550c362e9a].
% - Prolog registration refuses every head the translator compiles before
% function dispatch, including heads added through translator_rule/1
% [tested: test_registering_any_translator_compiled_head_is_refused_by_name].
% - Python source imports restore sibling modules and sys.path after setup
% or execution errors [tested 2026-08-14:
% metta_python_import_cleanup].
% - Every seam:grounded_extra_type/2 clause is consulted whether or not a host
% bridge answers seam:grounded_type_names/2, so a (py-atom f Type)
% declaration survives the Python library being loaded [tested 2026-08-18:
% extensions/python/tests/ch11_python_as_a_notation/test_ops.py::test_a_declared_type_survives_the_library_being_loaded]
% [measured 2026-08-18: +2 inferences per get-type on a Python object and
% 0 on every other value].
% - register-token! and unregister-token! are ordinary registered builtins,
% so source programs and host APIs reach the same reader-token mapping
% [tested: test_a_registered_token_class_parses_like_a_shipped_one;
% commit=2c741dda928a30d0ce1c7e1fcf0b263b4d1bb97b].
% - The engine loads and runs the full examples/ corpus with
% set_prolog_flag(autoload, false) already in effect: the
% directory_file_path/3 directive below needs library(filesex) before
% the rest of this section's use_module block would otherwise supply
% it, and next_lambda_name/1 (translator.pl) needs library(gensym) for
% every foldl-atom/map-atom/filter-atom/'|->' compile, both silently
% supplied by autoload before now [measured 2026-08-18: NO_AUTOLOAD=1
% sh test.sh, 200/200 examples; run.sh's own header has the mechanism].
% Cost: +1.50% instructions:u on a bare boot (swipl -s engine/metta.pl,
% no seats), +0.54% with the seats loaded too, +0.14% over a full
% example run that also exercises the opt-in libraries' own fixes
% (lib/lib_constraints/lib_constraints.pl, lib/lib_memo/lib_memo.pl) [measured 2026-08-18:
% interleaved min-of-3, perf stat -e instructions:u, spread under
% 0.003% within each side].
% - library(thread), library(time), library(process), library(crypto) and
% library(redis) are optional: a build without one records the capability
% absent through metta_platform/4 and loads without an error. A dependent
% operation refuses by name, except the five SHA hashes that library(sha)
% still supplies without crypto [tested: platform_capabilities,
% platform_capabilities_reduced;
% commit=59792b524568755a2fbfe1c5f7cdb571bd78a3bf]. The original three-row
% census cost between +0.25% and +0.44% instructions:u on a boot, the range
% being the
% measurement's own layout sensitivity, which an inert padding block that
% neither side executes moves by about the same amount [measured
% 2026-08-27: 1,062,764,116 -> 1,067,395,694 unpadded, 1,064,396,538 ->
% 1,067,019,910 with five inert rules, 1,063,925,775 -> 1,067,710,574 with
% ten; interleaved min-of-5, perf stat -e instructions:u, swipl -q -g halt
% -t halt -s engine/main.pl on twelve-character paths; boot inferences
% 688,190 -> 690,780 and examples/ch07-control-flow/07-01-if-and-booleans/09-xor.metta identical at 9,289;
% commit=87d998c24278fc7f020ccb0e408ebcd9332b63eb].
% Open Obligations:
% To Do: None. check.sh runs the no-autoload lane through test.sh
% [source: check.sh no-autoload lane; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720].
% Hacks: None
% Future Enhancements: None
% The core owns its predicates; user holds the host's registrations and imports.
% Execution resolves through &self, prelude, metta_engine, user and system.
% Keeping user below the core preserves consulted host predicates while local
% equations shadow the inherited implementation only in their execution module.
% [tested: engine_modules:the_chain_is_self_then_prelude_then_engine_then_user,
% engine_modules:removing_a_local_shadow_restores_a_library_export; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
%
% Modules own their helper names and autoload tables. The boundary suite also
% loads plain-file controls that demonstrate both collisions without modules.
% [tested: engine_modules; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
%
% Exports cover cross-subsystem calls, generated goals, registered builtin heads
% and the host's query strings. Subsystems set their base before compilation so
% goal_expansion/2 is visible while their clauses are read.
% [tested: sh check.sh layering prolog-static; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
:- module(metta_engine,
% The LANGUAGE: builtin heads the host tier, the prelude tier and the
% test suites call BY NAME. A MeTTa program reaches these through its
% space's own chain instead, so this list is about Prolog callers.
[ '=alpha'/3,
'assert-answers'/5,
'assert-includes-answers'/5,
'car-atom'/2,
'cdr-atom'/2,
'decons-atom'/2,
'get-metatype'/2,
'get-type'/2,
'import!'/3,
'index-atom'/3,
'is-alpha-member'/3,
'is-space'/2,
'map-atom'/3,
'new-space'/1,
'read-form!'/1,
'require-extension!'/2,
'size-atom'/2,
'subtraction-atom'/3,
(#<)/3,
(#=)/3,
(#>)/3,
(#\=)/3,
% Publish division so a host lookup cannot select yall's lambda.
% [tested: relational_arithmetic; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
(/)/3,
(==)/3,
call_goals_in/2,
call_goals_in_/2,
catch_recover/2,
eval/2,
'eval-one'/2,
'on-unwind'/3,
evalc/3,
has_type/2,
metta/4,
metta_eval_step/2,
metta_predicate_goal/2,
metta_run_with_fuel/3,
metta_speculate/1,
or/3,
repr/2,
%
% Export the module queries for callers outside the expansion chain.
% [source: engine/metta.pl:goal_expansion/2; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
current_metta_module/1,
metta_self_module/1,
metta_exec_module_prefix/1,
current_metta_space/1,
metta_reference_declare/3,
metta_reference_admit_text/2,
metta_reference_changed/1,
metta_reference_restored/2,
metta_reference_retired/2,
metta_reference_definition_changed/1,
metta_reference_face_wave/0,
metta_reference_prepare/3,
% The reference and package services the prelude library calls.
% A declaration publishes a callable service even where no Prolog
% clause in this engine reaches it, because Python calls engine
% predicates through query strings, so a declared service that is
% not exported is one the host cannot reach
% [tested: engine_modules:every_declared_service_is_exported_to_the_host].
metta_reference_check_prolog_source/1,
metta_reference_face/3,
metta_reference_option/3,
metta_reference_read_exports/3,
metta_reference_register_prolog/4,
metta_loader_source/1,
metta_package_loading/3,
metta_package_normalise/3,
metta_package_perform/3,
metta_package_reload/3,
metta_perform_package_rows/2,
metta_source_singleflight/2,
metta_graded_pair/5,
'get-property'/2,
metta_head_property/3,
metta_head_claims/3,
metta_head_origins/3,
metta_form_unevaluated_variable_paths/3,
metta_argument_admitted/3,
forget_registered_function/1,
fun_here/1,
fun_here_in/2,
metta_emits/2,
metta_function_cacheable/1,
metta_function_cacheable/2,
metta_grounded_token/1,
metta_shared_registry/1,
register_arity/2,
register_fun/1,
register_fun_in/2,
unregister_fun_everywhere/1,
unregister_fun_in/2,
with_metta_module/2,
%
% TYPES: the declaration tables, the normalizers and the witnesses. The
% typing RULES are engine/type_rules.pl's; these are the core's tables.
check_argument_type/3,
check_argument_type_under_live_policy/3,
declared_type_for_check/2,
definition_type_declaration_in/3,
enable_type_alias_scope/1,
governing_type_chains_in/4,
governing_type_declaration/2,
governing_type_declaration_in/3,
has_declared_type/2,
metatype_argument_admitted/4,
metta_argument_type_origins/2,
metta_arrow_type_shape/5,
metta_refined_type/3,
metta_refined_union_type/1,
metta_runtime_type/2,
metta_shipped_types_match/2,
metta_typed_dispatch_applies/2,
metta_types_match_in/3,
normalize_callable_type_in/3,
normalize_cast_type/3,
normalize_source_type_declarations/3,
normalize_type_in/3,
normalize_type_in/4,
normalized_self_type_declaration/2,
raw_definition_type_declaration_in/3,
raw_governing_type_declaration_in/4,
retire_type_alias_scope/1,
runtime_type_guarded/1,
throw_metta_type_error/3,
type_alias_lookup_changed/2,
type_alias_lookups_changed/2,
type_alias_scope_module/2,
type_alias_scope_space/2,
type_annotation_support/3,
type_declaration/2,
type_declaration_in/3,
type_position_modifier/3,
type_witness_in/3,
typing_union_decision/7,
untypable_declarations/2,
validate_type_alias_declaration/3,
%
% EFFECTS, ALGEBRA AND ANNOTATIONS: the classification a planner reads
% and the carrier a host installs around a query.
metta_algebra_one/2,
metta_algebra_order/2,
metta_annotation/2,
metta_annotated_operation_effect/2,
metta_annotations/2,
metta_annotations_order/2,
metta_annotations_ordered/1,
metta_apply_algebra_operation/5,
metta_apply_algebra_negation/4,
metta_algebra_claim/3,
metta_algebra_fixpoint/4,
'match-under'/4,
metta_formula_clear/0,
metta_formula_model_count/3,
metta_formula_variables/2,
metta_formula_witnesses/2,
metta_current_algebra/3,
metta_effect_compose/2,
metta_effect_construct/2,
metta_effect_covered/2,
metta_effect_rank/2,
metta_effect_walk/3,
metta_effective_algebra/2,
metta_evaluation_context/1,
metta_k_extend/4,
metta_operation_effect/2,
metta_with_evaluation_context/2,
metta_with_under/2,
%
% FUEL, BUDGETS, TRANSACTIONS AND WORLDS.
metta_call_with_inference_bound/2,
metta_forget_world_coverage/1,
metta_fuel_budget_configured/0,
metta_fuel_note_chargeless/1,
metta_fuel_step_goal/3,
metta_host_hold/3,
metta_host_hold_chunk/3,
metta_host_hold_close/1,
metta_host_hold_next/2,
metta_host_hold_post/3,
metta_host_inference_budget/3,
metta_host_stack_charge/3,
metta_host_time_budget/3,
metta_host_with_stack_limit/2,
metta_in_user_transaction/0,
metta_discarded_inferences/1,
%The three join doors below are exported at a measured price: their
%names in this list move every seat row measured in a fresh process
%(engine/metta/control.pl, above the doors), and the seam's laws
%require it, a library calling only published services and a
%published service being exported.
metta_join_discarding/2,
metta_join_measured/3,
metta_discard_inferences/1,
metta_negation_world_guard/1,
metta_transaction/1,
metta_transaction/2,
metta_transaction_notified/3,
metta_after_foreign/2,
metta_foreign_completion/2,
metta_with_state_write_fence/1,
metta_with_trailed/3,
metta_with_trailed_enumeration/3,
metta_with_trailed_push/3,
metta_world_effect_coverage/2,
%
% ERRORS AND REFUSALS: the vocabulary every tier raises and every host reads.
guarded_input_position/3,
metta_assertion_failure/6,
metta_bad_argument_error/3,
metta_bad_argument_reason/3,
metta_operation_parameters/4,
metta_error_answer/3,
metta_error_atom/4,
metta_error_context/3,
metta_host_error_kind/3,
metta_host_error_kind_row/3,
metta_host_operation_error/5,
metta_host_refusal/6,
metta_host_refusal_row/4,
metta_on_error_mode/3,
metta_record_error/1,
metta_refinement_violation/3,
metta_shallow_call_refused/2,
metta_transport_failure/1,
refuse_unbound_input/2,
refuse_untypable_declaration/2,
rethrow_metta_operation_error/2,
throw_missing_import/1,
%
% Shared native policies: presentation and IEEE arithmetic recovery.
metta_console_text/2,
metta_saturating_recover/4,
%
% IMPORTS, SOURCES AND EXTENSIONS: what a MeTTa source pulls in, where a
% host loader puts it, and the seat census behind require-extension!.
check_prolog_function_names/3,
consult_global/1,
consult_string_global/2,
metta_load_source/2,
current_working_dir/1,
import_file_string/2,
import_prolog_function/2,
import_prolog_functions/2,
import_when/4,
metta_enlist_foreign/1,
metta_ensure_source_observation/0,
metta_extension_controls/2,
metta_host_source_compile_effect_plan/4,
metta_host_source_effect_plan/4,
metta_host_source_runtime_effect_plan/4,
metta_import_record/2,
metta_install_bridges/0,
metta_load_extension/1,
metta_platform/4,
metta_require_events/2,
metta_require_platform/2,
metta_source/2,
metta_source_declarations/2,
metta_source_guard/1,
metta_source_reset/1,
metta_unimport/2,
register_metta_library_path/3,
resolve_existing_import_path/3,
unregister_metta_extension/1,
use_module_global/1,
use_module_global/2,
%
% HOST DOORS: the rest of what a binding's transport calls, every one of
% them declared kind(..., host_service) in engine/ext_points.pl.
metta_host_adopt_function/4,
metta_host_control_signal_info/3,
metta_host_control_signal_line/2,
metta_host_drop_function/2,
metta_host_forget_function/1,
metta_host_function_callable_from/2,
metta_host_reference_names/2,
metta_host_function_generation/1,
metta_host_goal_effect_plan/4,
metta_host_goal_repeatable/2,
metta_host_open_function/3,
%
% THE PRELUDE TIER'S DOORS, and the hook, contract, token, pragma and
% catalog tables a subsystem, a library or a seat reads.
evict_prelude_declaration/2,
evict_prelude_definition/1,
host_process_tier_loader/3,
metta_admission_claim/2,
metta_arguments_match_in/4,
metta_call_accepted/2,
metta_contract_fact/1,
metta_cost_declaration/4,
metta_declare_hook/3,
metta_deprecation/3,
metta_event_capability/3,
metta_explain/2,
metta_foreign_writes_lost/2,
metta_handles_coherent/1,
metta_handles_route/4,
metta_handles_route/5,
metta_hook_claim_idle/1,
metta_hook_drop_compiled/2,
metta_hook_eval/6,
metta_live_state_cell/1,
metta_merge_route/2,
metta_presented_arrow_chain/3,
metta_restore_token_snapshot/3,
metta_string_declarations/2,
metta_string_registrations/2,
metta_substitute_self/3,
metta_token/2,
metta_token_snapshot/2,
metta_undeclare_hook/2,
metta_vocabulary_claim/3,
metta_writes/2,
read_form_step/4,
retire_metta_tokens_in/1,
retract_prelude_declarations/1,
rewrite_parsed_form/5,
set_metta_pragma/2,
substitute_bound_tokens/2,
%
% These heads also occur in generated or host-supplied goals.
% [tested: sh engine/test.sh; commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
'!='/3,
'Predicate'/2,
'abs-math'/2,
'acos-math'/2,
'alpha-unique-atom'/2,
'cons-atom'/3,
'format-args'/3,
'get-doc-function'/4,
'get-type-space'/3,
'intersection-atom'/3,
'isinf-math'/2,
'log-math'/3,
'max-atom'/2,
'min-atom'/2,
'new-state'/2,
'pow-math'/3,
'pragma!'/3,
'println!'/2,
'sin-math'/2,
'sort-strings'/2,
'sqrt-math'/2,
'union-atom'/3,
(+)/3,
(-)/3,
(<)/3,
(>)/3,
(>=)/3,
(xor)/3,
alpha_bucket_insert/5,
and/3,
application_arrow_declared/1,
builtin_described_name/1,
builtin_fun/1,
builtin_implementation/2,
builtin_implementation_coverage_inventory/1,
builtin_registration_coverage_inventory/1,
builtin_surface_name/1,
builtin_surface_predicate/2,
builtin_tree_defined_arity/2,
check_argument_type_in/4,
check_argument_type_under_policy/3,
claim_function_name/3,
control_exception/1,
empty/1,
exp/2,
fun_in/2,
get_function_type/2,
get_function_type_in/3,
has_type_under_policy/3,
implies/3,
import_receipt/4,
include/2,
install_engine_prelude/0,
library/2,
library/3,
list_shaped/1,
max/3,
metatype_of/2,
metta_adorn_strip/3,
metta_adorn_strip/4,
metta_algebra_descriptor/9,
metta_annotation/1,
metta_arrow_type_chain/2,
metta_discharge_reset/0,
metta_discharges_verified/0,
metta_effect_join/3,
metta_engine_module/1,
metta_export/1,
metta_extension/2,
metta_extension_api_version/2,
metta_extension_loaded/1,
metta_extension_unmet/2,
metta_finish_foreign/3,
metta_function_determinism/2,
metta_function_origin/3,
metta_grounded_type/2,
metta_hook_claim/4,
metta_inferences/3,
metta_math_operation/2,
metta_open_fuel_scope/0,
metta_operation_answer/3,
metta_operation_plan_effect/2,
metta_platform_absent/1,
metta_platform_load/2,
metta_pragma/2,
metta_refinement_head/1,
metta_refinement_holds/2,
metta_refinement_violated/3,
metta_registration_names/2,
metta_requires/1,
metta_residual_check/3,
metta_restore_pragmas/2,
metta_timeout/3,
metta_with_pragmas/3,
min/3,
not/2,
prelude_cost_claim/1,
prelude_declaration/2,
prelude_doc_atom/2,
prelude_document/2,
prelude_head/2,
prelude_owned/1,
prelude_rule_registration/2,
prelude_shipped_equation/2,
prelude_translator_rule/1,
prelude_type_declaration/2,
record_metta_export/2,
register_builtin_fun/1,
release_function_name/1,
resolve_metta_import_path/2,
retract_unrelated_system_arities/0,
shallow_argument_types/2,
shallow_declared_type/2,
test/3,
test_answer_value/2,
tuple_positions_witness/3,
type_alias_gate_ref/2,
type_witness_candidate_matches/3,
unguarded_input_position/2,
unrelated_system_predicate/2,
validate_builtin_exemptions/0,
'bind!'/3,
'cos-math'/2,
'get-doc-params'/4,
'get-doc-single-atom'/3,
'pretty-atom'/2,
'test-no-answer'/2,
(*)/3,
builtin_implementation_hook_exists/2,
claimed_export_name/2,
cons/3,
declared_predicate_arity/2,
exists_file/2,
get_type_candidate/2,
import_receipt_current/2,
imported_metta_source/2,
metta_algebra_law/2,
metta_close_fuel_scope/0,
metta_compensation/2,
metta_discharge_coverage/1,
metta_evaluation_fuel/1,
metta_extension_info/3,
metta_extension_member/2,
metta_file_export/2,
metta_fuel_exhausted/1,
metta_function_volatility/2,
metta_reaction/4,
metta_semantic_effect/2,
metta_shape_route/5,
metta_state_cell/1,
pending_metta_export/3,
prelude_wrote_builtin_type/2,
register_prolog_arities/1,
validate_builtin_registration_coverage/0,
verified_discharge/3,
'%'/3,
'change-state!'/3,
'tan-math'/2,
metta_builtin_effect_override/2,
metta_discharge_report/0,
validate_builtin_implementation_coverage/0,
'asin-math'/2,
metta_builtin_structural/1,
validate_builtin_registry/0,
'atan-math'/2,
declare_function_volatility/2,
%
% Registered builtin heads are part of the public language surface.
% assert/2 and exists_file/1 keep explicit core qualification because
% user already imports SWI's predicates under those indicators.
% [tested: engine_modules:every_core_builtin_head_is_exported;
% commit=ede2ac57e213a0d4502c6bbbca6227f97015b720]
(#+)/3,
(#=<)/3,
(#>=)/3,
(#*)/3,
argv/2,
assertaPredicate/2,
assertzPredicate/2,
'atom-subst'/4,
'bit-and'/3,
'bit-not'/2,
'bit-or'/3,
'bit-shift-left'/3,
'bit-shift-right'/3,
'bit-xor'/3,
callPredicate/2,
'ceil-math'/2,
'context-space'/1,
'current-time'/1,
'declare-post-add!'/3,
'declare-pre-add!'/3,
decons/2,
'defined-name'/1,
'#div'/3,
documented/1,
'documented-space'/2,
'exclude-item'/3,
'exp-math'/2,
'filter-atom'/3,
first/2,
'first-from-pair'/2,
'floor-div'/3,
'floor-math'/2,
'foldl-atom'/4,
'format-time'/2,
'get-doc'/2,
'get-doc'/3,
'get-doc-atom'/3,
'get-doc-space'/3,
'get-state'/2,
'help!'/2,
id/2,
'if-decons-expr'/6,
'is-expr'/2,
'is-ground'/2,
'is-member'/3,
'isnan-math'/2,
'is-var'/2,
'#max'/3,
member/3,
'metta-thread'/4,
'#min'/3,
'#mod'/3,
'new-space'/2,
'new-space'/3,
noeval/2,
parse/2,
'parse-command'/2,
'random-float'/3,
'random-float'/4,
'random-int'/3,
'random-int'/4,
'readln!'/1,
repra/2,
retractPredicate/2,
'round-math'/2,
'second-from-pair'/2,
sleep/2,
'sort-atom'/2,
superpose/2,
'trunc-math'/2,
'undeclare-post-add!'/2,
'undeclare-pre-add!'/2,
undocumented/1,
'undocumented-space'/2,
'unique-atom'/2,
metta_engine_operator/1,
<= / 3,
install_prelude_rule/2,
metta_hook_apply/6,
refuse_other_tiers_name/2,
run_under_pragmas/1,
validate_builtin_exemption_liveness/0,
validate_builtin_exemption_schema/0,
validate_builtin_implementation_hooks/0,
validate_builtin_implementation_schema/0,
validate_builtin_implementation_unique/0,
(=)/3,
'=?'/3,
'#-'/3,
'#//'/3
]).
%%%%%%%%%% Dependencies %%%%%%%%%%
%directory_file_path/3 is library(filesex)'s, not a built-in, and the
%directive a few lines down calls it immediately at load time to compute
%standard_library_path/1, before the rest of this section's use_module
%block would otherwise supply it. Autoload papers over the ordering when
%it is on; with autoload=false the directive fails on an unknown
%procedure and standard_library_path/1 is never asserted, which then
%aborts the boot at load_builtin_type_surface's first (library ...) call.
%So this one import has to come first, ahead of even the section header's
%own first clause.
:- encoding(utf8).
:- use_module(library(filesex)).
%A shipped library lives in its own DIRECTORY under lib/, named for the
%library: lib/lib_memo/lib_memo.metta beside lib/lib_memo/lib_memo.pl and
%lib/lib_memo/lib_memo_doc.md. A library is a MeTTa surface, the Prolog it
%rides on and the prose that explains it, and a flat lib/ scattered those
%three across an alphabetical listing of nearly sixty files.
%
%So a spec with no directory component gets one: `lib_memo` resolves to
%lib/lib_memo/lib_memo and `lib_builtin_types.metta` to
%lib/lib_builtin_types/lib_builtin_types.metta. A spec that already names a
%directory is taken as written, which is what keeps builtin_mods/skel.metta,
%the engine's own shipped-module spelling, resolving unchanged.
library(X, Path) :- standard_library_path(Base),
library_within(X, Relative),
directory_file_path(Base, Relative, Path).
%A string reaches here from MeTTa source, `(library "builtin_mods/skel.pl")`,
%and an atom from Prolog, so both spellings are normalised before the test.
%file_name_extension/3 answers Stem=Name and Ext='' for a name with no
%extension, which is why the extension-bearing and bare cases are one clause.
library_within(Spec, Relative) :-
( atom(Spec) -> Name = Spec ; atom_string(Name, Spec) ),
( sub_atom(Name, _, _, _, '/')
-> Relative = Name
; file_name_extension(Stem, _, Name),
directory_file_path(Stem, Name, Relative)
).
%A named library directory, git-fetched or registered. A library that
%pip-installs is under neither: standard_library_path/1 is one directory,
%<src>/../lib, so (library fast.pl) cannot reach a package's own files and a
%downstream library has to pass absolute paths, which is what
%lib/minimal_metta_lib/minimal_metta_lib.py does with os.path.dirname(os.path.abspath(__file__)).
%
%SWI already owns the answer. file_search_path/2 is a "dynamic multifile hook
%predicate used to specify path aliases ... called by absolute_file_name/3 to
%search files specified as Alias(Name)" [source: SWI-Prolog 10.1 Reference
%Manual, section 4.36], it composes (the second argument may be another
%alias), and every SWI tool that understands an alias understands one
%registered here. So a package registers its directory once and
%(library pettorch fast.pl) resolves
%[tested: a_registered_library_path_resolves].
library(X, Y, Path) :- git_library_path(X, Base), !,
directory_file_path(Base, Y, Path).
library(X, Y, Path) :- Spec =.. [X, Y],
( absolute_file_name(Spec, Resolved,
[access(read), file_errors(fail)])
-> Path = Resolved
; refuse_unresolved_library(X, Y)
).
%An alias that resolves to nothing RAISES rather than failing, and the
%distinction is CPython's: returning None from find_spec means "not mine, keep
%looking" and raising means "definitively absent", because "the latter
%indicates that the meta path search should continue, while raising an
%exception terminates it immediately" [source: CPython, the import system,
%finders and loaders].
%
%Failing was the keep-looking signal with nothing left to look with, so
%(import! &self (library imp3 plain)) with the extension forgotten answered
%the empty set, imported nothing, and left every name from that file
%undefined. That surfaces much later as an expression evaluating to itself,
%which is the hardest failure in this language to trace back to its cause. A
%plain path that is absent already raised and named itself; this is the same
%rule reaching the alias form
%[tested: an_unresolvable_library_alias_raises].
refuse_unresolved_library(Alias, File) :-
findall(Directory, file_search_path(Alias, Directory), Directories),
throw(error(metta_unresolved_library(Alias, File, Directories),
context(library/3, 'no readable file of that name'))).
prolog:error_message(metta_unresolved_library(Alias, File, [])) -->
[ '(library ~w ~w) does not resolve: nothing is registered under the \c
alias ~w. Register the directory with register_metta_library_path, or \c
import the file by path.'-[Alias, File, Alias] ].
prolog:error_message(metta_unresolved_library(Alias, File, Directories)) -->
[ '(library ~w ~w) does not resolve: no readable ~w under ~w. Check the \c
spelling, and that the file carries its extension.'
-[Alias, File, File, Directories] ].
%Register a directory under a name, so a Python package can point MeTTa at
%the Prolog and MeTTa files it ships beside itself. Idempotent, and a
%directory that is not there is refused where the caller can still act on it
%rather than at the first import that needs it.
register_metta_library_path(Alias, Directory0, true) :-
must_be(atom, Alias),
( atom(Directory0) -> Directory = Directory0 ; atom_string(Directory, Directory0) ),
( exists_directory(Directory)
-> true
; throw(error(existence_error(directory, Directory),
context(register_metta_library_path/3,
'a library path must be a directory that exists')))