libprolog library

A modern, ISO-compliant Prolog engine for Dart.

libprolog provides a complete Prolog implementation designed for embedding in Dart applications. It supports ISO Prolog core predicates and DCG.

Example usage:

import 'package:libprolog/libprolog.dart';

void main() async {
  // Create a Prolog engine
  final prolog = PrologEngine();

  // Assert facts
  prolog.assertz('parent(tom, bob)');
  prolog.assertz('parent(bob, ann)');

  // Query
  await for (final solution in prolog.query('parent(X, Y)')) {
    print('${solution['X']} is parent of ${solution['Y']}');
  }
}

Classes

AnsiColors
ANSI color codes for syntax highlighting.
Atom
A Prolog atom - an atomic constant.
BuiltinContext
Context provided to built-in predicates during execution.
BuiltinError
Built-in threw an error (ISO exception).
BuiltinFailure
Built-in failed (no solutions).
BuiltinNotFound
Built-in is not recognized (let resolver try database).
BuiltinRegistry
Registry of built-in predicates.
BuiltinResult
Result of executing a built-in predicate.
BuiltinStream
Built-in succeeded with multiple solutions (non-deterministic).
BuiltinSuccess
Built-in succeeded with no choice points (deterministic success).
ByteInputStream
Byte input stream.
ByteOutputStream
Byte output stream.
CatchFrame
Catch frame for ISO exception handling.
CharacterInputStream
Character input stream.
CharacterOutputStream
Character output stream.
ChoicePoint
A choice point for backtracking.
ChoicePointStack
Stack of choice points for backtracking.
Clause
A Prolog clause (fact or rule).
Compound
A Prolog compound term (structure).
Database
A Prolog clause database.
DCGTranslator
Translates a DCG rule into a regular Prolog clause.
FileInputStream
File-based character input stream.
FileOutputStream
File-based character output stream.
Goal
A goal to be proven during execution.
GoalStack
A goal stack representing the current execution state.
Highlighter
Syntax highlighter for Prolog code.
Lexer
ISO-compliant Prolog lexer.
MemoryStorage
In-memory clause storage with first-argument indexing.
Operator
An operator definition.
OperatorTable
Operator table for parser.
Parser
ISO-compliant Prolog parser.
PersistentStorage
Optional interface for storage implementations that support persistence.
PrologEngine
High-level Dart API for the Prolog engine.
PrologFloat
A Prolog floating-point number.
PrologInteger
A Prolog integer number.
PrologNumber
Base class for numeric terms.
PrologStream
Abstract base class for Prolog streams.
QueryResult
Represents the result of a query that expects at most one solution.
Resolver
SLD Resolution engine.
Solution
A solution to a query.
StderrStream
Standard error stream (stderr).
StdinStream
Standard input stream (stdin).
StdoutStream
Standard output stream (stdout).
Storage
Abstract interface for clause storage engines.
StorageStats
Statistics about storage operations (for optimization).
StorageWithStats
Optional interface for storage implementations that support statistics.
StreamManager
Global stream manager for Prolog I/O.
StreamProperties
Properties of a Prolog stream.
Substitution
A substitution (environment) mapping variables to terms.
Term
Base class for all Prolog terms.
TermConversion
Utilities for converting between Dart values and Prolog terms.
Token
A token from the lexer.
Trail
A trail for recording variable bindings during unification.
TransactionalStorage
Optional interface for storage implementations that support transactions.
Unify
Unification algorithm implementing Robinson's algorithm with occur check.
Variable
A Prolog logic variable.

Enums

EndOfStream
End-of-stream status.
OperatorType
Operator associativity and position.
StreamDirection
Direction of a stream.
StreamType
Type of stream content.
TokenType
Token types for Prolog lexer.
TracePort
Trace port types for debugging.

Extensions

SolutionExtensions on Solution
Extension methods for Solution to provide Dart-friendly access.

Functions

createStandardRegistry({StreamManager? streamManager, Database? database}) BuiltinRegistry
Creates a registry with all standard ISO built-ins registered.
evaluateArithmetic(Term term, Substitution substitution, [String context = 'is/2']) Term
Evaluates an arithmetic expression to a numeric term.
isDCGRule(Term term) bool
Helper to check if a clause is a DCG rule.
registerArithmeticBuiltins(BuiltinRegistry registry) → void
Registers ISO Prolog arithmetic built-in predicates.
registerAtomProcessingBuiltins(BuiltinRegistry registry) → void
Registers ISO Prolog atom and string processing built-in predicates.
registerControlBuiltins(BuiltinRegistry registry) → void
Registers control flow built-ins.
registerDatabaseBuiltins(BuiltinRegistry registry, Database database) → void
Registers database manipulation built-ins.
registerDCGBuiltins(BuiltinRegistry registry) → void
Registers DCG built-in predicates.
registerIOBuiltins(BuiltinRegistry registry, StreamManager streamManager) → void
Registers I/O built-in predicates.
registerListPredicates(BuiltinRegistry registry) → void
Registers list manipulation built-ins.
registerMetaPredicates(BuiltinRegistry registry) → void
Registers meta-predicates (all-solutions).
registerTermComparisonBuiltins(BuiltinRegistry registry) → void
Registers ISO Prolog term comparison built-in predicates.
registerTermManipulationBuiltins(BuiltinRegistry registry) → void
Registers ISO Prolog term manipulation built-in predicates.

Typedefs

ArithmeticError = ArithmeticException
BuiltinPredicate = BuiltinResult Function(BuiltinContext context)
A built-in predicate implementation.
TraceCallback = bool Function(TracePort port, int depth, Term goal)
Callback for trace events.

Exceptions / Errors

ArgumentError
Argument error for invalid API usage (Dart-level).
ArithmeticException
Exception thrown during arithmetic evaluation.
DomainError
ISO 7.2.3.c: Error when argument is out of valid domain.
EvaluationError
ISO 7.2.3.f: Error when evaluation fails.
ExistenceError
ISO 7.2.3.i: Error when predicate does not exist.
InstantiationError
ISO 7.2.3.a: Error when argument is not sufficiently instantiated.
LexerError
Lexer error exception.
ParserError
Parser error for syntax problems.
PermissionError
ISO 7.2.3.h: Error when permission is denied.
PrologException
Base class for all Prolog-related exceptions.
RepresentationError
ISO 7.2.3.e: Error when representation is outside implementation limits.
ResourceError
Resource error when system resource is exhausted.
TypeError
ISO 7.2.3.b: Error when argument is of wrong type.