Skip to content

Commit 29faf1e

Browse files
authored
Merge branch 'main' into dev
2 parents 320e632 + 0907ce7 commit 29faf1e

2 files changed

Lines changed: 132 additions & 1 deletion

File tree

.github/TypeConvention.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
---

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ A short example, from beginning to end, can be found in this Jupyter notebook (c
1515
There is also a considerable amount of information in the OpenCog wiki: http://wiki.opencog.org/w/Meta-Optimizing_Semantic_Evolutionary_Search
1616

1717
## Running the code
18-
- Make sure to install MeTTaLog `SWI-Prolog version 9.3.25` following the instruction on the [metta-wam][https://github.com/trueagi-io/hyperon-experimental](https://github.com/trueagi-io/metta-wam?tab=readme-ov-file#windows-installation) repository.
18+
- Make sure to install MeTTaLog `SWI-Prolog version 9.3.25` following the instruction on the [metta-wam](https://github.com/trueagi-io/metta-wam?tab=readme-ov-file#windows-installation) repository.
19+
- The entry point to our algorithm is found in [this file](https://github.com/iCog-Labs-Dev/metta-moses/blob/main/deme/tests/expand-demes-test.metta). There are test cases in an assert equal, but there are also additional running examples you can use which are commented out for now. You can use the following command to run the tests using `mettalog` after successfully installing mettalog on your machine.
20+
```sh
21+
mettalog deme/tests/expand-demes-test.metta
22+
```
1923
## Contributing
2024
Before you start contributing to this repository, make sure to read the [CONTRIBUTING.md](https://github.com/iCog-Labs-Dev/metta-moses/tree/main/.github/CONTRIBUTING.md) file from our repository.
25+
26+
27+

0 commit comments

Comments
 (0)