Did you know ... Search Documentation:
Pack xlibrary -- prolog/local_dynamic.pl
PublicShow source

Scoped dynamic predicates.

This module provides a disciplined way to use dynamic predicates with a well-defined scope and lifetime. Dynamic predicates declared through with_local_dynamic/2 or with_local_dynamic/3 exist only for the duration of a goal and are automatically cleaned up on exit, regardless of success, failure, or exception.

The intent is to allow temporary or working-memory-style use of dynamic predicates without relying on global state, while preserving normal Prolog semantics such as backtracking, logical update semantics, and indexing (JITI).

A local dynamic context is introduced using with_local_dynamic/… . Within this context, predicates described by a schema (a list of Name/Arity pairs, optionally module-qualified) may be asserted, retracted, and called using the ld_* predicates. Outside the context, these predicates are not visible logically.

Dynamic predicate definitions are reused internally and cleared using retractall/1 with most-general heads. This avoids unbounded growth of dynamic predicate definitions, keeps cleanup efficient, and preserves predicate identity and indexing across invocations.

Contexts are thread-local: each thread has its own independent set of local dynamic predicates. No synchronization is required between threads, but it is the caller’s responsibility to ensure that a local dynamic context is not accessed after its scope has ended.

The public API mirrors Prolog’s built-in dynamic database predicates:

Implicit ld_* operations refer to the most recently entered local dynamic context; explicit forms accept a Scope argument to select a specific context.

This module is intended for temporary data, working memories, rule engines, planners, and similar patterns where dynamic predicates are convenient but global visibility is undesirable.

 with_local_dynamic(:Schema, :Goal) is det
 with_local_dynamic(:Schema, -Scope, :Goal) is det
Execute Goal in a context where the dynamic predicates described by Schema exist only locally and for the duration of Goal.

Schema is a list of Name/Arity pairs (optionally module-qualified) defining the relations available in the local dynamic context. Any clauses asserted into these predicates are visible only within Goal and are automatically removed on exit, regardless of success, failure, or exception.

Predicates in the schema are reused across invocations and cleared using retractall/1, avoiding unbounded growth of dynamic predicate definitions and preserving indexing (JITI) information.

Scope, when provided, is an identifier for the local dynamic context and can be used with the explicit ld_* predicates. If omitted, the most recently entered local dynamic context is used implicitly.

This predicate provides scoped, volatile dynamic predicates and is intended as a disciplined alternative to using global dynamics for temporary or working-memory data.

 ld_assertz(+Store, +Fact)
Insert a fact at the end in the store
 ld_asserta(+Store, +Fact)
Insert a fact at the beginning in the store
 ld_retract(+Store, +Pattern)
Delete a fact matching Pattern from the store. Could retract more facts on backtracking
 ld_retractall(+Store, +Pattern)
Delete all facts matching Pattern from the store.
 ld_call(+Store, +Pattern)
Query a pattern nondeterministically.

Undocumented predicates

The following predicates are exported, but not or incorrectly documented.

 with_local_dynamic(Arg1, Arg2, Arg3)
 ld_asserta(Arg1)
 ld_assertz(Arg1)
 ld_retract(Arg1)
 ld_retractall(Arg1)
 ld_call(Arg1)