PublicShow sourceprolog_code.pl -- Utilities for reasoning about code

This library collects utilities to reason about terms commonly needed for reasoning about Prolog code. Note that many related facilities can be found in the core as well as other libraries:

Source comma_list(?CommaList, ?List)
Source semicolon_list(?SemicolonList, ?List)
True if CommaList is a nested term over the ','/2 (';'/2) functor and List is a list expressing the elements of the conjunction. The predicate is deterministic if at least CommaList or List is sufficiently instantiated. If both are partial structures it enumerates ever growing conjunctions and lists. CommaList may be left or right associative on input. When generated, the CommaList is always right associative.

This predicate is typically used to reason about Prolog conjunctions (disjunctions) as many operations are easier on lists than on binary trees over some operator.

Source mkconj(A, B, Conj) is det
Source mkdisj(A, B, Disj) is det
Create a conjunction or disjunction from two terms. Reduces on true (mkconj/2) and false (mkdisj/2). Note that a false encountered in a conjunction does not cause the conjunction to be false, i.e. semantics under side effects are preserved.

The Prolog `, and ;` operators are of type xfy, i.e. right associative. These predicates preserve this grouping. For example,

?- mkconj((a,b), c, Conj)
Conj = (a,b,c)
Source is_predicate_indicator(@Term) is semidet
True when Term is a predicate indicator
Source pi_head(?PredicateIndicator, ?Goal) is det
Translate between a PredicateIndicator and a Goal term. The terms may have a module qualification.
Errors
- type_error(predicate_indicator, PredicateIndicator)
Source head_name_arity(?Goal, ?Name, ?Arity) is det
Similar to functor/3, but deals with SWI-Prolog's zero-argument callable terms and avoids creating a non-callable term if Name is not an atom and Arity is zero.
Source most_general_goal(+Goal, -General) is det
General is the most general version of Goal. Goal can be qualified.
See also
- is_most_general_term/1.
Source extend_goal(:Goal0, +Extra, -Goal) is det
Extend the possibly qualified Goal0 with additional arguments from Extra. If Goal0 is insufficiantly instantiated (i.e., a variable), a term call(Goal0, ...) is returned.
Source predicate_label(++PI, -Label) is det
Create a human-readable label for the given predicate indicator. This notably hides the module qualification from user and built-in predicates. This predicate is intended for reporting predicate information to the user, for example in the profiler.

First PI is converted to a head and the hook prolog_predicate_name/2 is tried.

Source predicate_sort_key(+PI, -Key) is det
Key is the (module-free) name of the predicate for sorting purposes.
Source is_control_goal(@Goal)
True if Goal is a compiled Prolog control structure. The difference between control structures and meta-predicates is rather unclear. The constructs below are recognised by the compiler and cannot be redefined. Note that (if->then;else) is recognised as ((if->then);else).
Source body_term_calls(:BodyTerm, -Goal) is nondet
True when BodyTerm calls Goal. This predicate looks into control structures as well as meta predicates based on predicate_property/2.

When a variable is called, this is normally returned in Goal. Currently if a variable is called with additional arguments, e.g., call(Var, a1), this call is reported as call(Var, a1).

Re-exported predicates

The following predicates are exported from this file while their implementation is defined in imported modules or non-module files loaded by this module.

Source comma_list(?CommaList, ?List)
Source semicolon_list(?SemicolonList, ?List)
True if CommaList is a nested term over the ','/2 (';'/2) functor and List is a list expressing the elements of the conjunction. The predicate is deterministic if at least CommaList or List is sufficiently instantiated. If both are partial structures it enumerates ever growing conjunctions and lists. CommaList may be left or right associative on input. When generated, the CommaList is always right associative.

This predicate is typically used to reason about Prolog conjunctions (disjunctions) as many operations are easier on lists than on binary trees over some operator.

Source mkconj(A, B, Conj) is det
Source mkdisj(A, B, Disj) is det
Create a conjunction or disjunction from two terms. Reduces on true (mkconj/2) and false (mkdisj/2). Note that a false encountered in a conjunction does not cause the conjunction to be false, i.e. semantics under side effects are preserved.

The Prolog `, and ;` operators are of type xfy, i.e. right associative. These predicates preserve this grouping. For example,

?- mkconj((a,b), c, Conj)
Conj = (a,b,c)