@@ -156,28 +156,28 @@ References Are Not Checked
156
156
.. code :: Ada
157
157
158
158
with Ada.Text_IO; use Ada.Text_IO;
159
- procedure Test is
159
+ procedure Even_Number_Test is
160
160
subtype Even is Integer with Dynamic_Predicate => Even mod 2 = 0;
161
- J, K : Even;
161
+ Current_Value, Next_Value : Even;
162
162
begin
163
163
-- 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);
166
166
-- 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 ;
171
171
172
172
* Output would look like
173
173
174
174
.. code :: Ada
175
175
176
- K is 1969492223
177
- J is 4220029
176
+ Current_Value is 1969492223
177
+ Next_Value is 4220029
178
178
179
179
raised SYSTEM.ASSERTIONS.ASSERT_FAILURE:
180
- Dynamic_Predicate failed at test .adb:9
180
+ Dynamic_Predicate failed at even_number_test .adb:9
181
181
182
182
------------------------------
183
183
Predicate Expression Content
@@ -189,7 +189,7 @@ Predicate Expression Content
189
189
190
190
subtype Even is Integer
191
191
with Dynamic_Predicate => Even mod 2 = 0;
192
- J, K : Even := 42;
192
+ Current_Value, Next_Value : Even := 42;
193
193
194
194
* Any visible object or function in scope
195
195
@@ -309,7 +309,7 @@ Types Controlling For-Loops
309
309
with Dynamic_Predicate => Even mod 2 = 0;
310
310
...
311
311
-- not legal - how many iterations?
312
- for K in Even loop
312
+ for A_Number in Even loop
313
313
...
314
314
end loop;
315
315
@@ -321,8 +321,8 @@ Types Controlling For-Loops
321
321
subtype Weekend is Days
322
322
with Static_Predicate => Weekend in Sat | Sun;
323
323
-- 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
326
326
...
327
327
end loop;
328
328
@@ -337,26 +337,26 @@ Why Allow Types with Static Predicates?
337
337
type Days is (Sun, Mon, Tues, We, Thu, Fri, Sat);
338
338
subtype Weekend is Days with Static_Predicate => Weekend in Sat | Sun;
339
339
...
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);
342
342
end loop;
343
343
344
344
* :ada: `for ` loop generates code like
345
345
346
346
.. code :: Ada
347
347
348
348
declare
349
- w : weekend := sun;
349
+ a_day : weekend := sun;
350
350
begin
351
351
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
354
354
when sun =>
355
- w := sat;
355
+ a_day := sat;
356
356
when sat =>
357
357
exit;
358
358
when others =>
359
- w := weekend'succ (w );
359
+ a_day := weekend'succ (a_day );
360
360
end case;
361
361
end loop;
362
362
end;
@@ -375,7 +375,7 @@ In Some Cases Neither Kind Is Allowed
375
375
376
376
type Play is array (Weekend) of Integer; -- illegal
377
377
type Vector is array (Days range <>) of Integer;
378
- L : Vector (Weekend); -- not legal
378
+ Not_Legal : Vector (Weekend); -- not legal
379
379
380
380
-----------------------------------------
381
381
Special Attributes for Predicated Types
@@ -409,7 +409,7 @@ Initial Values Can Be Problematic
409
409
410
410
subtype Even is Integer
411
411
with Dynamic_Predicate => Even mod 2 = 0;
412
- K : Even; -- unknown (invalid?) initial value
412
+ Some_Number : Even; -- unknown (invalid?) initial value
413
413
414
414
* The predicate is not checked on a declaration when no initial value is given
415
415
* So can reference such junk values before assigned
@@ -428,8 +428,8 @@ Subtype Predicates Aren't Bullet-Proof
428
428
type Table is array (1 .. 5) of Integer
429
429
-- array should always be sorted
430
430
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 )));
433
433
Values : Table := (1, 3, 5, 7, 9);
434
434
begin
435
435
...
@@ -460,9 +460,9 @@ Beware Accidental Recursion in Predicate
460
460
461
461
type Sorted_Table is array (1 .. N) of Integer with
462
462
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 )));
466
466
467
467
* Type-based example
468
468
@@ -526,20 +526,20 @@ Quiz
526
526
.. code :: Ada
527
527
528
528
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);
531
531
532
532
Which of the following is a valid subtype predicate?
533
533
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 ``
541
541
| ``Static_Predicate =>``
542
- | ``case T is when Sat | Sun => True,``
542
+ | ``case Sub_Day is when Sat | Sun => True,``
543
543
| ``when others => False;``
544
544
545
545
.. container :: animate
0 commit comments