|
| 1 | +# Type Porting Guide: C++ to MeTTa |
| 2 | + |
| 3 | +This guide outlines the conventions for porting C++ types (classes, structs, and templates) to MeTTa. The focus is on clarity, consistency, and maintainability. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Declaring Types in MeTTa |
| 8 | + |
| 9 | +- Every type should always be declared as `Type`. |
| 10 | +- The structure of a type is specified using a **type constructor**. |
| 11 | +- The type constructor: |
| 12 | + - Starts with `mk*`, followed by an intuitive name (e.g., `mkDeme`). |
| 13 | + - Returns the type it constructs. |
| 14 | + - Should **not** have a body that fully reduces it to another form. |
| 15 | + - Can be overloaded for flexibility. |
| 16 | + |
| 17 | +### Example: |
| 18 | +```scheme |
| 19 | +(: List (-> $a Type)) |
| 20 | +(: Nil (List $a)) |
| 21 | +(: Cons (-> Number (List Number) (List Number)) |
| 22 | +``` |
| 23 | +```scheme |
| 24 | +(: Instance Type) |
| 25 | +(: mkInstance (-> (List Number) Instance)) |
| 26 | +
|
| 27 | +(: ScoredInstance (-> $score Type)) |
| 28 | +(: mkScoredInstance (-> (Instance $score) (ScoredInstance $score))) |
| 29 | +``` |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## 2. Type Parameterization |
| 34 | + |
| 35 | +A type should be **parameterized** if it can operate on different subtypes. |
| 36 | + |
| 37 | +- Example: A `List` can hold `Int`, `Bool`, or other elements, so it should be generic. |
| 38 | +- Parameterization occurs **at the type level**, not at the constructor. |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## 3. Handling Inheritance |
| 43 | + |
| 44 | +### 3.1. Regular Inheritance |
| 45 | + |
| 46 | +- Define a **separate type** for the parent class. |
| 47 | +- Include the parent type as an **argument** to the child's type constructor. |
| 48 | + |
| 49 | +### Example (C++ to MeTTa): |
| 50 | +#### C++: |
| 51 | +```cpp |
| 52 | +class Animal {}; |
| 53 | +class Dog : public Animal {}; |
| 54 | +``` |
| 55 | +#### MeTTa: |
| 56 | +```metta |
| 57 | +(: Animal Type) |
| 58 | +(: mkAnimal (-> $name Animal)) |
| 59 | +
|
| 60 | +(: Dog Type) |
| 61 | +(: mkDog (-> Animal Dog)) |
| 62 | +
|
| 63 | +!(mkDog (mkAnimal Dog)) |
| 64 | +``` |
| 65 | + |
| 66 | +### 3.2. Abstract Classes |
| 67 | + |
| 68 | +- **Do not declare** an abstract class as a type. |
| 69 | +- Handle polymorphism by **manually defining functions** for each concrete type. |
| 70 | + |
| 71 | +### Example (C++ to MeTTa): |
| 72 | +#### C++: |
| 73 | +```cpp |
| 74 | +class Shape { virtual double area() = 0; }; |
| 75 | +class Circle : public Shape { double area() override; }; |
| 76 | +class Square : public Shape { double area() override; }; |
| 77 | +``` |
| 78 | +#### MeTTa: |
| 79 | +```metta |
| 80 | +(: Circle Type) |
| 81 | +(: Square Type) |
| 82 | +(: area (-> Circle Float)) |
| 83 | +(: area (-> Square Float)) |
| 84 | +``` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## 4. Naming Conventions |
| 89 | + |
| 90 | +### 4.1. Function Naming for Data Structures |
| 91 | + |
| 92 | +- Prefix functions with their associated data structure. |
| 93 | +- Example: |
| 94 | + - `Map.insert`, `Set.insert` |
| 95 | + |
| 96 | +### 4.2. Type vs. Constructor Naming |
| 97 | + |
| 98 | +- Type names should be **full and descriptive**. |
| 99 | +- Constructors can be **abbreviated for convenience**. |
| 100 | +- Example: |
| 101 | + - `MultiMap` → Constructors: `NilMMap`, `ConsMMap` |
| 102 | + |
| 103 | +### 4.3. Naming Type Constructors for Data Structures |
| 104 | + |
| 105 | +- The constructor should reflect its function in the data structure. |
| 106 | +- Example: |
| 107 | + - A `MultiMap` data structure: |
| 108 | + ```metta |
| 109 | + (: MultiMap (-> Type Type)) |
| 110 | + (: NilMMap (MultiMap $a)) |
| 111 | + (: ConsMMap (-> $a (MultiMap $a) (MultiMap $a))) |
| 112 | + ``` |
| 113 | +
|
| 114 | +### 4.4. Implicit Ordering in Data Structures |
| 115 | +
|
| 116 | +- A `Map` or `Set` is **ordered by default**. |
| 117 | +- If unordered, explicitly specify: `UnorderedMap`. |
| 118 | +- Example: |
| 119 | + ```metta |
| 120 | + (: Map (-> $k $v Type)) |
| 121 | + (: UnorderedMap (-> $k $v Type)) |
| 122 | + ``` |
| 123 | + |
| 124 | +--- |
0 commit comments