Skip to content

Commit

Permalink
Add natural transformation for StateF to State
Browse files Browse the repository at this point in the history
  • Loading branch information
flyfish30 committed Nov 18, 2024
1 parent 0e2a590 commit 38cc2d7
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 33 deletions.
14 changes: 14 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ pub fn build(b: *std.Build) void {
// step when running `zig build`).
b.installArtifact(exe);

const lib = b.addStaticLibrary(.{
.name = "zig-cats",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});

// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);

// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
Expand Down
24 changes: 13 additions & 11 deletions src/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn ComposableLam(
any_lam: *anyopaque,
call_fn: *const fn (*anyopaque, A) B,
ref_fn: *const fn (*Self) *Self,
unref_fn: *const fn (*Self) void,
unref_fn: *const fn (*Self) bool,

const Self = @This();
const self_cfg = cfg;
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn ComposableLam(
return self;
}

fn unref(self: *Self) void {
fn unref(self: *Self) bool {
const self_any_lam_bytes = std.mem.asBytes(&self.any_lam);
var lam: InitLam = undefined;
const lam_bytes = std.mem.asBytes(&lam);
Expand All @@ -263,13 +263,14 @@ pub fn ComposableLam(
// lam.unrefSubLam()
else
tryStrongUnref(lam);
return;
return false;
}

deinitOrUnref(lam);
// std.debug.print("destroy SmallLam comp_lam = {*}\n", .{self});
self.ref_count = 0;
cfg.allocator.destroy(self);
return true;
}

fn call(self_any_lam: *anyopaque, a: A) B {
Expand Down Expand Up @@ -305,7 +306,7 @@ pub fn ComposableLam(
return self;
}

fn unref(self: *Self) void {
fn unref(self: *Self) bool {
const real_lam: *InitLam = @alignCast(@ptrCast(self.any_lam));
// std.debug.print("unref ref_count={d}, NormalLam = {*}\n", .{ self.ref_count, self });
if (self.ref_count > 1) {
Expand All @@ -314,7 +315,7 @@ pub fn ComposableLam(
real_lam.unrefSubLam()
else
tryStrongUnref(real_lam);
return;
return false;
}

deinitOrUnref(real_lam.*);
Expand All @@ -325,6 +326,7 @@ pub fn ComposableLam(
self.ref_count = 0;
// std.debug.print("destroy NormalLam comp_lam = {*}\n", .{self});
cfg.allocator.destroy(self);
return true;
}

fn call(self_any_lam: *anyopaque, a: A) B {
Expand All @@ -348,8 +350,8 @@ pub fn ComposableLam(
return self;
}

pub fn strongUnref(self: *Self) void {
self.unref_fn(self);
pub fn strongUnref(self: *Self) bool {
return self.unref_fn(self);
}

fn AppendedRetType(comptime InType: type, comptime Lam: type) type {
Expand Down Expand Up @@ -397,13 +399,13 @@ pub fn ComposableLam(

pub fn unrefSubLam(self_new: SelfNew) void {
// std.debug.print("appended_comp_lam unrefSubLam comp_lam={*}\n", .{self_new.comp_lam});
self_new.comp_lam.strongUnref();
_ = self_new.comp_lam.strongUnref();
tryStrongUnref(self_new.append_lam);
}

pub fn deinit(self_new: SelfNew) void {
// std.debug.print("appended_comp_lam deinit comp_lam={*}\n", .{self_new.comp_lam});
self_new.comp_lam.strongUnref();
_ = self_new.comp_lam.strongUnref();
deinitOrUnref(self_new.append_lam);
}

Expand Down Expand Up @@ -446,13 +448,13 @@ test ComposableLam {
try testing.expectEqual(8, comp2.call(40));
const comp3 = try comp2.appendLam(add_e_f32_lam);
try testing.expectEqual(10.72, comp3.call(40));
comp3.strongUnref();
_ = comp3.strongUnref();

const comp11 = try ComposableLam(cfg, u32, f64).create(add_pi_lam);
const comp12 = try comp11.appendLam(div_5_lam);
const comp13 = try comp12.appendLam(point_move_lam);
try testing.expectEqual(.{ 54.2, 34.83, 80.56 }, comp13.call(40));
comp13.strongUnref();
_ = comp13.strongUnref();
}

pub fn LamWrapper(comptime cfg: anytype, comptime Lam: type) type {
Expand Down
6 changes: 3 additions & 3 deletions src/free_types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn FreeM(comptime F: TCtor) TCtor {
self.free_m[0].deinit();
allocator.destroy(self.free_m[0]);
} else if (self == .free_fop) {
self.free_fop[0].strongUnref();
_ = self.free_fop[0].strongUnref();
const op_info = self.free_fop[1];
const fa_op_ctors = GetOpCtors(F, A);
const op_ctor_info = fa_op_ctors[op_info.op_e];
Expand Down Expand Up @@ -274,7 +274,7 @@ pub fn FreeM(comptime F: TCtor) TCtor {
const x_to_freem = self.free_fop[0].strongRef();
const comp_iter = try x_to_freem.appendLam(inner_iter);
const fa = try f_impl.fmapLam(.InplaceMap, comp_iter, fx);
comp_iter.strongUnref();
_ = comp_iter.strongUnref();
defer base.deinitOrUnref(fa);
return f(fa);
}
Expand Down Expand Up @@ -377,7 +377,7 @@ pub fn FreeM(comptime F: TCtor) TCtor {
const comp_fold = try x_to_freem.appendLam(inner_fold);
const f_acc_m = try f_impl.fmapLam(.InplaceMap, comp_fold, fx);
const m_acc_m = try nat_impl.trans(MImpl.F(A), f_acc_m);
comp_fold.strongUnref();
_ = comp_fold.strongUnref();
base.deinitOrUnref(f_acc_m);
defer MImpl.deinitFa(m_acc_m, base.getDeinitOrUnref(MImpl.F(A)));
return try @constCast(&m_impl).join(A, m_acc_m);
Expand Down
Loading

0 comments on commit 38cc2d7

Please sign in to comment.