The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Assertions in Mathematica?
Replies: 22   Last Post: Nov 3, 2010 5:22 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Charles Gillingham

Posts: 14
Registered: 10/30/10
Re: Assertions in Mathematica?
Posted: Oct 30, 2010 4:36 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


Attributes[Assert] = {HoldAll};
Assert[exp_, msg_] := If[Not[exp], Message[Unevaluated[msg]]; Abort[];]

You also might want to consider adding arguments for some messages,

Assert[exp_, msg_, args___] := If[Not[exp],
Message[Unevaluated[msg],args]; Abort[];]

And you might also want to consider what happens if "exp" does not
evaluate and remains in symbolic form, e.g.:

Assert[exp_, msg_,args___] := If[exp =!= True,
Message[Unevaluated[msg],args]; Abort[];]

>> 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

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.