comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <onewingedshark@gmail.com>
Subject: Re: "unconstrained subtype in component declaration" vs "discriminant in constraint must appear alone"
Date: Wed, 24 Feb 2021 07:57:51 -0800 (PST)	[thread overview]
Message-ID: <a69451b6-9c4d-4d6a-a4c1-51c532d06bf0n@googlegroups.com> (raw)
In-Reply-To: <6716a4e5-02ac-4735-83c9-28bff3f8aae6n@googlegroups.com>

On Tuesday, February 23, 2021 at 3:06:19 PM UTC-7, 0012... wrote:
> Please help figure out this: 
> 
> type CARDS is new cards_numbers range 1..52; 
> --> 
> 41:10 missing case values: -128 .. 0 
> 41:10 missing case values: 5 .. 127 
> 41:25 subtype of expression is not static, alternatives must cover base type 
> from --> 
> [card in CARDS, of course] 
> case (card / 13) +1 is 
> when 1 => put("Spades"); 
> when 2 =>put("Hearts"); 
> when 3 =>put("Spades"); 
> when 4 =>put("Clovers"); 
> end case; 

Hm, I would recommend using an enumeration here.
Type Suit is (Spades, Hearts, Clubs, Diamonds); -- Or whatever you have/terms you want.
Function Get_Suit( Card : Cards ) return Suit is
    ( Suit'Val( Natural(Card)/13 ) );

Then use:
Case Get_Suit(card) is
  When Diamonds => ...
  When Hearts => ...
  When Spades => ...
  When Clubs => ...
End case;

> 
> how can the compiler not know that CARD is limited from 1 to 52, including 0 for the base type (type Cards_numbers is range 0..52) 
> 
> also what's wrong here: 
> Context => 
> type Sets_of_Card is array (Nombre_cartes range <>) of Cards;
> type CARDS_SETS (Nb: Nombre_cartes := 0) is record 
> Set: Sets_of_Card (1..Nb) := INITIALIZATION (Nb); 
> end record;
> pack.nb = 5 
> pack.set'last = 5 
> faulty line: 
> PACK := (pack.nb-1, Pack.Set(pack.set'First..Pack.Nb-1)); 
> issue: 
> raised CONSTRAINT_ERROR : discriminant check failed 
If you have something like PACK : CARD_SETS(7):= [...], then you cannot have a CARD_SETS(4) assigned to it.
Likewise, if you have a PACK : CARD_SETS := (Nb => 3, [proper-set-of-cards]), you cannot assign a (Nb => 2, [proper-set-of-cards]) to it, because to do so would be to alter the discriminant.

Now, one thing that you may not be cognizant of is that Sets_of_Cards is a full deck* of cards, so CARDS_SET(3) is three decks of cards.
* -- But this isn't quite true, you could set the full range of the array to a single value, say 4.

A bit of advice: Ada is really good at describing the problem-space using the type-system, so don't try to force the problem-space into some other domain.
There is nothing wrong with saying
Type Suit is (Hearts, Clubs, Diamonds, Spades);
Type Color is (Red, Black);
Function Is_Red(Item : Suit) return Boolean is ( Color'Val(Suit'Pos(Item) mod 2) );
Type Rank is ('1','2','3','4','5','6','7','8','9','0','J','Q','K','A'); -- Or numbers, card-ranking is kinda "squishy".
Type Card is record
  My_Suit : Suit;
  My_Rank : Rank;
end type;
Function "<"(Left, Right: Card) return Boolean is --Ranking mechanism.
--...

> and why is this expression not rejected: 
> Pack := (pack.nb+1, pack.set & Card); 
> while this one is rejected ? 
I believe it's because you're trying to re-set the discriminant; without a suitable example it's hard to tell though.

> Pack := (pack.nb+1, pack.set & CARDS_SET'(1=> Card)); 
> a95cop1.adb:126:39: invalid operand types for operator "&" 
> this works but overkill... -> Pack := (pack.nb+1, (pack.set with delta PACK.NB+1 => Card));
Yeah, here you're straight-up trying to overwrite the discriminant.

  parent reply	other threads:[~2021-02-24 15:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-22  0:14 "unconstrained subtype in component declaration" vs "discriminant in constraint must appear alone" Mehdi Saada
2021-02-22  6:32 ` J-P. Rosen
2021-02-22 10:14   ` AdaMagica
2021-02-22 10:57     ` Dmitry A. Kazakov
2021-02-22 14:20       ` Mehdi Saada
2021-02-22 15:11         ` J-P. Rosen
2021-02-22 15:55         ` Dmitry A. Kazakov
2021-02-22 16:21           ` Mehdi Saada
2021-02-23 22:06             ` Mehdi Saada
2021-02-24  9:59               ` AdaMagica
2021-02-24 10:04               ` Jeffrey R. Carter
2021-02-24 14:11               ` Simon Wright
2021-02-24 15:57               ` Shark8 [this message]
2021-02-25  0:13                 ` Mehdi Saada
2021-02-25  8:58                   ` AdaMagica
2021-02-25 10:25                     ` Mehdi Saada
2021-02-25 10:28                     ` Mehdi Saada
2021-02-22 10:35 ` Jeffrey R. Carter
2021-02-22 14:21   ` Mehdi Saada
2021-02-22 15:02     ` Jeffrey R. Carter
2021-02-22 15:08       ` J-P. Rosen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox