Unity Prolog Reference

Flow control

,(:goal1:goal2)
True if both goals are true.
;(:goal1:goal2)
True if both goals are true.
->(:test:consequent)
Proves CONSEQUENT if TEST is true.
not(*goal)
True if GOAL is unprovable. GOAL must be ground.
\+(:goal)
True if GOAL is unprovable
once(:goal)
Attempts to prove GOAL, but suppresses backtracking for a second solution.
ignore(:goals)
Attempts to prove GOAL, but suppresses backtracking for a second solution.
call(:goal?optionalArguments)
Attempts to prove the specified GOAL, adding any additional arguments, if specified.
apply(:goal+arglist)
Adds arguments in ARGLIST to end of GOAL and attempts to prove the resulting goal.
begin(:goal)
Runs each goal in sequence, throwing an exception if any goal fails. Cannot be backtracked.
.(*objectmethod(*arguments...))
Calls the specified method of the specified object.
::(*module:goal)
Attempts to prove the specified goal in the specified module.
freeze(?var:goal)
Runs GOAL when VAR becomes bound; unification will fail if GOAL fails.
frozen(?term-goal)
Unifies GOAL with the goal frozen on TERM, if TERM is an unbound variable with a frozen goal; otherwise unifies GOAL with true.
randomizable(:predicateIndicator)
Declares that the specified predicate is allowed to have its clauses explored in random order, when clause randomization is enabled.
shadow(:predicateIndicator)
Declares that declarations for the specified predicate are allowed in this knowledgebase and override any declarations in the parent.
external(:predicateIndicator)
Declares that the specified predicate is optional to define and/or defined elsewhere; thus, it should not generate undefined predicate warnings.
public(:predicateIndicator)
Declares that the specified predicate is expected to be called from elsewhere. It should not generate unreferenced predicate warnings.
higher_order(:predicateIndicator)
Declares that the specified predicate is may call its arguments as subgoals. For example, higher_order(find_all(0,1,0)) means find_all/3 calls its second argument. Used by the static checker to weed out unreferenced predicates.
disassemble(:predicateIndicator)
Prints bytecode for a compiled predicate.
compile(:predicateIndicator)
Declares that the predicate should be byte compiled rather than interpreted.
trace(:predicateIndicator)
Declares that the specified predicate should be traced when executing.
notrace(:predicateIndicator)
Declares that the specified predicate should be traced when executing.
pause_game(:predicateIndicator)
Pauses the game, leaving GUI, etc. running.
unpause_game(:predicateIndicator)
Restores normal flow of time in game.
!
Prohibits backtracking past this point for the current goal.
fail
Forces failure of the current goal.
true
Always succeeds.
repeat
Always succeeds, and allows infinite backtracking.
throw(+exception)
Throws the specified exception.
catch(:goal=exception:recover)
Attempts to prove the specified GOAL, catching exceptions. If an exception is thrown, it is unified with EXCEPTION and RECOVER is run.
break(?Term)
Pauses game within the Unity editor and prints TERMS as a line in the unity console.
break_cs
Breakpoints the Prolog interpreter itself.

Meta-logical predicates

once(:goal)
Attempts to prove GOAL, but suppresses backtracking for a second solution.
ignore(:goals)
Attempts to prove GOAL, but suppresses backtracking for a second solution.
call(:goal?optionalArguments)
Attempts to prove the specified GOAL, adding any additional arguments, if specified.
apply(:goal+arglist)
Adds arguments in ARGLIST to end of GOAL and attempts to prove the resulting goal.
randomize(:goal)
Proves GOAL, while randomizing clause order for clauses declared randomizable.
begin(:goal)
Runs each goal in sequence, throwing an exception if any goal fails. Cannot be backtracked.
maplist(:predicate?list1?list2)
True if PREDICATE is true of all successive pairs of elements from LIST1 and LIST2.
!
Prohibits backtracking past this point for the current goal.
throw(+exception)
Throws the specified exception.
catch(:goal=exception:recover)
Attempts to prove the specified GOAL, catching exceptions. If an exception is thrown, it is unified with EXCEPTION and RECOVER is run.
var(?x)
True if X is an uninstantiated variable.
nonvar(?x)
True if X isn't an uninstantiated variable, that is, if it's instantiated to some term.
ground(?x)
True if X is a ground term, i.e. contains no unbound variables.
set(*predicate*value)
Forcibly asserts PREDICATE(VALUE) and retracts all other clauses for PREDICATE.

