Skip to content

Commit 8af6a7e

Browse files
authored
Merge pull request #164 from gHashTag/feat/issue-163
fix(query): replace 5 catch unreachable with try in tri_query_commands.zig
2 parents 7a44bb2 + 883b029 commit 8af6a7e

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

src/tri/tri_query_commands.zig

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,9 +1038,9 @@ const BipolarBigInt = struct {
10381038
/// HRR Binding: Circular convolution
10391039
/// c[k] = Σ A[i] * B[(k-i) mod n]
10401040
/// Provides better unbind accuracy than element-wise multiplication
1041-
pub fn bindHRR(self: BipolarBigInt, other: BipolarBigInt) BipolarBigInt {
1041+
pub fn bindHRR(self: BipolarBigInt, other: BipolarBigInt) !BipolarBigInt {
10421042
var result = BipolarBigInt{ .trits = undefined, .allocator = self.allocator };
1043-
result.trits = self.allocator.alloc(i8, self.trits.len) catch unreachable;
1043+
result.trits = try self.allocator.alloc(i8, self.trits.len);
10441044
const n = self.trits.len;
10451045

10461046
// Circular convolution
@@ -1062,10 +1062,10 @@ const BipolarBigInt = struct {
10621062
/// HRR Unbinding: Circular correlation (convolve with inversed vector)
10631063
/// For real-valued HRR, unbind is correlation with the reversed vector
10641064
/// Since our vectors are symmetric in distribution, we use convolution
1065-
pub fn unbindHRR(self: BipolarBigInt, key: BipolarBigInt) BipolarBigInt {
1065+
pub fn unbindHRR(self: BipolarBigInt, key: BipolarBigInt) !BipolarBigInt {
10661066
// Create inversed key (reversed)
10671067
var inversed = BipolarBigInt{ .trits = undefined, .allocator = self.allocator };
1068-
inversed.trits = self.allocator.alloc(i8, key.trits.len) catch unreachable;
1068+
inversed.trits = try self.allocator.alloc(i8, key.trits.len);
10691069
const n = key.trits.len;
10701070

10711071
// Reverse the key vector (safe circular index)
@@ -1076,24 +1076,24 @@ const BipolarBigInt = struct {
10761076
}
10771077

10781078
// Correlate = convolve with inversed
1079-
const result = self.bindHRR(inversed);
1079+
const result = try self.bindHRR(inversed);
10801080
inversed.deinit();
10811081
return result;
10821082
}
10831083

10841084
// Legacy element-wise binding (kept for comparison)
1085-
pub fn bind(self: BipolarBigInt, other: BipolarBigInt) BipolarBigInt {
1085+
pub fn bind(self: BipolarBigInt, other: BipolarBigInt) !BipolarBigInt {
10861086
var result = BipolarBigInt{ .trits = undefined, .allocator = self.allocator };
1087-
result.trits = self.allocator.alloc(i8, self.trits.len) catch unreachable;
1087+
result.trits = try self.allocator.alloc(i8, self.trits.len);
10881088
for (0..self.trits.len) |i| {
10891089
result.trits[i] = self.trits[i] * other.trits[i];
10901090
}
10911091
return result;
10921092
}
10931093

1094-
pub fn unbind(self: BipolarBigInt, key: BipolarBigInt) BipolarBigInt {
1094+
pub fn unbind(self: BipolarBigInt, key: BipolarBigInt) !BipolarBigInt {
10951095
// Unbind = bind with inverse (same as bind for bipolar)
1096-
return self.bind(key);
1096+
return try self.bind(key);
10971097
}
10981098

10991099
pub fn cosineSimilarity(self: BipolarBigInt, other: BipolarBigInt) f64 {
@@ -1117,9 +1117,9 @@ const BipolarBigInt = struct {
11171117
return self.cosineSimilarity(other);
11181118
}
11191119

1120-
pub fn bundle(self: BipolarBigInt, other: BipolarBigInt) BipolarBigInt {
1120+
pub fn bundle(self: BipolarBigInt, other: BipolarBigInt) !BipolarBigInt {
11211121
var result = BipolarBigInt{ .trits = undefined, .allocator = self.allocator };
1122-
result.trits = self.allocator.alloc(i8, self.trits.len) catch unreachable;
1122+
result.trits = try self.allocator.alloc(i8, self.trits.len);
11231123
for (0..self.trits.len) |i| {
11241124
const sum = self.trits[i] + other.trits[i];
11251125
result.trits[i] = if (sum > 0) @as(i8, 1) else if (sum < 0) @as(i8, -1) else @as(i8, 0);
@@ -1499,7 +1499,7 @@ pub fn runQueryCommand(allocator: std.mem.Allocator, args: []const []const u8) !
14991499
const seed = 0xCCDD000 + @as(u64, @intCast(i)) * 7919;
15001500

15011501
// Use semanticRandom with configurable dimension (Phase 2.3)
1502-
entities[i] = BipolarBigInt.semanticRandom(std.heap.page_allocator, dim, seed, category_id) catch unreachable;
1502+
entities[i] = try BipolarBigInt.semanticRandom(std.heap.page_allocator, dim, seed, category_id);
15031503
}
15041504

15051505
// Build relation memories (bundle pairs)
@@ -1518,13 +1518,16 @@ pub fn runQueryCommand(allocator: std.mem.Allocator, args: []const []const u8) !
15181518
for (0..5) |i| {
15191519
// Phase 2.3: Choose binding method based on use_hrr flag
15201520
if (use_hrr) {
1521-
binds[i] = entities[all_pairs[rel][i][0]].bindHRR(entities[all_pairs[rel][i][1]]);
1521+
binds[i] = try entities[all_pairs[rel][i][0]].bindHRR(entities[all_pairs[rel][i][1]]);
15221522
} else {
1523-
binds[i] = entities[all_pairs[rel][i][0]].bind(entities[all_pairs[rel][i][1]]);
1523+
binds[i] = try entities[all_pairs[rel][i][0]].bind(entities[all_pairs[rel][i][1]]);
15241524
}
15251525
}
15261526
// Bundle all 5 pairs
1527-
mem[rel] = binds[0].bundle(binds[1]).bundle(binds[2]).bundle(binds[3]).bundle(binds[4]);
1527+
mem[rel] = try binds[0].bundle(binds[1]);
1528+
mem[rel] = try mem[rel].bundle(binds[2]);
1529+
mem[rel] = try mem[rel].bundle(binds[3]);
1530+
mem[rel] = try mem[rel].bundle(binds[4]);
15281531
}
15291532

15301533
print("KG ready.\n\n", .{});
@@ -1565,7 +1568,7 @@ pub fn runQueryCommand(allocator: std.mem.Allocator, args: []const []const u8) !
15651568

15661569
const key = entities[current_idx];
15671570
// Phase 2.3: Choose unbind method based on use_hrr flag
1568-
var res = if (use_hrr) mem[rel_idx].unbindHRR(key) else mem[rel_idx].unbind(key);
1571+
var res = if (use_hrr) try mem[rel_idx].unbindHRR(key) else try mem[rel_idx].unbind(key);
15691572

15701573
var best_idx: usize = 0;
15711574
var best_sim: f64 = -2.0;
@@ -1634,7 +1637,7 @@ pub fn runQueryCommand(allocator: std.mem.Allocator, args: []const []const u8) !
16341637

16351638
const key = entities[entity_idx];
16361639
// Phase 2.3: Choose unbind method based on use_hrr flag
1637-
var res = if (use_hrr) mem[rel_idx].unbindHRR(key) else mem[rel_idx].unbind(key);
1640+
var res = if (use_hrr) try mem[rel_idx].unbindHRR(key) else try mem[rel_idx].unbind(key);
16381641

16391642
var best_idx: usize = 0;
16401643
var best_sim: f64 = -2.0;

0 commit comments

Comments
 (0)