Re: Assertions in Mathematica?
- To: mathgroup at smc.vnet.net
- Subject: [mg113445] Re: Assertions in Mathematica?
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Fri, 29 Oct 2010 06:27:12 -0400 (EDT)
Hi, here is one way: In[1]:= ClearAll[assert]; assert::fail = "Assertion `1` failed!"; assert::alt = "Assertion failed!"; SetAttributes[assert, HoldAll]; assert[exp_, msg_] := If[! exp, Message[msg, ToString@Unevaluated[exp]]; Abort[]]; Here are some possible use cases: In[6]:= assert[1 == 2, assert::fail] During evaluation of In[6]:= assert::fail: Assertion 1 == 2 failed! Out[6]= $Aborted In[7]:= assert[1 == 2, assert::alt] During evaluation of In[7]:= assert::alt: Assertion failed! Out[7]= $Aborted I included an alternative message assert::alt to illustrate that you don't have to use the extra parameter that the code supplies to the Message, if you don't want to. Now, how you could arrive to this. The error you referred to is not "cryptic", it gives you a valuable piece of information. It tells that either your message is improperly formed (not of the form Symbol::name, for example if you just pass a string), or it is properly formed but it gets evaluated to a resulting string before being passed to the function's body. So, you have to: 1) Only use proper messages, which have to be defined with MessageName (::), and 2) prevent their premature evaluation by giving your function one of the Hold* attributes. I chose HoldAll since then you can also use the assertion condition in a useful way. Regarding your general remarks on how often the "elementary" tasks are hard in Mathematica, a single piece of advice: read about and understand the evaluation sequence in Mathematica. After that, the majority of these things will be as trivial for you in Mathematica as they are in other languages. Two sources I'd recommend are a book "Power programming with Mathematica: the kernel" by David Wagner (out of print, alas) and a technical report by David Withoff called "Mathematica internals" available on the web as pdf or ps. Finally, regarding documentation. It seems that you misunderstand its purpose. It is not at all useless, it just is not aimed at teaching you Mathematica programming in a systematic way, like some textbook will do. It is there to describe the details of each built-in function syntax and semantics, decoupled from other functions as much as possible. It mostly gives you the building blocks for your programs, but not the "glue" (which is basically the Mathematica core language). And that is what documentation is supposed to do. This is not unique to Mathematica. Of many Java programmers I know, no one learned Java by reading Javadocs. You either have to read some books on the language (Wagner's book and also "Programming in Mathematica" by Roman Maeder are two good choices, in that order), or read the working code and learn from it, or both. Hope this helps. Regards, Leonid On Thu, Oct 28, 2010 at 12:27 PM, kj <no.email at please.post> wrote: > What's the best to implement assertions in Mathematica? By assertions > I mean statements like assert(exp) in C, which generate an error > if exp evaluates to false. > > This *should* be trivial, but it's Mathematica, so... > > Naively, I tried defining: > > Assert[exp_, msg__] := If[!exp, Message[msg]; Abort[]] > > ...which, of course, failed to work (as I've learned to expect); > instead it produced a cryptic error "Message::name : Message name > ... is not of the form symbol::name or symbol::name::language." > > I tried many other things, but after wasting 1 hour on this > ridiculously trivial programming task, I'm reduced to begging for > help. (This, by the way, is always the way it is with me and > Mathematica, and I've been using it on-and-off for almost 20 years. > The documentation is as useless to me today as it was 20 years ago. > I find it as horrible as the rest of Mathematica is brilliant.) > > I've posted desperate questions over programming mind-numbing > trivialities like this one in Mathematica before, i.e. questions > that seem so elementary and fundamental that no one who has access > to the documentation and who can read should *ever* have to ask > them. I ask them less wanting to get the answer to the questions > themselves than hoping to learn how I could have answered such > questions by myself. But I've never found how to do this. Those > who know the answers *already* can give them to me if they feel so > inclined. (And how they got to know the answer to begin with, I > don't know; I imagine it took years of sustained Mathematica > programming. Or maybe they asked a similar question before to > someone who already knew the answer...) But no one has been able > to tell me how someone who *doesn't* know the answers to such questions > already can figure it out without outside help. > > But hope springs eternal! If someone is kind enough to tell me > how I could implement my Assert, I'd be most grateful. If someone > can tell me how I could have arrived at this answer by myself by > consulting the documentation, I'd be ecstatic. > > TIA, > > kj > >