Pure functions? What the heck are those ?!?!

What is the basis for reason? And mathematics?

Moderators: AMod, iMod

Post Reply
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Pure functions? What the heck are those ?!?!

Post by Skepdick »

Mathematicians keep talking about "pure" functions. A pure function is a function that has no external input and no external output (no side-effects). What incoherent nonsense is this?

An Input is the universe affecting the function.
An output is the function affecting the universe.

Inputs/Outputs ARE side-effects!

If we use Python as a model of computation this is a pure function, but it does nothing:

Code: Select all

   def f(): pass # Mathematical equivalent unknown 
This is NOT a pure function. It has outputs.

Code: Select all

   def g(): return 1 #Mathematical equivalent f() = 1 
This is NOT a pure function. It mandates input.

Code: Select all

   def h(x): pass # Mathematical equivalent unknown 
This is NOT a pure function - it mandates input and produces output:

Code: Select all

   def j(x): return x. # Mathematical equivalent f(x) = x 
This is a pure function. It calculates the answer to 2+2, but doesn't give you the answer.

Code: Select all

   def k(): 2+2 # Mathematical equivalent unknown 
If one takes an extensional view of functions, thereare no such things as pure functions!

Mathematicians, do you even know what you are talking about when you talk about "pure functions"?
wtf
Posts: 1178
Joined: Tue Sep 08, 2015 11:36 pm

Re: Pure functions? What the heck are those ?!?!

Post by wtf »

Skepdick wrote: Tue Jul 20, 2021 1:40 pm Mathematicians keep talking about "pure" functions.
I wonder if you could name them. I've never heard the term used in math, only in computer science. Never in math, not once. Can you name any of these mathematicians who "keep talking" about something that there is zero evidence they're talking about?

Skepdick wrote: Tue Jul 20, 2021 1:40 pm A pure function is a function that has no external input and no external output (no side-effects). What incoherent nonsense is this?
It's your understanding that's incoherent. According to Wiki, a pure function is " a function that has the following properties:[1][2]

The function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams).
The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams)."

There's no restriction on external input or output. On the contrary, any function must receive some input (even if null) and must return some output value, even if null. You are misunderstanding side effects.

A pure function in Python3 would be

Code: Select all

def pure_squarer(x) : 
    return x * x
It always returns the same output for the same input, and has no external or side effects.


Skepdick wrote: Tue Jul 20, 2021 1:40 pm An Input is the universe affecting the function.
An output is the function affecting the universe.
Please read the Wiki page on pure functions. Every function takes an input and returns an output. A pure function does so without creating side effects, and without relying on anything outside the function to determine the output.

For example a non-pure function would be

Code: Select all

import datetime()
def not_pure :
    return datetime.datetime.now()
As you can see, the function returns a different value each time it's called, and depends on the time of day.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm Inputs/Outputs ARE side-effects!
For gosh sakes, man. That's nonsense.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm If we use Python as a model of computation this is a pure function, but it does nothing:

Code: Select all

   def f(): pass # Mathematical equivalent unknown 
The only way you could have a pure function by your definition is that if it does nothing. Obviously that's wrong.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm This is NOT a pure function. It has outputs.

Code: Select all

   def g(): return 1 #Mathematical equivalent f() = 1 
That's a pure function. Always returns the same on the same input, no reliance on external state, no side effects.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm This is NOT a pure function. It mandates input.

Code: Select all

   def h(x): pass # Mathematical equivalent unknown 
That's pathetic. How do you manage to have such wrong ideas? That's a pure function.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm This is NOT a pure function - it mandates input and produces output:

Code: Select all

   def j(x): return x. # Mathematical equivalent f(x) = x 
That's a classic example of a pure function.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm This is a pure function. It calculates the answer to 2+2, but doesn't give you the answer.

Code: Select all

   def k(): 2+2 # Mathematical equivalent unknown 
