Queries and Latent Queries

A query is a Flora statement that asks the Flora reasoner to decide whether it is true or not. The syntax of a query is the same as for a Flora fact, except with ?- in the beginning. For example,

?- Greg : Man.

Read this as “is Greg a Man?”. Flora will answer this query with “Yes” or “No”.

Queries can use all the syntax described above, such as composite and nested frames.

A query typically contains variables. The reasoner will then answer the query with all the possible values for all the variables. Variables are identifiers starting with ?. For example, given all the facts about Greg above, the query

Greg [ child -> ?x ].

which can be read as “who are Greg’s children?”, will result in two solutions,

?x = Kelly
?x = Louisa

In this case, the Flora reasoner only had to do a simple lookup. In the general case, answering a query can involve complex (and sometimes time-consuming) reasoning, due to the presence of rules, the subject of the next section.

Queries in a Flora file are executed when the file is loaded. This is typically not the desired behavior. We only use such plain queries to import other Flora files, using the add{…} operator (discussed later in this chapter).

In general, we recommend using latent queries, which are not executed when a file is loaded. In Sunflower, latent queries can be browsed and executed in the Queries tab. Latent queries have the form

@!{'Who are Louisas parents?'(?Parent)} !-
  ?Parent [ child -> Louisa ].

First we have a query label, inside @!{…}, then the special !- operator, then the query body. The query body works just like the queries we have discussed so far, i.e. it is like a Flora fact, usually with variables, that we want to ask the reasoner about. The query label is a name of the query, followed by a comma-separated list of variables in parentheses. The variables are the ones whose bindings we want to see in the query result – normally the same as those found in the query body.

Note that the query label is just a Flora identifier. In Flora, you can use single-quotes around an identifier in order to use special characters (such as space and question mark) in it. Here, we have used this feature in order to write the query label as a descriptive English sentence, ’Who are Louisas parents?’. Alternatively, we could have called it something like LouisaParentsQuery.