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

What is the basis for reason? And mathematics?

Moderators: AMod, iMod

Skepdick
Posts: 14410
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:49 am Yes of course they're different functions. But for purposes of discussing pure functions there is no difference, because they are both pure functions.
The purpose of the discussion is whether the functions I am pointing out satisfy the definition.

You say "yes - they satisfy it".
I say "no - they don't".

We seem to be interpreting the definition differently.
wtf wrote: Fri Aug 06, 2021 3:49 am You gave two functions, one that does not return a value and another that does.
That's precisely where we are diverging. The definition only talks about functions which return a value. I am pointing out functions which fail to return a value.

And it appears that you are insisting that the non-return of value (or the return of no value) is a value. That's a tautology.
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
This is not an equivalent analogy. I am not arguing different fruits: apples vs oranges. I am arguing fruits vs non-fruits. I address this further below.
wtf wrote: Fri Aug 06, 2021 3:49 am I'm ignoring distinctions between the two functions that are not relevant, because they are both pure functions.
And I am pointing out a distinction between two functions which is a sufficient difference to re-classify a function as impure.
wtf wrote: Fri Aug 06, 2021 3:49 am That's because they're both examples of pure functions.
I disagree.
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
False analogy.

Values and non-values are both... ???. So if we are counting total pieces of ??? there's no relevant difference between values and non-values, even though there is a difference between them.

But, of course that's a contradiction. You are basically saying that a difference is not a difference. There IS a difference. You are choosing to discard information using some yet-unstated relevance metric.
wtf wrote: Fri Aug 06, 2021 3:49 am I'm not actually arguing about anything. The Wiki definition is perfectly clear. If you prefer a different definition that's your right.
I'm not actually arguing about anything either. I am using the "perfectly clear" Wiki definition too. I don't prefer a different definition - I am using the same definition you prefer.

If we are both using the same definition. And we are both talking about the same functions - how come we are arriving at different classification schemes? How come you are classifying some functions as pure and I am classifying them as impure?

Why do we disagree? Why are our respective sorting functions producing different outputs for the same inputs?
wtf wrote: Fri Aug 06, 2021 3:49 am No, that would be defined by the specification in the documentation.
You are using the english determiner "the". What do you think is THE authoritative specification and THE authoritative documentation for pure functions?
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
My observations are conflicted with your words. You SAY that you are not one of those people, but I am observing you being one of those people so I don't know whether to trust my eyes or your words.

As I have already demonstrated "x=5" can be re-written as a function without a return value!

Code: Select all

def assign_x(y):
    global x
    x = y
ANYTHING you pass to the above function will be assigned to the variable x.
And since you can pass absolutely ANY valid object from the Mathematical Universe can be passed as a parameter to the assign_x() function I am simply asking you the question: Is None/nil/null a valid, assignable object?
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
OK, great! I agree with you 100%

So can you point me to a pure function which implements Mathematical equality?

Code: Select all

def equal(x,y):
      # Implement logic to return a Boolean(True of False)
      # such that assertions below pass.

assert equal(0,0)
assert equal(1,1)
assert not equal(0,1)
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
No, my point is that there is ONLY one pure function. NOOP. def f(): pass

Everything else is an impure function. The definition attempts to distinguish between inputs and input-streams, but that's just special pleading.

Inputs are the same things as streams of unit-length 1.
Outputs are the same thing as streams of unit-length 1.
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
It does matter to cryptographers. Side-channel attacks (timing attacks) are a real thing. NO-OPS are used for padding the runtime of a function so that the algorithm can be made to appear as executing with O(1) time-complexity.

That is precisely what makes these two functions different. Runtime.

Code: Select all

def f(): pass
def g(): x**x
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
OK, but can you live with "any function that takes inputs; or produces outputs" is impure?

If you can't live with it.... can you put forth an example of a pure implementation of equality for the domain of integers?
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
Yes. Cryptographers. But this is beyond the point.
wtf wrote: Fri Aug 06, 2021 3:49 am 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.
OK, so using the above definition: What is the domain of the assignment function? What can you assign to x?

Computationally, the answer is "ANYTHING that exists in the domain can be assigned to x".
Invoking the value of x is the same thing as invoking a function which returns x as value.

Call-by-value vs call-by-reference.

Code: Select all

