No edit summary |
No edit summary |
||
Line 31: | Line 31: | ||
** apply the function, e.g., <tt>parity(10001)</tt> is the value <tt>1</tt> | ** apply the function, e.g., <tt>parity(10001)</tt> is the value <tt>1</tt> | ||
** compute the image of the function, e.g., <tt>parity[10..20]</tt> is <tt>{0,1}</tt> | ** compute the image of the function, e.g., <tt>parity[10..20]</tt> is <tt>{0,1}</tt> | ||
** check if a tuple is a member of the function, e.g., <tt>20|->0 : parity</tt> | |||
** check if a tuple is not a member of the function, e.g., <tt>21|->0 /: parity</tt> | |||
** check if a finite set of tuples is a subset of the function, e.g., <tt>{20|->0, 120|->0, 121|->1, 1001|->1} <: parity</tt> | |||
** check if a finite set of tuples is not a subset of the function, e.g., <tt>{20|->0, 120|->0, 121|->1, 1001|->2} /<: parity</tt> | |||
** compose the function with a finite relation, e.g., <tt>(id(1..10) ; parity)</tt> gives the value <tt>[1,0,1,0,1,0,1,0,1,0]</tt> | ** compose the function with a finite relation, e.g., <tt>(id(1..10) ; parity)</tt> gives the value <tt>[1,0,1,0,1,0,1,0,1,0]</tt> | ||
** sometimes compute the domain of the function, here, <tt>dom(parity)</tt> is determined to be <tt>NATURAL</tt>. But this only works for simple infinite functions. | ** sometimes compute the domain of the function, here, <tt>dom(parity)</tt> is determined to be <tt>NATURAL</tt>. But this only works for simple infinite functions. |
Take the following function:
CONSTANTS parity PROPERTIES parity : (NATURAL --> {0,1}) & parity(0) = 0 & !x.(x:NATURAL => parity(x+1) = 1 - parity(x))
Here, ProB will complain that it cannot find a solution for parity. The reason is that parity is a function over an infinite domain, but ProB tries to represent the function as a finite set of maplets.
There are basically four solutions to this problem:
parity : (NAT --> {0,1}) & parity(0) = 0 & !x.(x:NAT & x<MAXINT => parity(x+1) = 1 - parity(x))
parity : (NATURAL --> {0,1}) & parity(0) = 0 & !x.(x:NATURAL1 => parity(x) = 1 - parity(x-1))
parity : INTEGER <-> INTEGER & parity = {0|->0} \/ %x.(x:NATURAL1|1-parity(x-1))
Note, you have to remove the check parity : (NATURAL --> {0,1}), as this will currently cause expansion of the recursive function. We describe this new scheme in more detail below.
parity : (NATURAL --> INTEGER) & parity = %x.(x:NATURAL|x mod 2)
You can experiment with those by using the Eval console of ProB, experimenting for example with the following complete machine. Note, you should use ProB 1.3.5-beta2 or higher.
(You can also type expressions and predicates such as parity = %x.(x:NATURAL|x mod 2) & parity[1..10] = res directly into the online version of the Eval console).
MACHINE InfiniteParityFunction CONSTANTS parity PROPERTIES parity : NATURAL --> INTEGER & parity = %x.(x:NATURAL|x mod 2) VARIABLES c INVARIANT c: NATURAL INITIALISATION c:=0 OPERATIONS Inc = BEGIN c:=c+1 END; r <-- Parity = BEGIN r:= parity(c) END; r <-- ParityImage = BEGIN r:= parity[0..c] END; r <-- ParityHistory = BEGIN r:= (%i.(i:1..c+1|i-1) ; parity) END END
You may also want to look at the tutorial page on modeling infinite datatypes.
Currently there are four cases when ProB tries to keep a function such as f = %x.(PRED|E) symbolically rather than computing an explicit representation:
As of version 1.3.5-beta7 ProB now accepts recursively defined functions. For this:
Here is a full example:
MACHINE Parity ABSTRACT_CONSTANTS parity PROPERTIES parity : INTEGER <-> INTEGER & parity = {0|->0} \/ %x.(x:NATURAL1|1-parity(x-1)) END
With such a recursive function you can:
However, composing it with another function (([1,2] ; parity)) does not work yet. This works for non-recursive infinite functions, as described above. Future versions of ProB will probably allow such constructs for recursive functions as well.
Also, you have to be careful to avoid accidentally expanding these functions. For example, trying to check parity : INTEGER <-> INTEGER or parity : INTEGER +-> INTEGER will cause ProB to try and expand the function. Future versions of ProB may overcome this limitation.
There are the following further restrictions: