Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon authored Nov 29, 2024
1 parent daa0ca3 commit 222c4f5
Showing 1 changed file with 143 additions and 48 deletions.
191 changes: 143 additions & 48 deletions tests/performance/compare_algo/nqueens/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Key Dataset: MeTTaLog vs. MeTTaRust
## **Key Dataset: MeTTaLog vs. MeTTaRust**

The following table compares the execution times for **MeTTaLog** and **MeTTaRust** for N-Queens sizes 4 through 7. This data highlights the significant disparity between the two implementations.

Expand All @@ -15,11 +15,11 @@ The following table compares the execution times for **MeTTaLog** and **MeTTaRus

---

## Proportionality of MeTTaLog and Plain Prolog
## **Proportionality of MeTTaLog and Plain Prolog**

MeTTaLog and Plain Prolog exhibit proportional scaling. Both implementations handle recursion and symbolic reasoning efficiently due to their declarative natures. However, Plain Prolog is significantly faster because of its optimized runtime environment and more mature backtracking mechanisms.

### Longer Timing Table
### **Longer Timing Table**

| **N-Queens Size** | **MeTTaLog (min)** | **MeTTaRust (min)** | **Plain Prolog (min)** | **Prolog CLP(FD) (min)** | **Python (min)** | **C/C++ (min)** |
|--------------------|--------------------|---------------------|-------------------------|--------------------------|------------------|-----------------|
Expand All @@ -43,36 +43,94 @@ MeTTaLog and Plain Prolog exhibit proportional scaling. Both implementations han

---

## Enhancing MeTTa: Integrating Prolog and CLP(FD) Features
MeTTa, with its declarative and symbolic reasoning capabilities, presents unique challenges when translating to other languages. This document explores why **Prolog** emerges as the most practical target for MeTTa logic, compared to procedural languages like **C**, functional languages like **Scheme** or **Common Lisp**, and modern object-oriented languages like **Python** or **Java**.

To maximize MeTTa’s flexibility and performance, it’s crucial to allow programmers to leverage **Plain Prolog** and **Constraint Logic Programming over Finite Domains (CLP(FD))** within the MeTTa language itself. By embedding constructs and distinctions that map to either approach, MeTTa can serve as a robust framework for both general symbolic reasoning and optimized constraint-solving.
Additionally, while **C/C++** is a fine ultimate target for performance-critical applications, this document argues that **Prolog** should serve as an intermediary stopgap. Translating MeTTa to Prolog first ensures that we preserve the **"superpowers"** of Prolog—such as native backtracking, symbolic logic, and constraints—before generating highly optimized C/C++ implementations.

### Enabling Both Prolog and CLP(FD) in MeTTa
---


### **1. Control Flow**
- MeTTa employs implicit control flow through **pattern matching** and **recursive reasoning**, which procedural and functional languages struggle to replicate directly.
- Languages like **C**, **Java**, and **Python** require explicit constructs (if, while, for) to model recursion and backtracking.

### **2. Symbolic Logic**
- MeTTa operates on **symbolic lists and atoms** as first-class citizens.
- Procedural and object-oriented languages treat symbols and lists as secondary constructs, requiring heavy manual implementation.

### **3. Backtracking**
- Backtracking is a cornerstone of MeTTa logic.
- **Prolog** naturally supports backtracking, making it a near-perfect match.
- **C**, **Python**, and other general-purpose languages require manual stack management and state tracking to replicate this behavior.

### **4. Constraint Handling**
- MeTTa inherently supports constraints through symbolic matching and logical rules.
- **Prolog with CLP(FD)** excels here, leveraging built-in constraint-solving capabilities.
- **C/C++** requires custom implementations of constraint solvers, and **Python** relies on external libraries such as z3 or pyDatalog.

### **5. Tail Call Optimization (TCO)**
- MeTTa relies on recursion extensively, making TCO critical for performance and scalability.
- Many procedural and object-oriented languages (e.g., **C**, **Python**) do not guarantee TCO, making deep recursion problematic.
- **Prolog** and some functional languages (e.g., **Scheme**) provide TCO natively.

---

## **Why Prolog Is the Ideal Intermediate Target**

| **Feature** | **MeTTa** | **Prolog** | **Common Lisp/Scheme** | **Python** | **C/C++** |
|--------------------------|----------------------------|---------------------------|--------------------------------|----------------------------|-----------------------------|
| **Control Flow** | Implicit, logic-driven | Implicit, backtracking | Explicit (if, cond) | Explicit (if, while) | Explicit (if, switch) |
| **Symbolic Logic** | Native | Native | Secondary (via macros/lists) | Libraries (manual logic) | Manual implementation |
| **Backtracking** | Native | Native | Manual (via stack management) | Libraries (manual logic) | Manual recursion/state |
| **Constraint Handling** | Symbolic Matching | Native (with CLP(FD)) | Manual | Libraries (e.g., z3) | Custom algorithms |
| **TCO** | Essential | Native | Native (some dialects only) | Not available | Compiler-dependent |
| **Ease of Translation** | N/A | High | Moderate | Moderate | Low |

---

## **Conclusion**

While **C/C++** is an excellent ultimate target for performance-critical applications, translating MeTTa directly to C/C++ risks losing key capabilities such as:
- **Native backtracking.**
- **Symbolic reasoning.**
- **Constraint-solving mechanisms.**

By translating MeTTa to **Prolog** first, we can:
1. Preserve its declarative logic and symbolic reasoning capabilities.
2. Leverage Prolog’s native backtracking and constraint-handling as a reference for generating C/C++ implementations.
3. Ensure correctness and maintain logical abstractions before focusing on performance optimizations.

#### Programmer Flexibility
- **Objective:** Allow developers to express logic in MeTTa that can be translated to either Plain Prolog or CLP(FD) based on performance and problem-specific needs.
- **Implementation:** Introduce language constructs that explicitly specify whether a rule or query uses general symbolic reasoning or constraint-based logic.
This two-step process provides a balance between leveraging **Prolog’s logical power** and achieving

Example:
```metta
(rule (n_queens_clp N Solution)
@constraints
(and (domain Solution 1..N)
(all_different Solution)
(safe_clp Solution)))
```
**C/C++’s execution speed**, ensuring no logical capabilities are lost while optimizing for scalability and efficiency.

This translates to:
```prolog
n_queens_clp(N, Solution) :-
length(Solution, N),
domain(Solution, 1, N),
all_different(Solution),
safe_clp(Solution),
labeling([], Solution).
```

### Differences Between Plain Prolog and CLP(FD)
## **Enhancing MeTTa: Integrating Prolog and CLP(FD) Features**

To further bridge the gap between **MeTTa**'s capabilities and optimized execution, it's crucial to recognize the distinct advantages of **Prolog** and **Constraint Logic Programming over Finite Domains (CLP(FD))**. By incorporating aspects of CLP(FD) into MeTTa’s design, we can enable programmers to leverage both paradigms seamlessly. This not only enhances MeTTa’s expressive power but also positions it as a robust intermediary capable of translating effectively into either **Plain Prolog** or **CLP(FD)**.

---

### **1. Key Features to Incorporate**

#### **1.1 Symbolic Logic and Pattern Matching (Prolog Basis)**
- MeTTa’s current foundation in symbolic reasoning is already well-aligned with Prolog’s strengths.
- **Enhancement:** Extend pattern matching to allow richer constraints, such as logical conditions (`X > Y`) or arithmetic operations (`X + Y = Z`), aligning with Prolog's predicate capabilities.

#### **1.2 Backtracking with Constraints (CLP(FD) Basis)**
- Plain Prolog relies on logical backtracking to explore solutions. CLP(FD) enhances this by introducing **domain-specific constraints** and **finite domains**, making it more efficient for problems like scheduling, optimization, and N-Queens.
- **Enhancement:** Add constructs for:
- Declaring finite domains (e.g., `domain(X, 1..8)`).
- Enforcing constraints like `all_different/1` and `sum/3`.
- Built-in operators for inequality and arithmetic constraints.

#### **1.3 Constraint Propagation**
- CLP(FD) propagates constraints during execution, reducing the search space early by pruning infeasible paths.
- **Enhancement:** Allow MeTTa programmers to define constraints explicitly, enabling **early evaluation** for better scalability.

---

### **2. Differences Between Plain Prolog and CLP(FD)**

| **Feature** | **Plain Prolog** | **CLP(FD)** |
|---------------------------|-----------------------------------|---------------------------------------|
Expand All @@ -82,32 +140,69 @@ n_queens_clp(N, Solution) :-
| **Constraint Propagation**| No | Yes |
| **Optimization** | Manual | Built-in (`labeling([minimize(X)])`) |

### Implementation Strategy for MeTTa
By integrating CLP(FD) concepts, MeTTa can offer programmers a choice between **basic symbolic reasoning** and **optimized constraint handling**.

---

### **3. Translating MeTTa to Plain Prolog or CLP(FD)**

#### **3.1 Default Translation to Plain Prolog**
- For cases where symbolic logic and backtracking are sufficient, MeTTa can compile directly to Plain Prolog.
- Example:
```metta
(rule (n_queens N Solution)
(and (permute [1..N] Solution)
(safe Solution)))
```

Translates to:
```prolog
n_queens(N, Solution) :-
permute([1..N], Solution),
safe(Solution).
```

#### **3.2 Enhanced Translation to CLP(FD)**
- When constraints are present, MeTTa can compile to CLP(FD) for efficiency.
- Example:
```metta
(rule (n_queens_clp N Solution)
(and (domain Solution 1..N)
(all_different Solution)
(safe_clp Solution)))
```

Translates to:
```prolog
n_queens_clp(N, Solution) :-
length(Solution, N),
domain(Solution, 1, N),
all_different(Solution),
safe_clp(Solution),
labeling([], Solution).
```

---

#### Unified Language Design
Introduce syntax or annotations to distinguish between Plain Prolog and CLP(FD) translations:
- Default: Translate to Plain Prolog.
- Explicit: Use annotations for constraints or optimization.
### **4. Benefits of Supporting Both Paradigms**
1. **Programmer Flexibility:** MeTTa developers can choose between simplicity (Plain Prolog) or efficiency (CLP(FD)).
2. **Scalability:** Integrating CLP(FD) concepts ensures that MeTTa remains efficient for larger problem sizes.
3. **Broader Applicability:** Constraint handling expands MeTTa’s use cases to domains like scheduling, optimization, and combinatorial problem-solving.

For example:
```metta
(rule (n_queens_clp N Solution)
@constraints
(and (domain Solution 1..N)
(all_different Solution)
(safe_clp Solution)))
```
---

#### Seamless Translation to Prolog
- Plain Prolog: Use standard logical constructs and backtracking.
- CLP(FD): Map MeTTa’s constraints directly to CLP(FD) predicates.
### **5. Implementation Roadmap**
1. **Symbolic Constraint Framework:** Enhance MeTTa’s language semantics to support declarative constraints.
2. **CLP(FD) Compatibility:** Implement translation modules to map MeTTa’s constraints to CLP(FD) predicates.
3. **Optimization Options:** Provide flags to select between Plain Prolog and CLP(FD) during translation.
4. **Testing and Benchmarking:** Validate performance improvements using benchmark problems (e.g., N-Queens, Sudoku, and Scheduling).

---

### Conclusion
### **6. Conclusion**

By integrating the differences between Plain Prolog and CLP(FD) into MeTTa’s language design:
- Programmers can write flexible, efficient logic that leverages the strengths of both paradigms.
- The MeTTa compiler ensures smooth translation, preserving logic for Prolog and optimizing constraints for CLP(FD).
- This dual-approach empowers MeTTa as a high-level, declarative language capable of addressing a broad spectrum of computational problems effectively.
By integrating CLP(FD) capabilities into MeTTa, we unlock powerful constraint-solving and optimization features while preserving the language’s declarative nature. This dual approach ensures:
- **Logical consistency** through Plain Prolog.
- **Performance efficiency** via CLP(FD) enhancements.

This strategy solidifies MeTTa as a versatile tool for symbolic and constraint-based reasoning, capable of targeting both general-purpose and performance-critical applications.

0 comments on commit 222c4f5

Please sign in to comment.