Annotated King Reference Manual/Types

From Wikibooks, open books for an open world
Jump to navigation Jump to search

This page is work in progress.

Type Declarations

[edit | edit source]

Some general principles:

  • All scalar subtypes have a default value. For bounded numeric and enumeration subtypes, this is the 'First of the subtype, unless a different value is specified with the Default_Value aspect. For unbounded numeric subtypes, the Default_Value aspect must be specified. Note that an unbounded numeric type can have bounded subtypes.
  • The Predicate aspect may be specified for all subtypes.
  • All scalar types have the attributes 'Image, 'Value, 'Min, 'Max, 'Previous, and 'Next.
  • Integer and enumeration types have the attributes 'Position, 'Value (integer), 'Representation, and 'From_Representation. For integer types, 'Position and 'Representation both give the value of the parameter, and 'Value (integer) and 'From_Representation give the value of the type with the value of the parameter (if that exists); this is similar to 'Pos and "Val for integer types in Ada.
  • 'Image <=>, 'Value (string) are inverses of each other, as are 'Position <=> 'Value (integer)

Syntax

[edit | edit source]
type_declaration ::= full_type_declaration | hidden_type_declaration

full_type_declaration ::=
          type defining_identifier [known_discriminant_part] is type_definition
             [aspect_specification];

hidden_type_declaration ::=
          type defining_identifier [discriminant_part] is hidden
             [aspect_specification];

discriminant_part ::= unknown_discriminant_part | known_discriminant_part

unknown_discriminant_part ::= (<>)

known_discriminant_part ::= (discriminant_specification {; discriminant_specification})

discriminant_specification ::=
          defining_identifier : subtype_mark [<- default_expression]  

type_definition ::=
            enumeration_type_definition
          | integer_type_definition
          | real_type_definition
          | map_type_definition
          | sequence_type_definition
          | set_type_definition
          | record_type_definition
          | derived_type_definition
          | module_type_definition
          | task_type_definition

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Enumeration Types

[edit | edit source]

Examples

[edit | edit source]
type Suit is (Spade, Club, Heart, Diamond);

type Philosopher_ID is (Archimedes, Descartes, Hegel, Kant, Socrates);

Syntax

[edit | edit source]
enumeration_type_definition ::=
         (enumeration_literal_specification {, enumeration_literal_specification})

enumeration_literal_specification ::= defining_identifier | defining_character_literal

Operators

[edit | edit source]

= /= < <= > >=

Attributes

[edit | edit source]

First, Last, Position, From_Representation, Representation, Image, Value, Min, Max, Previous, Next

Aspects

[edit | edit source]

Bit_Size, Byte_Size

Object declaration

[edit | edit source]
My_Card : Suit;

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Integer Types

[edit | edit source]

Examples

[edit | edit source]

Bounded integer types

[edit | edit source]
type Day is range 1 .. 31;

Unbounded integer types

[edit | edit source]
type Factorial is range <> with Default_Value => 0;

Syntax

[edit | edit source]
integer_type_definition ::= bounded_integer_type_definition | unbounded_integer_type_definition

bounded_integer_type_definition ::= range static_simple_expression .. static_simple_expression

unbounded_integer_type_definition ::= range <>

Operators

[edit | edit source]

+ - * / rem mod ^ = /= < <= > >= and or xor not

Attributes

[edit | edit source]

First, Last, Bit_Wise_Operators, Image, Value, Min, Max, Previous, Next, Valid

Additional attributes with Bit_Wise_Operators = True:

Shift_Left, Shift_Right, Rotate_Left, Rotate_Right, Mod

Aspects

[edit | edit source]

Default_Value, Predicate, Signed_Representation, Overflow_Checking

Object declaration

[edit | edit source]
N : Factorial;

Rationale

[edit | edit source]

Bit-wise operations are available for the base type of all bounded integer types that fit in a single hardware integer.

Discussion

[edit | edit source]

-

Real Types

[edit | edit source]

Syntax

[edit | edit source]
real_type_definition ::= floating_point_definition | fixed_point_definition

Floating Point Types

[edit | edit source]

Examples

[edit | edit source]
Bounded floating-Point Types
[edit | edit source]
type Risk is digits 7 range 0 .. 1;
Unbounded floating-Point Types
[edit | edit source]
type Rational is digits <> with Default_Value => 0;

Syntax

[edit | edit source]
floating_point_definition ::=
          bounded_floating_point_definition | unbounded_floating_point_definition

bounded_floating_point_definition ::= digits static_expression [real_range_specification]

real_range_specification ::= range static_simple_expression .. static_simple_expression

unbounded_floating_point_definition ::= digits <>

Operators

[edit | edit source]

+ - * / ^ = /= < <= > >=

Attributes

[edit | edit source]

Image, Value, Min, Max, Previous, Next, Valid, digits

Aspects

[edit | edit source]

Default_Value, Predicate

Object declaration

[edit | edit source]
Speed : Rational;

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Fixed Point Types

[edit | edit source]

Examples

[edit | edit source]
Bounded fixed-Point Types
[edit | edit source]
type Angle is delta 0.01 range -3.14 .. 3.14;
Unbounded fixed-Point Types
[edit | edit source]
type Duration is delta 10.0 ^ -9 range <> with Default_Value => 0;

Syntax

[edit | edit source]
fixed_point_definition ::= bounded_fixed_point_definition | unbounded_fixed_point_definition

bounded_fixed_point_definition ::= delta static_expression real_range_specification

unbounded_fixed_point_definition ::= delta static_expression range <>

Operators

[edit | edit source]

+ - * / ^ = /= < <= > >=

Attributes

[edit | edit source]

Image, Value, Min, Max, Previous, Next, Valid, delta

Aspects

[edit | edit source]

Default_Value, Predicate

Object declaration

[edit | edit source]
Bearing : Angle;

Rationale

[edit | edit source]

Shouldn't decimal types be available, in order to cover the corresponding available type of Ada?

A King fixed-point type with a delta that is a power of 10 is equivalent to an Ada decimal fixed-point type.

type Money is delta 0.01 digits 14;

The above would be equivalent to:

type Money is delta 0.01 range -999_999_999_999.99 .. 999_999_999_999.99;

Discussion

[edit | edit source]

-

Map Types

[edit | edit source]

Examples

[edit | edit source]
type Store is map Part_Key => Stock_Info;

Syntax

[edit | edit source]
map_type_definition := map key_subtype_mark => value_subtype_mark

Operators

[edit | edit source]

= /=

"=" is only defined if it is defined for the value subtype.

Attributes

[edit | edit source]

-

Operations

[edit | edit source]

Delete, Clear, Defined, Size

Aspects

[edit | edit source]

-

Object declaration

[edit | edit source]
My_Store : Store;

Component deference

[edit | edit source]
My_Store (K99)

Aggregates

[edit | edit source]
Store'(K1 => SI1, KE => SIE)

Store'(null) -- Empty map

Iteration

[edit | edit source]
for K in My_Store'range

for E of My_Store

Rationale

[edit | edit source]

Maps may also be implemented by functions, so King uses a common notation for both.

Discussion

[edit | edit source]

-

Sequence Types

[edit | edit source]

Examples

[edit | edit source]
type Stack is sequence of Message;

Syntax

[edit | edit source]
sequence_type_definition := sequence of element_subtype_mark

Operators

[edit | edit source]

= /= &

"=" is only defined if it is defined for the value subtype.

Attributes

[edit | edit source]

-

Operations

[edit | edit source]

Delete, Clear, Length

Aspects

[edit | edit source]

-

Object declaration

[edit | edit source]
My_Stack : Stack;

Component deference

[edit | edit source]
My_Stack [Curent]

Aggregates

[edit | edit source]
My_Stack'[MC, ML, MZ]

My_Stack'[null] -- Empty sequence

Iteration

[edit | edit source]
for P in [reverse] My_Stack'range

for E of [reverse] My_Stack

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Set Types

[edit | edit source]

Examples

