Arc Solution ae3edfdc in metta - trueagi-io/metta-wam GitHub Wiki

🧠 Translating Logic: From Python to Declarative MeTTa

This guide walks through a three-step translation pipeline to convert typical Python logic into fully declarative, symbolic MeTTa — a language designed for higher-order reasoning and unification-based pattern matching.

Our example comes from the ARC (Abstraction and Reasoning Corpus), where solving visual puzzles involves manipulating objects, colors, and spatial transformations.

alt text

Along the way, we demonstrate higher-order programming, where functions are passed and composed dynamically — a core strength preserved in the final MeTTa form.


✅ Step 1: Start with Python

Here's a Python function using functional composition and direct logic:

def solve_ae3edfdc(I):
    objs = objects(I, T, F, T)
    x = replace(replace(I, THREE, ZERO), SEVEN, ZERO)
    filt = lbind(colorfilter, objs)
    apply_grav = chain(lbind(rbind, gravitate), first, filt)
    x7, x8 = apply_grav(TWO), apply_grav(ONE)
    paint1 = mapply(fork(shift, identity, x7), filt(THREE))
    paint2 = mapply(fork(shift, identity, x8), filt(SEVEN))
    return paint(paint(x, paint1), paint2)

📝 This version is tight, but the logic is implicit — intermediate values are opaque (x, x7, etc), and behavior is hard to follow.


🧩 Step 2: Expand with Meaningful Names

We rewrite the logic using descriptive names. This reveals the modular structure and makes logic pipelines explicit.

def solve_ae3edfdc(I):
    objs = objects(I, T, F, T)

    replaced_3 = replace(I, THREE, ZERO)
    replaced_3_7 = replace(replaced_3, SEVEN, ZERO)

    color_filtered = lbind(colorfilter, objs)

    gravity_applier = chain(lbind(rbind, gravitate), first, color_filtered)

    gravity_vec_2 = gravity_applier(TWO)
    gravity_vec_1 = gravity_applier(ONE)

    color3_objs = color_filtered(THREE)
    color7_objs = color_filtered(SEVEN)

    shift_3 = fork(shift, identity, gravity_vec_2)
    shift_7 = fork(shift, identity, gravity_vec_1)

    moved_3_objs = mapply(shift_3, color3_objs)
    moved_7_objs = mapply(shift_7, color7_objs)

    painted_3 = paint(replaced_3_7, moved_3_objs)
    output = paint(painted_3, moved_7_objs)

    return output

🔍 This version clarifies:

  • Object extraction
  • Color filtering
  • Gravity vector computation
  • Object shifting
  • Compositional painting

📎 View Source on GitHub


🧬 Step 3: Convert to Declarative MeTTa

Now we translate the program into MeTTa, a Datalog-inspired symbolic language using S-expressions and logic variables.

(= (solve_ae3edfdc $I $output)
   (match-and &self
     (objects $I T F T $objs)

     (replace $I THREE ZERO $replaced_3)
     (replace $replaced_3 SEVEN ZERO $replaced_3_7)

     (lbind colorfilter $objs $color_filtered)

     (lbind rbind gravitate $grav_func)
     (chain $grav_func first $color_filtered $gravity_applier)
     ($gravity_applier TWO $gravity_vec_2)
     ($gravity_applier ONE $gravity_vec_1)

     ($color_filtered THREE $color3_objs)
     ($color_filtered SEVEN $color7_objs)

     (fork shift identity $gravity_vec_2 $shift_3)
     (fork shift identity $gravity_vec_1 $shift_7)

     (mapply $shift_3 $color3_objs $moved_3_objs)
     (mapply $shift_7 $color7_objs $moved_7_objs)

     (paint $replaced_3_7 $moved_3_objs $painted_3)
     (paint $painted_3 $moved_7_objs $output)))

🎯 Each step is declarative and referentially transparent. Logic is explicit and composable, enabling:

  • Backward and forward reasoning
  • Symbolic queries
  • Program introspection

🔄 Why This Workflow?

Step Purpose
🧩 Expand Python Improve readability and structure
🧬 Convert to MeTTa Unlock symbolic reasoning and queryability
♻️ Retain Higher-Order Preserve abstraction and logic pipelines

This supports logic-heavy systems and AI-focused symbolic inference.


🧠 What MeTTa Unlocks

By translating to MeTTa, we gain:

Bidirectional reasoning — any variable can be an input or a goal

Composable relations — logic can be reused and inverted

Higher-order logic programming — function pipelines as first-class objects

Explainability — every clause is traceable and transparent

Symbolic search — pose abstract questions like "what shift aligns color-3 with color-7?"


🧰 Integration with ARC DSL

This MeTTa program works with the arc-dsl.metta library, which provides:

  • objects, paint, replace
  • Combinators like lbind, chain, fork
  • Spatial and logical operators

It composes with other ARC solvers to handle complex visual logic declaratively.