Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ SRCS = src/kernel/cogdiod_kernel.c \
src/elbo/elm_loader.c \
src/elbo/elbo_compiler.c \
src/llm/cogdiod_llm.c \
src/pm/unify.c \
packages/concept_node/concept_node_pkg.c \
packages/evaluation_link/evaluation_link_pkg.c \
packages/implication_link/implication_link_pkg.c
packages/implication_link/implication_link_pkg.c \
packages/predicate_node/predicate_node_pkg.c \
packages/number_node/number_node_pkg.c \
packages/variable_node/variable_node_pkg.c \
packages/inheritance_link/inheritance_link_pkg.c \
packages/similarity_link/similarity_link_pkg.c \
packages/member_link/member_link_pkg.c \
packages/and_link/and_link_pkg.c \
packages/or_link/or_link_pkg.c \
packages/not_link/not_link_pkg.c \
packages/equivalence_link/equivalence_link_pkg.c \
packages/list_link/list_link_pkg.c \
packages/bind_link/bind_link_pkg.c

TEST_SRC = tests/test_cogdiod.c
UNIT_TESTS = tests/test_pln.c tests/test_elm_loader.c \
Expand Down
15 changes: 15 additions & 0 deletions include/cogdiod.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,25 @@ int cogdiod_enqueue(CogDiodKernel* k, AtomIsolate* a);

/* ECAN */
void cogdiod_ecan_diffuse(CogDiodKernel* k);
void cogdiod_ecan_collect_rent(CogDiodKernel* k);
void cogdiod_ecan_normalize(CogDiodKernel* k);

/* Garbage Collection (Phase 1.3) */
int cogdiod_gc_sweep(CogDiodKernel* k);

/* Persistence (Phase 1.2) */
int cogdiod_save(CogDiodKernel* k, const char* path);
CogDiodKernel* cogdiod_load(const char* path);

/* Episodic TV History (Phase 1.5) */
TruthValue cogdiod_get_tv_history(CogDiodKernel* k, uint64_t uuid, int version);

/* Hebbian learning */
int cogdiod_hebbian_update(CogDiodKernel* k,
uint64_t src_uuid, uint64_t dst_uuid);
void cogdiod_hebbian_decay_all(CogDiodKernel* k);
int cogdiod_prune_weak_channels(CogDiodKernel* k, float threshold);
void cogdiod_set_ecan_params(CogDiodKernel* k, float spread, float decay, float rent);

/* PLN rules (defined in pln.c) */
TruthValue pln_deduce(TruthValue ab, TruthValue a);
Expand Down
49 changes: 49 additions & 0 deletions include/pattern_match.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* pattern_match.h — Pattern matching engine types and API (Phase 3)
*/

#pragma once

#include "cogdiod.h"
#include <stdint.h>

/* ─────────────────────────────────────────────────────────────────────────
* Binding structures
* ───────────────────────────────────────────────────────────────────────── */

#define MAX_BINDINGS 64

typedef struct {
uint64_t var_uuid; /* UUID of the VariableNode */
uint64_t bound_uuid; /* UUID of the atom it's bound to */
} Binding;

typedef struct {
Binding entries[MAX_BINDINGS];
int count;
} Bindings;

/* ─────────────────────────────────────────────────────────────────────────
* Binding operations
* ───────────────────────────────────────────────────────────────────────── */

void bindings_init(Bindings* b);
int bindings_add(Bindings* b, uint64_t var, uint64_t val);
uint64_t bindings_lookup(Bindings* b, uint64_t var);
int bindings_to_json(Bindings* b, char* buf, size_t max);

/* ─────────────────────────────────────────────────────────────────────────
* Unification and pattern matching
* ───────────────────────────────────────────────────────────────────────── */

typedef void (*MatchCallback)(uint64_t matched_uuid, Bindings* bindings, void* ctx);

int unify(CogDiodKernel* k,
AtomIsolate* pattern,
AtomIsolate* graph,
Bindings* out);

int cogdiod_match(CogDiodKernel* k,
uint64_t bind_link_uuid,
MatchCallback cb,
void* ctx);
50 changes: 50 additions & 0 deletions packages/and_link/and_link_pkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* and_link_pkg.c — C stub package for AndLink
*
* AndLink behaviour (Phase 2.9):
* - Boolean conjunction of child atoms
* - TV = min(strength) of all children, confidence combined
* - Fuzzy AND: strength = product of child strengths (when close to 1)
*/

