Annotated King Reference Manual/Types
This page is work in progress.
Type Declarations
editSome 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
edittype_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-
Discussion
edit-
Enumeration Types
editExamples
edittype Suit is (Spade, Club, Heart, Diamond);
type Philosopher_ID is (Archimedes, Descartes, Hegel, Kant, Socrates);
Syntax
editenumeration_type_definition ::= (enumeration_literal_specification {, enumeration_literal_specification}) enumeration_literal_specification ::= defining_identifier | defining_character_literal
Operators
edit= /= < <= > >=
Attributes
editFirst, Last, Position, From_Representation, Representation, Image, Value, Min, Max, Previous, Next
Aspects
editBit_Size, Byte_Size
Object declaration
editMy_Card : Suit;
Rationale
edit-
Discussion
edit-
Integer Types
editExamples
editBounded integer types
edittype Day is range 1 .. 31;
Unbounded integer types
edittype Factorial is range <> with Default_Value => 0;
Syntax
editinteger_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+ - * / rem mod ^ = /= < <= > >= and or xor not
Attributes
editFirst, 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
editDefault_Value, Predicate, Signed_Representation, Overflow_Checking
Object declaration
editN : Factorial;
Rationale
editBit-wise operations are available for the base type of all bounded integer types that fit in a single hardware integer.
Discussion
edit-
Real Types
editSyntax
editreal_type_definition ::= floating_point_definition | fixed_point_definition
Floating Point Types
editExamples
editBounded floating-Point Types
edittype Risk is digits 7 range 0 .. 1;
Unbounded floating-Point Types
edittype Rational is digits <> with Default_Value => 0;
Syntax
editfloating_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+ - * / ^ = /= < <= > >=
Attributes
editImage, Value, Min, Max, Previous, Next, Valid, digits
Aspects
editDefault_Value, Predicate
Object declaration
editSpeed : Rational;
Rationale
edit-
Discussion
edit-
Fixed Point Types
editExamples
editBounded fixed-Point Types
edittype Angle is delta 0.01 range -3.14 .. 3.14;
Unbounded fixed-Point Types
edittype Duration is delta 10.0 ^ -9 range <> with Default_Value => 0;
Syntax
editfixed_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+ - * / ^ = /= < <= > >=
Attributes
editImage, Value, Min, Max, Previous, Next, Valid, delta
Aspects
editDefault_Value, Predicate
Object declaration
editBearing : Angle;
Rationale
editShouldn'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-
Map Types
editExamples
edittype Store is map Part_Key => Stock_Info;
Syntax
editmap_type_definition := map key_subtype_mark => value_subtype_mark
Operators
edit= /=
"=" is only defined if it is defined for the value subtype.
Attributes
edit-
Operations
editDelete, Clear, Defined, Size
Aspects
edit-
Object declaration
editMy_Store : Store;
Component deference
editMy_Store (K99)
Aggregates
editStore'(K1 => SI1, KE => SIE)
Store'(null) -- Empty map
Iteration
editfor K in My_Store'range
for E of My_Store
Rationale
editMaps may also be implemented by functions, so King uses a common notation for both.
Discussion
edit-
Sequence Types
editExamples
edittype Stack is sequence of Message;
Syntax
editsequence_type_definition := sequence of element_subtype_mark
Operators
edit= /= &
"=" is only defined if it is defined for the value subtype.
Attributes
edit-
Operations
editDelete, Clear, Length
Aspects
edit-
Object declaration
editMy_Stack : Stack;
Component deference
editMy_Stack [Curent]
Aggregates
editMy_Stack'[MC, ML, MZ]
My_Stack'[null] -- Empty sequence
Iteration
editfor P in [reverse] My_Stack'range
for E of [reverse] My_Stack
Rationale
edit-
Discussion
edit-
Set Types
editExamples
edittype File_Mask is set of File_Mode;
Syntax
editset_type_definition := set of element_subtype_mark
Operators
edit+ - * / = /= < <= > >= [not] in
Attributes
edit-
Operations
editSize
Aspects
edit-
Object declaration
editMy_Mask : File_Mask:
Component deference
edit-
Aggregates
editMy_Mask'{Read, Exec}
My_Mask'{null} -- Empty set
My_Mask'{all} -- Full set
Iteration
editfor M of My_Mask
Rationale
edit-
Discussion
edit-
Record Types
editExamples
edittype Complex is record
Re : Float;
Im : Float;
end record Complex;
Syntax
editrecord_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= /=
Attributes
edit-
Aspects
edit-
Object declaration
editZ : Complex;
Component deference
editZ.Re
Rationale
edit-
Discussion
edit-
Derived Types
editExamples
edittype 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
editderived_type_definition ::= new parent_subtype_indication
Operators
edit-
Attributes
edit-
Aspects
edit-
Rationale
editThe operations, attributes, and so on are based on the parent type.
Discussion
edit-
Module Types
editExamples
edittype 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
editmodule_type_definition ::= module subprogram_declaration {subprogram_declaration} end module module_identifier
Operators
edit-
Attributes
edit-
Aspects
edit-
Rationale
edit-
Discussion
edit-
Active Task Types
editExamples
edittype Philosopher (ID : Philosopher_ID <- Archimedes) is task;
Syntax
edittask_type_definition ::= task
Operators
edit= /=
Attributes
edit-
Aspects
edit-
Object declaration
editT1 : Philosopher;
Rationale
edit-
Discussion
edit-
Passive Task Types
editExamples
edittype Semaphore is task
Secure : procedure;
Release : procedure;
end task Semaphore;
Syntax
edittask_type_definition ::= task subprogram_declaration {subprogram_declaration} end task task_identifier
Operators
edit= /=
Attributes
edit-
Aspects
edit-
Object declaration
editS1 : Semaphore;
Rationale
editPassive tasks are the only way that active tasks may communicate.
Discussion
edit-