Fact is an operator which tests if a term has been set in a preceding unification+evaluation step. It is helpful when working with data which is incomplete or contains optional values. The associated keyword unknown can be assigned to an untyped variable to indicate a value is not available. A fact test returns false on seeing unknown. A variable set to unknown when formated to text produces "unknown".
Here is example of the fact operator being used in a calculator to check if a query returned a result (fact is true) or short circuited (fact is false). The calculator calls the "age_rating" Choice from tutorial 10 which short circuits if the person's age is under 21:
template age_rating(age_weight) << age_rating(age),
? fact(age_rating),
double rating = age_weight +
0.2 * (starsign == \"gemini\"),
axiom star_person = { name, sex, starsign, rating },
star_people += star_person
);
Here the fact operator tests the inner template and is used in a short circuit expression so that the following steps are skipped. When used on a template, the fact operator will check each term of the template, otherwise, it just checks a single term.
The tutorial17 AgeDiscrimination2 code example runs this query.
Here are the 3 solutions listed when the query is run:
name = Sam, sex = m, starsign = scorpio, rating = 0.3
name = Jenny, sex = f, starsign = gemini, rating = 0.8
unknown
A variable with a number type can be assigned keyword NaN to indicate a value is not available. Otherwise keyword unknown can be used, but the variable must be untyped in this case.
The next example is different from the previous one in that the Choice returns "NaN" as the age rating for people under 21. In the calculator, the fact operator is applied only to age rating, which defaults to "unknown".
