No edit summary |
No edit summary |
||
Line 21: | Line 21: | ||
parity(0) = 0 & | parity(0) = 0 & | ||
!x.(x:NATURAL1 => parity(x) = 1 - parity(x-1)) | !x.(x:NATURAL1 => parity(x) = 1 - parity(x-1)) | ||
* | * Write your function constructively using a single recursive equation using set comprehensions, lambda abstractions, finite sets and set union. This requires ProB 1.3.5-beta7 or higher and you need to declare <tt>parity</tt> as <tt>ABSTRACT_CONSTANT</tt>. Here is a possible equation: | ||
parity : INTEGER <-> INTEGER & | |||
parity = {0|->0} \/ %x.(x:NATURAL1|1-parity(x-1)) | |||
* Another solution is try and write your function constructively and non-recursively, ideally using a lambda abstraction: | * Another solution is try and write your function constructively and non-recursively, ideally using a lambda abstraction: | ||
parity : (NATURAL --> {0,1}) & | parity : (NATURAL --> {0,1}) & |
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 has to represent the function as a finite set of maplets.
There are basically three 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))
parity : (NATURAL --> {0,1}) & 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.
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.