| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
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:
- An allocation heap for terms: mapped onto the 4096-cell chunked
SoAStore. - A continuation stack for clause dispatch: mapped onto the unrolled 32-frame
KontStack. - 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(whereheapWatermarkis 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 toCT_Unboundupon 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
- data LogicTerm
- = LVar !Int
- | LLit !Int32
- | LFun !Word32 !(SmallArray LogicTerm)
- data Goal
- data LogicEngine s = LogicEngine {
- leStore :: !(SoAStore s)
- leKont :: !(KontStack s)
- leTrail :: !(MutableByteArray s)
- leCpUnboxed :: !(MutableByteArray s)
- leCpKontBlk :: !(SmallMutableArray s (KontBlock s))
- leCpAlts :: !(SmallMutableArray s ([Goal], [Int32]))
- leCounters :: !(MutableByteArray s)
- pattern FT_ConjR :: FrameTag
- pattern FT_CommitCut :: FrameTag
- pattern FT_UnifyArgs :: FrameTag
- newLogicEngine :: ST s (LogicEngine s)
- solveMercury :: Int -> Int -> Goal -> [[Maybe Int32]]
Relational AST and Goals
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 |
Relational goal calculus supporting conjunction, disjunction, fresh variables, and commits.
Constructors
| GSuccess | Trivial success goal ( |
| GFail | Trivial failure goal that immediately triggers backtracking. |
| GUnify !LogicTerm !LogicTerm | Bidirectional Robinson unification between two terms |
| GFresh !Goal | Fresh logic variable binder introducing a new unbound cell in |
| GConj !Goal !Goal | Logical conjunction |
| GDisj ![Goal] | Nondeterministic disjunction |
| GOnce !Goal | Prune choice points created within |
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.