#include "cogdiod.h"
#include "elm_types.h"
#include <stdlib.h>

static const uint8_t and_init_bc[] = {
OP_GET_TV,
OP_HALT
};

/*
* on-message: conjunctive TV combination
* For AND: strength = min(s1, s2, ...) or product for fuzzy logic
* confidence = min of child confidences
*/
static const uint8_t and_msg_bc[] = {
OP_GET_TV, /* regs[0..1] = current AND TV */
/* In a full impl: iterate children, take min */
/* Stub: revise with incoming to approximate */
OP_PLN_REV,
OP_SET_TV,
OP_ECAN_SP,
OP_HALT
};

static const uint8_t and_gc_bc[] = {
OP_NOP,
OP_HALT
};

ElmPackage* and_link_build_package(void) {
ElmStubDef def = {
.type_name = "AndLink",
.init_bc = and_init_bc,
.init_bc_len = sizeof(and_init_bc),
.msg_bc = and_msg_bc,
.msg_bc_len = sizeof(and_msg_bc),
.gc_bc = and_gc_bc,
.gc_bc_len = sizeof(and_gc_bc),
};
return elm_build_stub(&def);
}
47 changes: 47 additions & 0 deletions packages/bind_link/bind_link_pkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* bind_link_pkg.c — C stub package for BindLink
*
* BindLink behaviour (Phase 2.14, Phase 3):
* - Pattern match root for query execution
* - MSG_QUERY triggers pattern matching engine
* - Returns bindings satisfying the pattern
*/

#include "cogdiod.h"
#include "elm_types.h"
#include <stdlib.h>

static const uint8_t bind_init_bc[] = {
OP_GET_TV,
OP_HALT
};

/*
* on-message: pattern match execution
* MSG_QUERY: run pattern match and send results
* Results are sent as MSG_CUSTOM with binding data
*/
static const uint8_t bind_msg_bc[] = {
OP_GET_TV, /* regs[0..1] = pattern confidence */
/* Pattern matching would be done by elm_exec_msg calling into C */
OP_ECAN_SP, /* spread results */
OP_HALT
};

static const uint8_t bind_gc_bc[] = {
OP_NOP,
OP_HALT
};

ElmPackage* bind_link_build_package(void) {
ElmStubDef def = {
.type_name = "BindLink",
.init_bc = bind_init_bc,
.init_bc_len = sizeof(bind_init_bc),
.msg_bc = bind_msg_bc,
.msg_bc_len = sizeof(bind_msg_bc),
.gc_bc = bind_gc_bc,
.gc_bc_len = sizeof(bind_gc_bc),
};
return elm_build_stub(&def);
}
47 changes: 47 additions & 0 deletions packages/equivalence_link/equivalence_link_pkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* equivalence_link_pkg.c — C stub package for EquivalenceLink
*
* EquivalenceLink behaviour (Phase 2.8):
* - Bidirectional implication (A ⟺ B)
* - Merges TV with PLN_REV in both directions
* - Symmetric: A⟺B has same TV as B⟺A
*/

#include "cogdiod.h"
#include "elm_types.h"
#include <stdlib.h>

static const uint8_t equiv_init_bc[] = {
OP_GET_TV,
OP_HALT
};

/*
* on-message: bidirectional PLN revision
* Evidence from either direction revises the equivalence TV
*/
static const uint8_t equiv_msg_bc[] = {
OP_GET_TV, /* regs[0..1] = equivalence TV */
OP_PLN_REV, /* revise with incoming evidence */
OP_SET_TV, /* update equivalence */
OP_ECAN_SP, /* spread to both endpoints */
OP_HALT
};

static const uint8_t equiv_gc_bc[] = {
OP_NOP,
OP_HALT
};

ElmPackage* equivalence_link_build_package(void) {
ElmStubDef def = {
.type_name = "EquivalenceLink",
.init_bc = equiv_init_bc,
.init_bc_len = sizeof(equiv_init_bc),
.msg_bc = equiv_msg_bc,
.msg_bc_len = sizeof(equiv_msg_bc),
.gc_bc = equiv_gc_bc,
.gc_bc_len = sizeof(equiv_gc_bc),
};
return elm_build_stub(&def);
}
49 changes: 49 additions & 0 deletions packages/inheritance_link/inheritance_link_pkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* inheritance_link_pkg.c — C stub package for InheritanceLink
*
* InheritanceLink behaviour (Phase 2.5):
* - Subtype/supertype relation between ConceptNodes
* - Triggers PLN_DED when either end changes TV
* - Supports transitive inheritance reasoning
*/