[edit | edit source]
type File_Mask is set of File_Mode;

Syntax

[edit | edit source]
set_type_definition := set of element_subtype_mark

Operators

[edit | edit source]

+ - * / = /= < <= > >= [not] in

Attributes

[edit | edit source]

-

Operations

[edit | edit source]

Size

Aspects

[edit | edit source]

-

Object declaration

[edit | edit source]
My_Mask : File_Mask:

Component deference

[edit | edit source]

-

Aggregates

[edit | edit source]
My_Mask'{Read, Exec}

My_Mask'{null} -- Empty set

My_Mask'{all} -- Full set

Iteration

[edit | edit source]
for M of My_Mask

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Record Types

[edit | edit source]

Examples

[edit | edit source]
type Complex is record
   Re : Float;
   Im : Float;
end record Complex;

Syntax

[edit | edit source]
record_type_definition ::= record_definition

record_definition ::=
           record
              component_list
           end record record_identifier

component_list ::=
            component_item {component_item}
          | {component_item} variant_part
          | null;

component_item ::= component_declaration

component_declaration ::= defining_identifier : component_definition [<- default_expression]
                          [aspect_specification];

component_definition ::= subtype_indication

variant_part ::= case discriminant_direct_name is
                   variant
                   {variant}
                 end case;

variant ::= when discrete_choice_list =>
               component_list

discrete_choice_list ::= discrete_choice {| discrete_choice}

discrete_choice ::= choice_expression | discrete_subtype_indication | range_specification | others

Operators

[edit | edit source]

= /=

Attributes

[edit | edit source]

-

Aspects

[edit | edit source]

-

Object declaration

[edit | edit source]
Z : Complex;

Component deference

[edit | edit source]
Z.Re

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Derived Types

[edit | edit source]

Examples

[edit | edit source]
type Boxes (Length : Position_Value) is record
   Box_A : Receive_Box (Max_Length => Length);
   Box_B : Receive_Box (Max_Length => Length);
end record;
type Boxes_4 is new Boxes (Length => 4);

Syntax

[edit | edit source]
derived_type_definition ::= new parent_subtype_indication

Operators

[edit | edit source]

-

Attributes

[edit | edit source]

-

Aspects

[edit | edit source]

-

Rationale

[edit | edit source]

The operations, attributes, and so on are based on the parent type.

Discussion

[edit | edit source]

-

Module Types

[edit | edit source]

Examples

[edit | edit source]
type Strings is module
   Set : procedure (S : String);
   Index : function (Pattern : String; Going : Direction <- Forward) return Position_Value;
   Head : function (Count : Natural; Pad : Unicode <- Space) return String;
   -- ...
end module Strings;

Syntax

[edit | edit source]
module_type_definition ::= module
                              subprogram_declaration {subprogram_declaration}
                           end module module_identifier

Operators

[edit | edit source]

-

Attributes

[edit | edit source]

-

Aspects

[edit | edit source]

-

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Active Task Types

[edit | edit source]

Examples

[edit | edit source]
type Philosopher (ID : Philosopher_ID <- Archimedes) is task;

Syntax

[edit | edit source]
task_type_definition ::= task

Operators

[edit | edit source]

= /=

Attributes

[edit | edit source]

-

Aspects

[edit | edit source]

-

Object declaration

[edit | edit source]
T1 : Philosopher;

Rationale

[edit | edit source]

-

Discussion

[edit | edit source]

-

Passive Task Types

[edit | edit source]

Examples

[edit | edit source]
type Semaphore is task
   Secure : procedure;
   Release : procedure;
end task Semaphore;

Syntax

[edit | edit source]
task_type_definition ::= task
                            subprogram_declaration {subprogram_declaration}
                         end task task_identifier

Operators

[edit | edit source]

= /=

Attributes

[edit | edit source]

-

Aspects

[edit | edit source]

-

Object declaration

[edit | edit source]
S1 : Semaphore;

Rationale

[edit | edit source]

Passive tasks are the only way that active tasks may communicate.

Discussion

[edit | edit source]

-