# 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 value - `ValueSymbol String` - Symbolic value ### InMemoryDB The main database structure containing: - `relations :: Map RelationId Relation` - Map of relation names to relations - `constants :: 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 term - `RuleElementVariable Text` - A variable name ### RelationRule A rule definition containing: - `headVariables :: [Text]` - List of variable names in the rule head - `body :: [(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 term - `ConstraintElementIndex 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 text - `NonFactException` - Expected a fact but got another statement type - `NonRuleException` - Expected a rule but got another statement type - `NonQueryException` - Expected a query but got another statement type - `BadArityException` - Mismatched arity for a relation - `VariableLookupException` - Variable not found in context - `UnexpectedConstantException` - 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 database - `withFacts` - Creates a database from a list of fact texts - `withFactsAndRules` - Creates a database from facts and rules - `lookupRelation` - Finds or creates a relation by name - `query` - Executes a query (currently not yet implemented)