Cours 2: Types discrets

Objectif: Types entiers, énumérés, booléens, caractères.
Déclaration. Valeur. Opérateur. Attribut. Conversion. Objets.

Un type Ada est un ensemble de valeurs accompagné d'un ensemble d'opérateurs primitifs.

Les types Ada sont classés selon une hiérarchie qui reflète une similitude de valeurs et/ou d'opérateurs.

Extrait du manuel de référence (RM95):


          all types
             elementary
                scalar
                   discrete
                      enumeration
                         character
                         boolean
                         other enumeration
                      integer
                         signed integer
                         modular integer
                   real
                      floating point
                      fixed point
                         ordinary fixed point
                         decimal fixed point
                access
                   access-to-object
                   access-to-subprogram
             composite
                array
                   string
                   other array
                untagged record
                tagged
                task
                protected

Extrait du manuel de référence (RM95):


       type_declaration ::=  full_type_declaration
          | incomplete_type_declaration
          | private_type_declaration
          | private_extension_declaration

       full_type_declaration ::=
            type defining_identifier [known_discriminant_part] is type_definition;
          | task_type_declaration
          | protected_type_declaration

       type_definition ::=
            enumeration_type_definition | integer_type_definition
          | real_type_definition        | array_type_definition
          | record_type_definition      | access_type_definition
          | derived_type_definition

       integer_type_definition ::= signed_integer_type_definition | modular_type_definition

       signed_integer_type_definition ::= range static_simple_expression .. static_simple_expression

       enumeration_type_definition ::=
          (enumeration_literal_specification {, enumeration_literal_specification})

       enumeration_literal_specification ::=  defining_identifier | defining_character_literal

       defining_character_literal ::= character_literal

Extrait du manuel de référence (RM95):


       Examples of type definitions:

       (White, Red, Yellow, Green, Blue, Brown, Black)

       range 1 .. 72

       Examples of type declarations:

       type Color  is (White, Red, Yellow, Green, Blue, Brown, Black);

       type Column is range 1 .. 72;

       type Day    is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

       type Suit   is (Clubs, Diamonds, Hearts, Spades);

       type Gender is (M, F);

       type Level  is (Low, Medium, Urgent);

       type Light  is (Red, Amber, Green); -- Red and Green are overloaded

       type Hexa   is ('A', 'B', 'C', 'D', 'E', 'F');

       type Mixed  is ('A', 'B', '*', B, None, '?', '%');

Une valeur surchargée est ambiguë.
Pour lever l'ambiguité, on qualifie la valeur.
Par exemple, le littéral Red est ambigu. Mais Light'(Red) et Color'(Red) ne le sont pas.

Extrait du manuel de référence (RM95):


       qualified_expression ::=
          subtype_mark'(expression) | subtype_mark'aggregate

On peut convertir une valeur d'un type numérique vers un autre.
Par exemple, on peut convertir un entier en un réel et vice-versa.
L'expression Integer(1.6) est l'entier 2. L'expression Real(2) est le réel 2.0.

Extrait du manuel de référence (RM95):


       type_conversion ::=
          subtype_mark(expression) | subtype_mark(name)

Un type caractère est un type énuméré dont l'énumération comporte au moins un littéral caractère.

Le type Character est prédéfini:


       type Character is (nul, soh, stx, ..., ' ', '!', '*', ..., '0', '1', ..., '9', ..., 'A', ..., 'Z', ...);
       -- ISO 0 to 255

Le type Boolean est prédéfini:


       type Boolean is (False, True);

Un sous-type est un type dont l'ensemble de valeurs est contraint.
Pour un sous-type de type discret, la contrainte fixe un intervalle.

Extrait du manuel de référence (RM95):


       subtype_declaration ::=
          subtype defining_identifier is subtype_indication;

       subtype_indication ::=  subtype_mark [constraint]

       subtype_mark ::= subtype_name

       constraint ::= scalar_constraint | composite_constraint

       scalar_constraint ::=
            range_constraint | digits_constraint | delta_constraint


       range_constraint ::=  range range

       range ::=  range_attribute_reference
          | simple_expression .. simple_expression