All solutions predicates

forall(:generator:goal)
True if GOAL is true for all bindings of all solutions of GENERATOR.
for_all_unique(-Template:generator:goal)
True if GOAL is true given variable bindings of each unique value of TEMPLATE produced by GENERATOR.
generate_unique(-Template:generator)
Succeeds once for each unique value of TEMPLATE produced by GENERATOR.
findall(=template:goal-solutions)
Unifies SOLUTIONS with a list of every value of TEMPLATE for every possible solution of GOAL.
findnsols(*n=template:goal-solutions)
Unifies SOLUTIONS with a list of every value of TEMPLATE for every possible solution of GOAL. Finds at most N solutiosn.
all(=template:goal-solutions)
Unifies SOLUTIONS with a list of every unique value of TEMPLATE for every possible solution of GOAL.
sumall(-numberVar:goal-sum)
Unifies SUM with sum of the values of NUMBERVAR in every possible solution of GOAL.
arg_min(>template-score+goal)
Find the value of TEMPLATE that gives the lowest SCORE among all solutions to GOAL.
arg_max(>template-score+goal)
Find the value of TEMPLATE that gives the highest SCORE among all solutions to GOAL.

Arithmetic

is(>variable*functional_expression)
Computes the value of FUNCTIONAL_EXPRESSION and unifies it with VARIABLE. Expression must be fully instantiated, i.e. all variables in it must already have values.

Comparisons

dif(?term1?term2)
Requires that TERM1 and TERM2 never be equal. If they are, the predicate fails. If they are not, it forces any future unifications that would make them equal to fail.
=(?x?y)
Succeeds if the two terms are unifiable.
unifiable(?x?y-unifier)
True if X and Y can be unified, but does not unify them. Instead returns the most general unifier in UNIFIER.
\=(?x?y)
Succeeds if the two terms are not unifiable.
==(?x?y)
Succeeds if the two terms are already identical, as opposed to =, which tries to make them identical through unification.
\==(?x?y)
Succeeds if the two terms are not identical, as opposed to \= which tests if it's possible to make them identical through unification.
<(*x*y)
True if number X is less than Y. Both must be ground.
>(*x*y)
True if number X is greater than Y. Both must be ground.
=<(*x*y)
True if number X is less than or equal to Y. Both must be ground.
>=(*x*y)
True if number X is greater than or equal to Y. Both must be ground.
@<(?x?y)
True if term X is less than Y given Prolog's ordering on terms. X and Y need not be numbers.
@>(?x?y)
True if term X is greater than Y given Prolog's ordering on terms. X and Y need not be numbers.
@=<(?x?y)
True if term X is less than or equal to Y given Prolog's ordering on terms. X and Y need not be numbers.
@>=(?x?y)
True if term X is greater than or equal to Y given Prolog's ordering on terms. X and Y need not be numbers.
=\=(*x*y)
True if X and Y are different numbers. Both must be ground.
=:=(*x*y)
True if functional expressions X and Y have the same values. Both must be ground.

List predicates

maplist(:predicate?list1?list2)
True if PREDICATE is true of all successive pairs of elements from LIST1 and LIST2.
=..(?term?list)
If TERM is instantiated, explodes it into a list: [Functor | Arguments] and unifies it with LIST; if TERM uninstantiated, converts LIST to a term and unifies it with TERM.
list(?x)
True if X is a list.
length(?list?length)
Unifies LENGTH with the length of LIST. This is a true relation, so if LIST is uninstantiated, it will create lists with specified lengths.
member(?element?list)
True if ELEMENT is an element of LIST. This is a true relation, so if necessary, it will create new LISTS.
memberchk(?element?list)
True if ELEMENT is an element of LIST, but will not backtrack different choices of the element. This is a true relation, so if necessary, it will create new LISTS.
random_member(?element+list)
Unifies ELEMENT with elements of LIST in random order.
append(?start?end?joined)
True if JOINED is a list that starts with the elements of START and is followed by the elements of END. This is a true relation, so it can be used to compute any argument from the others.
reverse(?forward?backward)
True if the lists FORWARD and BACKWARD are reversed versions of one another.
flatten(+listoflists?flatlist)
True if FLATLIST contains all the atoms of LISTOFLISTS, in order.
prefix(?prefix?list)
True if LIST starts with PREFIX.
suffix(?suffix?list)
True if LIST ends with SUFFIX.
select(?x?list_with?list_without)
True if X is an element of LIST_WITH and LIST_WITHOUT is LIST_WITH minus an occurance of X.
delete(?list?x?HasNoXs)
True if HasNoXs is LIST without X.
msort(+list-result)
True if RESULT is unifiable with a sorted version of LIST. Does not remove duplicates.
sort(+list-result)
True if RESULT is unifiable with a sorted version of LIST. Removes duplicates.
keysort(+list-result)
LIST should be of the format [KEY-VALUE, ...]. True if RESULT is unifiable with a version of LIST sorted by its KEYs. Does not remove duplicates.

