cek-mercury
Safe HaskellNone
LanguageHaskell2010

Cek.Mercury

Description

Demonstrates how KontStack (O(1) copy-on-write continuation capture) and SoAStore (monotonic cell allocation with WAM conditional trailing) support nondeterministic relational search, deep term unification, and semidet commits.

Architecture: WAM Relational Search on SoA Structures

The Warren Abstract Machine (WAM) achieves high-speed execution of Prolog and Mercury by pairing three distinct memory areas:

  1. An allocation heap for terms: mapped onto the 4096-cell chunked SoAStore.
  2. A continuation stack for clause dispatch: mapped onto the unrolled 32-frame KontStack.
  3. An undo trail for unbinding variables during backtracking: stored in an unboxed MutableByteArray (leTrail).

WAM Conditional Trailing Invariant

When binding an unbound logic variable at cell varAddr to a target term at targetAddr:

  • If varAddr < heapWatermark (where heapWatermark is the store allocation cursor at the most recent choice point), the variable was allocated before the choice point and must be recorded on the trail (leTrail) so it can be reset to CT_Unbound upon failure.
  • If varAddr >= heapWatermark, the variable was created within the current alternative. Backtracking simply resets the store allocation pointer, so trailing is bypassed.

Delimited Commits and Choice-Point Pruning

Mercury's semidet deterministic commits (or Prolog cuts !) prune choice points. In solveMercury, cuts simply reset the choice point counter cpCursor to the saved watermark, instantly reclaiming all intermediate choice point states.

**Demonstrative Reference Implementation**: This package is an educational
and demonstrative reference implementation showcasing relational unification,
WAM conditional trailing, and nondeterministic search over KontStack choice points.
It is provided primarily as an executable architectural example within the @cek@
ecosystem documentation.
Synopsis

Relational AST and Goals

data LogicTerm Source #

First-order logic terms (variables, unboxed integer constants, and compound functors).

Constructors

LVar !Int

Logic variable reference resolved through the local lexical environment.

LLit !Int32

32-bit signed integer literal constant.

LFun !Word32 !(SmallArray LogicTerm)

Compound functor term f(t0, t1, ...) with symbol ID and argument array.

Instances

Instances details
Eq LogicTerm Source # 
Instance details

Defined in Cek.Mercury

Methods

(==) :: LogicTerm -> LogicTerm -> Bool

(/=) :: LogicTerm -> LogicTerm -> Bool

Show LogicTerm Source # 
Instance details

Defined in Cek.Mercury

Methods

showsPrec :: Int -> LogicTerm -> ShowS

show :: LogicTerm -> String

showList :: [LogicTerm] -> ShowS

data Goal Source #

Relational goal calculus supporting conjunction, disjunction, fresh variables, and commits.

Constructors

GSuccess

Trivial success goal (true in Prolog).

GFail

Trivial failure goal that immediately triggers backtracking.

GUnify !LogicTerm !LogicTerm

Bidirectional Robinson unification between two terms t1 = t2.

GFresh !Goal

Fresh logic variable binder introducing a new unbound cell in SoAStore.

GConj !Goal !Goal

Logical conjunction (g1 , g2) evaluated left-to-right.

GDisj ![Goal]

Nondeterministic disjunction (g1 ; g2 ; ...) creating a choice point.

GOnce !Goal

Prune choice points created within g upon first successful solution (once/1).

Instances

Instances details
Eq Goal Source # 
Instance details

Defined in Cek.Mercury

Methods

(==) :: Goal -> Goal -> Bool

(/=) :: Goal -> Goal -> Bool

Show Goal Source # 
Instance details

Defined in Cek.Mercury

Methods

showsPrec :: Int -> Goal -> ShowS

show :: Goal -> String

showList :: [Goal] -> ShowS

WAM Engine State

data LogicEngine s Source #

Mutable WAM execution state combining SoAStore, KontStack, an unboxed trail, and an unboxed choice-point watermark stack.

Constructors

LogicEngine 

Fields

Continuation Frame Tags

pattern FT_ConjR :: FrameTag Source #

Frame tag for evaluating the right-hand conjunct g2 after g1 succeeds.

pattern FT_CommitCut :: FrameTag Source #

Frame tag for committing a once / cut block by pruning choice points.

pattern FT_UnifyArgs :: FrameTag Source #

Frame tag for continuing deep unification of remaining compound functor arguments.

Engine Lifecycle and Solver

newLogicEngine :: ST s (LogicEngine s) Source #

Initialize a fresh LogicEngine with pre-allocated trail and choice-point slabs.

solveMercury :: Int -> Int -> Goal -> [[Maybe Int32]] Source #

Execute a relational Goal over numQueryVars root variables and collect up to maxSolutions integer bindings per query variable across backtracking branches.