Sather Home Page

Section 8.12.2.1:
CARD

immutable class CARD < $CARDINAL{CARD}, $OPTION,$EXACT_FMT

$CARDINAL{CARD} $OPTION $EXACT_FMT CARD
Inheritance Diagram

Formal Types

types

SAME = CARD ;

CARD = nat
   inv num ==
      let loc_bits : seq of BIT be st loc_bits = num in
         len loc_bits = NUM_BITS.Num_Bits

This class is the implementation of the mathematical concept of a cardinal number which is represented in a limited range of values. The range is limited to those numbers representable in binary number format in the implementation defined number of bits in the required class NUM_BITS in accordance with the requirement in section 4.2 of this document.

Auxiliary Functions

Creation of cardinal numbers from strings requires the ability to create a natural number from any sequence of text digits and separators. The two functions defined below permit this to be done.

functions

make_one : STR * nat * nat * CHAR --> nat

make_one(res,val_so_far,base,separator) ==
      cases res :
         res = [] -> return val_so_far,
         hd res = separator -> return make_one(tl res, val_so_far, base, separator),
         others -> let loc_lib : LIBCHARS = STR.index_lib(str) in
                          let next_val = base * val_so_far + LIBCHARS.digit_value(hd res) in
                             return make_one(tl res,next_val,base,separator) ;


ideal_create : STR * nat * CHAR --> nat

ideal_create(str, base, sep) ==
     return make_one(str,0,base,sep)

This class is the implementation of the mathematical concept of a cardinal number which is represented in a limited range of values. The range is limited to those numbers representable in binary number format in the implementation defined number of bits in the required class NUM_BITS in accordance with the requirement in section 4.2 of this document.

External specifications

The following feature is required to be implemented for this class in accordance with the specification given in $IS_EQ of which $CARDINAL{CARD} is a sub-type :-


The following features are required to be implemented for this class in accordance with the specifications given in $BINARY from which this class inherits :-


The following features are required to be implemented for this class in accordance with the specifications given in $EXACT_FMT from which this class inherits :-


The following feature is required to be implemented for this class in accordance with the specification given in $HASH from which this class inherits :-


The following features are required to be implemented for this class in accordance with the specification given in $STR from which this class inherits :-


The following feature is required to be implemented for this class in accordance with the specification given in $IS_LT{CARD} of which $CARDINAL{CARD} is a sub-type :-


The following feature is required to be implemented for this class in accordance with the specification given in $IS_NIL of which $CARDINAL{CARD} is a sub-type :-


The following feature is required to be implemented for this class in accordance with the specification given in $NIL of which $CARDINAL{CARD} is a sub-type :-


The following feature is required to be implemented for this class in accordance with the specification given in $TEXT which is a sub-type of the class $CARDINAL{CARD} from which this class inherits :-


The following feature is required to be implemented for this class in accordance with the specification given in $ZERO{CARD} which is a sub-type of the class $CARDINAL{CARD} from which this class inherits :-

middle( first, second : SAME ) : SAME

middle

This is a convenience routine to return that value of the three given which lies between the other two.

middle (
first : SAME,
second : SAME
) : SAME
Formal Signature
middle(self : SAME, first : SAME, second : SAME) res : SAME
Pre-condition
pre not is_nil(self)
      and not is_nil(first)
      and not is_nil(second)
Post-condition
post ((res = self)
         and (first < self)
         and (self < second))
      or ((res = first)
         and (self < first)
         and (self < second))
      or (res = second)

This routine returns the result of min(second) being applied to max(first).


aelt!

This iter feature yields true for each occasion that the current bit has the value setbit.

aelt!
Formal Signature

Note that the formal name of the iter has been changed to replace the exclamation mark iter symbol to a name acceptable to vdm tools.

aelt_iter(self : SAME) res : BOOL
Pre-condition

Since the argument is no optional then this pre-condition is vacuously true.

Post-condition

This post-condition makes use of the history concept from vdm++ (see the vdm dialect notes).

post let bits : seq of BIT be st bits = self in
      res = (bits(len history) = setbit)
      and (history~ = []
            and history = [res])
         or (history = history~ ^ [res])
Quit condition

For quit actions see the specification of the quit statement.

errs QUIT : (len history = NUM_BITS.Num_Bits) -> quit

This iter is effectively a predicate which yields true if and only if the next bit of self has the value setbit.


binary!

This iter feature successive powers of two until the next value would be greater than limit.

binary! (
once limit : SAME
) : SAME
Formal Signature

Note that the formal name of the iter has been changed to replace the exclamation mark iter symbol to a name acceptable to vdm tools.

binary_iter(self : SAME) res : BOOL
Pre-condition

Since the argument is not optional then this pre-condition is vacuously true.

Post-condition

This post-condition makes use of the history concept from vdm++ (see the vdm dialect notes).

post (history~ = []
         and (history = [1])
         and res = history(1))
      or let loc_res = history(len history~) in
            res = loc_res * 2
               and history = history~ ^ [res]
Quit condition

For quit actions see the specification of the quit statement.

errs QUIT : (
         let loc_res = history~(len history~) * 2 in
            loc_res > limit) -> quit

This iter yields successive powers of two, starting with the value 1 and terminating before yielding a value which would be greater than the limit given.

is_card(str : STR) : CONVERSION_RESULTS

is_card

This predicate returns the result which would be given were the argument string to be converted to a cardinal number. If All_Right is returned then conversion will be successful.

is_card (
str : STR
) : CONVERSION_RESULTS
Formal Signature
is_card(str : STR) res : CONVERSION_RESULTS
Pre-condition

Since the argument is not optional then this pre-condition is vacuously true.

Post-condition
post let loc_cult : CULTURE = LIBCHARS.culture(LIBCHARS.index_lib(str)) in
      let loc_fmt = NUMBERS.format(CULTURE.numeric(loc_cult)) in
         let separator = NUMBER_FMT.thousands_sep(loc_fmt) in

      ((res = CONVERSION_RESULTS.All_Right)
         and not (create(str) = CARD.nil))
      or ((res = CONVERSION_RESULTS.Empty)
         and (len str = 0))
      or ((res = CONVERSION_RESULTS.Bad_Format)
         and exists elem : CHAR | elem in set dom str &
            not (CHAR.is_digit(elem)
                  or elem = separator)
      or ((res = CONVERSION_RESULTS.Out_of_Range)
         and (let num : nat = ideal_create(str,10, separator) in
                  num > maxval))

This feature returns the result which would be indicated were an attempt to convert the string to a cardinal number to be made.

is_octal(str : STR) : CONVERSION_RESULTS is_hex(str : STR) : CONVERSION_RESULTS hex_create(str : STR) : SAME ; oct_create(str : STR) : SAME ; oct_str(lib : LIBCHARS) : STR oct_str : STR decimal_str(lib : LIBCHARS, min_count : CARD) : STR decimal_str(lib : LIBCHARS) : STR decimal_str(min_count : CARD) : STR decimal_str : STR hex_str(lib : LIBCHARS, min_count : CARD) : STR hex_str(lib : LIBCHARS) : STR hex_str(min_count : CARD) : STR hex_str : STR
Language Index Library Index Numeric Index
Comments or enquiries should be made to Keith Hopper.
Page last modified: Friday, 1 June 2001.
Produced with Amaya