Term manipulation

indexical_named(?name?indexical)
The INDEXICAL is an indexical object with name NAME.
copy_term(=original-copy)
Makes a new copy of ORIGINAL with fresh variables, and unifies it with COPY.
=..(?term?list)
If TERM is instantiated, explodes it into a list: [Functor | Arguments] and unifies it with LIST; if TERM uninstantiated, converts LIST to a term and unifies it with TERM.
functor(?term?functor?arity)
True if TERM has the specified FUNCTOR and ARITY.
arg(*arg+structure?argumentValue)
True if argument number ARG (counting from 1, not zero) of STRUCTURE is TERM.

Type predicates

number(?x)
True if X is a number.
integer(?x)
True if X is an integer.
float(?x)
True if X is a floating-point number.
atomic(?x)
True if X is not a structured term, i.e. it's a number, symbol, etc..
string(?x)
True if X is a string.
atom(?x)
True if X is a symbol.
symbol(?x)
True if X is a symbol.
compound(?x)
True if X is a structured term or list.

Constraint programming

freeze(?var:goal)
Runs GOAL when VAR becomes bound; unification will fail if GOAL fails.
frozen(?term-goal)
Unifies GOAL with the goal frozen on TERM, if TERM is an unbound variable with a frozen goal; otherwise unifies GOAL with true.
dif(?term1?term2)
Requires that TERM1 and TERM2 never be equal. If they are, the predicate fails. If they are not, it forces any future unifications that would make them equal to fail.

.NET interoperation

property(*object*property_name>value)
Unifies VALUE with the value of OBJECT's property named PROPERTY_NAME.Always succeeds exactly once (unless it throws an exception).
set_property(*object*property_name*new_value)
Sets OBJECT's property named PROPERTY_NAME to NEW_VALUE. Always succeeds exactly once (unless it throws an exception).
call_method(*object*method_and_args>result)
Calls the specified method on OBJECT with the specified arguments and unifies RESULT with its return value. Always succeeds exactly once (unless it throws an exception).
is_class(?object?class)
True if OBJECT is of the specified CLASS. If CLASS is a subclass of TwigGameComponent and OBJECT is uninstantiated, then it will enumerate objects if the specified type.
component_of_gameobject_with_type(?component?gameobject+class)
True if component is a component of gameobject with type class.
parent_of_gameobject(?child?parent)
True if CHILD is a child of PARENT in the game's rendering hierarchy.

Declarations

bind(*IndexicalName=Value)
Dynamically binds IndexicalName to Value until this goal is backtracked.
indexical(*Name=*DefaultValue)
Declares that the specified name is an indexical name that can be bound using bind.
randomizable(:predicateIndicator)
Declares that the specified predicate is allowed to have its clauses explored in random order, when clause randomization is enabled.
shadow(:predicateIndicator)
Declares that declarations for the specified predicate are allowed in this knowledgebase and override any declarations in the parent.
external(:predicateIndicator)
Declares that the specified predicate is optional to define and/or defined elsewhere; thus, it should not generate undefined predicate warnings.
public(:predicateIndicator)
Declares that the specified predicate is expected to be called from elsewhere. It should not generate unreferenced predicate warnings.
higher_order(:predicateIndicator)
Declares that the specified predicate is may call its arguments as subgoals. For example, higher_order(find_all(0,1,0)) means find_all/3 calls its second argument. Used by the static checker to weed out unreferenced predicates.
predicate_property(:goal+property)
True if PROPERTY holds of the predicate of GOAL.
discontiguous(:predicateIndicator)
Declares that the specified predicate is allowed to be scattered through a file. Currently unused but provided for compatibility with other Prolog implementation.
multifile(:predicateIndicator)
Declares that the specified predicate is allowed to be scattered through multiple files. Currently unused but provided for compatibility with other Prolog implementation.
dynamic(:predicateIndicator)
Declares that the specified predicate is allowed be dynamically modified using assert. Currently unused but provided for compatibility with other Prolog implementation.
trace(:predicateIndicator)
Declares that the specified predicate should be traced when executing.
notrace(:predicateIndicator)
Declares that the specified predicate should be traced when executing.
set_prolog_flag(*flag?value)
Sets/gets value of the specified control parameter for the prolog system.
op(*priority*type*operator)
Declares the type and priority of an infix, prefix, or postfix operator.