Then it's absolutely useless. Surely you can see that nobody would bother to invent a concept like pure functions if they were no-ops.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm If one takes an extensional view of functions, thereare no such things as pure functions!
So wrong. Read the Wiki page. Read a book.
Skepdick wrote: Tue Jul 20, 2021 1:40 pm Mathematicians, do you even know what you are talking about when you talk about "pure functions"?
I have never seen a mathematician talk about pure functions. Pure functions are part of computer science. Of course if you call computer science a branch of math, as some do, then mathematicians talk about pure functions: namely, those mathematicians who are computer scientists.

I expect the usual incoherent insults in reply. Go.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

wtf wrote: Tue Aug 03, 2021 3:17 am I wonder if you could name them. I've never heard the term used in math, only in computer science. Never in math, not once. Can you name any of these mathematicians who "keep talking" about something that there is zero evidence they're talking about?
You are missing the point. Surely you understand the difference between syntax and semantics?
I'll paraphrase my meaning: The functions that Mathematicians keep talking ABOUT (ALL functions that Mathematicians concern themselves with) are (supposedly) pure functions!

And I'll paraphrase it once more for an attempt at clarity:
IF a pure function is a function without side-effects;
AND ALL Mathematical functions are without side-effects
THEN Mathematicians ONLY ever talk about pure functions.

In doing Mathematics they are doing it (talking ABOUT pure functions) implicitly even if they are not saying it expliticly.
My claim is semantically true, even if it's not syntactically provable!

And so, of course I can name them! I name any and all Mathematicians! I name you.
wtf wrote: Tue Aug 03, 2021 3:17 am It's your understanding that's incoherent. According to Wiki, a pure function is " a function that has the following properties:[1][2]

The function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams).
The function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams)."

There's no restriction on external input or output. On the contrary, any function must receive some input (even if null) and must return some output value, even if null. You are misunderstanding side effects.
I am not misunderstanding. I am disagreeing with the definition (on grounds that will become clear further below). I am disagreeing with the distinction between inputs/outputs and side-effects on the grounds that they are the exact same thing, so the distinction is a case of special pleading. The distinction is an (unnecessary) double standard.

Your point about null is irelevant too because these all four functions (below) DO the same thing. Nothing!

Code: Select all

def f(x): pass
def g(x=None): pass
def h(a,b,c,d,e,f,g,h,i,.....): pass
def j(): pass
If, for a moment you allow yourself to see through my lens, the lens in which I/O is the exact same thing as side-effects then this happens...
wtf wrote: Tue Aug 03, 2021 3:17 am A pure function in Python3 would be

Code: Select all

def pure_squarer(x) : 
    return x * x
It always returns the same output for the same input, and has no external or side effects.
It takes an input! That's a dependency on universe. It's not pure.
It returns! That's an output! That's a side-effect. It's not pure.

Remove the "return" statement. Is this still pure in your view? Can we at least agree that it's not the same function as the one you have given me? It's missing a "return"!

Code: Select all

def pure_squarer(x) : 
    x * x
wtf wrote: Tue Aug 03, 2021 3:17 am For example a non-pure function would be
I have a much much better example of an non-pure function. Equality itself!

Code: Select all

def equal(a,b):
      # something happens here. Don't know what.
      # If a = b: return True
      # else: return False
      # Only this is ciruclar. it invokes "=". It invokes itself.
I am yet to see an implementation of equality that does NOT depend on an external input from the Universe.
I am yet to see an implementation that can pass the following unit tests:

Code: Select all

assert equal(1,1)
assert equal(0,0)
assert not equal(0,1)
Until somebody shows me such a function, I am going to go with my intuition on this. Equality is undecidable.

wtf wrote: Tue Aug 03, 2021 3:17 am For gosh sakes, man. That's nonsense.

The only way you could have a pure function by your definition is that if it does nothing. Obviously that's wrong.
Well, YES! That's neither right nor wrong! That is just a direct implication of that particular definition. Is the definition "wrong"? No! It's just a definition. If you insist that the definition is wrong then define "wrong"!

