Sign in

    prolog

    A small but real Prolog interpreter written in MoonBit: unification, backtracking, cut, findall, and a REPL.

    prolog
    interpreter
    logic-programming
    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    2 months ago
    Downloads
    57

    bobzhang/prolog does not have a README file

    Clause

    pub struct Clause {
    head : Term
    body : Array[Term]
    }
    A clause: head :- body. (body is the flattened goal list; empty for facts).

    Ctx

    pub struct Ctx {
    clauses : Array[Clause]
    choice_stack : Array[Array[Bool]]
    next_var : Int
    stop : Array[Bool]
    output : String
    halting : Bool
    }
    Interpreter state. Create with Ctx::new().

    Ctx::new

    fn Ctx::new() -> Ctx

    Ctx::query

    fn Ctx::query(self : Ctx, goals : Array[Term], vars : Array[(String, Int)], on_solution : (Array[Term?], Array[(String, Int)]) -> Bool) -> (Int, Bool)
    Run a query, returning (solutions_found, stopped_early).

    Ctx::submit

    fn Ctx::submit(self : Ctx, text : String, on_solution : (Array[Term?], Array[(String, Int)]) -> Bool) -> Submit
    Parse and run one statement.

    on_solution is called for every answer found, with the substitution; return true to keep searching, false to stop (e.g. an interactive ; prompt answered with anything but ;).

    Ctx::take_output

    fn Ctx::take_output(self : Ctx) -> String
    Take and clear the text captured by write/1, writeln/1 and nl/0.

    Statement

    pub enum Statement {
    StClause(Clause)
    StQuery(Array[Term], Array[(String, Int)])
    }

    Submit

    pub enum Submit {
    SyntaxError
    Fact
    Halt
    Query(Int, Bool)
    }
    Result of submitting one statement to Ctx::submit.

    Term

    pub(all) enum Term {
    Var(Int)
    Atom(String)
    Num(Int)
    Str(String)
    Compound(String, Array[Term])
    }
    Prolog terms.

    Variables are represented by unique integer ids (assigned by the parser and by clause renaming). Atoms, integers and strings are atomic constants; Compound(f, args) is a structure with functor f.

    deref

    fn deref(subst : Array[Term?], t : Term) -> Term
    Follow the binding chain of t and return its head.

    eval

    fn eval(subst : Array[Term?], t : Term) -> Int?
    Evaluate an arithmetic expression to an integer (for is/2, =:=/2, comparisons, between/3).

    eval2

    fn eval2(subst : Array[Term?], a : Term, b : Term) -> (Int, Int)?
    Evaluate two arithmetic expressions together (for comparisons).

    parse_statement

    fn parse_statement(ctx : Ctx, text : String) -> Statement?
    Parse a complete statement (fact, rule or query) from one line of text. Returns None on any syntax error.

    pretty

    fn pretty(subst : Array[Term?], t : Term) -> String
    Pretty-print a term for answers (writeq style: strings quoted).

    strip_comments

    fn strip_comments(s : String) -> String
    Remove % comments (outside of quotes) from a possibly multi-line buffer.

    term_equal

    fn term_equal(subst : Array[Term?], a : Term, b : Term) -> Bool
    Structural equality for ==/2: variables compare positionally (an unbound variable equals any other unbound variable), so this is variant equality rather than ISO identity comparison.

    trim

    fn trim(s : String) -> String
    Trim whitespace, returning a plain String.

    unify

    fn unify(a : Term, b : Term, subst : Array[Term?]) -> Array[Term?]?
    First-order unification with occurs check. Returns the extended substitution, or None on failure.

    write_term

    fn write_term(subst : Array[Term?], t : Term) -> String
    Print a term for write/1 (strings unquoted).

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io