RE: Defining a function P[A_ | B_]:=f[A,B] overriding A lternatives
- To: mathgroup at smc.vnet.net
- Subject: [mg19249] RE: [mg19205] Defining a function P[A_ | B_]:=f[A,B] overriding A lternatives
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Wed, 11 Aug 1999 02:06:57 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Peltio wrote: ------------------ Is there a general way to call a function with arguments separated not by commas but by user defined symbols (particularly Mathematica sacred symbols)? Here's what I mean: ----[Peltio]---- I was willing to define a function for the calculation of the conditional probabilty, via the Bayes formula, and I wanted to define it in such a way that the function call would be: p[A|B] but Mathematica correctly interpreted the lhs of the definition p[A_|B_] as if there were two alternative arguments. I can unprotect Alternatives and clear it and that would work, Unprotect[Alternatives]; Clear[Alternatives]; p[A_|B_]:= {A,B} p[A|B] {A,B} ---[Ted Ersek]--- Are you sure? That isn't what I get, and it isn't what I expect. For one thing your definition above is associated with (p) not Alternatives. So unprotecting Alternatives makes no difference. By default Alternatives has no assigned values, so Clear[Alternatives] makes no difference either. Instead consider the example below. By the way as a matter of style I avoid using single letter capitals since the kernel uses some of them (I, C, D, ...). In[1]:= Unprotect[Alternatives]; Clear[p,Alternatives]; p[a_|b_]:={{a},{b}} In[4]:= p[s|t] Out[4]= {{s|t},{}} At In[4] above (a_|b_) means some pattern called (a) or some other pattern called (b). When In[4] is evaluated the expression (s|t) fits the bill for "some pattern called (a)". Then (Null) which is Mathematica jargon for nothing is assigned to (b). Finally {{a},{b}} evaluates to {{a|b},{}} as we have in Out[4]. ---[Peltio]---- <snip> It would be fine to avoid its evaluation only when inside a [ ] function, by means of upvalues. But the following does not seems to work: Unprotect[Alternatives]; Alternatives /: p[A_|B_]:={A,B} ---[Ted Ersek]--- Using UpValues doesn't prevent evaluation of the arguments. To prevent evaluation of the arguments you would give (p) the HoldAll attribute. I think you have the use of upvalues confused. I will provide what I think is a good demonstration of upvalues, but I want to resolve some subtle details first. In several days I should have this resolved and I will send another email. To solve your problem you can use (Verbatim). The usage message for Verbatim is given below. In[5]:= ?Verbatim Verbatim[expr] represents expr in pattern matching, requiring that expr be matched exactly as it appears, with no substitutions for blanks or other transformations. ------------- What you want to do is say that (p) will take one argument with the head (Alternatives). Verbatim will ensure the meaning of Alternatives is ignored by the pattern matcher. Then inside the head Alternatives you want two arguments, and you are using named patterns for the two arguments. In[6]:= Clear[p] p[Verbatim[Alternatives][a_,b_]]:={a,b} In[8]:= p[s|t] Out[8]= {s,t} In[9]:= {p[s],p[s,t]} Out[9]= {p[s], p[s, t]} ----------------------- I think this is what you wanted. At Out[8] the definition is used for p[s,t]. At Out[9] the definition isn't used because it doesn't apply. At first one might be tempted to use the definition below. At Out[12] we see this definition is only used when the argument of (p) is literally (a_|b_). In[10]:= Clear[p]; p[Verbatim[a_|b_]]:={a,b} In[12]:= {p[s|t],p[s_|t_],p[a_|b_]} Out[12]= {p[s|t], p[s_|t_], {a, b}} ------------ Regards, Ted Ersek