Given that definition it follows that there is only one pure function: NOOP.
Every other function is NOT a pure function.
wtf wrote: Tue Aug 03, 2021 3:17 am That's a pure function. Always returns the same on the same input, no reliance on external state, no side effects.
Yeah, but not through the lens of my definition. It returns. That's an output. Output is a side-effect. Not a pure function.
wtf wrote: Tue Aug 03, 2021 3:17 am That's pathetic. How do you manage to have such wrong ideas? That's a pure function.
Yeah, but not through the lens of my definition. It takes an input. Input is a dependency on the external world. Not a pure function.
wtf wrote: Tue Aug 03, 2021 3:17 am That's a classic example of a pure function.
Yeah, but not throguh the lens of my definition. It takes input (depends on the external world), and returns output (produces a side-effect).

Not a pure function.
wtf wrote: Tue Aug 03, 2021 3:17 am

Code: Select all

def k(): 2+2
Then it's absolutely useless. Surely you can see that nobody would bother to invent a concept like pure functions if they were no-ops.
That is NOT a no-op function - obviously it DOES something. THIS is a no-op function.

Code: Select all

def k(): pass
I am not opposing anybody's concepts/inventions. I am just pointing out a muchmore detailed classification schema based on the function's properties!
Surely you can see the semantic difference between the two functions?
wtf wrote: Tue Aug 03, 2021 3:17 am So wrong. Read the Wiki page. Read a book.
Define "wrong". Or point me to a book which defines it.
wtf wrote: Tue Aug 03, 2021 3:17 am I have never seen a mathematician talk about pure functions. Pure functions are part of computer science. Of course if you call computer science a branch of math, as some do, then mathematicians talk about pure functions: namely, those mathematicians who are computer scientists.

I expect the usual incoherent insults in reply. Go.
Look. I am willing to concede the point that Mathematicians never use the term "pure functions" (this is not even relevant to my argument). IF you are willing to concede that mathematicians use the term "function". And since mathematicians use the term "function", and since mathematical functions have inputs and outputs it follows that ALL Mathematical functions are impure (as computer scientists define purity).

Heck! Even assignment IS a side-effect! It mutates memory!

Code: Select all

let x = 5
Last edited by Skepdick on Tue Aug 03, 2021 2:23 pm, edited 15 times in total.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

wtf wrote: Tue Aug 03, 2021 3:17 am
Skepdick wrote: Tue Jul 20, 2021 1:40 pm This is NOT a pure function. It mandates input.

Code: Select all

   def h(x): pass # Mathematical equivalent unknown 
That's pathetic. How do you manage to have such wrong ideas? That's a pure function.
I'll will gladly take you on your word! I accept that you accept that as a pure function.

Then you must surely accept this as pure function too (N.B no "return" clause):

Code: Select all

def f(x): x+x
And this:

Code: Select all

def g(x): x*x
And this:

Code: Select all

def h(x): x/x
And this:

Code: Select all

def i(x): x == 0
And this (my favourite!):

Code: Select all

def j(x): x != x
Last edited by Skepdick on Tue Aug 03, 2021 11:27 am, edited 1 time in total.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

purity.png
purity.png (39.1 KiB) Viewed 3480 times
User avatar
attofishpi
Posts: 9939
Joined: Tue Aug 16, 2011 8:10 am
Location: Orion Spur
Contact:

Re: Pure functions? What the heck are those ?!?!

Post by attofishpi »

Skepdick wrote: Tue Aug 03, 2021 10:12 amImage
Flashing your Python around Skeppy...wot would your wife think, hey..hey? naaaw.
wtf
Posts: 1178
Joined: Tue Sep 08, 2015 11:36 pm

Re: Pure functions? What the heck are those ?!?!

Post by wtf »

Skepdick wrote: Tue Aug 03, 2021 8:42 am
And this:

Code: Select all

def i(x): x == 0
Well of course, all those functions that don't return anything are pure functions. But functions that do output return values are also pure functions, if they have no side effects and always return the same output for the same input.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

wtf wrote: Thu Aug 05, 2021 3:40 am Well of course, all those functions that don't return anything are pure functions.
It seems like you are contradicting yourself; or I am misunderstanding you. The definition insists that a pure function has the property of "returns a value".
You are also insisting that a function which doesn't return ANY value satisfies this property.

Help me understand how you see this and why you view return and non-return as semantically equivalent?
wtf wrote: Thu Aug 05, 2021 3:40 am But functions that do output return values are also pure functions, if they have no side effects and always return the same output for the same input.
Well, what do you think is the difference between a function output and a function side-effect?

Personally, I don't see a difference (as I stated originally). And to the best of my understanding neither do people who use effect algebras.
wtf
Posts: 1178
Joined: Tue Sep 08, 2015 11:36 pm

Re: Pure functions? What the heck are those ?!?!

Post by wtf »

Skepdick wrote: Thu Aug 05, 2021 5:14 am It seems like you are contradicting yourself; or I am misunderstanding you. The definition insists that a pure function has the property of "returns a value".
You are also insisting that a function which doesn't return ANY value satisfies this property.

Help me understand how you see this and why you view return and non-return as semantically equivalent?
The difference is irrelevant. Functions that return a value and functions that don't return a value are both pure functions if they either return the same thing (or nothing) for the same input, and have no side effects.
Skepdick wrote: Thu Aug 05, 2021 5:14 am Well, what do you think is the difference between a function output and a function side-effect?
The output is the return value, and is visible to the caller. A side effect is a change made to the program state that is invisible to the caller. That's why side effects are often bad. Example:

Code: Select all

def impure() :
    global x
    x = 112
    return 6

x = 47  
returned_value = impure() 
print(x)  # Prints 112. Surprise!!
Now at this point the caller gets the value 6 returned to returned_value, which the caller can see. But the value of the variable x has been changed from 47 to 112, without the caller having any visibility into that fact. That's what makes impure() a non-pure function. And that's why in general side effects are bad. Because the caller has no way to know that the function call has changed some other variable.

If the caller then uses the variable 'x' without realizing that the function call has modified it, bad things might happen.

You can see that, right?

Skepdick wrote: Thu Aug 05, 2021 5:14 am Personally, I don't see a difference (as I stated originally).
Whether you personally see the difference or not, that's how pure functions are defined as on the Wiki page. They can return a value, but they may not have side effects. That's just the definition in Wiki, whether or not you personally agree with it or like it.

Skepdick wrote: Thu Aug 05, 2021 5:14 am And to the best of my understanding neither do people who use effect algebras.
I looked that up and it's something to do with quantum physics. So I think you're just dropping buzzwords off-topic from the conversation. I can't speak for "the people who use effect algebras." Perhaps they have their own definition that differs from the standard one. I can't say.
Last edited by wtf on Thu Aug 05, 2021 8:37 am, edited 6 times in total.
wtf
Posts: 1178
Joined: Tue Sep 08, 2015 11:36 pm

Re: Pure functions? What the heck are those ?!?!

Post by wtf »

Skepdick wrote: Tue Aug 03, 2021 8:11 am You are missing the point. Surely you understand the difference between syntax and semantics?
I'll paraphrase my meaning: The functions that Mathematicians keep talking ABOUT (ALL functions that Mathematicians concern themselves with) are (supposedly) pure functions!
Ok I see what you are saying. You are saying that mathematical functions are what computer scientists call pure functions. That's entirely correct.

But now YOU are contradicting yourself, because mathematical functions do return values. f(x) = cosine(x) returns the cosine of x, and of course without any side effects, because there are no such things as side effects in mathematical functions.

