Skip to content

Latest commit

 

History

History
265 lines (191 loc) · 9.82 KB

File metadata and controls

265 lines (191 loc) · 9.82 KB

类型系统重构实现总结 / Type System Refactoring Implementation Summary

中文 | English


中文

已完成的工作

1. 核心类型系统重构 ✅

头文件重构

  • include/kos_ontology.h
    • 移除了 C 语言结构体定义(AtomicTypeDef, PredicateTypeDef, EventTypeDef
    • 新设计基于类型构造器(Π、Σ、Sum等)
    • 所有类型定义都是 kos_term* 类型
    • 新增 TypeDefinition 结构,存储类型定义的名称、类型定义(kos_term*)和上下文

实现文件重构

  • src/core/ontology_manager.c
    • 完全重写,实现了基于类型构造的类型本体管理
    • 实现了类型定义的CRUD操作(添加、查找、更新、删除)
    • 实现了类型实例化和验证(通过类型检查)
    • 实现了持久化存储框架(序列化/反序列化)

2. 领域代码部分更新 ✅

类型本体初始化

  • src/domain/manufacturing/ontology_setup.c
    • 更新为使用类型构造器构造类型定义
    • 使用 kos_mk_sigma 构造事件类型(FailEvt, ProcStep, Anomaly)
    • 使用 kos_mk_pi 构造谓词类型(InRoute, Overlap)
    • 使用基础Sort(kos_mk_id, kos_mk_time, kos_mk_prop)构造基础类型

头文件更新

  • include/kos_manufacturing.h
    • 注释掉了旧的API函数声明(等待迁移)
    • 添加了TODO说明,指导后续迁移

类型构建器部分更新

  • src/domain/manufacturing/types.c
    • 更新了 kos_mk_batch_id 函数以使用新的类型系统

3. 文档 ✅

  • TYPE_CONSTRUCTION_EXAMPLES.md - 详细的类型构造示例和代码
  • TYPE_CONSTRUCTION_API.md - 完整的API设计文档
  • TYPE_SYSTEM_REFACTORING_PLAN.md - 重构计划
  • MANUFACTURING_MIGRATION_GUIDE.md - 迁移指南
  • CODE_MIGRATION_STATUS.md - 迁移状态
  • IMPLEMENTATION_STATUS.md - 实现状态

核心设计要点

类型论原则

  1. 所有类型都是类型构造的产物

    • 通过Π类型、Σ类型、Sum类型等构造
    • 不存在预定义的C语言结构体类型
  2. 类型定义是 kos_term* 类型

    • 类型定义本身是类型系统中的项(term)
    • 存储在 TypeDefinition 结构中
  3. 类型实例化需要类型检查

    • 必须通过 kos_checkkos_type_check 验证
    • 只有通过类型检查的实例才是有效的

类型构造示例

事件类型(Σ类型)

// FailEvt ≡ Σ(b: BatchID). Σ(err: ErrorCode). Σ(t: Time). Prop
kos_term* time_prop_sigma = kos_mk_sigma(time_type, prop_type);
kos_term* error_time_prop_sigma = kos_mk_sigma(error_code_type, time_prop_sigma);
kos_term* fail_evt_type = kos_mk_sigma(batch_id_type, error_time_prop_sigma);
kos_ontology_add_type_definition(ontology, "FailEvt", fail_evt_type, NULL);

谓词类型(Π类型)

// InRoute ≡ Π(b: BatchID). Π(m: Machine). Prop
kos_term* machine_prop_pi = kos_mk_pi(machine_type, prop_type);
kos_term* in_route_type = kos_mk_pi(batch_id_type, machine_prop_pi);
kos_ontology_add_type_definition(ontology, "InRoute", in_route_type, NULL);

待完成的工作

1. 领域代码迁移 ⏳

src/domain/manufacturing/ontology_crud.c

  • 需要完全重写以使用新的类型定义API
  • 当前仍使用旧的API(已注释)

src/domain/manufacturing/types.c

  • 需要更新所有类型构造函数
  • 当前只更新了 kos_mk_batch_id

include/kos_manufacturing.h

  • 需要添加新的基于类型构造的API函数声明
  • 旧API函数已注释,等待迁移

2. 序列化/反序列化完善 ⏳

  • kos_ontology_serialize 函数已实现基本框架,但需要完善
  • kos_ontology_deserialize 函数需要完整的JSON解析实现

3. 类型检查集成 ⏳

  • 确保类型检查器(kos_check)能够正确处理所有类型构造
  • 添加类型检查的错误报告机制

当前编译状态

编译时会出现以下错误:

  1. ontology_crud.c 中仍使用已注释的旧API函数
  2. 一些类型构造函数需要更新

这些错误是预期的,因为领域代码的迁移仍在进行中。

下一步建议

  1. 暂时禁用相关代码:注释掉 ontology_crud.c 中使用旧API的代码
  2. 逐步迁移:逐个文件迁移到新的类型系统
  3. 添加测试:为新的类型系统添加单元测试
  4. 完善文档:添加更多使用示例和最佳实践

成就

这次重构成功地将类型系统从基于C语言结构体的设计迁移到了基于类型论的设计,完全符合直觉类型论(ITT)的原则。所有类型定义现在都通过类型构造器构造,类型实例化必须通过类型检查验证,这确保了类型系统的正确性和一致性。


English

Type System Refactoring Implementation Summary

Completed Work

1. Core Type System Refactoring ✅

Header File Refactoring

  • include/kos_ontology.h
    • Removed C language struct definitions (AtomicTypeDef, PredicateTypeDef, EventTypeDef)
    • New design based on type constructors (Π, Σ, Sum, etc.)
    • All type definitions are kos_term* types
    • Added TypeDefinition structure to store type definition name, type definition (kos_term*), and context

Implementation File Refactoring

  • src/core/ontology_manager.c
    • Completely rewritten, implemented type ontology management based on type construction
    • Implemented CRUD operations for type definitions (add, find, update, delete)
    • Implemented type instantiation and verification (through type checking)
    • Implemented persistence storage framework (serialization/deserialization)

2. Domain Code Partial Updates ✅

Type Ontology Initialization

  • src/domain/manufacturing/ontology_setup.c
    • Updated to use type constructors to construct type definitions
    • Used kos_mk_sigma to construct event types (FailEvt, ProcStep, Anomaly)
    • Used kos_mk_pi to construct predicate types (InRoute, Overlap)
    • Used basic Sorts (kos_mk_id, kos_mk_time, kos_mk_prop) to construct basic types

Header File Updates

  • include/kos_manufacturing.h
    • Commented out old API function declarations (awaiting migration)
    • Added TODO notes to guide subsequent migration

Type Builder Partial Updates

  • src/domain/manufacturing/types.c
    • Updated kos_mk_batch_id function to use new type system

3. Documentation ✅

  • TYPE_CONSTRUCTION_EXAMPLES.md - Detailed type construction examples and code
  • TYPE_CONSTRUCTION_API.md - Complete API design documentation
  • TYPE_SYSTEM_REFACTORING_PLAN.md - Refactoring plan
  • MANUFACTURING_MIGRATION_GUIDE.md - Migration guide
  • CODE_MIGRATION_STATUS.md - Migration status
  • IMPLEMENTATION_STATUS.md - Implementation status

Core Design Points

Type Theory Principles

  1. All types are products of type construction

    • Constructed through Π types, Σ types, Sum types, etc.
    • No predefined C language struct types
  2. Type definitions are kos_term* types

    • Type definitions themselves are terms in the type system
    • Stored in TypeDefinition structure
  3. Type instantiation requires type checking

    • Must be verified through kos_check or kos_type_check
    • Only instances that pass type checking are valid

Type Construction Examples

Event Type (Σ Type)

// FailEvt ≡ Σ(b: BatchID). Σ(err: ErrorCode). Σ(t: Time). Prop
kos_term* time_prop_sigma = kos_mk_sigma(time_type, prop_type);
kos_term* error_time_prop_sigma = kos_mk_sigma(error_code_type, time_prop_sigma);
kos_term* fail_evt_type = kos_mk_sigma(batch_id_type, error_time_prop_sigma);
kos_ontology_add_type_definition(ontology, "FailEvt", fail_evt_type, NULL);

Predicate Type (Π Type)

// InRoute ≡ Π(b: BatchID). Π(m: Machine). Prop
kos_term* machine_prop_pi = kos_mk_pi(machine_type, prop_type);
kos_term* in_route_type = kos_mk_pi(batch_id_type, machine_prop_pi);
kos_ontology_add_type_definition(ontology, "InRoute", in_route_type, NULL);

Pending Work

1. Domain Code Migration ⏳

src/domain/manufacturing/ontology_crud.c

  • Needs complete rewrite to use new type definition API
  • Currently still uses old API (commented out)

src/domain/manufacturing/types.c

  • Needs to update all type constructor functions
  • Currently only kos_mk_batch_id is updated

include/kos_manufacturing.h

  • Needs to add new API function declarations based on type construction
  • Old API functions are commented out, awaiting migration

2. Serialization/Deserialization Improvement ⏳

  • kos_ontology_serialize function has basic framework implemented but needs improvement
  • kos_ontology_deserialize function needs complete JSON parsing implementation

3. Type Checking Integration ⏳

  • Ensure type checker (kos_check) can correctly handle all type constructions
  • Add error reporting mechanism for type checking

Current Compilation Status

The following errors will occur during compilation:

  1. ontology_crud.c still uses commented-out old API functions
  2. Some type constructor functions need updates

These errors are expected because domain code migration is still in progress.

Next Steps

  1. Temporarily disable related code: Comment out code in ontology_crud.c using old API
  2. Gradual migration: Migrate files one by one to new type system
  3. Add tests: Add unit tests for new type system
  4. Improve documentation: Add more usage examples and best practices

Achievement

This refactoring successfully migrated the type system from a C language struct-based design to a type theory-based design, fully conforming to Intuitionistic Type Theory (ITT) principles. All type definitions are now constructed through type constructors, and type instantiation must be verified through type checking, ensuring the correctness and consistency of the type system.