2.8 KiB
2.8 KiB
InMemoryDB.hs Summary
This module implements an in-memory Datalog database with support for facts, rules, and queries.
Data Types
Value
A simple algebraic data type representing values in the database.
ValueInt Int- Integer valueValueSymbol String- Symbolic value
InMemoryDB
The main database structure containing:
relations :: Map RelationId Relation- Map of relation names to relationsconstants :: Set Constant- The Herbrand universe (all constants in the database)
RuleElement
Represents an entry occurring in a head or body relation.
RuleElementConstant Constant- A constant termRuleElementVariable Text- A variable name
RelationRule
A rule definition containing:
headVariables :: [Text]- List of variable names in the rule headbody :: [(Relation, [RuleElement])]- List of body relations with their elements
Relation
A database relation with:
_name :: Text- Relation name_arity :: Int- Number of columns_tuples :: Set [Constant]- Set of tuples (facts)_rules :: [RelationRule]- Associated rules for deriving new tuples
ConstraintElement
Represents an entry in a rule body constraint, using indices for variables.
ConstraintElementConstant Constant- A constant termConstraintElementIndex Int- Index into the variable list
BodyConstraint
A body constraint used during rule processing:
_relation :: Relation- The relation being constrained_elements :: [ConstraintElement]- List of constraint elements
RuleContext
Context maintained while processing rules:
__relation :: Relation- The relation being defined_headVariables :: [RuleElement]- Variables collected from head and body_bodyConstraints :: [BodyConstraint]- Constraints from rule body_db :: InMemoryDB- Current database state
InMemoryDBException
Exception types for error handling:
CannotParseStatementException- Failed to parse Datalog textNonFactException- Expected a fact but got another statement typeNonRuleException- Expected a rule but got another statement typeNonQueryException- Expected a query but got another statement typeBadArityException- Mismatched arity for a relationVariableLookupException- Variable not found in contextUnexpectedConstantException- Found constant where variable expected
Type Aliases
Constant = Term- Constants are Datalog terms (ints, variables, symbols)RelationId = Text- Relation identifiers are text strings
Key Functions
emptyDB- Creates an empty databasewithFacts- Creates a database from a list of fact textswithFactsAndRules- Creates a database from facts and ruleslookupRelation- Finds or creates a relation by namequery- Executes a query (currently not yet implemented)