NAME

Logic::Probabilistic::Program - Represents a probabilistic logic program.

SYNOPSIS

use Logic::Probabilistic::Program;

my $prog = Logic::Probabilistic::Program->new;
$prog->pfact(0.7, 'rain');
$prog->rule(head => ..., body => ...);

my $result = $prog->query('rain');

METHODS

pfact( $prob, $pred, @args, [ \%opts ] )

Asserts an independent probabilistic fact with probability $prob. Optional trailing options hashref supports expires_in = $seconds> or expires_at = $timestamp>.

assert_pfact( %args )

Named-argument variant of pfact. Accepts probability, predicate, args, expires_in, and expires_at.

retract_pfact( $pred, @args )

Retracts the first matching probabilistic fact matching the predicate and arguments. Returns the removed Logic::Probabilistic::PFact object, or undef if not found.

retract_all_pfacts( $pred, @args )

Retracts all matching probabilistic facts. Returns the list of removed objects.

update_pfact( $prob, $pred, @args )

Updates the probability of an existing probabilistic fact in-place. Returns the updated Logic::Probabilistic::PFact object, or undef if not found.

set_pfact( $prob, $pred, @args )

Upsert method: updates the probability if the fact already exists, or asserts it if not.

pchoice( $pred, \@outcomes, @args )

Registers a categorical random variable (annotated disjunction).

retract_pchoice( $pred, @args )

Retracts a categorical random choice.

update_pchoice( $pred, \@outcomes, @args )

Updates the outcomes distribution of an existing categorical choice in-place.

assert_fact( %args )

Delegates to "assert_fact" in Logic::Relational::Program. Supports expires_in for TTL.

retract( $pred, @args )

Delegates to "retract" in Logic::Relational::Program. Retracts a deterministic fact.

retract_all( $pred, @args )

Delegates to "retract_all" in Logic::Relational::Program. Retracts all matching deterministic facts.

cleanup_expired_pfacts( [$now] )

Prunes any probabilistic facts whose expires_at timestamp has passed.

prule( %args )

Registers a probabilistic clause / rule. Accepts probability, predicate, params, and body. Automatically desugars into an auxiliary probabilistic cause fact template and a deterministic Horn clause, auto-grounding across the domain of the body.

query( $goal, %options )

Performs probabilistic query inference using exact enumeration, rejection sampling, or MCMC.

save_snapshot( $target, %options )

Serializes the current program state (probabilistic facts, categorical choices, deterministic ground facts, and metadata) to a JSON snapshot. $target may be a file path string, an open filehandle, or a scalar reference for in-memory serialization. Options include pretty => 1 for formatted JSON output.

load_snapshot( $source, %options )

Loads a previously serialized snapshot into the program. $source may be a file path, an open filehandle, or a scalar reference containing JSON text.

Options:

  • mode => 'replace' | 'merge': Default is 'replace', which clears previous facts and choices while preserving compiled rules. 'merge' retains existing facts.

  • ttl_mode => 'absolute' | 'relative': Default is 'absolute'. Setting 'relative' (or pause_ttl => 1) recalculates expiration deadlines relative to the current clock time, allowing simulations to be paused and resumed.

  • filter_expired => 1: Prunes any facts whose TTL has elapsed (default 1).