In [1]: x = None
In [2]: def y(): pass
In [3]: type(x)
Out[3]: NoneType
In [4]: type(y())
Out[4]: NoneType
So it's pretty obvious that you can assign x to be ANYTHING you want it to be. Which (in so far as definitions go) is pretty useless. It imposes
no limits/constraints on you whatsoever.

So the really important question then is this: Is there anything that you can not assign to x?
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

It is clear that in mathematics a function must return a value for every member of its domain and that is by the definition of a function itself. Computer science actually adopts its definition of a function from mathematics. So in computer science too, maintaining consistency with mathematics, a function must return a value. In.CS, however, a distinction is often made between a function and a procedure. While a function returns a value, a procedure does not!

In the Python programming language, even when the keyword "return" is not used in the definition of a function, the function still returns a value! In that case, it returns the value "None". "None" is an object in Python! In Python everything is an object. The value "None" in Python is of the type NoneType. So, technically, every Python function defined in this thread, whether they explicitly used a return statement or not,were returning a value. Hence, they were functions and not procedures!
Skepdick
Posts: 14410
Joined: Fri Jun 14, 2019 11:16 am

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

Post by Skepdick »

Averroes wrote: Sat Aug 07, 2021 3:00 pm It is clear that in mathematics a function must return a value for every member of its domain and that is by the definition of a function itself.
Great. So what's the domain of f(x) = x ?

Which in python is equivalent to:

Code: Select all

def f(x): return x
Averroes wrote: Sat Aug 07, 2021 3:00 pm Computer science actually adopts its definition of a function from mathematics. So in computer science too, maintaining consistency with mathematics, a function must return a value. In.CS, however, a distinction is often made between a function and a procedure. While a function returns a value, a procedure does not!
This is a distinction without a difference. Procedural programming and Functional programming are just two different paradigms for expressing computations.

Procedural programs translate into functional programs.
Functional programs translate into procedural programs.

This is a 1:1 relationship.
Averroes wrote: Sat Aug 07, 2021 3:00 pm In the Python programming language, even when the keyword "return" is not used in the definition of a function, the function still returns a value!
In that case, it returns the value "None". "None" is an object in Python! In Python everything is an object. The value "None" in Python is of the type NoneType. So, technically, every Python function defined in this thread, whether they explicitly used a return statement or not,were returning a value. Hence, they were functions and not procedures!
This not true. A function that returns None does NOT return it as a value. The function simply terminates without any output. The type of f(None) is different to the type of f(1). The type of f(x) = x is completely determined by the type of its input.
none is not a type.png
none is not a type.png (34.85 KiB) Viewed 2013 times
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

Skepdick wrote: Sat Aug 07, 2021 3:31 pm
Averroes wrote: Sat Aug 07, 2021 3:00 pm It is clear that in mathematics a function must return a value for every member of its domain and that is by the definition of a function itself.
Great. So what's the domain of f(x) = x ?
In the paragraph you quoted of me, I was talking about mathematical functions. May I ask, in the question you asked me, what is "f(x)=x"? Is it meant to express a mathematical function?
Skepdick wrote: Sat Aug 07, 2021 3:31 pm
Averroes wrote: Sat Aug 07, 2021 3:00 pm Computer science actually adopts its definition of a function from mathematics. So in computer science too, maintaining consistency with mathematics, a function must return a value. In.CS, however, a distinction is often made between a function and a procedure. While a function returns a value, a procedure does not!
This is a distinction without a difference.
We disagree here.
Skepdick wrote: Sat Aug 07, 2021 3:31 pm
Averroes wrote: Sat Aug 07, 2021 3:00 pm In the Python programming language, even when the keyword "return" is not used in the definition of a function, the function still returns a value!
In that case, it returns the value "None". "None" is an object in Python! In Python everything is an object. The value "None" in Python is of the type NoneType. So, technically, every Python function defined in this thread, whether they explicitly used a return statement or not,were returning a value. Hence, they were functions and not procedures!
This not true. A function that returns None does NOT return it as a value. The function simply terminates without any output.
Here too, we are in disagreement. In the Python function below, the value None is clearly the result of calling the Python function f() which does not have a "return" statement.

Code: Select all

