Kb – Prolog 知识库
Kb – Prolog Knowledge Base

原始链接: https://github.com/mat-mgm/kb-prolog

本项目是一个作为硕士论文原型开发的本地优先、超关系型知识库。它利用**内容可寻址存储(CAS)**系统,通过 SHA-256 哈希对文件进行去重,从而确保数据完整性和原子提交。 该系统的核心采用**超关系图**结构,使用 `statement(Subject, Predicate, Object, Properties)` 形式的 Prolog 条款。通过支持具体化(即主语和宾语本身也可以是陈述),该系统允许构建复杂的嵌套式主张。架构主要基于 **Trealla Prolog** 构建,并集成了 **SQLite** 用于持久化存储,以及 **Raylib** 用于交互式图形界面。 主要功能包括: * **时间旅行:** 使用 `replaces_id` 的版本控制系统,用于追踪陈述的历史记录。 * **性能:** 使用双向递归公用表表达式(CTE),仅将必要的子图加载到内存中。 * **可扩展性:** 基于 C 语言的 FFI 层提供了高性能的实用工具和渲染能力。 该平台包含用于数据摄取、搜索和数据库维护的命令行界面(CLI),以及一个可视化图形浏览器。这是一个开源(GPL-3.0)工具,专为深度、演进式知识管理而设计,并通过 Nix 支持可复现的构建。

最近一篇 Hacker News 文章分享了 **Kb**,这是一个基于 Prolog 的知识库系统 (github.com/mat-mgm)。该项目立即引起了开发社区的关注,并在正在构建类似逻辑编程引擎和声明式 DSL 的用户中引发了讨论。 该讨论帖的要点包括: * **同行关注:** 目前正在开发自己的逻辑引擎的开发者对该项目的设计选择很感兴趣,并正在探索潜在的合作或架构集成,例如替换 SQLite/Prolog 后端。 * **功能潜力:** 参与者正在询问高级功能,特别是该系统是否能支持真值维护系统,或作为一种封闭的、可重放的环境使用。 * **实际应用:** 用户正在寻求明确该工具的理想应用场景,以及其查询系统的长期可扩展性。 这次对话突显了逻辑编程领域对轻量级、可编程知识管理工具这一小众但活跃领域的兴趣。
相关文章

原文

A local-first, hyper-relational knowledge base with content-addressable storage (CAS). Built as a Master's thesis prototype.

screenshot

  • Hyper-relational graph: Knowledge is stored as statement(Subject, Predicate, Object, Properties). Subjects and objects can themselves be statements (reification), enabling claims about claims.
  • Content-addressable storage: Files are staged, SHA-256 hashed, and committed atomically alongside their graph metadata. Deduplication is automatic.
  • Time-travel: Updates create new statement versions linked via replaces_id. pl history walks the version chain.
  • Prolog-first: Trealla Prolog is the main runtime. SQLite and Raylib are accessed via C shared libraries loaded through FFI.
  • Interactive GUI: A Raylib-based graph viewer with image previews, a query bar, and node search.

Dependencies: Clang, X11 (Linux) or Xcode CLT (macOS). Trealla Prolog, Raylib, raygui, and SQLite are included as submodules.

git clone --recurse-submodules <repo-url>
cd kb
make

This builds libcas.so, libgui.so, and libsqlite3.so in the project root. Raylib is compiled from source during make.

To build Trealla from source:

cd vendor/trealla && make

Then ensure tpl is on your $PATH.

# Load a context into memory and open a REPL
tpl -l main.pl -- pl load concept(mathematics)

# Assert a new statement
tpl -l main.pl -- pl assert \
  "statement(concept(mathematics), foundation_of, concept(logic), [])"

# Full-text search
tpl -l main.pl -- pl search mathematics

# View version history of a term
tpl -l main.pl -- pl history concept(mathematics)

# Ingest a file into CAS
tpl -l main.pl -- cas add document.pdf

# List CAS objects
tpl -l main.pl -- cas list

# Launch the GUI
tpl -l main.pl -- gui

# Check database consistency
tpl -l main.pl -- pl verify

# Run garbage collection
tpl -l main.pl -- pl gc
main.pl             CLI router and REPL
prolog/
  sync.pl           2-phase commit: marshal Prolog terms ↔ SQLite
  cas.pl            FFI bindings to libcas.so
  db.pl             SQLite queries
  gui.pl            Raylib frontend (yield-loop pattern)
  ontology.pl       In-memory knowledge graph
src/
  cas.c / cas.h     CAS: stage, SHA-256 hash, commit atomically, verify
  gui.c / gui.h     Raylib renderer and input handler
  graph.c / graph.h Graph layout
  util.c / util.h   SHA-256, MD5
sql/
  schema.sql        SQLite schema (WAL mode, FTS5, reification)
vendor/             Trealla, Raylib, raygui, SQLite (submodules)

Every piece of knowledge is a statement/4 term:

statement(Subject, Predicate, Object, Properties)

Subject and Object are Prolog terms or integer IDs pointing to other rows in the statement table, enabling arbitrary nesting. The SQLite schema mirrors this with ANY-typed columns and a replaces_id foreign key for versioning.

Example:

% Alice claims (with certainty 0.9) that Bob likes pizza
statement(
  person(alice),
  claims,
  statement(person(bob), likes, food(pizza), []),
  [certainty(0.9)]
)

Context loading uses bidirectional recursive CTEs to pull only the subgraph reachable from a seed term into Prolog's in-memory working set.

A kb.nix shell is provided for reproducible builds:

GPL-3.0 — see LICENSE.md.

联系我们 contact @ memedata.com