Code Types

There are two different uses of code types (for relations, actions and functions): for the declaration of the code; and for specifying an argument of a relation, fuction or action as a code type.

Instead of using the key word def we use rel, fun and act as in the examples below.

rel descendant_is(!human,?human)
fun num_children(human) -> nat
act do_parse(!string, ?parse_tree)

The arguments of relation and action declarations are moded types. If the mode is missing it defaults to !. The arguments of a function declaration are types - modes are not required as the arguments are always ground.

By listing multiple type declarations for a single relation, function or action we are actually declaring an intersection type as below (for a user declared version of append).

rel app(!list(!T), !list(!T), ?list(?T)),
app(?list(?T), ?list(?T), !list(!T)),
app(!list(??T), !list(??T), ?list(??T)),
app(?list(??T), ?list(??T), !list(??T)),
app(??list(??T), ??list(??T), ??list(??T))

Below are some examples of using code types as arguments of declarations of other code types.

fun mapF((fun(T1) -> T2), list(T1)) -> list(T2)
rel mapR(!rel(!T1,?T2), !list(T1), ?list(T2))
fun curry(fun(T1,T2) -> T3) -> fun(T1) -> fun(T2) -> T3

We can also declare a special case of rel for dynamic relations using dyn as below.

dyn child_of(human,human)

These dynamic relations simply store ground facts in the Belief Store and the intention is to be able to query thse facts and so, for these declarations we have types (not moded types) as the implicit mode is ? as opposed to ! for functions.

A special case of dynamic relation is what we call “global values” - they can either be of type int or num. The declaration also initializes the value. The current value can be accessed using a $ prefix and the value can be modified by using the operators :=, +:= and -:= as in the examples below.

int a:=0
num b:=0.0

act inc_a(?int)
"Increment the global value a and return the incremented value"
inc_a(N) :: N = $a+1 ~> a +:= 1

In declaring relations and functions (typically that depend on the belief store) we can use mrel and mfun instead. The m stands for memoization. Please refer to the section entitled “Automatic Memoization of Functions and Relations” in the reference manual for details.