Python 3.8.3 (default, May 27 2020, 02:08:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...  pass
...
>>> y=f()
>>> type(y)
<class 'NoneType'>
>>> id(y)
482803917608
>>> id(None)
482803917608
>>> y==None
True
>>>
User avatar
Sculptor
Posts: 8631
Joined: Wed Jun 26, 2019 11:32 pm

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

Post by Sculptor »

Skepdick wrote: Tue Jul 20, 2021 1:40 pm 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.
Gosh! What are they???
:lol:
One thing is for sure: the internet says they are something other than you have said.
Maybe before you go on a childish rant, you might want to check your facts, before building a little strawman you can "cleverly" destroy?
Skepdick
Posts: 14410
Joined: Fri Jun 14, 2019 11:16 am

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

Post by Skepdick »

Averroes wrote: Sat Aug 07, 2021 4:36 pm In the paragraph you quoted of me, I was talking about mathematical functions. May I ask, in the question you asked me, what is "f(x)=x"? Is it meant to express a mathematical function?
It's not meant to express anything. It stands before you as function! A real-world existent that you can study empirically. It is what it is!

Any adjective you ascribe to it (such as Mathematical or Computational) is entirely subjective.

Code: Select all

def f(x): return x
Averroes wrote: Sat Aug 07, 2021 4:36 pm We disagree here.
You don't disagree with me. You disagree with Python.
Averroes wrote: Sat Aug 07, 2021 3:00 pm >>> y=f()
>>> type(y)
>>> id(y)
482803917608
>>> id(None)
482803917608
>>> y==None
True
>>>
[/code]
This too is an error in your understanding. You have fooled yourself by assigning f() to x!

When you assign None to a variable calling the variable produces NO output
When you assign 1 to a variable calling the variable produces an output

Observe the absence of an output when x = None
Observe the presence of an output when x = 1
➜ ~ python
Python 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 07:56:27)
[Clang 9.0.1 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = None
>>> type(x)
class 'NoneType'
>>> x
>>> x = 1
>>> type(x)
class 'int'
>>> x
1
Hereis your error:

Code: Select all

>>> def f(x): return x
...
>>> type(f(None)) == type(f(1))
False
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

Skepdick wrote: Sat Aug 07, 2021 5:19 pm
Averroes wrote: Sat Aug 07, 2021 4:36 pm In the paragraph you quoted of me, I was talking about mathematical functions. May I ask, in the question you asked me, what is "f(x)=x"? Is it meant to express a mathematical function?
It's not meant to express anything.
Alright. So, you asked me a question in which there is a sequence of characters "not meant to express anything". Thank you for replying to my question.
Skepdick wrote: Sat Aug 07, 2021 5:19 pm When you assign None to a variable calling the variable produces NO output
When you assign 1 to a variable calling the variable produces an output
Exactly! Clearly, the value (whether None or 1) must be assigned to the variable before calling the variable. Here is the code again:

Code: Select all

Python 3.8.3 (default, May 27 2020, 02:08:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...  pass
...
>>> y=f()
>>> y==None
True
>>> id(y)
518045279016
>>> id(None)
518045279016
So, calling the function f() and assigning its return value to y results in the value None being assigned to y. Now, before the assignment, the value None must be returned by function call! And clearly, y has the value None assigned to it as shown by the code. Lets now print the value of y:

Code: Select all

Python 3.8.3 (default, May 27 2020, 02:08:17)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...  pass
...
>>> y=f()
>>> y==None
True
>>> id(y)
518045279016
>>> id(None)
518045279016
>>> print(y)
None
>>>
The value None is produced as expected.
Skepdick
Posts: 14410
Joined: Fri Jun 14, 2019 11:16 am

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

Post by Skepdick »

Averroes wrote: Sat Aug 07, 2021 5:59 pm Alright. So, you asked me a question in which there is a sequence of characters "not meant to express anything". Thank you for replying to my question.
What?! it's not just a sequence of characters. That's reductionism at its worst.

Type those characters into Python.

Code: Select all

def f(x): return x
What English adjective would you use to describe this function?
Averroes wrote: Sat Aug 07, 2021 5:59 pm Exactly! Clearly, the value (whether None or 1) must be assigned to the variable before calling the variable.
It MUST be assigned to the variable? Says who?

The expression is "x = f()"

What do you assign to the left-hand side (x) when the right-hand side ( f() ) returns nothing?!?!
Averroes wrote: Sat Aug 07, 2021 5:59 pm The value None is produced as expected.
It's expected to you and unexpected to me. You know. Since the function doesn't actually return anything.

Which is why I am removing the assignment operator from the demonstartion (so that you stop fooling yourself).

no output.png
no output.png (33.8 KiB) Viewed 1974 times
One more time (since you don't understand):

Code: Select all

>>> def f(x): return x
>>> type(f(None)) == type(f(1))
False
This is the Identity of Indiscernables. And these two functions are NOT indiscernable. Therefore they are not identical.
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

Skepdick wrote: Sat Aug 07, 2021 6:12 pm
Averroes wrote: Sat Aug 07, 2021 5:59 pm The value None is produced as expected.
It's expected to you and unexpected to me.
Alright then, that's a sure sign you have learned something. I am glad to have exchanged with you in that case.
Skepdick
Posts: 14410
Joined: Fri Jun 14, 2019 11:16 am

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

Post by Skepdick »

Averroes wrote: Sat Aug 07, 2021 6:39 pm Alright then, that's a sure sign you have learned something. I am glad to have exchanged with you in that case.
indeed! Surprise is a measure of new information, after all.

I have learned that the assignment operator has side-effects, which is fooling you into arriving at an erroneously-"expected" conclusions of your own making. That's called confirmation bias, is it not?

I am still trying to determine whether you have learned anything, though. How surprised are you that the same function has different types?

Code: Select all

>>> def f(x): return x
>>> type(f(None)) == type(f(1))
False
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

Skepdick wrote: Sat Aug 07, 2021 6:49 pm
Averroes wrote: Sat Aug 07, 2021 6:39 pm Alright then, that's a sure sign you have learned something. I am glad to have exchanged with you in that case.
indeed, I have learned something.
I am glad for you. And I am glad that you benefitted from exchanging with me. Thank you for the exchange.
Skepdick
Posts: 14410
Joined: Fri Jun 14, 2019 11:16 am

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

Post by Skepdick »

Averroes wrote: Sat Aug 07, 2021 6:57 pm I am glad for you. And I am glad that you benefitted from exchanging with me. Thank you for the exchange.
I am terribly disappointed that you are congratulating yourself. What I learned, I learned before you engaged me.

I attained no benefit from exchanging with you. I thank you for the exchange too, but I am not sure what I am thanking you for.
Last edited by Skepdick on Sat Aug 07, 2021 7:16 pm, edited 1 time in total.
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

Skepdick wrote: Sat Aug 07, 2021 7:10 pm
Averroes wrote: Sat Aug 07, 2021 6:57 pm I am glad for you. And I am glad that you benefitted from exchanging with me. Thank you for the exchange.
I am terribly disappointed that you are congratulating yourself.
Don't be disappointed. It's my way of motivating myself to continue teaching you and bringing "unexpected" knowledge to your attention. It's all good for you.
Skepdick
Posts: 14410
Joined: Fri Jun 14, 2019 11:16 am

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

Post by Skepdick »

Averroes wrote: Sat Aug 07, 2021 7:15 pm Don't be disappointed. It's my way of motivating myself to continue teaching you and bringing "unexpected" knowledge to your attention. It's all good for you.
Is there any intellectually honest reason as to why you are partially quoting me?

You didn't bring any knowledge to my attention.
Skepdick wrote: Sat Aug 07, 2021 7:10 pm I am terribly disappointed that you are congratulating yourself. What I learned, I learned before you engaged me.

I attained no benefit from exchanging with you. I thank you for the exchange too, but I am not sure what I am thanking you for.
Averroes
Posts: 535
Joined: Thu Jul 20, 2017 8:48 pm

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

Post by Averroes »

Skepdick wrote: Sat Aug 07, 2021 7:17 pm
Averroes wrote: Sat Aug 07, 2021 7:15 pm Don't be disappointed. It's my way of motivating myself to continue teaching you and bringing "unexpected" knowledge to your attention. It's all good for you.
Is there any intellectually honest reason as to why you are partially quoting me?
I am as intellectually honest as the one I am exchanging with. Besides, I like keeping myself motivated in life. Looking at the blessings that we have been given keep us in a grateful and proactive state. That's much better than complaining when things don't turn out one's way or when one meet "unexpected" things.
Post Reply