Is Date.now referential transparent?

Is DateTime.Now or Date.now is referential transparent?

This is one of the controversial topics in the Qiita Functional Programming article .

First of all, we must be very careful, since the word "reference transparent" is in a sense a complex word / concept, and in

What is link transparency?

Question:

What does the term referential transparency mean? I heard that it is described as "it means that you can replace equals with equal ones", but this seems like an inadequate explanation.

A very typical explanation, but the idea that usually leads us to a misunderstanding is this: (# 2 answer to the above page by @Brian R. Bondy)

Referential transparency, a term commonly used in functional programming, means that given the function and input value, you always get the same result. That is, there is no external state used in the function.

Typical statements that I have always heard and considered incorrect are as follows:

In the programming language, Date.now always returns a different value corresponding to the current time, and according to

given the function and input value, you always get the same result.

therefore Date.now not referential transparent!

I know that some (functional) programmers strongly believe that the above statement is trustworthy, however, answer # 1 and number 3 from @Uday Reddy explains the following:

Any talk of “referential transparency” without understanding the difference between L, R values ​​and other complex objects that fill the conceptual universe of an imperative programmer is fundamentally wrong.

The idea of ​​functional programmers regarding referential transparency seems to differ from the standard concept in three ways:

  • While philosophers / logicians use terms such as “reference,” “designation,” “designate” and “sheet” (Frege term), functional programmers use the term “value”. (This is not entirely true. I notice that Landin, Fears and their descendants also used the term “value” to refer to the reference / designation. It may just be a terminological simplification introduced by Landin and the Eaves, but there seems to be a big difference when using in a naive way.)

  • Functional programmers seem to believe that these “values” exist inside the programming language, and not outside. However, they differ from philosophers and semantists of the programming language.

  • They seem to believe that these “values” should be obtained through evaluation.

Think about it, “external state” is also a complex word / concept.

Referential transparency, a term commonly used in functional programming, means that given the function and input value, you always get the same result. That is, there is no external state used in the function.

Is "current time" an "external state" or an "external value"?

If we call "current time" an "external state", what about a "mouse event"?

a mouse event is not a state that should be controlled by a programming context, but rather an external event .

given the function and input value, you always get the same result.

So we can understand the following:

"current time" is neither an "input value", nor an "external value", nor an "external state", and Date.now always returns the same result that corresponds to the current event "current time".

If you still insist or want to call "current time" as "value" , again

  • Functional programmers seem to believe that these “values” exist inside the programming language, and not outside. However, they differ from philosophers and semantists of the programming language.

The value of "current time" never exists in a programming language, but only outside , and the value of "current time" outside, obviously, is updated through a world time stream that is not in the context of programming.

Therefore, I understand that Date.now is referential transparent.

I would like to read your idea. Thanks.


EDIT1

Q What is (functional) reactive programming?

Conal Elliott @Conal also explains functional reactive programming (FRP).

He is one of the earliest who develops FRP and explains this as follows:

  • FRPs are “data types that represent a value” over time

  • Dynamic / changing values ​​(that is, "over time" values) are first-class values ​​per se.

At this point FRP

  • Date can be considered as the value of the first class with time, which is an immutable object on the time axis.

  • .now is a property / function for addressing the "current time" in Date

    Therefore, Date.time returns an immutable and referential transparent value that represents our "current time".


EDIT2

(in JavaScript)

reference opaque function

 let a = 1; let f = () => (a); 

Function:f input Function:f - no; the output of Function:f depends on a , which depends on the context outside f

referential transparent function

 let t = Date.now(); let f = (Date) => (Date.now()); 

Although the Date value resides in our physical world, Date can be thought of as the immutable value of the first FRP class “over time”.

Since Date , which refers to any programming context, is identical, we can usually implicitly omit Date as an input value and simply as

 let f = () => (Date.now()); 

EDIT3

Actually, I emailed Conal Elliott @Conal, who is one of the earliest FRP developers. He kindly answered and told me there a similar question.

How can a time function exist in functional programming?

Question:

So my question is: can a time function (which returns the current time) in functional programming?

  • If so, how can it exist? Does this not violate the principle of functional programming? This especially violates referential transparency, which is one of the properties of functional programming (if I understand it correctly).

  • Or, if not, how do you know the current time in functional programming?

and answer from Conal Elliott @Conal on stackoverflow:

Yes, it is possible for a pure function to return time if it gave that time as a parameter. Different time arguments, different time results. Then we form other time functions and combine them with a simple dictionary of functions (-of-time) -transformation (higher orders). Since this approach does not have a status, time here can be continuous (independent of resolution) rather than discrete, which significantly increases modularity. This intuition is the foundation of functional reactive programming (FRP).


Edit4 My thanks for @Roman Sausarnes answer.

Please allow me to introduce my perspective for functional programming and FRP.

First of all, I think that programming is mainly related to mathematics and functional programming is pursuing this aspect. On the other hand, imperative programming is a way of describing how a machine works, which is not necessarily mathematics.

Purely functional programming such as Haskel is hardly handled by state or IO, and I think the whole problem comes from time.

"state" or "time" is a very subjective entity for us, man. We naturally, “time” flows or passes, and the “state” changes Naive realism .

I think that naive realism for “time” is a fundamental danger and cause for all the confusion in the programmer community, and very few discuss this aspect. In modern physics, or even from Newtonian physics, we process time in a pure mathematical way, so if we look at our world in physics, nothing should be difficult to relate to our world using pure mathematical functional programming.

So, I look through our world / universe unchanged, like a previously recorded DVD, and only our subjective opinion is changeable, including “time” or “state”.

In programming, the only connection between an unchanging Universe and our changing subjective experience is an “event”. A pure functional programming language such as Haskell basically lacks this view, although some astute researchers, including Cornell Elliott, continue with FRP, but most still believe that the FRP method is still imperfect or difficult to use, and many of them consider a volatile state for granted.

Naturally, FRP is the only intellectual solution, and especially Cornel Elliott, as the founder, used this philosophical perspective and declared the first value of the class "over time" . Perhaps, unfortunately, many programmers will not understand what he really meant, since they were trapped by Naïve realism, and it is difficult to understand the “time” philosophically, or physically immutable essence.

So, if they are discussing “ pure functional ” or “ referential transparency ” for the benefit of mathematical integrity / consistency, for me, “Date.now” is a natural referential transparent as part of pure functional programming, simply because “Date.time” gets access to a certain point, the unchanging timeline of an unchanging universe.


So, what about referential transparency in denotational semantics like @Reddy or @Roman Sausarnes, for example, is disappointing?

An overview of overview transparency in FP, especially in the Haskell community, deals with mathematical integrity / consistency.

Of course, perhaps I could follow the updated definition of “referential transparency” of the Haskell community, and in practice, judging by the fact that the code is mathematically inconsistent, if we judge this to be not referentially transparent, correct?

Actually, again,

How can a time function exist in functional programming?

The programmer questioned the following:

So my question is: can a time function (which returns the current time) in functional programming?

  • If so, how can it exist? Does this not violate the principle of functional programming? This especially violates referential transparency, which is one of the properties of functional programming (if I understand it correctly).

  • Or, if not, how do you know the current time in functional programming?


Consensus

violate the principle of functional programming

= violates referential transparency, which is one of the properties of functional programming

= Mathematically Inconsistent !!


This is our common perception, right?

In this question, many answered that the “function returning the current time” is not referenced, especially in the definition of “referential transparency” of the Haskell community, and many mentioned mathematical consistency.

However, only a few responded that the "function returning the current time" is referential transparent, and one of the answers is from the point of view of FRP from Conal Elliott @Conal.

IMO, FRP, the prospect of processing a stream over time as an indisputable first-class value “over time” is the correct manner with a mathematical principle, such as physics, as I mentioned above.

Then how did it turn out that the Date.now/ function "returning the current time" became reference opaque in the Haskell context?

Well, the only explanation that I can think of is the updated definition of "link transparency" of the Haskell community, somewhat wrong.

Event Driven and Mathematical Integrity / Consistency

I mentioned: in programming, the only connection between an unchanging Universe and our changing subjective experience is “event” or “event”.

Functional programming is evaluated taking into account events, on the other hand, imperative programming is evaluated by steps / routine of work in the machine described in the code.

Date.now depends on the event, and in principle the event is unknown to the code context.

So, does the event control the destruction of mathematical integrity / consistency? Absolutely not.

Matching syntax for value - index (index finger)

CS Pierce introduced the term "index" to suggest the idea of ​​pointing (as in the "index finger") . ⟦I⟧, [[here]], [[now]] etc.

Perhaps this is a mathematically identical concept of "Monad", "functor" in Haskell. In denotational semantics, even in Haskell [[now]] as the "index finger" is clear.

Index (index finger) is subjective and therefore event driven

[[I]], [[here]], [[now]], etc. is subjective and, again, in programming, the only connection between the immutable objective universe and our volatile subjective experience is an “event” or “event-driven event”

Therefore, as long as [[now]] is associated with the announcement of the event, "Event-Driven Programming," I think that subjective (context-sensitive) mathematical inconsistency does not occur.


Edit5

@ Bergi gave me a great comment:

Yes, Date.now , an external value, is referentially transparent. It always means "current time."

But Date.now() not, it is a function call returning different numbers depending on an external state. The problem with the reference transparent “current time concept” is that we cannot calculate anything with it.

@KenOKABE: This seems to be the same case as Date.now() . The problem is that this does not mean the current time at the same time, but at different times - the program takes time to execute, and this is what makes it unclean.

Of course, we could create a link-transparent function Date.now / getter, which always returns the start time of the program (as if the execution of the program was immediate), but this is not how Date.now()/Date.Now works. They depend on the status of the program. - Bergi

I think we need to discuss this.

Date.now , an external value, is referentially transparent.

[[Date.now]], as I mention in # Edit4, the index (index finger), which is subjective, but as long as it remains in the index domain (without performing / evaluating), it is referentially transparent , about which we agreed.

However, @Bergi offers Date.now() (with execution / evaluation) returns "different values" at different times and which is no longer referentially transparent. That we did not agree.

I think this problem, which he showed, of course, but exists only in imperative programming:

 console.log(Date.now()); //some numeric for 2016/05/18 xx:xx:xx .... console.log(Date.now()); //different numeric for 2016/05/18 xx:xx:xx .... 

In this case, Date.now() not referentially transparent, I agree.

However, in the paradigm of functional programming / declarative programming, we will never write as described above. We must write this:

 const f = () => (Date.now()); 

and this f is evaluated in some “event driven” context . This is how functional programming code behaves.

Yes, this code is identical

 const f = Date.now; 

Therefore, in the paradigm of functional programming / declarative programming, Date.now or Date.now() (with execution / evaluation) there never arises the problem of returning "different values" at different times.

So, again, as I mentioned in EDIT4 , as long as [[now]] is associated with the announcement of the event, "Event- Driven Programming," a subjective (context-dependent) mathematical inconsistency never occurs, I think.

+3
computer-science functional-programming frp
source share
1 answer

Ok, I'm going to make a punch. I am not an expert on this, but I spent some time thinking about @UdayReddy's answers to this question that you contacted, and I think that my head wrapped around it.

Referential transparency in analytic philosophy

I think you need to start when Mr. Reddy answered another question. Mr. Reddy wrote:

The term "referent" is used in analytical philosophy to talk about what is related to expression. This is roughly the same as what we mean by “meaning” or “designation” in the semantics of a programming language.

Pay attention to the use of the word "designation". Programming languages ​​have syntax or grammar, but they also have semantics or meaning. Denotational semantics is the practice of translating the syntax of a language into its mathematical meaning.

As far as I can tell, denotational semantics are not widespread, although they are one of the most powerful tools for understanding, designing, and analyzing computer programs. I have to spend some time on this to lay the foundation for answering your question.

Denotational semantics: matching syntax with value

The idea of ​​denotational semantics is that each syntactic element in a computer language has a corresponding mathematical meaning or semantics. Denotational semantics are an explicit comparison between syntax and semantics. Take the syntax number 1 . You can match it with its mathematical value, which is just the mathematical number 1 . A semantic function might look like this:

 syntax ↓ ⟦1⟧ ∷ One ↑ semantics 

Sometimes double square brackets are used to mean “value,” in which case the number 1 on the semantic side is indicated as One . These are just tools to indicate when we are talking about semantics and when we are talking about syntax. You can read this function, which means: "The value of the syntax character 1 is the number One ."

The example I used above looks trivial. Of course, 1 means One . What else would that mean? However, this is not necessary. You can do it:

 ⟦1⟧ ∷ Four 

That would be stupid, and no one would use such a stupid language, but it would still be the right language. But the fact is that denotational semantics allows us to talk about the mathematical meaning of the programs we write. Here we denote a function that squares the integer x using lambda notation:

 ⟦square x⟧ ∷ λx → x² 

Now we can move on and talk about referential transparency.

Link transparency is the value

Let me answer Mr. Udd again. He's writing:

A context in a sentence is “referentially transparent” if replacing a term in this context with another term that refers to the same object does not change the meaning.

Compare this to the answer you get when you ask the average programmer what link referential means. Usually they say something like the answer above:

Referential transparency, a term commonly used in functional programming, means that given the function and input value, you always get the same result. That is, there is no external state used in the function.

, .

, :

 var x = 0 func changeX() -> Int { x += 1 return x } 

- , . , , , . . .

, - .

Why? .

. , . - , s , s' , , , :

 ⟦changeX⟧ ∷ λs → (s → s') 

It is right? . , , . , , , , . What for? . -, , . , , changeX , , , .

, Date.now ?

. , , , Javascript. . ? What does it mean? ?

, ! , ( - , , ). , 20 , . .

Haskell . . , ghci, , , , , , . , , . , .

, . ( .) FPers, , . , Haskell, . , , , , , , .

, FPer , , . . , , , , , , . , , ( ), . ?

, . , , . , , Date.now . ? , , , . , , .

, , , , - - " " , , , .

+4
source share

All Articles