code wiki / _hdl_build / nx_connect_match_lib.nx
nx_connect_match_lib.nx source
↩ module page · 81 lines · 4041 B
1// nx_connect_match_lib.nx -- the RECIPROCAL language-exchange match engine, extracted as a shared library
2// so the verifying gate (nx_connect_match) and the LIVE app (nx_connect_serve /partners) bind ONE
3// implementation of the law. Before this split the live page printed HARDCODED scores while claiming they
4// came from "the gated match engine" -- the page asserted a computation it never ran. No main, no syscalls,
5// no allocation, so any consumer can import it.
6//
7// THE LAW: language exchange is COMPLEMENTARY, not similarity. A good partner TEACHES what you want to LEARN
8// and LEARNS what you can TEACH, so the core signal is a HARMONIC MEAN of the two directional fits -- if
9// either direction is zero the pair collapses to zero, which is what makes reciprocity NECESSARY rather than
10// merely preferred. Shared interests modulate but can never rescue a one-sided pair. Integer only, no floats.
11// The output is an INTERNAL RANK WEIGHT 0..100 and must never be presented as a compatibility score (Joel &
12// Eastwick, PNAS 2020: partner traits explain ~5% of long-term variance).
13//
14// SAFETY BY CONSTRUCTION: the dating/romance lane is UNREACHABLE to or from a minor in BOTH directions, even
15// when the underlying reciprocal score is perfect, while language exchange stays fully open to them.
16// license_tier: ORIGINAL
17
18const MX_LANE_LANG: i64 = 0
19const MX_LANE_FRIEND: i64 = 1
20const MX_LANE_COMMUNITY: i64 = 2
21const MX_LANE_DATE: i64 = 3
22const MX_MINOR_AGE: i64 = 18
23const MX_EXCLUDED: i64 = 0-1 // by-construction exclusion sentinel (negative, never a low score)
24
25// count of bits set in BOTH masks (intersection size), walked bit-by-bit so no bitwise operators are needed.
26func mx_inter_popc(a: i64, b: i64) -> i64 {
27 var c: i64 = 0
28 var x: i64 = a
29 var y: i64 = b
30 while x > 0 {
31 let ax: i64 = x - (x / 2) * 2
32 let by: i64 = y - (y / 2) * 2
33 if ax == 1 { if by == 1 { c = c + 1 } }
34 x = x / 2
35 y = y / 2
36 }
37 return c
38}
39func mx_clamp100(v: i64) -> i64 { if v > 100 { return 100 } return v }
40
41// integer harmonic mean; zero if EITHER side is zero -- this single line is the reciprocity enforcer.
42func mx_hm(a: i64, b: i64) -> i64 {
43 if a == 0 { return 0 }
44 if b == 0 { return 0 }
45 return (2 * a * b) / (a + b)
46}
47
48// reciprocal complementarity 0..100: each directional fit scaled (1 teachable language -> 50, 2+ -> 100), then HM.
49func mx_recip_score(al: i64, at: i64, bl: i64, bt: i64) -> i64 {
50 let ab: i64 = mx_inter_popc(al, bt) // A learns from B
51 let ba: i64 = mx_inter_popc(bl, at) // B learns from A
52 let asc: i64 = mx_clamp100(ab * 50)
53 let bsc: i64 = mx_clamp100(ba * 50)
54 return mx_hm(asc, bsc)
55}
56
57// NEG-CONTROL twin: the naive arithmetic mean, which does NOT enforce reciprocity. Kept in the library so the
58// gate measures the real alternative rather than a re-typed approximation of it.
59func mx_recip_arith(al: i64, at: i64, bl: i64, bt: i64) -> i64 {
60 let ab: i64 = mx_inter_popc(al, bt)
61 let ba: i64 = mx_inter_popc(bl, at)
62 let asc: i64 = mx_clamp100(ab * 50)
63 let bsc: i64 = mx_clamp100(ba * 50)
64 return (asc + bsc) / 2
65}
66
67func mx_interest_score(ai: i64, bi: i64) -> i64 {
68 let s: i64 = mx_inter_popc(ai, bi)
69 return mx_clamp100(s * 34)
70}
71
72// the engine: lane-aware and safety-gated. Returns MX_EXCLUDED (<0) or an internal rank weight 0..100.
73func mx_match_weight(a_age: i64, a_learn: i64, a_teach: i64, a_int: i64,
74 b_age: i64, b_learn: i64, b_teach: i64, b_int: i64, lane: i64) -> i64 {
75 if lane == MX_LANE_DATE { if a_age < MX_MINOR_AGE { return MX_EXCLUDED } }
76 if lane == MX_LANE_DATE { if b_age < MX_MINOR_AGE { return MX_EXCLUDED } }
77 let r: i64 = mx_recip_score(a_learn, a_teach, b_learn, b_teach)
78 if r == 0 { return 0 } // reciprocity is NECESSARY, not merely weighted
79 let isc: i64 = mx_interest_score(a_int, b_int)
80 return (r * 70 + isc * 30) / 100 // 70% reciprocity, 30% shared interests
81}