#include "cogdiod.h"
#include "elm_types.h"
#include <stdlib.h>

static const uint8_t inh_init_bc[] = {
OP_GET_TV,
OP_HALT
};

/*
* on-message: PLN deduction for inheritance
* When subtype TV changes, deduce supertype TV
* regs[0..1] = this link's TV (inheritance strength)
* regs[10..11] = subtype TV (from MSG_SOURCE_CHANGED)
* PLN_DED → supertype TV
*/
static const uint8_t inh_msg_bc[] = {
OP_GET_TV, /* regs[0..1] = P(Super|Sub) */
OP_PLN_DED, /* deduce(P(Super|Sub), P(Sub)) → regs[4..5] */
OP_ECAN_SP, /* Spread to supertype */
OP_HALT
};

static const uint8_t inh_gc_bc[] = {
OP_NOP,
OP_HALT
};

ElmPackage* inheritance_link_build_package(void) {
ElmStubDef def = {
.type_name = "InheritanceLink",
.init_bc = inh_init_bc,
.init_bc_len = sizeof(inh_init_bc),
.msg_bc = inh_msg_bc,
.msg_bc_len = sizeof(inh_msg_bc),
.gc_bc = inh_gc_bc,
.gc_bc_len = sizeof(inh_gc_bc),
};
return elm_build_stub(&def);
}
45 changes: 45 additions & 0 deletions packages/list_link/list_link_pkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* list_link_pkg.c — C stub package for ListLink
*
* ListLink behaviour (Phase 2.4):
* - Ordered list of child atom UUIDs
* - MSG_SOURCE_CHANGED rebroadcasts to all children
* - Maintains ordered sequence semantics
*/

#include "cogdiod.h"
#include "elm_types.h"
#include <stdlib.h>

static const uint8_t list_init_bc[] = {
OP_GET_TV,
OP_HALT
};

/*
* on-message: propagate changes to all children
* ListLink acts as an aggregator, spreading attention to its ordered members
*/
static const uint8_t list_msg_bc[] = {
OP_GET_TV, /* regs[0..1] = list TV (metadata) */
OP_ECAN_SP, /* spread to all children in order */
OP_HALT
};

static const uint8_t list_gc_bc[] = {
OP_NOP,
OP_HALT
};

ElmPackage* list_link_build_package(void) {
ElmStubDef def = {
.type_name = "ListLink",
.init_bc = list_init_bc,
.init_bc_len = sizeof(list_init_bc),
.msg_bc = list_msg_bc,
.msg_bc_len = sizeof(list_msg_bc),
.gc_bc = list_gc_bc,
.gc_bc_len = sizeof(list_gc_bc),
};
return elm_build_stub(&def);
}
46 changes: 46 additions & 0 deletions packages/member_link/member_link_pkg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* member_link_pkg.c — C stub package for MemberLink
*
* MemberLink behaviour (Phase 2.7):
* - Set membership relation (element ∈ set)
* - Participates in abduction (OP_PLN_ABD)
* - TV represents membership probability
*/

#include "cogdiod.h"
#include "elm_types.h"
#include <stdlib.h>

static const uint8_t mem_init_bc[] = {
OP_GET_TV,
OP_HALT
};

/*
* on-message: abductive reasoning for set membership
* Given set properties, abduct member properties
*/
static const uint8_t mem_msg_bc[] = {
OP_GET_TV, /* regs[0..1] = membership TV */
OP_PLN_ABD, /* abduction: infer member properties from set */
OP_ECAN_SP, /* spread to member and set */
OP_HALT
};

static const uint8_t mem_gc_bc[] = {
OP_NOP,
OP_HALT
};

ElmPackage* member_link_build_package(void) {
ElmStubDef def = {
.type_name = "MemberLink",
.init_bc = mem_init_bc,
.init_bc_len = sizeof(mem_init_bc),
.msg_bc = mem_msg_bc,
.msg_bc_len = sizeof(mem_msg_bc),
.gc_bc = mem_gc_bc,
.gc_bc_len = sizeof(mem_gc_bc),
};
return elm_build_stub(&def);
}
Loading
Loading