Methods and Types
Iterations
categories
Tables, Rules and Hypothetical Reasoning
Tables
Rules

Tables, Rules and Hypothetical Reasoning

Tables

Named arrays, called tables, can be defined in CLAIRE with the following syntax :
 <name>[var:<domain>] : <type:= <expression(var)>
The <type> is the range of the table and <expression> is an expression that is used to fill the table. This expression may either be a constant or a function of the variables of the table (i.e., an expression in which the variables appear). If the expression is a constant, it is implicitly considered as a default value, the domain of the table may thus be infinite. If the default expression is a function, then the table is filled when it is created, so the domain needs to be finite. When one wants to represent incomplete information, one should fill this spot with the value unknown. For instance, we can define :
 square[x:(0 .. 20)] : integer := (x * x)
 creator[x:class: string := "who created that class"
 maximum[x:set[0 .. 10]] : integer := (if x min(x,> @ integerelse 0)
Tables can be accessed through square brackets and can be modified with assignment expressions like for local variables :
 square[1], square[2:= 4square[4:+ 5
We can also define two-dimensional tables such as :
 distance[x:tuple(city,city)] : integer := 0
 cost[x:tuple(1 .. 101 .. 10)] : integer := 0
The proper way to use such a table is distance[list(denver,miami)] but CLAIRE also supports distance[denver,miami]. CLAIRE also supports a more straightforward declaration such as :
 cost[x:(1 .. 10), y:(1 .. 10)] : integer := 0
Last, tables can be defined in an unamed fashion through the method make_table, such unamed can be collected by the GC :
 let square := make_table((1 .. 10), integer0)
 in (for n in (1 .. 10))
         square[n:= n * n,
     ...)


categories Tablesnormal dispatch Core method

erase(a:table) -> void

erase(a) removes all value pairs contained in the table. This means that, on one hand, the value a[x] becomes unknown for each object x, and also that any references to an object from the table's domain or an associated value is lost, which may be useful to allow for complete garbage collection.


categories Tablesnormal dispatch Kernel method

make_table(d:type, t:type, x:any) -> table

returns a table with a domain of type d and a range t. The parameter x is the default value, thus x must belong to t, as well as any future value that will be put in the table.

[XL] In XL CLAIRE, tables created by make_table are seen as ephemeral, thus collectable by the GC.


categories Tablesnormal dispatch Kernel method

nth(t:table, x:any) -> any

nth(t,x) returns the element of t that as the key x. nth(t,x) is equivalent to t[x].


categories Tablesnormal dispatch Kernel method

nth(t:table, x:any, y:any) -> any

nth(t,x) returns the element of t that as the key tuple(x, y). nth(t,x) is equivalent to t[x,y], t[tuple(x,y)] and nth(t,tuple(x,y)).


categories Tablesnormal dispatch Kernel method

nth=(t:table, x:any, y:any) -> any

nth=(t,x,y) sets the item of t with key x to y. nth=(t,x,y) is equivalent to t[x] := y.


categories Tablesnormal dispatch Kernel method

nth=(t:table, x1:any, x2:any, y:any) -> any

nth=(t,x1,x2,y) sets the item t with key tuple(x1,x2) to y nth=(t,x1,x2,y) is equivalent to t[x1,x2] := y.


categories Tablesnormal dispatch Kernel method

put(t:table, x:object, y:any) -> any

put(t,x,y) is equivalent to t[x] := y but does not trigger the rules associated to a. Besides, this operation is performed without any type-checking.