Skip to content

Commit 7c5f62c

Browse files
committed
Refactored metta_typed_functions.pl:
- Updated implement_predicate and implement_predicate_nr to include the Self parameter for better scoping. - Replaced call(Body) with eval_args for more precise evaluation and return value handling. - Enhanced assignable_to logic with can_assign to improve type compatibility checks. - Removed unused type guard tests and fallback logic for streamlined functionality.
1 parent f7878f8 commit 7c5f62c

File tree

1 file changed

+8
-121
lines changed

1 file changed

+8
-121
lines changed

prolog/metta_lang/metta_typed_functions.pl

Lines changed: 8 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@
289289
score_term(Types, Score):- term_to_list(Types, XX), maplist(nc_weight, XX, XXL), sumlist(XXL, Score).
290290

291291
% Main Entry Point
292-
implement_predicate([Op | Parameters], ReturnVal) :-
292+
implement_predicate(Self, [Op | Parameters], ReturnVal) :-
293293
% Safely execute the main logic, falling back on a default behavior if needed.
294-
catch(implement_predicate_nr([Op | Parameters], ReturnVal), metta_notreducable(Original), ReturnVal = Original).
294+
catch(implement_predicate_nr(Self, [Op | Parameters], ReturnVal), metta_notreducable(Original), ReturnVal = Original).
295295

296296
:- op(700,xfx,('haz_value')).
297297
'haz_value'(List,E):- member(EE,List),EE==E.
298298

299299
% Main Logic
300-
implement_predicate_nr([Op | Parameters], ReturnVal) :-
300+
implement_predicate_nr(Self, [Op | Parameters], ReturnVal) :-
301301

302302
Original = [Op | Parameters],
303303

@@ -315,10 +315,10 @@
315315
% Extract parameter types and group them by index across all clauses
316316
findall(Types, (member(thbr(Types, _, _, _, RetType), Clauses)), ParamTypesPerClause),
317317
group_types_by_param_index(ParamTypesPerClause, Grouped),
318-
convert_to_unique_sets(Grouped, GroupedParamTypes),
318+
convert_to_unique_sets(Grouped, ParamTypeSets),
319319

320320
% Generate a coercion table mapping parameters to their possible coerced types
321-
parameter_coercion_table(Parameters, GroupedParamTypes, CoercionTable),
321+
parameter_coercion_table(Parameters, ParamTypeSets, CoercionTable),
322322