So in fact that's exactly what a pure function (in programming) is. It's like a mathematical function. It always produces the same output for a given input; and it has no side effects. In the context of math, side effects make no sense.
Skepdick wrote: Tue Aug 03, 2021 8:11 am And I'll paraphrase it once more for an attempt at clarity:
IF a pure function is a function without side-effects;
AND ALL Mathematical functions are without side-effects
THEN Mathematicians ONLY ever talk about pure functions.
Yes I see what you mean. But of course mathematicians NEVER "talk about" pure functions, they just call them functions, Mathematical functions are pure functions as defined by computer scientists. But no mathematician ever said, "Cosine(x) is a pure function." They couldn't say that, because all mathematical functions are pure in the computer science sense. Mathematicians don't have any other kind of functions besides pure functions. So they just call them functions.

It's only in computer science that a program has state that might be changed as a side effect of calling a function, or that might be used by the function to decide what to return.

So yes, you've said something correct in a funny way. Mathematical functions are all pure functions.
Skepdick wrote: Tue Aug 03, 2021 8:11 am I am not misunderstanding. I am disagreeing with the definition (on grounds that will become clear further below). I am disagreeing with the distinction between inputs/outputs and side-effects on the grounds that they are the exact same thing, so the distinction is a case of special pleading. The distinction is an (unnecessary) double standard.
Ok you are disagreeing with the standard definition. That's good, I appreciate your acknowledging that. So whether you agree with the standard definition or not, you do agree that YOUR definition is NOT the standard one. Much confusion would be avoided if you would not redefine standard terms. Call a function pure if it satisfies the Wiki definition, and Skepdick-pure if it satisfies your definition. Then we would have no disagreement of opinion.

I did not read the rest since I could not possibly have any disagreement with you on this point. If you agree that Wiki has the standard definition, and Skepdick-pure functions have a different definition, then your definition is perfectly valid.

If you think your definition should BECOME the standard one, I'm not the right person to talk to. You should write to the authors of computer science textbooks or you can go change the Wiki page and see if they perhaps fail to notice your edit.

I can't argue with your private definition. If you think the standard definition SHOULD BE changed to your definition, I can't even argue with that. It's only a definition. No underlying reality is ever changed by a definition. It's just what you call things.

If you think pure functions should be called something else, or should have a different definition, I don't even have any opinion about that. As a programmer I try to write pure or at least pure-ish functions, because they create less confusion. But you can't always do that, for example if I write a logging function that timestamps its output, then that must depend on outside state to determine what it does. Practical programming is often a messy affair. I write clean code but I don't get hung up on computer science perfection.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

wtf wrote: Thu Aug 05, 2021 8:04 am The difference is irrelevant.
Wait, what? No!

There IS a difference. That is a semantic fact. It dirrectly follows from that semantic fact that these are TWO DIFFERENT FUNCTIONS:

Code: Select all

def f(x): x

Code: Select all

 def f(x): return x
To declare the difference "irrelevant" is to blatantly refuse to account for the difference. That is NOT in the spirit of intellectual honesty and transparency. That is in the spirit of obscurantism.

You are ignoring/discarding/hiding information.
wtf wrote: Thu Aug 05, 2021 8:04 am Functions that return a value and functions that don't return a value are both pure functions if they either return the same thing (or nothing) for the same input, and have no side effects.
That is just another semantic difference you are arbitrarily choosing to discard! The difference between a function that returns a value and a function that does not return a value.
wtf wrote: Thu Aug 05, 2021 8:04 am The output is the return value, and is visible to the caller.
Precisely! So then a function that doesn't return anything has NO value visible to the caller!

What is the value of this function?

Code: Select all

def f(x): pass
Surely you aren't trying to argue that a non-value is a value? That would lead to a contradiction!
wtf wrote: Thu Aug 05, 2021 8:04 am A side effect is a change made to the program state that is invisible to the caller. That's why side effects are often bad. Example:

Code: Select all

x = 47

def impure() :
    x = 112
    return 6
    
returned_value = impure()
Now at this point the caller gets the value 6 returned to returned_value, which the caller can see. But the value of the variable x has been changed from 47 to 112, without the caller having any visibility into that fact. That's what makes impure() a non-pure function. And that's why in general side effects are bad. Because the caller has no way to know that the function call has changed some other variable.

