Skip to content

Universal apply for custom types: 4‑field Cscore and InstanceSet support; preserve structural equality #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: ref/mettalog
Choose a base branch
from

Conversation

Biruk-gebru
Copy link
Contributor

Description

This PR introduces a generalized apply function that bridges standard library operations with our custom types and data structures, reducing redundant ad-hoc functions and improving ergonomics and reuse.

Key changes:

  • Generalized comparisons via a key projection:
    • Supports both 5-field mkCscore (from scoring/cscore.metta) and 4-field mkCscore (from metapopulation/exemplar-type.metta), computing a composite key (penalizedScore, -complexity). For 4-field Cscore, penalizedScore := scor - cpxyPen - uniPen.
    • Maps NaN penalized scores to a low sentinel, ensuring total ordering.
  • Wrapper delegation:
    • apply on mkSInst, mkExemplar, mkXmplr, and mkInst delegates to their inner values (e.g., Cscore or List).
    • Added apply delegation for mkSInstSet to operate on the inner List when using list-like operations.
  • Data-structure bridging:
    • Retained generic delegation for Map, MultiMap, OS (Ordered Set), OMS (Ordered Multiset), and List for length/contains.
  • Equality semantics:
    • apply == preserves exact equality for scalar keys and uses approximate numeric equality for composite Cscore keys only.
    • Structural equality (==) remains the recommended choice for identity/deduplication (e.g., ScoredInstance/Exemplar equality checks).

Files of interest:

  • utilities/general-helpers.metta:
    • Implemented 4-field mkCscore handling in key.
    • Added apply support for mkSInstSet.
    • Kept generic DS delegations for length/contains.
    • Kept apply comparison behavior for <, >=, == with the refined equality semantics above.

Motivation and Context

  • Problem: After work in and around [PR MOSES MVP first working version #283], using standard operators across many custom types (Cscore, ScoredInstance, Exemplar, Deme, etc.) became cumbersome. We were introducing multiple type-specific functions (e.g., cscore>=) leading to redundancy, cognitive load, and easy-to-miss semantics drift.
  • Goal: Provide a single, reusable “application bridge” so call sites can use standard functions with custom types without duplicating logic.
  • Solution: Implement apply as a type-aware adapter:
    • Delegates to inner data for wrapper types.
    • Defers to corresponding DS-specific operations for collections.
    • Normalizes comparison semantics for domain-specific types like Cscore.
  • Important note: Structural identity vs value-comparison. Some code paths require identity/dedup (structural (==)), while ordering and popularity ranking require Cscore-aware comparisons (apply <, apply >=). We ensured we don’t change dedup semantics (e.g., removeDupInsts), which rely on strict equality.

How Has This Been Tested?

  • Due to memory/time constraints with the full runner, impacted tests were executed individually with mettalog:
    • utilities: general-helpers tests — PASS
    • scoring: cscore tests — PASS
    • representation: instance tests — PASS
    • utilities: map, ordered-set, ordered-multiset — PASS
    • deme: merge-demes test — verified; the last test initially hung when apply == was used to dedup ScoredInstance/Exemplar. Restoring structural (==) for identity resolved it. Ordering still uses apply comparisons as intended.
  • Environment: Linux, mettalog runner, tests invoked per-file.
  • Also tested apply behavior with both 4-field and 5-field mkCscore forms to confirm consistent ordering and equality where applicable.

Types of changes

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue: avoids unintended dedup collapses by keeping structural (==) where required)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • I have added/refined logic to cover the new unified apply behavior (including 4-field mkCscore and mkSInstSet).
  • All new and existing tests passed when run individually with mettalog.
  • Verified that structural (==) is used where identity is required (e.g., removeDupInsts), and apply ordering comparisons are used where semantically correct.

Linking context:

  • Addresses the usability and redundancy pain discussed in and around [PR MOSES MVP first working version #283], by introducing a robust apply abstraction and clarifying the equality vs. ordering usage guidelines.

@Biruk-gebru Biruk-gebru changed the title Ref/mettalog Universal apply for custom types: 4‑field Cscore and InstanceSet support; preserve structural equality Aug 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant