This document describes the MeTTaIL Scala prototype, its current capabilities, and how it relates to the goal of semantic type checking for MeTTa.
Location: /home/dylon/Workspace/f1r3fly.io/MeTTaIL/
The MeTTaIL Scala prototype is a theory transformer that:
MeTTaIL follows the principle of theories as first-class objects:
┌─────────────────────────────────────────────────────────────┐
│ MeTTaIL Scala │
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ │
│ │ Parser │ │ Theory │ │ Hypercube │ │
│ │ (Source) │──│ Model │──│ Transform │ │
│ └─────────────┘ └──────────────┘ └─────────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ │
│ │ AST │ │ Validation │ │ BNFC │ │
│ │ │──│ (Category) │──│ Generator │ │
│ └─────────────┘ └──────────────┘ └─────────────────┘ │
└─────────────────────────────────────────────────────────────┘
// Theory representation
case class Theory(
name: String,
sorts: List[Sort],
constructors: List[Constructor],
equations: List[Equation],
interpretations: List[Interpretation]
)
// Sort (type in the theory)
case class Sort(
name: String,
category: Category // Domain category for semantics
)
// Constructor (operation in the theory)
case class Constructor(
name: String,
domain: List[Sort], // Input types
codomain: Sort, // Output type
modality: Option[Modality] // Optional modal annotation
)
// Equation (axiom)
case class Equation(
lhs: Term,
rhs: Term
)
theory MyTheory {
// Sort declarations
sort Term
sort List
sort Nat
// Constructor declarations
constructor nil : List
constructor cons : Term × List → List
constructor zero : Nat
constructor succ : Nat → Nat
// Equations (optional)
equation {
length(nil) = zero
length(cons(x, xs)) = succ(length(xs))
}
}
Sorts can be annotated with their categorical domain:
theory TypedTerms {
sort Term : Set // Terms form a set
sort Type : Preorder // Types form a preorder (subtyping)
sort Context : Category // Contexts form a category
// Typing judgment as morphism
constructor typeof : Context × Term → Type
}
Constructors can have modality markers:
theory ModalTheory {
sort Prop
sort World
constructor box : Prop → Prop // Necessity □
constructor diamond : Prop → Prop // Possibility ◇
// Modal axiom
equation {
box(P) → diamond(P) // Necessity implies possibility
}
}
The hypercube transformation mechanically lifts an untyped theory to a typed version by introducing type indices.
Given an untyped theory T, the hypercube H(T) has:
S[\tau ]$ indexed by typesf[\tau ]$ : $A[\tau ] \to B[\tau ]$Input: Untyped lambda calculus theory
theory UntypedLambda {
sort Term
constructor var : Nat → Term
constructor app : Term × Term → Term
constructor lam : Term → Term
}
Output: Typed lambda calculus via hypercube
theory TypedLambda {
sort Type
sort Term[Type] // Terms indexed by type
constructor base : Type
constructor arrow : Type × Type → Type
constructor var[τ] : Nat → Term[τ]
constructor app[σ,τ] : Term[arrow(σ,τ)] × Term[σ] → Term[τ]
constructor lam[σ,τ] : Term[τ] → Term[arrow(σ,τ)]
}
The hypercube transformation is related but distinct from OSLF:
| Hypercube | OSLF |
|---|---|
| Lifts constructors to indexed families | Derives predicates via presheaf |
| Syntactic transformation | Semantic construction |
| Types as indices | Types as predicates |
| Mechanical, uniform | More expressive |
Hypercube is a subset of what OSLF can express - it captures type indexing but not general predicates on terms or behavioral types.
MeTTaIL generates BNFC (BNF Converter) grammars from theory definitions.
BNFC is a tool that generates:
From a grammar specification.
Theory Definition ──▶ BNFC Grammar ──▶ Parser/Lexer
│ │
▼ ▼
Validation Target Language
(Category) (Haskell, Scala, etc.)
For theory:
theory SimpleExpr {
sort Expr
constructor num : Int → Expr
constructor add : Expr × Expr → Expr
constructor mul : Expr × Expr → Expr
}
Generated BNFC:
-- Automatically generated by MeTTaIL
entrypoints Expr ;
Num. Expr ::= Integer ;
Add. Expr ::= Expr "+" Expr1 ;
Mul. Expr ::= Expr1 "*" Expr2 ;
coercions Expr 2 ;
| Feature | Status | Relation to OSLF |
|---|---|---|
| Theory definition syntax | ✅ Complete | Provides λ-theory input |
| Hypercube transformation | ✅ Working | Related, less expressive |
| Modal types (◇) | 🔄 Partial | Similar goal, different mechanism |
| Category/sort validation | ✅ Complete | Necessary foundation |
| BNFC generation | ✅ Complete | Parser generation |
| Interpretation checking | ✅ Complete | Semantic consistency |
To implement full OSLF, MeTTaIL Scala would need:
Presheaf construction
Internal language extraction
Predicate language
\varphi$ : $A \to \Omega$Behavioral types
The existing infrastructure is a good foundation:
// Current: Theory validation
def validateTheory(t: Theory): ValidationResult = ...
// Extension: Presheaf construction
def presheafConstruction(t: Theory): Presheaf[Theory] = ...
// Extension: Internal language
def internalLanguage(p: Presheaf[Theory]): TypeTheory = ...
| Component | Status | Notes |
|---|---|---|
| Theory parser | ✅ Complete | Full syntax support |
| Sort validation | ✅ Complete | Category constraints |
| Constructor validation | ✅ Complete | Type checking |
| Equation parsing | ✅ Complete | Axiom support |
| Hypercube transform | ✅ Working | Index lifting |
| Modal annotations | 🔄 Partial | Basic support |
| BNFC generation | ✅ Complete | Multiple targets |
| Interpretation check | ✅ Complete | Semantic validation |
import mettail._
val lambdaTheory = Theory(
name = "Lambda",
sorts = List(
Sort("Term", Category.Set),
Sort("Type", Category.Set)
),
constructors = List(
Constructor("var", List("Nat"), "Term"),
Constructor("app", List("Term", "Term"), "Term"),
Constructor("lam", List("Term"), "Term")
),
equations = List()
)
val result = Validator.validate(lambdaTheory)
val bnfc = BNFCGenerator.generate(lambdaTheory)
val typedTheory = Hypercube.transform(lambdaTheory, "Type")
// Result: Term[Type] indexed by types
The MeTTaIL Scala prototype provides:
It is a solid foundation for semantic type checking, but requires extension for full OSLF implementation. The recommended approach is to use MeTTaIL Scala for theory definition and BNFC, while implementing OSLF in mettail-rust.
/home/dylon/Workspace/f1r3fly.io/MeTTaIL/Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |