|
289 | 289 | score_term(Types, Score):- term_to_list(Types, XX), maplist(nc_weight, XX, XXL), sumlist(XXL, Score).
|
290 | 290 |
|
291 | 291 | % Main Entry Point
|
292 |
| -implement_predicate([Op | Parameters], ReturnVal) :- |
| 292 | +implement_predicate(Self, [Op | Parameters], ReturnVal) :- |
293 | 293 | % 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). |
295 | 295 |
|
296 | 296 | :- op(700,xfx,('haz_value')).
|
297 | 297 | 'haz_value'(List,E):- member(EE,List),EE==E.
|
298 | 298 |
|
299 | 299 | % Main Logic
|
300 |
| -implement_predicate_nr([Op | Parameters], ReturnVal) :- |
| 300 | +implement_predicate_nr(Self, [Op | Parameters], ReturnVal) :- |
301 | 301 |
|
302 | 302 | Original = [Op | Parameters],
|
303 | 303 |
|
|
315 | 315 | % Extract parameter types and group them by index across all clauses
|
316 | 316 | findall(Types, (member(thbr(Types, _, _, _, RetType), Clauses)), ParamTypesPerClause),
|
317 | 317 | group_types_by_param_index(ParamTypesPerClause, Grouped),
|
318 |
| - convert_to_unique_sets(Grouped, GroupedParamTypes), |
| 318 | + convert_to_unique_sets(Grouped, ParamTypeSets), |
319 | 319 |
|
320 | 320 | % 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), |
322 | 322 |
|
323 | 323 | % Phase 1: Filter and Score Type Matching
|
324 | 324 | findall(TypeScore - (MinimizedTypes, ReducedParams, Params, Body, ReturnVal, RetType),
|
|
350 | 350 |
|
351 | 351 | % Process Ordered Bodies
|
352 | 352 | (((member(_TypeScore - (_MinimizedTypes, ReducedParams, Params, Body, ReturnVal, RetType), OrderedBodies), match_head(Params, ReducedParams)) *->
|
353 |
| - (call(Body) *-> |
| 353 | + (eval_args(Body,ReturnVal) *-> |
354 | 354 | (SuccessBehavior haz_value 'Deterministic' -> ! ; true) % vs Nondeterministic
|
355 | 355 | ;
|
356 | 356 | (FailureBehavior haz_value 'ClauseFailDet' -> % vs ClauseFailNonDet
|
|
435 | 435 |
|
436 | 436 | % Subtype Relationships
|
437 | 437 | assignable_to(Was, _):- Was = '%Undefined%', !, fail.
|
438 |
| -assignable_to(_, _). |
| 438 | +assignable_to(From,To):- can_assign(From,To). |
| 439 | +%assignable_to(_, _). |
439 | 440 |
|
440 | 441 | % Enums Validation
|
441 | 442 | validate_function_type_enums(MismatchBehavior, NoMatchBehavior, EvaluationOrder, SuccessBehavior, FailureBehavior, OutOfClausesBehavior) :-
|
|
461 | 462 |
|
462 | 463 |
|
463 | 464 |
|
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 |
| - |
578 | 465 | %! freeist(+X, +Y, -Result) is det.
|
579 | 466 | %
|
580 | 467 | % A comparison predicate for `predsort/3` that sorts terms by freeness.
|
|
0 commit comments