Extrait du manuel de référence (RM95):


       Examples of subtype declarations:

       subtype Rainbow   is Color range Red .. Blue;

       subtype Red_Blue  is Rainbow;

       subtype Int       is Integer;

       subtype Small_Int is Integer range -10 .. 10;

       subtype Up_To_K   is Column range 1 .. K;

       subtype Weekday is Day   range Mon .. Fri;

       subtype Major   is Suit  range Hearts .. Spades;

       subtype Rainbow is Color range Red .. Blue;  --  the Color Red, not the Light

Les types scalaires regroupent les types entiers, énumérés et réels.
Les types scalaires sont ordonnés et les opérateurs d'ordre sont prédéfinis (<, <=, >, >=).
Les attributs sont des opérateurs prédéfinis.
Les attributs suivants sont prédéfinis pour tous les types scalaires:

Extrait du manuel de référence (RM95):


       S'First
                S'First denotes the lower bound of the range of S.

       S'Last
                S'Last denotes the upper bound of the range of S.

       S'Range
                S'Range is equivalent to the range S'First .. S'Last.

       S'Base
                S'Base denotes an unconstrained subtype of the type of S.
                This unconstrained subtype is called the base subtype of the type.

       S'Min
                S'Min denotes a function with the following specification:

                     function S'Min(Left, Right : S'Base) return S'Base

       S'Max
                S'Max denotes a function with the following specification:

                     function S'Max(Left, Right : S'Base) return S'Base

       S'Succ
                S'Succ denotes a function with the following specification:

                     function S'Succ(Arg : S'Base) return S'Base

       S'Pred
                S'Pred denotes a function with the following specification:

                     function S'Pred(Arg : S'Base) return S'Base

Exemple d'emploi des attributs:


       subtype Natural is Integer range 0 .. Integer'Last;

       subtype Positive is Integer range 1 .. Integer'Last;

Un objet est une entité qui contient une valeur de son type.
Un objet peut être constant (on ne peut pas modifier sa valeur) ou variable (on peut le lire et l'écrire).
L'ensemble des valeurs qu'un objet peut recevoir peut être contraint.

Extrait du manuel de référence (RM95):


       object_declaration ::=
           defining_identifier_list : [aliased] [constant] subtype_indication [:= expression];
         | defining_identifier_list : [aliased] [constant] array_type_definition [:= expression];
         | single_task_declaration
         | single_protected_declaration

       defining_identifier_list ::=
         defining_identifier {, defining_identifier}

Un objet variable peut être initialisé lors de sa déclaration.
Un objet constant doit être initialisé lors de sa déclaration (sauf le cas particulier des constantes différées).
La valeur initiale d'un objet doit être de même type que l'objet.

Extrait du manuel de référence (RM95):


       Examples of variable declarations:

       Count, Sum  : Integer;

       Size        : Integer range 0 .. 10_000 := 0;

       Sorted      : Boolean := False;

       Examples of constant declarations:

       Limit     : constant Integer := 10_000;

       Low_Limit : constant Integer := Limit/10;

On peut déclarer des nombres.
Un nombre déclaré est soit un entier soit un réel (son type est implicite).
(Ne pas confondre avec un objet constant de type numérique dont le type est explicitement mentionné).

Extrait du manuel de référence (RM95):


       number_declaration ::=
            defining_identifier_list : constant := static_expression;

Extrait du manuel de référence (RM95):


       Examples of number declarations:

       Two_Pi        : constant := 2.0*Ada.Numerics.Pi;   -- a real number

       Max           : constant := 500;                   -- an integer number

       Max_Line_Size : constant := Max/6                  -- the integer 83

       Power_16      : constant := 2**16;                 -- the integer 65_536

       One, Un, Eins : constant := 1;                     -- three different namesfor 1