The FO(·) Language¶
Overview¶
The FO(·) (aka FO-dot) language is used to create knowledge bases. An FO-dot knowledge base is a text file containing the following blocks of code:
- vocabulary
specify the types, predicates, functions and constants used to describe the problem domain.
- theory
specify the definitions and axioms satisfied by any solutions.
- structure
(optional) specify the interpretation of some predicates, functions and constants.
The basic skeleton of an FO-dot knowledge base is as follows:
vocabulary {
// here comes the specification of the vocabulary
}
theory {
// here comes the definitions and axioms
}
structure {
// here comes the interpretation of some symbols
}
Everything between //
and the end of the line is a comment.
Shebang¶
New in version 0.5.5
The first line of an IDP-Z3 program may be a shebang line, specifying the version of IDP-Z3 to be used. When a version is specified, the Interactive Consultant and webIDE will be redirected to a server on the web running that version. The list of versions is available here. (The IDP-Z3 executable ignores the shebang.)
Example: #! IDP-Z3 0.5.4
Vocabulary¶
vocabulary V {
// here comes the vocabulary named V
}
The vocabulary block specifies the types, predicates, functions and constants used to describe the problem domain. If the name is omitted, the vocabulary is named V.
Each declaration goes on a new line (or are space separated).
Symbols begins with a word character excluding digits, followed by word characters.
Word characters include alphabetic characters, digits, _
, and unicode characters that can occur in words.
Symbols can also be string literals delimited by '
, e.g., 'blue planet'
.
Types¶
IDP-Z3 supports built-in and custom types.
The built-in types are: 𝔹
, ℤ
, ℝ
, Date
, and Concept
.
The equivalent ASCII symbols are Bool
, Int
, and Real
.
Boolean literals are true
and false
.
Number literals follow Python’s conventions.
Date literals follow ISO 8601 conventions, prefixed with #
(#yyyy-mm-dd
).
#TODAY
is also a Date literal.
The type Concept
has one constructor for each symbol (i.e., function, predicate or constant) declared in the vocabulary.
The constructors are the names of the symbol, prefixed with `
.
Custom types are declared using the keyword type
, e.g., type color
.
Their name should be singular and capitalized, by convention.
Their extension can be defined in a structure, or directly in the vocabulary, by specifying:
a list of (ranges of) numeric literals, e.g.,
type someNumbers := {0,1,2}
ortype byte := {0..255}
a list of (ranges of) dates, e.g.,
type dates := {#2021-01-01, #2022-01-01}
ortype dates := {#2021-01-01 .. #2022-01-01}
a list of nullary constructors, e.g.,
type Color := {Red, Blue, Green}
a list of n-ary constructors; in that case, the enumeration must be preceded by
constructed from
, e.g.,type Color2 := constructed from {Red, Blue, Green, RGB(R: Byte, G: Byte, B: Byte)}
In the above example, the constructors of `Color
are : Red
, Blue
, Green
.
The constructors of `Color2
are : Red
, Blue
, Green
and RGB
.
Each constructor have an associated function (e.g., is_Red
, or is_RGB
) to test if a Color2 term was created with that constructor.
The RGB
constructor takes 3 arguments of type Byte
.
R
, G
and B
are accessor functions: when given a Color2 term constructed with RGB, they return the associated Byte.
(When given a Color2 not constructed with RGB, they may raise an error)
Functions¶
The functions with name myFunc1
, myFunc2
, input types T1
, T2
, T3
and output type T
, are declared by:
myFunc1, myFunc2 : T1 ⨯ T2 ⨯ T3 → T
Their name should not start with a capital letter, by convention.
The ASCII equivalent of ⨯
is *
, and of →
is ->
.
The input and output types of myFunc1 can be sets of concepts with a specific type signature, indicated in brackets. For example, T1 can be Concept[TT1 -> TT2] to denote the concepts with type signature TT1 -> TT2.
IDP-Z3 does not support partial functions.
Built-in functions¶
The following functions are built-in:
abs: Int → Int
(orabs: Float → Float
) yields the absolute value of an integer (or float) expression;arity: Concept → Concept
yields the arity of a symbol;input_domain : Concept ⨯ ℤ → Concept
yields the n-th input-domain of a symbol;output_domain: Concept → Concept
yields the output domain of a symbol.
Predicates¶
The predicates with name myPred1
, myPred2
and argument types T1
, T2
, T3
are declared by:
myPred1, myPred2 : T1 ⨯ T2 ⨯ T3 → 𝔹
Their name should not start with a capital letter, by convention.
The ASCII equivalent of →
is ->
, and of 𝔹
is Bool
.
The input and output types of myPred1 can be sets of concepts with a specific type signature, indicated in brackets. For example, T1 can be Concept[TT1 -> TT2] to denote the concepts with type signature TT1 -> TT2.
There is a built-in predicate T: T → 𝔹
for each type T (T(x)
is true
for any x
in T
).
Propositions and Constants¶
A proposition is a predicate of arity 0; a constant is a function of arity 0.
MyProposition : () → 𝔹
MyConstant: () → Int
Include another vocabulary¶
A vocabulary W may include a previously defined vocabulary V:
vocabulary W {
extern vocabulary V
// here comes the vocabulary named V
}
Symbol annotations¶
To improve the display of functions and predicates in the Interactive Consultant,
their declaration in the vocabulary can be annotated with their intended meaning, a short comment, or a long comment.
These annotations are enclosed in [
and ]
, and come before the symbol declaration.
- Intended meaning
[this is a text]
specifies the intended meaning of the symbol. This text is shown in the header of the symbol’s box.- Short info
[short:this is a short comment]
specifies the short comment of the symbol. This comment is shown when the mouse is over the info icon in the header of the symbol’s box.- Long info
[long:this is a long comment]
specifies the long comment of the symbol. This comment is shown when the user clicks the info icon in the header of the symbol’s box.
Theory¶
theory T:V {
// here comes the theory named T, on vocabulary named V
}
A theory is a set of axioms and definitions to be satisfied. If the names are omitted, the theory is named T, for vocabulary V.
Before explaining their syntax, we need to introduce the concept of term.
Mathematical expressions and Terms¶
A term is inductively defined as follows:
- Numeric literal
Numeric literals that follow the Python conventions are numerical terms of type
Int
orReal
.- Constructor
Each constructor of a type is a term having that type.
- Variable
a variable is a term. Its type is derived from the quantifier expression that declares it (see below).
- Function application
F(t_1, t_2,.., t_n)
is a term, whenF
is a function symbol of arityn
, andt_1, t_2,.., t_n
are terms. Each term must be of the appropriate type, as defined in the function declaration in the vocabulary. The resulting type of the function application is also defined in the function declaration. If the arity ofF
is 0, i.e., ifF
is a constant, thenF()
is a term.$(s)(t_1, t_2,.., t_n)
is a term, whens
is an expression of type Concept that denotes a function of arityn
, andt_1, t_2,.., t_n
are terms.Please note that there are built-in functions (see Built-in functions).
- Negation
-
t
is a numerical term, whent
is a numerical term.- Arithmetic
t_1 ꕕ t_2
is a numerical term, whent_1
,t_2
are two numerical terms, andꕕ
is one of the following math operators+, -, * (or ⨯), /, ^, %
. Mathematical operators can be chained as customary (e.g.x+y+z
). The usual order of binding is used.- Parenthesis
(
t
) is a term, whent
is a term- Cardinality aggregate
#{v_1 in typeOfV_1, .., v_n in typeOfV_n : ϕ}
is a numerical term whenv_1 v_2 .. v_n
are variables, andϕ
is a sentence containing these variables.The term denotes the number of tuples of distinct values for
v_1 v_2 .. v_n
which makeϕ
true.- Aggregate over anonymous function
agg(lambda v_1 in typeOfV_1, .., v_n in typeOfV_n : t)
is a numerical term whereagg
can be any of (sum
,min
,max
),v_1 v_2 .. v_n
are variables andt
is a term.The term
sum(lambda v in T : t(v))
denotes the sum oft(v)
for eachv
inT
. Similarly,min
(resp.max
) can be used to compute the minimum (resp. maximum) oft(v)
for eachv
inT
.t(v)
can use the construct(if .. then .. else ..)
to filter out unwantedv
values.- (if .. then .. else ..)
(if t_1 then t_2 else t_3)
is a term whent_1
is a sentence,t_2
andt_3
are terms of the same type.
Sentences and axioms¶
An axiom is a sentence followed by .
.
A sentence is inductively defined as follows:
- true and false
true
andfalse
are sentences.
- Predicate application
P(t_1, t_2,.., t_n)
is a sentence, whenP
is a predicate symbol of arityn
, andt_1, t_2,.., t_n
are terms. Each term must be of the appropriate type, as defined in the predicate declaration. If the arity ofP
is 0, i.e., ifP
is a proposition, thenP()
is a sentence.$(s)(t_1, t_2,.., t_n)
is a sentence, whens
is an expression of type Concept that denotes a predicate of arityn
, andt_1, t_2,.., t_n
are terms.- Comparison
t_1 ꕕ t_2
is a sentence, whent_1
,t_2
are two numerical terms andꕕ
is one of the following comparison operators<, ≤, =, ≥, >, ≠
(or, using ascii characters:=<, >=, ~=
). Comparison operators can be chained as customary.- Negation
¬ϕ
is a sentence (or, using ascii characters:~ϕ
) whenϕ
is a sentence.- Logic connectives
ϕ_1 ꕕ ϕ_2
is a sentence whenϕ_1, ϕ_2
are two sentences andꕕ
is one of the following logic connectives∨,∧,⇒,⇐,⇔
(or using ascii characters:|, \&, =>, <=, <=>
respectively). Logic connectives can be chained as customary.- Parenthesis
(
ϕ
) is a sentence whenϕ
is a sentence.
- Enumeration
An enumeration (e.g.
p := {1;2;3}
) is a sentence. Enumerations follow the syntax described in structure.- Quantified formulas
Quantified formulas are sentences. They have one of the following forms, where
v_1, .., v_n
are variables,p, p_1, .., p_n
are types or predicates, andϕ
is a sentence involving those variables:∀ v_1, v_n: ϕ(v_1, v_n). ∀ v_1, v_n ∈ p: ϕ(v_1, v_n). ∀ (v_1, v_n) ∈ p: ϕ(v_1, v_n). ∀ v_1 ∈ p_1, v_n ∈ p_n: ϕ(v_1, v_n).
Alternatively, the existential quantifier,
∃
, can be used. Ascii characters can also be used:?
,!
, respectively. For example,! x, y in Int: f(x,y)=f(y,x).
A variable may only occur in the
ϕ
sentence of a quantifier declaring that variable. In the first form above, the type of each variable is inferred from their use inϕ
.When quantifying a formula of type
Concept
, the expression must contain a “guard” to prevent arity or type error. A guard is a condition that can be resolved using the available enumerations. In the following example,symmetric
must be defined by enumeration.symmetric := {`edge} ∀s ∈ Concept: symmetric(s) => (∀x, y : $(s)(x,y) ⇒ $(s)(y,x)).
An alternative is to use the introspection functions
arity, input_domain, output_domain
:∀s ∈ Concept: arity(s)=2 ∧ input_domain(s,1)=input_domain(s,2) ⇒ (∀x ∈ $(input_domain(s,1)), y ∈ $(input_domain(s,2)) : $(s)(x,y) ⇒ $(s)(y,x)).
Another alternative is to add the signature of the Concept in the quantification:
∃x ∈ Concept[()→B]: $(x)().
- “is (not) enumerated”
f(a,b) is enumerated
andf(a,b) is not enumerated
are sentences, wheref
is a function defined by an enumeration and applied to argumentsa
andb
. Its truth value reflects whether(a,b)
is enumerated inf
’s enumeration. If the enumeration has a default value, every tuple of arguments is enumerated.- “(not) in {1,2,3,4}”
f(args) in enum
andf(args) not in enum
are sentences, wheref
is a function applied to argumentsargs
andenum
is an enumeration. This can also be written using Unicode:f() ∈ {1,2,3}
orf() ∉ {1,2,3}
.- if .. then .. else ..
if t_1 then t_2 else t_3
is a sentence whent_1
,t_2
andt_3
are sentences.
Definitions¶
A definition defines concepts, i.e. predicates or functions, in terms of other concepts.
If a predicate is inductively defined in terms of itself, the definition employs the well-founded semantics.
A definition consists of a set of rules, enclosed by {
and }
.
Rules have one of the following forms:
∀ v_1 ∈ T_1, v_n ∈ T_n: P(t_1, .., t_n) ← |phi|.
∀ v_1 ∈ T_1, v_n ∈ T_n: F(t_1, .., t_n) = t ← |phi|.
where P is a predicate symbol, F is a function symbol, t
, t_1, t_2,.., t_n
are terms that may contain the variables v_1 v_2 .. v_n
and ϕ
is a formula that may contain these variables.
P(t_1, t_2,.., t_n)
is called the head of the rule and ϕ
the body.
<-
can be used instead of ←
.
If the body is true
, the left arrow and body of the rule can be omitted.
Annotations¶
Some expressions can be annotated with their informal meaning, between brackets.
For example, [age is a positive number] 0 =< age()
.
Such annotations are used in the Interactive Consultant.
The following expressions can be annotated:
Definitions
Rules
Constraints
Quantified formula
Comparisons
Membership in an enumeration
Brackets
When necessary, use parenthesis to avoid ambiguity, e.g. [Positive or p] ( [Positive] x()<0 ) | p().
.
Structure¶
structure S:V {
// here comes the structure named S, for vocabulary named V
}
A structure specifies the interpretation of some type, predicates and functions, by enumeration. If the names are omitted, the structure is named S, for vocabulary V.
A structure is a set of statement of the form <symbol> := <enumeration>
,
e.g., P := {1..9}
, where the enumeration can be:
- for nullary predicates (propositions)
true
orfalse
- for non-numeric types and unary predicates:
a set of rigid terms (numbers, dates, identifiers, or constructors applied to rigid terms), e.g.,
{red, blue, green}
.- for numeric types and unary predicates:
a set of numeric literals and ranges, e.g.,
{0,1,2}
,{0..255}
or{0..9, 90..99}
- for date types and unary predicates:
a set of date literals and ranges, e.g.,
{#2021-01-01, #2022-01-01}
or{#2021-01-01 .. #2022-01-01}
- for types:
a set of n-ary constructors, preceded by
constructed from
, e.g.,constructed from {Red, Blue, Green, RGB(R: Byte, G: Byte, B: Byte)}
(see more details in types)- for n-ary predicates:
a set of tuples of rigid terms, e.g.,
{(a,b), (a,c)}
.- for nullary functions:
a rigid term, e.g.
5
or#2021-01-01
, orred
orrgb(0,0,0)
- for n-ary functions:
a set of tuples and their associated values, e.g.,
{ (1,2)->3, (4, 5)->6 }
Additional notes:
the enumeration for a predicate specifies the tuples that make the predicate true; any other tuple make it false.
the enumeration for a function may be followed by
else <default>
, where<default>
is a default value (a rigid term), i.e., a value for the non-enumerated tuples, if any.parenthesis around a tuple can be omitted when the arity is 1, e.g.,
{1-2, 3->4}
a predicate may be enumerated using a CSV format, with one tuple per line, e.g., :
P := {
1 2
3 4
5 6
}
The enumeration of
goal_string
is used to compute relevance relative to goals (see thedetermine_relevance
method in the Theory class).
Differences with IDP3¶
Here are the main differences with IDP3 (the previous version of IDP-Z3), listed for migration purposes:
- Infinite domains
IDP-Z3 supports infinite domains:
Int, Real
. However, quantifications over infinite domains is discouraged.- Type
IDP-Z3 does not support type hierarchies.
- LTC
IDP-Z3 does not support LTC vocabularies.
- Namespaces
IDP-Z3 does not support namespaces.
- Partial functions
IDP-Z3 does not support partial functions. The handling of division by 0 may differ. See IEP 07
- Syntax changes
The syntax of quantifications and aggregates has slightly change. IDP-Z3 supports quantification over the tuples satisfying a predicate. IDP-Z3 does not support qualified quantifications, e.g.
!2 x[color]: p(x).
. (p. 11 of the IDP3 manual).- if .. then .. else ..
IDP-Z3 supports if .. then .. else .. terms and sentences.
- Structure
IDP-Z3 does not support
u
uncertain interpretations (p.17 of IDP3 manual). Function enumerations must have anelse
part. (see also IEP 04)
To improve performance, do not quantify over the value of a function.
Use p(f(x))
instead of ?y: f(x)=y & p(y)
.