# Issue with automated function definitions

**URL:** <https://community.wolfram.com/t/issue-with-automated-function-definitions/13856>\
**Category:** Questions\
**Tags:** symbolic-computations, tuning-and-debugging\
**Created:** [April 1, 2019, 1:43pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856 "2019-04-01T13:43:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnstrass](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@johnstrass](https://community.wolfram.com/u/johnstrass)\
**Post date:** [April 1, 2019, 1:43pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/1 "2019-04-01T13:43:28Z")

</div>

I am using version 11.3. I want to generate some definitions doing some time consuming symbolic computations. I just changed the time-consuming part to simple ones here.

```
In[1]:=Clear[f]; 
Do[
 Block[{tmp}, tmp = x + y + z + 10; 
      f[x, y, z] := Evaluate[Module[{tmp1}, tmp1 = tmp; tmp1^2]]], 
  {x, -1,1}, {y, 0, 2}, {z, 0, 2, 2}]

In[2]:= ?f

Global`f
f[-1,0,0]=81
f[-1,0,2]=121
f[-1,1,0]=100
f[-1,1,2]=144
f[-1,2,0]=121
f[-1,2,2]=169
f[0,0,0]=100
f[0,0,2]=144
f[0,1,0]=121
f[0,1,2]=169
f[0,2,0]=144
f[0,2,2]=196
f[1,0,0]=121
f[1,0,2]=169
f[1,1,0]=144
f[1,1,2]=196
f[1,2,0]=169
f[1,2,2]=225

```

The “=” in the output of “?f” should be “:=”. If you change the range of x to “{x,-1,0}” , the answers for “?f” are using “:=”. Similar for the other iteration variables y and z. Is this a bug or something?

---

<div class="post-metadata">

**Author:** ![fkampas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/fkampas/32/41217_2.png) [@fkampas](https://community.wolfram.com/u/fkampas)\
**Post date:** [April 1, 2019, 8:41pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/2 "2019-04-01T20:41:50Z")

</div>

I don’t understand why you want to use delayed evaluation for numbers

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [April 1, 2019, 10:15pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/3 "2019-04-01T22:15:41Z")

</div>

Thank you for taking the time to inform us about this issue. I’ve forwarded the example to the developers.

For the majority of cases, you should always avoid programmatically defining down-values of functions. It’s impossible in most programming languages and in any language I know where it is possible, not encouraged. That said there are cases where it’s apparently unavoidable.

You can see a similar issue here: [https://mathematica.stackexchange.com/questions/188517/distributedefinitions-is-evaluating-the-definitions-and-this-only-for-a-large-n](https://mathematica.stackexchange.com/questions/188517/distributedefinitions-is-evaluating-the-definitions-and-this-only-for-a-large-n)

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [April 1, 2019, 10:41pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/4 "2019-04-01T22:41:26Z")

</div>

> “For the majority of cases, you should always avoid programmatically  
> defining down-values of functions.”

What would distinguish this from memoization? Stated differently, how is memoization not a form of programatically creating down values?

---

<div class="post-metadata">

**Author:** ![anon21378091](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon21378091](https://community.wolfram.com/u/anon21378091)\
**Post date:** [April 2, 2019, 2:05am UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/5 "2019-04-02T02:05:29Z")

</div>

Memoization, if you want to view it as programmatically creating function definitions, is a least a very very limited form of it. And in most languages that have memoization it’s not done as a side-effect of the evaluation of the function, but instead it’s a language level feature. Even in Python, where you’re likely to do with a decorator, you hide any obvious state and try to make it look like it’s a language level feature. I’d saw the WL is pretty unique in having such an explicit and structurally obvious syntax for it.

The only reason it’s not more commonly advised against, AFAIK, is that it’s basically impossible to define a function in a loop like this in most languages. The closest I’ve seen in production code was a loop calling a factory. You have to use some very heavy abstraction to effect it.

Overall, programming advice is subjective, but I think the fact this is widely disallowed in most styles is a pretty solid sign. Most cases of this I’ve seen in WL tend to involve Symbolize and the creation of unique function names or the attempt to use Indexed/Subscript in some way.

---

<div class="post-metadata">

**Author:** ![b3m2a1](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/b3m2a1/32/7562_2.png) [@b3m2a1](https://community.wolfram.com/u/b3m2a1)\
**Post date:** [April 2, 2019, 2:24am UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/6 "2019-04-02T02:24:28Z")

</div>

Just my two cents, but I would say that this kind of thing is actually a powerful byproduct of the design of the language. I use programmatic `DownValues` / `UpValues` / `SubValues` all the time in meta-programming and OOP. To be honest, there’s no really good alternative simply by design of the language. In particular, because Mathematica provides such weak encapsulation mechanisms, meta-programming is one of the only ways to _cleanly_ provide an intelligently modularized interface without having to introduce hundreds of new symbols each of which requires maintenance and bug-fixes and documentation.

On the other hand, in this _particular_ example there’s no reason for this kind of thing.

---

<div class="post-metadata">

**Author:** ![johnstrass](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@johnstrass](https://community.wolfram.com/u/johnstrass)\
**Post date:** [April 2, 2019, 4:56am UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/7 "2019-04-02T04:56:07Z")

</div>

Obviously, my original program is not just for numbers. On the right hand side of the definition there could be global variables. But this is not essential to demonstrate the bug.

The situation is as follows. For each set of arguments, the calculation will take a long time. Because the arguments can be enumerated, I want it to be partly evaluated in the definition for each set of arguments. I will copy the results and run separately, and thus given each set of arguments it may not run a long time when the other global variables are set on the right hand side. It seems that “=” also do the same thing as long as it is run before the global variables are set. Anyway, I prefer to use “:=” in the definition and just met this bug.

I don’t know whether there are better way to do this.

---

<div class="post-metadata">

**Author:** ![sjoerds](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sjoerds/32/19589_2.png) [@sjoerds](https://community.wolfram.com/u/sjoerds)\
**Post date:** [April 2, 2019, 8:28am UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/8 "2019-04-02T08:28:50Z")

</div>

The `=` sign in `Definition[f]` doesn’t mean much: it’s just cosmetic. You should look at the `DownValues` of `f`. As you can see, all `DownValues` are delayed rules:

```
In[13]:= DownValues[f]

Out[13]= {HoldPattern[f[-1, 0, 0]] :> 81, 
 HoldPattern[f[-1, 0, 2]] :> 121, HoldPattern[f[-1, 1, 0]] :> 100, 
 HoldPattern[f[-1, 1, 2]] :> 144, HoldPattern[f[-1, 2, 0]] :> 121, 
 HoldPattern[f[-1, 2, 2]] :> 169, HoldPattern[f[0, 0, 0]] :> 100, 
 HoldPattern[f[0, 0, 2]] :> 144, HoldPattern[f[0, 1, 0]] :> 121, 
 HoldPattern[f[0, 1, 2]] :> 169, HoldPattern[f[0, 2, 0]] :> 144, 
 HoldPattern[f[0, 2, 2]] :> 196, HoldPattern[f[1, 0, 0]] :> 121, 
 HoldPattern[f[1, 0, 2]] :> 169, HoldPattern[f[1, 1, 0]] :> 144, 
 HoldPattern[f[1, 1, 2]] :> 196, HoldPattern[f[1, 2, 0]] :> 169, 
 HoldPattern[f[1, 2, 2]] :> 225}

```

Furthermore: as a rule of thumb, I would say there’s no difference between `Set` and `SetDelayed[..., Evaluated[...]]`. You can replace your function definition with `f[x, y, z] = Module[{tmp1}, tmp1 = tmp; tmp1^2]` and you’ll get the exact same result.

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [April 2, 2019, 2:05pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/9 "2019-04-02T14:05:25Z")

</div>

I use it often as a way of caching values needed for lookup later. For example, say you wanted to implement the default version of `DeleteDupolicates` in the Wolfram Language. (I use this as an example because it was proof-of-concept code once upon a time for why/how we should improve on our shabby O(n^2) method then in use.)

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [April 2, 2019, 2:46pm UTC](https://community.wolfram.com/t/issue-with-automated-function-definitions/13856/10 "2019-04-02T14:46:47Z")

</div>

It would seem to me that this should be equivalent to the above, with the {x,-1,0} range:

```
Clear[f];
Do[f[x, y, z] = (x + y + z + 10)^2,
  {x, -1, 0}, {y, 0, 2}, {z, 0, 2, 2}];
?f

```

The numbers are the same, but with “`=`” instead of “`:=`”. The `DownValues` are exactly the same. Why does `?f` format the output differently in the two cases, from the very same information?
