From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public From: eachus@spectre.mitre.org (Robert I. Eachus) Subject: Re: Expressive Case Statements (was: Software landmines) Date: 1998/09/02 Message-ID: #1/1 X-Deja-AN: 387196115 References: <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> <6r1glm$bvh$1@nnrp1.dejanews.com> <6r9f8h$jtm$1@nnrp1.dejanews.com> <6renh8$ga7$1@nnrp1.dejanews.com> <6rf59b$2ud$1@nnrp1.dejanews.com> <6rfra4$rul$1@nnrp1.dejanews.com> <35DBDD24.D003404D@calfp.co.uk> <6sbuod$fra$1@hirame.wwa.com> <35f51e53.48044143@ <904556531.666222@miso.it.uq.edu.au> <35EAEC47.164424A7@s054.aone.net.au> <6sgn8l$7aq$1@nnrp1.dejanews.com> <6sh487$lnq$1@nnrp1.dejanews.com> <6shit4$eaj@dfw-ixnews5.ix.netcom.com> <6si98q$fbo@sjx-ixn4.ix.netcom.com> Organization: The Mitre Corp., Bedford, MA. Newsgroups: comp.lang.ada Date: 1998-09-02T00:00:00+00:00 List-Id: In article <6si98q$fbo@sjx-ixn4.ix.netcom.com> Richard D Riehle writes: > I need to code this as a compilable example and see if I can > generalize it. As it is, it appears to satisfy the special > case of a three-valued table. Hmmm. When I was thinking about your challenge, I did try a "more generalized approach. Basically you need a modular type, some aliases, and to make sure your case expressions are static: type Truth_Table is mod 8; True1: constant Truth_Table := 128; True2: constant Truth_Table := 64; True3: constant Truth_Table := 32; True4: constant Truth_Table := 16; True5: constant Truth_Table := 8; True6: constant Truth_Table := 4; True7: constant Truth_Table := 2; True8: constant Truth_Table := 1; False: constant Truth_Table := 0; function TT (Cond_1, Cond_2, Cond_3, Cond_4, Cond_5, Cond_6, Cond_7, Cond_8 : Boolean := False) return Truth_Table; ... case TT(Condition_1,Condition_2,Condition_3) is when False | True1 => Do_Something; when True1 + True2 => Do_Something_Else; ... when others => Do_Nothing; end case; If works, but I think that the notation becomes confusing. In fact it becomes VERY confusing if you use the boolean operators. For example, "True1 and True2" evaluates to False, and "True1 or True3" evaluates to the same value as TT(True, False, True). Of course, you could replace the modular type with an integer type to disallow such bogusity. So I thought the approach I sketched yesterday was better. You would need a package with probably five types, covering two to six booleans. (You could go higher, but I've never done a 128 value table, and I think I've only gone to six choices once.) The complexity is all in the package--the user just needs to know how many choices he has. However, there is one additional thing I remembered on the way home. I have used this approach before, and ended up using an enumeration type that was: type Truth_Table_3 is (NNN, NNY, NYN, NYY, YNN, YNY, YYN, YYY); -- could have used T and F, but Y and N fit better. function All_False renames NNN; function True_3 renames NNY; ... function All_True renames YYY; or: All_False: constant Truth_Table_3 := NNN; ... whichever you prefer. Basically, you can have constant or renaming declarations which allow you to name the cases in a way that makes sense locally. With bigger truth tables, that turns out to be a big help. In fact thinking about it, I almost always reify the states of a truth table by using an enumeration type specific to that particular table. -- Robert I. Eachus with Standard_Disclaimer; use Standard_Disclaimer; function Message (Text: in Clever_Ideas) return Better_Ideas is...