Skip to content

Commit 0eb1b59

Browse files
committed
276_type_contracts: Renaming single var names to something a little more meaningful
1 parent b68378d commit 0eb1b59

File tree

2 files changed

+80
-80
lines changed

2 files changed

+80
-80
lines changed

courses/fundamentals_of_ada/276_type_contracts/02-type_invariants.rst

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,20 @@ Default Type Initialization for Invariants
135135

136136
.. code:: Ada
137137
138-
package P is
138+
package Operations is
139139
-- Type is private, so we can't use Default_Value here
140-
type T is private with Type_Invariant => Zero (T);
141-
procedure Op (This : in out T);
142-
function Zero (This : T) return Boolean;
140+
type Private_T is private with Type_Invariant => Zero (Private_T);
141+
procedure Op (This : in out Private_T);
142+
function Zero (This : Private_T) return Boolean;
143143
private
144144
-- Type is not a record, so we need to use aspect
145145
-- (A record could use default values for its components)
146-
type T is new Integer with Default_Value => 0;
147-
function Zero (This : T) return Boolean is
146+
type Private_T is new Integer with Default_Value => 0;
147+
function Zero (This : Private_T) return Boolean is
148148
begin
149149
return (This = 0);
150150
end Zero;
151-
end P;
151+
end Operations;
152152
153153
---------------------------------
154154
Type Invariant Clause Placement
@@ -158,14 +158,14 @@ Type Invariant Clause Placement
158158

159159
.. code:: Ada
160160
161-
package P is
162-
type T is private;
163-
procedure Op (This : in out T);
161+
package Operations is
162+
type Private_T is private;
163+
procedure Op (This : in out Private_T);
164164
private
165-
type T is new Integer with
166-
Type_Invariant => T = 0,
165+
type Private_T is new Integer with
166+
Type_Invariant => Private_T = 0,
167167
Default_Value => 0;
168-
end P;
168+
end Operations;
169169
170170
* It is really an implementation aspect
171171

@@ -197,35 +197,37 @@ Quiz
197197

198198
.. code:: Ada
199199
200-
package P is
201-
type Some_T is private;
202-
procedure Do_Something (X : in out Some_T);
200+
package Counter_Package is
201+
type Counter_T is private;
202+
procedure Increment (Val : in out Counter_T);
203203
private
204-
function Counter (I : Integer) return Boolean;
205-
type Some_T is new Integer with
206-
Type_Invariant => Counter (Integer (Some_T));
207-
end P;
208-
209-
package body P is
210-
function Local_Do_Something (X : Some_T)
211-
return Some_T is
212-
Z : Some_T := X + 1;
204+
function Check_Threshold (Value : Integer)
205+
return Boolean;
206+
type Counter_T is new Integer with
207+
Type_Invariant => Check_Threshold
208+
(Integer (Counter_T));
209+
end Counter_Package;
210+
211+
package body Counter_Package is
212+
function Increment_Helper (Helper_Val : Counter_T)
213+
return Counter_T is
214+
Next_Value : Counter_T := Helper_Val + 1;
213215
begin
214-
return Z;
215-
end Local_Do_Something;
216-
procedure Do_Something (X : in out Some_T) is
216+
return Next_Value;
217+
end Increment_Helper;
218+
procedure Increment (Val : in out Counter_T) is
217219
begin
218-
X := X + 1;
219-
X := Local_Do_Something (X);
220-
end Do_Something;
221-
function Counter (I : Integer)
222-
return Boolean is
223-
(True);
224-
end P;
220+
Val := Val + 1;
221+
Val := Increment_Helper (Val);
222+
end Increment;
223+
function Check_Threshold (Value : Integer)
224+
return Boolean is
225+
(Value <= 100); -- check against constraint
226+
end Counter_Package;
225227
226228
.. container:: column
227229

228-
If `Do_Something` is called from outside of P, how many times is `Counter` called?
230+
If `Increment` is called from outside of Counter_Package, how many times is `Check_Threshold` called?
229231

230232
A. 1
231233
B. :answer:`2`
@@ -234,8 +236,6 @@ Quiz
234236

235237
.. container:: animate
236238

237-
Type Invariants are only evaluated on entry into and exit from
238-
externally visible subprograms. So :ada:`Counter` is called when
239-
entering and exiting :ada:`Do_Something` - not :ada:`Local_Do_Something`,
240-
even though a new instance of :ada:`Some_T` is created
241-
239+
Type Invariants are only evaluated on entry into/exit from
240+
externally visible subprograms. So :ada:`Check_Threshold` is called when
241+
entering/exiting :ada:`Increment` - not :ada:`Increment_Helper`

courses/fundamentals_of_ada/276_type_contracts/03-subtype_predicates.rst

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,28 @@ References Are Not Checked
156156
.. code:: Ada
157157
158158
with Ada.Text_IO; use Ada.Text_IO;
159-
procedure Test is
159+
procedure Even_Number_Test is
160160
subtype Even is Integer with Dynamic_Predicate => Even mod 2 = 0;
161-
J, K : Even;
161+
Current_Value, Next_Value : Even;
162162
begin
163163
-- predicates are not checked here
164-
Put_Line ("K is" & K'Image);
165-
Put_Line ("J is" & J'Image);
164+
Put_Line ("Current_Value is" & Current_Value'Image);
165+
Put_Line ("Next_Value is" & Next_Value'Image);
166166
-- predicate is checked here
167-
K := J; -- assertion failure here
168-
Put_Line ("K is" & K'Image);
169-
Put_Line ("J is" & J'Image);
170-
end Test;
167+
Current_Value := Next_Value; -- assertion failure here
168+
Put_Line ("Current_Value is" & Current_Value'Image);
169+
Put_Line ("Next_Value is" & Next_Value'Image);
170+
end Even_Number_Test;
171171
172172
* Output would look like
173173

174174
.. code:: Ada
175175
176-
K is 1969492223
177-
J is 4220029
176+
Current_Value is 1969492223
177+
Next_Value is 4220029
178178
179179
raised SYSTEM.ASSERTIONS.ASSERT_FAILURE:
180-
Dynamic_Predicate failed at test.adb:9
180+
Dynamic_Predicate failed at even_number_test.adb:9
181181
182182
------------------------------
183183
Predicate Expression Content
@@ -189,7 +189,7 @@ Predicate Expression Content
189189
190190
subtype Even is Integer
191191
with Dynamic_Predicate => Even mod 2 = 0;
192-
J, K : Even := 42;
192+
Current_Value, Next_Value : Even := 42;
193193
194194
* Any visible object or function in scope
195195

@@ -309,7 +309,7 @@ Types Controlling For-Loops
309309
with Dynamic_Predicate => Even mod 2 = 0;
310310
...
311311
-- not legal - how many iterations?
312-
for K in Even loop
312+
for A_Number in Even loop
313313
...
314314
end loop;
315315
@@ -321,8 +321,8 @@ Types Controlling For-Loops
321321
subtype Weekend is Days
322322
with Static_Predicate => Weekend in Sat | Sun;
323323
-- Loop uses "Days", and only enters loop when in Weekend
324-
-- So "Sun" is first value for K
325-
for K in Weekend loop
324+
-- So "Sun" is first value for A_Day
325+
for A_Day in Weekend loop
326326
...
327327
end loop;
328328
@@ -337,26 +337,26 @@ Why Allow Types with Static Predicates?
337337
type Days is (Sun, Mon, Tues, We, Thu, Fri, Sat);
338338
subtype Weekend is Days with Static_Predicate => Weekend in Sat | Sun;
339339
...
340-
for W in Weekend loop
341-
GNAT.IO.Put_Line (W'Image);
340+
for A_Day in Weekend loop
341+
GNAT.IO.Put_Line (A_Day'Image);
342342
end loop;
343343
344344
* :ada:`for` loop generates code like
345345

346346
.. code:: Ada
347347
348348
declare
349-
w : weekend := sun;
349+
a_day : weekend := sun;
350350
begin
351351
loop
352-
gnat__io__put_line__2 (w'Image);
353-
case w is
352+
gnat__io__put_line__2 (a_day'Image);
353+
case a_day is
354354
when sun =>
355-
w := sat;
355+
a_day := sat;
356356
when sat =>
357357
exit;
358358
when others =>
359-
w := weekend'succ (w);
359+
a_day := weekend'succ (a_day);
360360
end case;
361361
end loop;
362362
end;
@@ -375,7 +375,7 @@ In Some Cases Neither Kind Is Allowed
375375
376376
type Play is array (Weekend) of Integer; -- illegal
377377
type Vector is array (Days range <>) of Integer;
378-
L : Vector (Weekend); -- not legal
378+
Not_Legal : Vector (Weekend); -- not legal
379379
380380
-----------------------------------------
381381
Special Attributes for Predicated Types
@@ -409,7 +409,7 @@ Initial Values Can Be Problematic
409409
410410
subtype Even is Integer
411411
with Dynamic_Predicate => Even mod 2 = 0;
412-
K : Even; -- unknown (invalid?) initial value
412+
Some_Number : Even; -- unknown (invalid?) initial value
413413
414414
* The predicate is not checked on a declaration when no initial value is given
415415
* So can reference such junk values before assigned
@@ -428,8 +428,8 @@ Subtype Predicates Aren't Bullet-Proof
428428
type Table is array (1 .. 5) of Integer
429429
-- array should always be sorted
430430
with Dynamic_Predicate =>
431-
(for all K in Table'Range =>
432-
(K = Table'First or else Table (K-1) <= Table (K)));
431+
(for all Idx in Table'Range =>
432+
(Idx = Table'First or else Table (Idx-1) <= Table (Idx)));
433433
Values : Table := (1, 3, 5, 7, 9);
434434
begin
435435
...
@@ -460,9 +460,9 @@ Beware Accidental Recursion in Predicate
460460
461461
type Sorted_Table is array (1 .. N) of Integer with
462462
Dynamic_Predicate =>
463-
(for all K in Sorted_Table'Range =>
464-
(K = Sorted_Table'First
465-
or else Sorted_Table (K - 1) <= Sorted_Table (K)));
463+
(for all Index in Sorted_Table'Range =>
464+
(Index = Sorted_Table'First
465+
or else Sorted_Table (Index - 1) <= Sorted_Table (Index)));
466466
467467
* Type-based example
468468

@@ -526,20 +526,20 @@ Quiz
526526
.. code:: Ada
527527
528528
type Days_T is (Sun, Mon, Tue, Wed, Thu, Fri, Sat);
529-
function Is_Weekday (D : Days_T) return Boolean is
530-
(D /= Sun and then D /= Sat);
529+
function Is_Weekday (Day : Days_T) return Boolean is
530+
(Day /= Sun and then Day /= Sat);
531531
532532
Which of the following is a valid subtype predicate?
533533

534-
A. | :answermono:`subtype T is Days_T with`
535-
| :answermono:`Static_Predicate => T in Sun | Sat;`
536-
B. | ``subtype T is Days_T with Static_Predicate =>``
537-
| ``(if T = Sun or else T = Sat then True else False);``
538-
C. | ``subtype T is Days_T with``
539-
| ``Static_Predicate => not Is_Weekday (T);``
540-
D. | ``subtype T is Days_T with``
534+
A. | :answermono:`subtype Sub_Day is Days_T with`
535+
| :answermono:`Static_Predicate => Sub_Day in Sun | Sat;`
536+
B. | ``subtype Sub_Day is Days_T with Static_Predicate =>``
537+
| ``(if Sub_Day = Sun or else Sub_Day = Sat then True else False);``
538+
C. | ``subtype Sub_Day is Days_T with``
539+
| ``Static_Predicate => not Is_Weekday (Sub_Day);``
540+
D. | ``subtype Sub_Day is Days_T with``
541541
| ``Static_Predicate =>``
542-
| ``case T is when Sat | Sun => True,``
542+
| ``case Sub_Day is when Sat | Sun => True,``
543543
| ``when others => False;``
544544
545545
.. container:: animate

0 commit comments

Comments
 (0)