French German Italian Spanish Portuguese Japanese Korean Chinese

- The
**all**Formula **all**,**one**,**min**and**max**Formulas- The Standard Order for Sorting
- The Whole Truth about Queries
- Query Output to Database Files
- Summary

The keyword

proc All_men(m:>list P_data_t) iff {'m' is the list of all men from the P_data file} all rec in m P_data(rec) & rec = (n, Male, b, d, c) endThe body of this procedure consists of an

Here's another example. Recall the predicate

proc All_evens(x:<list L, x_evens:>list L) iff { 'x_evens' is a sorted list of all the even elements of 'x'} all n in x_evens n in x & Even(n) endThe

all All_evens((1, 7, 3, 6, 9, 6, 2, 5, 7, 4, Nil), x)would result in the single solution

x = (2, 4, 6, Nil)FormulaOne has extracted the even elements of the list (6, 6, 2, and 4), sorted them, and removed duplicates.

all var_{1}, var_{2},...,var_{n} in listvar
formula
end

In this format,

*listvar*is a declared variable which can be assigned a value directly; that is, an input/output variable or an output variable on its first use. It must be of type list T or file T, where T is a tuple type or a simple type (such as**L**or**S**).*var*are variables not declared outside the all formula. They may be declared within the body of the formula; if not, they are implicitly declared according to the declaration of the_{1}... var_{n}*listvar*. If there is more than one variable in the list, they stand for the various fields of the elements of listvar (in this case the element type of listvar must be a tuple type). If there is only one variable, then the variable stands for an entire element.- The formula is computed as the body of a true predicate, so there can be no input/output variables declared in it, nor any calls to subrs. It can refer to variables which have values (such as input variables or initialized output or input/output variables), but not change their values or put constraints on them.

Logically, the

all var_{1}, var_{2}, ..., var_{n} in var form end

min var_{1}, var_{2}, ..., var_{n} form end

max var_{1}, var_{2}, ..., var_{n} form end

one var_{1}, var_{2}, ..., var_{n} form end

All four formulas try to find the values of variables 'varAll four kinds of formulas can be used only in procedure or subroutine contexts. Formula

y :> I & one xy = 3 & x=l | x = 2 end & y = 2 y :> I & y = 2 & one xy = 3 & x = l | x = 2 endAccording to the logic both formulas should yield the same result y = 2 & x = 2. The first formula fails because of the non-local assignment y = 3 within the one formula. The variable

The variables

x :> I & min x & x::L & T(x) endThe variable

If the variable

Reals are sorted according to the usual order (lowest before highest). Strings are sorted in lexicographic order - that is, the order in which they would be found in a dictionary. Pair terms, including tuples, arrays, and unions, are sorted recursively as follows:

if a < b, or if a = b and c < d, then (a, c) < (b, d) . if a = b and c = d, then (a, c) = (b, d). Otherwise,(a, c) > (b, d).

All reals (including integers) are less than all strings, which are less than all pairs. FormulaOne thus provides the programmer with a general sorting facility. We can use this fact to program an inefficient, but working general sorting procedure.The idea behind this procedure is that the user provides a

proc Sort(x :< list U, key_code :< I, sx :> list U) iff { 'sx' is 'x' sorted by the Key function by the given 'key_code', with duplicates removed } ksx :> list (U, U) & {keyed, sorted x} all key, xe in ksx { get elements of x, pair with keys, sort } xe in x & Key(xe, key_code, key) end & Unkeyed_list(ksx, sx) { take off keys } proc Unkeyed_list(ky :< list (U, U), y :> list U) iff { 'ky' is a list of elements paired with their keys; 'y' is the list of just the elements } case ky of Nil => y = Nil; ((key, elt), tail) => y = (elt, Unkeyed_list(tail)); end {sample Key function} proc Key(x :< U, key_code :< I, key :> U) iff case key_code:[0..4] of 0 => {P_data_t, sort by name} y = (x:P_data_t) & key = y.name; 1 => {P_data_t, sort by birthdate} y = (x:P_data_t) & key = y.b_date; 2 => {Date_t, sort by month} y = (x:Date_t) & key = y.month; 3 => {Real or other number, sort in descending order} x = R(n) & key = -n; else {sort by standard order; use 0 as key} key = 0 end

[all [ var_{l} [, ...., var_{n}]] [in outspec]] formula [end]

var- a string constant containing the DOS file name of a standard ASCII file (for example, 'query.out' or '\\misc\\results.txt'), in which case the solutions are printed on that file in the same format as they would in the results screen; or
- a database file name (see the next section for details).

If no

So we can see a query with an

all n, g, b, d, c in 'all_pdata.txt' P_data(n, g, b, d, c)A query which can only be a subroutine or procedure body cannot have an

all n, g, b, d, c in Men_data P_data(n, g, b, d, c) & g = MaleThis query has the effect that the output of the query proper (the part on the second line) is not displayed on the screen, but re-routed to the file

Men_data :< P_data_ft = 'mendata.dbs':P_data_ftSay

'Heather',Female,(1961,7,2),(0,1,1),'Musician' 'Isadora',Female,(1924,3,22),(0,1,1),'Homemaker' 'Steven',Male,(1921,9,20),(0,1,1),'Engineer'Then

'Steven',Male,(1921,9,20),(0,1,1),'Engineer'after the query. The records that met the conditions in the query will have been copied over in a sorted order.

We can do the same kind of thing from within a procedure. We could define a procedure

proc Get_men(f:.P_data_ft) iff {get all men from P_data into f} all n, g, b, d, c in f P_data(n, g, b, d, c) & g = Male endThen the query

Get_men(Men_data)would have exactly the same effect as the query at the start of this section.

- The
**all**formula allows all solutions to a backtracking formula to be collected and sorted within a procedure or subroutine. - Output of solutions can be done to either a list variable or a file variable.
- Calling predicates (
**pred**) or to backtrack in the procedure or subroutine can be done only by mean of the**all/one/min/max**formulas. - There is a standard order by which all elements in the FormulaOne universe are sorted.
- The syntax of a query is really just a variant of the syntax of the
**all**formula. - A query's output can be redirected to a DOS file in the same format as it appears in the results window.
- The
**all**construct can be used to fill a database file with records corresponding to solutions to a backtracking formula, either from the query level or from within a procedure.

Top Database Files Miscellaneous Advanced Topics