323323
% Phase 1: Filter and Score Type Matching
324324
findall(TypeScore - (MinimizedTypes, ReducedParams, Params, Body, ReturnVal, RetType),
@@ -350,7 +350,7 @@
350350

351351
% Process Ordered Bodies
352352
(((member(_TypeScore - (_MinimizedTypes, ReducedParams, Params, Body, ReturnVal, RetType), OrderedBodies), match_head(Params, ReducedParams)) *->
353-
(call(Body) *->
353+
(eval_args(Body,ReturnVal) *->
354354
(SuccessBehavior haz_value 'Deterministic' -> ! ; true) % vs Nondeterministic
355355
;
356356
(FailureBehavior haz_value 'ClauseFailDet' -> % vs ClauseFailNonDet
@@ -435,7 +435,8 @@
435435

436436
% Subtype Relationships
437437
assignable_to(Was, _):- Was = '%Undefined%', !, fail.
438-
assignable_to(_, _).
438+
assignable_to(From,To):- can_assign(From,To).
439+
%assignable_to(_, _).
439440

440441
% Enums Validation
441442
validate_function_type_enums(MismatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) :-
@@ -461,120 +462,6 @@
461462

462463

463464

464-
% ------------------------------------------------------------------------------
465-
% Core Logic with Type Guards
466-
% ------------------------------------------------------------------------------
467-
468-
% Helper to check type guards.
469-
guard_match(X, number) :- number(X).
470-
guard_match(X, atom) :- atom(X).
471-
guard_match(X, list) :- is_list(X).
472-
guard_match(X, complex) :- is_list(X), length(X, N), N > 5.
473-
guard_match(X, simple) :- is_list(X), length(X, N), N =< 5.
474-
guard_match(_, generic).
475-
476-
% Define what happens inside the guarded body.
477-
guarded_body(X, Result, success) :-
478-
writeln(successful_guard(X)),
479-
Result = processed(X).
480-
481-
guarded_body(X, Result, failure) :-
482-
writeln(failed_guard(X)),
483-
Result = return_original(X).
484-
485-
% Fallback logic if no guards match.
486-
fallback_logic(X, Result) :-
487-
writeln('No type guard matched. Executing fallback.'),
488-
Result = default_value(X).
489-
490-
% Nested guard logic.
491-
nested_guard(X, Result) :-
492-
( X = hello ->
493-
Result = special_case_handled
494-
; Result = default_atom_result
495-
).
496-
497-
% ------------------------------------------------------------------------------
498-
% Tests
499-
% ------------------------------------------------------------------------------
500-
501-
% Test 1: Simple Type Guard Matching
502-
test_simple_guard :-
503-
function(42, Result1), writeln(Result1),
504-
function(hello, Result2), writeln(Result2),
505-
function([], Result3), writeln(Result3),
506-
function(foo, Result4), writeln(Result4).
507-
508-
% Test 2: Fallback Behavior
509-
test_fallback :-
510-
function_with_fallback([], Result), writeln(Result).
511-
512-
% Test 3: Prioritized Type Guard Evaluation
513-
test_prioritized :-
514-
prioritized_function([1, 2, 3], Result1), writeln(Result1),
515-
prioritized_function([1, 2, 3, 4, 5, 6], Result2), writeln(Result2),
516-
prioritized_function(hello, Result3), writeln(Result3).
517-
518-
% Test 4: Nested Guarded Logic with Errors
519-
test_nested :-
520-
nested_function(42, Result1), writeln(Result1),
521-
nested_function(hello, Result2), writeln(Result2),
522-
nested_function(world, Result3), writeln(Result3),
523-
nested_function([], Result4), writeln(Result4).
524-
525-
% ------------------------------------------------------------------------------
526-
% Function Definitions
527-
% ------------------------------------------------------------------------------
528-
529-
% Function with basic guards.
530-
function(X, Result) :-
531-
( guard_match(X, number) ->
532-
guarded_body(X, Result, success)
533-
; guard_match(X, atom) ->
534-
guarded_body(X, Result, success)
535-
; guard_match(X, list) ->
536-
guarded_body(X, Result, success)
537-
; guarded_body(X, Result, failure)
538-
).
539-
540-
% Function with a fallback mechanism.
541-
function_with_fallback(X, Result) :-
542-
( guard_match(X, number) ->
543-
guarded_body(X, Result, success)
544-
; guard_match(X, atom) ->
545-
guarded_body(X, Result, success)
546-
; fallback_logic(X, Result)
547-
).
548-
549-
% Function with prioritized guards.
550-
prioritized_function(X, Result) :-
551-
evaluation_order(fittest_first), % Assume we process most specific guards first.
552-
( guard_match(X, complex) ->
553-
guarded_body(X, Result, success)
554-
; guard_match(X, simple) ->
555-
guarded_body(X, Result, success)
556-
; guard_match(X, generic) ->
557-
guarded_body(X, Result, success)
558-
; guarded_body(X, Result, failure)
559-
).
560-
561-
% Function with nested guards and error handling.
562-
nested_function(X, Result) :-
563-
( guard_match(X, number) ->
564-
guarded_body(X, Result, success)
565-
; guard_match(X, atom) ->
566-
nested_guard(X, Result)
567-
; fallback_logic(X, Result)
568-
).
569-
570-
ffffff:- writeln('
571-
?- test_simple_guard.
572-
?- test_fallback.
573-
?- test_prioritized.
574-
?- test_nested.
575-
').
576-
577-
578465
%! freeist(+X, +Y, -Result) is det.
579466
%
580467
% A comparison predicate for `predsort/3` that sorts terms by freeness.

0 commit comments

Comments
 (0)