Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slides/0408 #359

Merged
merged 12 commits into from
Apr 29, 2024
47 changes: 26 additions & 21 deletions courses/fundamentals_of_ada/020_declarations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,27 @@ Declarations
Introduction
==============

---------------------
------------
Declarations
------------

* :dfn:`Declaration` associates a :dfn:`name` to an :dfn:`entity`

- Objects
- Types
- Subprograms
- et cetera

* N is usually an :dfn:`identifier`
* Declaration **must precede** use
* **Some** implicit declarations

- **Standard** types and operations
- **Implementation**-defined

-----------
Identifiers
---------------------
-----------

.. image:: identifier_flow.png
:width: 60%
Expand Down Expand Up @@ -270,23 +288,6 @@ Which statement is legal?
Object Declarations
=====================

--------------
Declarations
--------------

* Associate a :dfn:`name` to an :dfn:`entity`

- Objects
- Types
- Subprograms
- et cetera

* :dfn:`Declaration` **must precede** use
* **Some** implicit declarations

- **Standard** types and operations
- **Implementation**-defined

---------------------
Object Declarations
---------------------
Expand All @@ -297,7 +298,11 @@ Object Declarations
.. code:: Ada

<name> : <subtype> [:= <initial value>];
<name> : constant <subtype> [:= <initial value>];
<name> : constant <subtype> := <initial value>;

* Constant should have a value

- Except for privacy (seen later)

* Examples

Expand Down Expand Up @@ -687,7 +692,7 @@ Overcoming Hiding
M : Float;
begin
M := 12.34;
Outer.M := Integer(M); -- reference "hidden" Integer M
Outer.M := Integer (M); -- reference "hidden" Integer M
end;
end Outer;

Expand Down
32 changes: 16 additions & 16 deletions courses/fundamentals_of_ada/030_basic_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,19 @@ Signed Integer Types
...
end;

--------------------------------
Specifying Integer Type Bounds
--------------------------------
---------------------
Signed Integer Bounds
---------------------

* Must be **static**

- Compiler selects **base type**
- Hardware-supported integer type
- Compilation **error** if not possible

--------------------------
Predefined Integer Types
--------------------------
-------------------------------
Predefined Signed Integer Types
-------------------------------

* :ada:`Integer` **>= 16 bits** wide
* Other **probably** available
Expand All @@ -235,9 +235,9 @@ Predefined Integer Types

- But may be difficult to avoid

--------------------------------
Operators for Any Integer Type
--------------------------------
---------------------------------
Operators for Signed Integer Type
---------------------------------

* By increasing precedence

Expand All @@ -249,14 +249,14 @@ Operators for Any Integer Type

* *Note*: for exponentiation :ada:`**`

- Result will be :ada:`Integer`
- Result will be a signed integer
- So power **must** be :ada:`Integer` ``>= 0``

* Division by zero |rightarrow| :ada:`Constraint_Error`

-----------------
Integer Overflows
-----------------
------------------------
Signed Integer Overflows
------------------------

* Finite binary representation
* Common source of bugs
Expand All @@ -274,9 +274,9 @@ Integer Overflows
=======================
2#1000_0000_0000_0000# = -32,768

-------------------------------
Integer Overflow: Ada vs others
-------------------------------
--------------------------------------
Signed Integer Overflow: Ada vs others
--------------------------------------

* Ada

Expand Down
10 changes: 5 additions & 5 deletions courses/fundamentals_of_ada/060_record_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,12 @@ Quiz
X, Y : Record_T;
Z : constant Nested_T := (others => -1);

Which assignment(s) is(are) **not** legal?
Which assignment(s) is(are) legal?

A. :answermono:`X := (1, '2', Three => 3, Four => (6))`
B. ``X := (Two => '2', Four => Z, others => 5)``
C. ``X := Y``
D. ``X := (1, '2', 4, (others => 5))``
A. ``X := (1, '2', Three => 3, Four => (6))``
B. :answermono:`X := (Two => '2', Four => Z, others => 5)`
C. :answermono:`X := Y`
D. :answermono:`X := (1, '2', 4, (others => 5))`

.. container:: animate

Expand Down
2 changes: 1 addition & 1 deletion courses/fundamentals_of_ada/140_access_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Null Values
begin
if V = null then
-- will go here
end if
end if;
V := new Integer'(0);
V := null; -- semantically correct, but memory leak

Expand Down
6 changes: 3 additions & 3 deletions courses/fundamentals_of_ada/intro_160_genericity.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ Quiz
Which of the following statement is true?

A. Generic instances must be nested inside a non-generic package
B. Generic instances must be created at compile-time
B. Generic instances must be instantiated at compile-time
C. :answer:`Generics instances can create new tagged types`
D. :answer:`Generics instances can create new tasks`

.. container:: animate

Generic instances can be created at any point, at a cost, and
Generic instances can be instantiated at any point, at a cost, and
can do anything a package or subprogram can do, which make them
versatile **but** potentially complex to use.

Expand Down Expand Up @@ -342,7 +342,7 @@ Generic Subprogram Parameters
begin
Callback;
end P;
procedure Something;
procedure Something is null;
procedure P_I is new P (Something);

------
Expand Down
31 changes: 7 additions & 24 deletions courses/fundamentals_of_ada/intro_270_contracts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ all values :ada:`L` and :ada:`H`?

The correct precondition would be

:ada:`L > 0 and then H > 0 and then Integer'Last / L <= H`
:ada:`Integer'Last / L <= H`

to prevent overflow errors on the range check.

Expand Down Expand Up @@ -543,7 +543,7 @@ Contracts Code Reuse
Assertion Policy
------------------

* Assertions checks can be controled with :ada:`pragma Assertion_Policy`
* Assertions checks can be controlled with :ada:`pragma Assertion_Policy`

.. code:: Ada

Expand Down Expand Up @@ -591,7 +591,6 @@ Strong Typing
* Number must be even
* Subset of non-consecutive enumerals
* Array should always be sorted
* Type invariants are only checked on external boundaries

* **Type Invariant**

Expand All @@ -609,11 +608,11 @@ Examples

.. include:: examples/adv_275_type_contracts/type_invariants.rst

------------------------------
Type Invariant Verifications
------------------------------
----------------
Type Invariant
----------------

* Automatically inserted by compiler
* Applied to :ada:`private` types
* Evaluated as postcondition of creation, evaluation, or return object

- When objects first created
Expand Down Expand Up @@ -772,23 +771,7 @@ Examples

+ Specified via aspect named :ada:`Dynamic_Predicate`

-------------------------------------
``type`` and ``subtype`` Predicates
-------------------------------------

* Applicable to both
* Applied via aspect clauses in both cases
* Syntax

.. code:: Ada

type name is type_definition
with aspect_mark [ => expression] { ,
aspect_mark [ => expression] }

subtype defining_identifier is subtype_indication
with aspect_mark [ => expression] { ,
aspect_mark [ => expression] }
* Can apply to :ada:`type` or :ada:`subtype`

--------------------------
Why Two Predicate Forms?
Expand Down
3 changes: 2 additions & 1 deletion courses/fundamentals_of_ada/spec_901_ada_2022_specific.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ Miscellaneous (1/2)

.. code:: Ada

type E is (A => 10, B => 20);
type E is (A, B);
for E use (A => 10, B => 20);
...
E'Enum_Rep (A); -- 10
E'Enum_Val (10); -- A
Expand Down