Definite clause grammars

C(?list?word?tail)
Used in implementation of DGCs. True if LIST starts with WORD and continues with TAIL.
word_list(?string?list)
Parses/unparses STRING into a LIST of word.
register_lexical_item(+word)
Adds WORD to list of words recognized by word_list.

Loading code

consult(*file[kb])
Reads the clauses in FILE and addds them to the database.
reconsult(*file)
Removes all clauses previously loaded from FILE, then reads the clauses in FILE and addds them to the database.
listing(*predicate)
Prints a listing of PREDICATE

Database manipulation

listing(*predicate)
Prints a listing of PREDICATE
asserta(+term)
Adds TERM (a rule or fact) to the database as the first clause for the predicate.
assert(+term)
Adds TERM (a rule or fact) to the database as the last clause for the predicate. Same as assertz.
assert(+term)
Adds TERM (a rule or fact) to the database as the last clause for the predicate. Same as assertz.
retractall(+head)
Removes all database entries whose heads unify with HEAD.
retract(+term)
Removes first database entry that unifies with TERM.
clause(+head?body)
Unifies HEAD and BODY with entries in the database.

eremic logic

/(parentkey)
Succeeds if expression can be matched against the EL knowledgebase.
:(parentkey)
Succeeds if expression can be matched against the EL knowledgebase.
>>(*expression-variable)
Binds VARIABLE to the subtree of the EL KB matching EXPRESSION.

Other Predicates

check(:goal)
Checks that Goal is true, and throws an exception if it fails. Only succeeds once, so similar to once/1.
step_limit(*maximum_steps)
Gets/sets the maximum number of inference steps allowed.
call_with_step_limit(*maximum_steps:Goal)
Runs GOAL, using at most MAXIMUM_STEPS.
benchmark(+goal*count)
Runs GOAL repeatedly, COUNT times.
string_representation(?term?string)
Parses/unparses between TERM and STRING.
starts_with(*substring*string_or_symbol)
The string or symbol's name begins with the specified substring.
ends_with(*substring*string_or_symbol)
The string or symbol's name end with the specified substring.
starts_with_one_of(*possible_first_chars_string*string_or_symbol)
The string or symbol's name begins with one of the characters in the specified string.
contains_substring(*substring*string_or_symbol)
The string or symbol's name contains the specified string.
plural_form(*singular?plural)
String plural is the plural form of string singular, using the default rules for English plurals.
atom_string(?atom?string)
Atom has the same print name as string.
game_object_name(?game_object?name_symbol)
True when name_symbol is the name of game_object.
set(*predicate*value)
Forcibly asserts PREDICATE(VALUE) and retracts all other clauses for PREDICATE.
display(?argument)
Prints arguments to the console; string arguments are not quoted.
displayln(?argument)
Prints arguments to the console; string arguments are not quoted.
write(?objectOrStream[+Object])
Prints the value of OBJECT to the console.
writeln(?objectOrStream[+Object])
Prints the value of OBJECT to the console, along with a newline.
nl
Prints a newline to the system console.
log(?Term)
Prints TERMS as a line in the Unity console.
log_warning(?Term)
Prints TERMS as a line in the Unity console.
log_error(?Term)
Prints TERMS as a line in the Unity console.
open(*path*mode-stream)
Opens a file for input or output.
close(*stream)
Closes an open file.
read(*stream-term)
Reads an expression from an open stream.
shell(commandarg_string)
Runs a shell command; disabled outside of editor builds.