// propositions_gate.el - the READ primitive over memory text (native el).
// Proves triples are recovered from free memory text and that SACRED polarity
// survives extraction (a negative memory must yield a NOT-triple).

fn pg_check(text: String, want_pol: String) -> String {
    let p: [String] = prop_extract_one(text, "nd-test")
    let pol: String = slots_get(p, "polarity")
    let ok: String = "MISMATCH"
    if str_eq(pol, want_pol) { let ok = "ok" }
    return "  " + prop_repr(p) + "   pol=" + pol + " expected=" + want_pol + " (" + ok + ")\n"
}

fn pg_pol_ok(text: String, want_pol: String) -> Int {
    let p: [String] = prop_extract_one(text, "nd-test")
    if str_eq(slots_get(p, "polarity"), want_pol) { return 1 }
    return 0
}

fn run_prop_gate() -> String {
    let m1: String = "Neuron stores memories in SQLite."
    let m2: String = "The engram does not delete a memory."
    let m3: String = "Salience never drops the negation."
    let m4: String = "The teacher gives the book to the children."

    let rep: String = "==== ELP proposition extraction (memory text -> triples) ====\n"
    let rep = rep + pg_check(m1, "aff")
    let rep = rep + pg_check(m2, "neg")
    let rep = rep + pg_check(m3, "neg")
    let rep = rep + pg_check(m4, "aff")

    // multi-sentence memory: one triple per sentence, order preserved
    let doc: String = "Neuron persists learning. It does not forget the library."
    let props: [String] = prop_extract(doc, "nd-doc")
    let rep = rep + "  --- multi-sentence doc (" + int_to_str(native_list_len(props)) + " props) ---\n"
    let di: Int = 0
    while di < native_list_len(props) {
        let rep = rep + "    " + native_list_get(props, di) + "\n"
        let di = di + 1
    }

    let ok: Int = 0
    if pg_pol_ok(m1, "aff") == 1 { let ok = ok + 1 }
    if pg_pol_ok(m2, "neg") == 1 { let ok = ok + 1 }
    if pg_pol_ok(m3, "neg") == 1 { let ok = ok + 1 }
    if pg_pol_ok(m4, "aff") == 1 { let ok = ok + 1 }
    let rep = rep + "-----------------------------------------------------------------\n"
    let rep = rep + "SACRED polarity correct on extraction: " + int_to_str(ok) + "/4\n"
    if ok == 4 { let rep = rep + "PROP GATE: PASS\n" } else { let rep = rep + "PROP GATE: FAIL\n" }
    return rep
}

println(run_prop_gate())