If the caller then uses the variable 'x' without realizing that the function call has modified it, bad things might happen.

You can see that, right?
I believe I have addressed your point already. Value assignment IS a function with side-effects!

This

Code: Select all

x = 5
Is the same as this

Code: Select all

def assign_x(y): 
    global x
    x = y
None of the functions I am showing you have any variable assignments in them.

I am ONLY working with function invocation here:

Code: Select all

In [1]: def f(x): return 6

In [2]: f(1)
Out[2]: 6

In [3]: f(2)
Out[3]: 6
wtf wrote: Thu Aug 05, 2021 8:04 am Whether you personally see the difference or not, that's how pure functions are defined as on the Wiki page. They can return a value, but they may not have side effects. That's just the definition in Wiki, whether or not you personally agree with it or like it.
OK, but it's pretty obvious that what we are disagreeing about is precisely the interpretaiton of this definition.

In so far as I can tell the definition is incomplete. The specification makes no mention about functions that return NO values. That's undefined behaviour.

So you are obviously going well beyond the definition in insisting that functions which return NO value are in the scope of the definition.
And I am taking the definition at face value. It talks about functions with return-values.

So ALL these functions are out of scope for that definition.

Code: Select all

def f(x): pass
def g(x): x
def h(x): x == 0
def i(x): x != x
Last edited by Skepdick on Thu Aug 05, 2021 10:26 am, edited 1 time in total.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

wtf wrote: Thu Aug 05, 2021 8:04 am

Code: Select all

def impure() :
    global x
    x = 112
    return 6

x = 47  
returned_value = impure() 
print(x)  # Prints 112. Surprise!!
This really deserves and absolutely demands its own objection!!!

The assignment operator (variable binding and declaration) is an impure function!!!

This

Code: Select all

let x = 5
is the same as this:
assignment-is-impure.png
assignment-is-impure.png (52.65 KiB) Viewed 3355 times
wtf
Posts: 1178
Joined: Tue Sep 08, 2015 11:36 pm

Re: Pure functions? What the heck are those ?!?!

Post by wtf »

Skepdick wrote: Thu Aug 05, 2021 9:30 am

The assignment operator (variable binding and declaration) is an impure function!!!
Could that be why I named the function impure() and gave it as an example of a function that has side effects and is therefore not pure? I am not following your point.

You asked what is the difference between a function's return (its output) and a side effect. I gave you a perfectly clear example of a side effect, one that in practice would cause a programmer an unpleasant surprise. I don't understand your objection.
wtf
Posts: 1178
Joined: Tue Sep 08, 2015 11:36 pm

Re: Pure functions? What the heck are those ?!?!

Post by wtf »

Skepdick wrote: Thu Aug 05, 2021 8:40 am
There IS a difference. That is a semantic fact. It dirrectly follows from that semantic fact that these are TWO DIFFERENT FUNCTIONS:
Yes of course they're different functions. But for purposes of discussing pure functions there is no difference, because they are both pure functions.

Skepdick wrote: Thu Aug 05, 2021 8:40 am To declare the difference "irrelevant" is to blatantly refuse to account for the difference. That is NOT in the spirit of intellectual honesty and transparency. That is in the spirit of obscurantism.
You gave two functions, one that does not return a value and another that does. They are both pure functions. Their differences are not relevant to the discussion of pure functions. It's like if we're discussing fruit and there's an apple and an orange. It's true that they're different, but they're both fruit.
Skepdick wrote: Thu Aug 05, 2021 8:40 am You are ignoring/discarding/hiding information.
I'm ignoring distinctions between the two functions that are not relevant, because they are both pure functions.
Skepdick wrote: Thu Aug 05, 2021 8:40 am That is just another semantic difference you are arbitrarily choosing to discard! The difference between a function that returns a value and a function that does not return a value.
That's because they're both examples of pure functions.
Skepdick wrote: Thu Aug 05, 2021 8:40 am Surely you aren't trying to argue that a non-value is a value? That would lead to a contradiction!
Apples and oranges are both fruit. So if we're counting total pieces of fruit, there's no relevant difference between apples and oranges, even though there is a difference between them.
Skepdick wrote: Thu Aug 05, 2021 8:40 am I believe I have addressed your point already. Value assignment IS a function with side-effects!
Right. I gave that example in response to your specific question as to what was the difference between a function return and a side effect.

