I am new to Z notation,
Lets say I have a function f defined as X |--> Y , where X is string and Y is number.
How can I get highest Y value in this function? Does 'loop' exist in formal method so I can solve it using loop?
I know there is recursion in Z notation, but based on the material provided, I only found it apply in multiset or bag, can it apply in function?
Any extra reference application of 'loop' or recursion application will be appreciated. Sorry for my English.
You can just use the predefined function
max
that takes a set of integers as input and returns the maximum number. The input values here are the range (the set of all values) of the function:Please note that the maximum is not defined for empty sets.
Regarding your question about recursion or loops: You can actually define a function recursively but I think your question aims more at a way to compute something. This is not easily expressed in Z and this is IMO a good thing because it is used for specifications and it is not a programming language. Even if there wouldn't be a
max
orran
function, you could still specify the numberm
you are looking for by:("
m
is a value off
, belonging to ans
and all other valuesi2
off
are smaller or equal")After getting used to the style it is usually far better to understand than any programming language (except your are trying to describe an algorithm itself and not its expected outcome).#
Just for reference: An example of a recursive definition (let's call it
rmax
) for the maximum would consist of a base case:and a recursive case:
But note that this is still not a "computation rule" of
rmax
becausee
in the second rule can be an arbitrary element ofS
. In more complex scenarios it might even be not obvious that the defined relation is a function at all because depending on the chosen elements different results could be computed.