Skepdick wrote: Thu Aug 05, 2021 8:40 am OK, but it's pretty obvious that what we are disagreeing about is precisely the interpretaiton of this definition.
I'm not actually arguing about anything. The Wiki definition is perfectly clear. If you prefer a different definition that's your right.
Skepdick wrote: Thu Aug 05, 2021 8:40 am In so far as I can tell the definition is incomplete. The specification makes no mention about functions that return NO values. That's undefined behaviour.
No, that would be defined by the specification in the documentation. If a function does not return a value but the caller assigns the return to a value, that's a coding error and would indeed result in undefined behavior. I don't think we have any substantive disagreement. Your disagreement seems to be with Wikipedia or the computer scientists who define pure functions a particular way. I'm not any of those people.
Skepdick wrote: Thu Aug 05, 2021 8:40 am So you are obviously going well beyond the definition in insisting that functions which return NO value are in the scope of the definition.
And I am taking the definition at face value. It talks about functions with return-values.
I suppose I can concede your point, to the extent that a function that does not return a value can't be much of a pure function, because the ONLY way it can do anything at all would be by way of a side effect. I am afraid I can't get too worked up over this. So if you want to say that functions that don't return a value can't be pure functions ... well no, I can't agree. If they have no side effects AND they don't return a value, then they're useless no-op functions that technically might fit the definition of pure functions, if anyone cared.
Skepdick wrote: Thu Aug 05, 2021 8:40 am So ALL these functions are out of scope for that definition.
Ok. If this is important to you I'm willing to agree that a function that has no side effects AND that does not return a value is a poor example of a pure function, because it's totally useless.

Is that your point? That a function that has no side effects yet also does not return a value should not be called a pure function? I'm ok with that. I'm not very computer science-y, I can't muster much emotion over this point. No actual programmer would write a function that does nothing and returns nothing. Well ok, every machine instruction set includes a NO-OP instruction for timing purposes. Is a NO-OP a pure function? I don't know, does it really matter? It's just a corner case of a definition.

Was this the purpose of your OP? To point out that a function that has no side effects AND does not return a value should not be called a pure function? Ok, I can live with that. I started with the opposite opinion but I think you convinced me. Although I am not knowledgable about what the official computer scientists would say. But how is this important? Nobody would ever write a function that has no side effects and that doesn't return a value, unless they were writing a no-op for timing purposes perhaps.

Note that mathematically a function that doesn't return anything must be the empty function. That's a corner case too. Every nonempty mathematical function returns a value for any input it its domain.
Skepdick
Posts: 14347
Joined: Fri Jun 14, 2019 11:16 am

Re: Pure functions? What the heck are those ?!?!

Post by Skepdick »

wtf wrote: Fri Aug 06, 2021 3:26 am Could that be why I named the function impure() and gave it as an example of a function that has side effects and is therefore not pure? I am not following your point.
I think we are tripping up over notation. Yes you gave an example. But the example is overly complicated and takes us further from the point.
Which is why I am using the below as a sufficient example of what I would classify as an impure function. Assignment.

Code: Select all

x=5
I think this is precisely where we diverge. In your conception I think you would classify the above as pure and I classify it as impure. Correct me if I misunderstand you.

And the divergence is not immediately obvious because of the notation being used, but it becomes absolutely clear when you rewrite x=5 as:

Code: Select all

def assign_x(y):
    global x
    x = y
assign_x(5)
But I will still ask you a yes/no question so as to avoid endless back-and-forth with definitions: do you see x=5 as a pure function?
Post Reply