3

DateTime.Now or Date.now is referential transparent?

This is one of the controversial topic in a functional programming article in Qiita.

First of all, we must be very careful since the word "referential transparent" is tricky word/concept in a sense, and a prominent discussion exists in

What is referential transparency?

The questioner states:

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

A very typical explanation but the idea that typically leads us misunderstanding is as follows: (#2 answer of the above page by @Brian R. Bondy )

Referential transparency, a term commonly used in functional programming, means that given a function and an input value, you will always receive the same output. That is to say there is no external state used in the function.

Typical claims I always have heard and thought wrong is like this:

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

given a function and an input value, you will always receive the same output.

therefore, Date.now is Not referential transparent!

I know some (functional) programmers firmly believe the above claim is trustworthy, however, #1 and #3 answer by @Uday Reddy explains as follows:

Any talk of "referential transparency" without understanding the distinction between L-values, R-values and other complex objects that populate the imperative programmer's conceptual universe is fundamentally mistaken.

The functional programmers' idea of referential transparency seems to differ from the standard notion in three ways:

  • Whereas the philosophers/logicians use terms like "reference", "denotation", "designatum" and "bedeutung" (Frege's German term), functional programmers use the term "value". (This is not entirely their doing. I notice that Landin, Strachey and their descendants also used the term "value" to talk about reference/denotation. It may be just a terminological simplification that Landin and Strachey introduced, but it seems to make a big difference when used in a naive way.)

  • Functional programmers seem to believe that these "values" exist within the programming language, not outside. In doing this, they differ from both the philosophers and the programming language semanticists.

  • They seem to believe that these "values" are supposed to be obtained by evaluation.

Come to think of it, "external state" is also tricky word/concept.

Referential transparency, a term commonly used in functional programming, means that given a function and an input value, you will always receive the same output. That is to say there is no external state used in the function.

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

If we call "current time" is "external state", how about "mouse event"??

"mouse event" is not a state that should be managed by programming context, it's rather an external event.

given a function and an input value, you will always receive the same output.

So, we can understand as follows:

"current time" is neither "input value" nor "external value" nor "external state" and Date.now always returns the same output corresponds to the on-going event "current time".

If one still insists or want to call "current time" as a "value", again,

  • Functional programmers seem to believe that these "values" exist within the programming language, not outside. In doing this, they differ from both the philosophers and the programming language semanticists.

The value of "current time" never exists within the programming language, but only outside, and the value of "current time" outside obviously updates via not programming context but the real-world's time-flow.

Therefore, I understand Date.now is referential transparent.

I'd like to read your idea. Thanks.


EDIT1

In What is (functional) reactive programming?

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

He is the one of the earliest who develops FRP, and explaines like this:

  • FRP is about - “datatypes that represent a value ‘over time’ “

  • Dynamic/evolving values (i.e., values “over time”) are first class values in themselves.

In this FRP perspective,

  • Date can be seen as a first-class value "over time" that is immutable object on the Time axis.

  • .now is a property/function to address "the current time" within the Date

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


EDIT2

(in JavaScript)

referential intransparent function

let a = 1;

let f = () => (a);

input of Function:f is none; output of Function:f depends on a that depends on a context outside f

referential transparent function

let t = Date.now();

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

Although, Date value resides in our physical world, Date can be seen as an immutable FRP first-class value "over time".

Since Date referred from any programming context is identical, we usually implicitly may omit Date as input value and simply like

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

EDIT3

Actually, I emailed to Conal Elliott @Conal who is one of the earliest developer of FRP. He kindly replied and informed me there's a similar question here.

How can a time function exist in functional programming?

The questioner states:

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

  • If yes, then how can it exist? Does it not violate the principle of functional programming? It particularly violates referential transparency which is one of the property of functional programming (if I correctly understand it).

  • Or if no, then how can one know the current time in functional programming?

and, the answer by Conal Elliott @Conal in stackoverflow:

Yes, it's possible for a pure function to return the time, if it's given that time as a parameter. Different time argument, different time result. Then form other functions of time as well and combine them with a simple vocabulary of function(-of-time)-transforming (higher-order) functions. Since the approach is stateless, time here can be continuous (resolution-independent) rather than discrete, greatly boosting modularity. This intuition is the basis of Functional Reactive Programming (FRP).


Edit4 My appreciation for the answer by @Roman Sausarnes .

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

First of all, I think programming is fundamentally all about mathematics, and functional programming pursuits that aspect. On the other hand, imperative programming is a way to describe steps of machine operation, that is not necessarily mathematics.

Pure functional programming like Haskel has some difficulty to handle "state" or IO, and I think the whole problem come from "time".

"state" or "time" is pretty much subjective entity to us, human. We naturally believe "time" is flowing or passing, and "state" is changing, that is Naïve realism.

I think Naïve realism for "time" is fundamental hazard and reason for all the confusion in programming community and very few discuss this aspect. In modern physics, or even from Newton Physics we treat time in pure mathematical way, so if we overview our world in physics way, nothing should be difficult to treat our world with pure mathematical functional programming.

So, I overview our world/universe is immutable like pre-recorded DVD, and only our subjective view is mutable, including "time" or "state".

In programming, the only connection between the immutable universe and our mutable subjective experience is "event". Pure functional programming language such as Haskell, basically lacks this view, although some insightful researchers including Cornel Elliott proceed FRP, but the majority still think FRP method is still minor or hard to use andm many of them treat mutable state as a matter of course.

Naturally, FRP is the only smart solution and especially Cornel Elliott as a founder applied this philosophical perspective and declare - first class value "over time". Perhaps, unfortunately, many programmers would not understand what he really meant since they are trapped by Naïve realism, and find it difficult to view "time" is philosophically, or physically immutable entity.

So, if they discuss "pure functional" or "referential transparency" for the advantage of mathematical integrity/consistency, to me, "Date.now" is naturally referential transparent within pure functional programming, simply because "Date.time" access a certain point of immutable time-line of immutable universe.


So What About referential transparency in Denotational semantics like @Reddy or @Roman Sausarnes disucusses?

I overview referential transparency in FP, especially in Haskell community is all about mathematical integrity/consistency.

Sure, maybe I could follow the updated definition of "referential transparency" by Haskell community, and practically, we judge the code is mathematically inconsistent if we judge it's not referential transparent, correct?

Actually, again,

How can a time function exist in functional programming?

A programmer questioned as follows:

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

  • If yes, then how can it exist? Does it not violate the principle of functional programming? It particularly violates referential transparency which is one of the property of functional programming (if I correctly understand it).

  • Or if no, then how can one know the current time in functional programming?


Consensus

violate the principle of functional programming

= violates referential transparency which is one of the property of functional programming

= Mathematically inconsistent!!


This is our common perception, correct?

In this question, many answered that "function returning the current time" is Not referential transparent especially in the definition of "referential transparency" by Haskell community, and many mentioned it's about mathematical consistency.

However only a few answered that "function returning the current time" is referential transparent, and one of the answer is from FRP perspective by Conal Elliott @Conal.

IMO, FRP, a perspective to handle a time-stream as a first-class immutable value "over time" is a correct manner with mathematical principle like Physics as I mentioned above.

Then how come "Date.now"/"function returning the current time" became referential intransparent by Haskell context?

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

Event-driven & Mathematical integrity/consistency

I mentioned - In programming, the only connection between the immutable universe and our mutable subjective experience is "event" or "Event-driven".

Functional Programming is evaluated in Event-driven manner, on the other hand, Imperative Programming is evaluated by steps/routine of machine operation described in the code.

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

So, does event-driven destroy mathematical integrity/consistency? Absolutely not.

Mapping Syntax to Meaning - indexical(index finger)

C.S. Peirce introduced the term ‘indexical’ to suggest the idea of pointing (as in ‘index finger’). ⟦I⟧ ,[[here]],[[now]] ,etc.. 

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

Indexical(index finger) is subjective and so is Event-driven

[[I]] ,[[here]],[[now]] ,etc.. is subjective, and again, in programming, the only connection between the immutable objective universe and our mutable subjective experience is "event" or "Event-driven"

Therefore, as long as [[now]] is bind to event declaration of "Event-driven" Programming, the subjective(context dependent) mathematical inconsistency never occurs, I think.


Edit5

@Bergi gave me an excellent comment:

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

But Date.now() is not, it's a function call returning different numbers depending on external state. The problem with the referentially transparent "concept of current time" is that we cannot compute anything with it.

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

Sure, we could devise a referentially transparent Date.now function/getter that always returns the time of the start of the program (as if a program execution was immediate), but that's not how Date.now()/Date.Now work. They depend on the execution state of the program. – Bergi

I think we need to discuss it.

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

[[Date.now]] is as I mention in #Edit4, an indexical(index finger) that is subjective, but as long as it remains in indexical domain (without an execution/evaluation), it is referentially transparent, that we agreed on.

However, @Bergi suggests Date.now() (with an execution/evaluation) returns "different values" at different times, and which is no longer referentially transparent. That we have not agreed on.

I think this problem he has shown surely but only exist 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() is not referentially transparent, I agree.

However, in Functional Programming/Declarative Programming paradigm, we would never write like the above. We must write this:

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

and, this f is evaluated in some "event-driven" context. That is how a Functional-programming code behaves.

Yes, this code is identical to

const f = Date.now;

Therefore, in Functional Programming/Declarative Programming paradigm, Date.now or Date.now() (with an execution/evaluation) never has problem to return "different values" at different times.

So, again, as I mentioned in EDIT4, as long as [[now]] is bind to event declaration of "Event-driven" Programming, the subjective(context dependent) mathematical inconsistency never occurs, I think.

Community
  • 1
  • 1
  • Trivial answer: If in Haskell, ``Date.now`` is in the IO Monad, it is not referentially transparent. ;) The Real time clock is a piece of hardware and a register read by that function. As such it is IO and the value obtained and observation of world state. Why? Because functional programs have disassociated from their physical form, aka hardware (which is a known mental disease). – BitTickler May 16 '16 at 02:14
  • Thanks for your comment :), and yes, I know Haskell `Date.now` is categorized so, and the article comment in http://stackoverflow.com/questions/210835/what-is-referential-transparency also somewhat criticizing that - favourite syntax and dressed up with a buzz word like "monad"-. Is functional programming really disassociated from physical fomrs? Especially with FRP.. –  May 16 '16 at 02:34
  • Could you give me an example of a function that is NOT referential transparent, and explain why? – ycsun May 16 '16 at 02:40
  • @ycsun I don't know exactly what you ask, but I add Edit2. thx. –  May 16 '16 at 03:08
  • Wrapping one function into another does not change the semantics. Just the name. Hence, the example you give does not appear to pinpoint the situation in my eyes. – BitTickler May 16 '16 at 05:07
  • Well, I agree, but at least, I gave an example of a function that is not referential transparent. I don't know what @ycsun requires, and just in case, I aligned the date.now. –  May 16 '16 at 06:00
  • Sorry but I don't get it. Why do you think `() => (Date.now())` is referential transparent, while `() => (a)` is not? – ycsun May 16 '16 at 06:58
  • As I mentioned in the Edit2 section, strictly speaking,` (Date) => (Date.now())` is referential transparent, and practically speaking, since `Date` is in principle identical in any context, I add extra mention on practical use. However, obviously, this is out of topic, and I don't know what you demand on me. –  May 16 '16 at 09:13
  • I don't get it because it seems to me that both `() => (a)` and `() => (Date.now())` (or `(Date) => (Date.now())`) return values that depend on some external states. If the latter is referential transparent, why not the former? – ycsun May 16 '16 at 09:21
  • (the former) `() => (a)` depends on some external states that never be known to the context of the function, so referential intransparent. `(Date) => (Date.now())` is referential transparent, I think, but you insist it's not because `.now()` depends on some external states, and again, I think it's Not external state that is unknown to the function because `Date.now` always returns the same output corresponds to the on-going event "current time" that is also shared even by the internal context of the function. `Date` is not evaluated within programming code, and shared by all context. –  May 16 '16 at 10:11
  • The old fashioned word for the modern ``referential integrity`` is ``reentrant``. Here a snippet for non-reentrant code: ``int x = 5; int a() { x++; return x;}`` – BitTickler May 16 '16 at 18:02
  • Consider this: `D = { a: 1 }`, and `f = (D) => (D.a)`. Is `f` referential transparent? If `(Date) => (Date.now())` is referential transparent, `(D) => (D.a)` should also be referential transparent, right? Then `() => (D.a)` should be referential transparent as well. Or not? Am I missing something here? – ycsun May 17 '16 at 00:47
  • Yes, `Date.now`, the external value, is referentially transparent. It always means "current time". But `Date.now()` is not, it's a function call returning different numbers depending on external state. The problem with the referentially transparent "concept of current time" is that we cannot compute anything with it. – Bergi May 17 '16 at 02:00
  • Thanks all for comments:) @Bergi what would you say C# `DateTime.Now` Property then? https://msdn.microsoft.com/en-us/library/system.datetime.now(v=vs.110).aspx I think it always means "current time" at the same time, the evaluation result. –  May 17 '16 at 04:53
  • @ycsun `f = (D) => (D.a)` transparent, sure. `() => (D.a)` intransparent. the function knows nothing of `D` –  May 17 '16 at 07:53
  • As interesting as this question is, it does not follow the "why does my code not work" format of SO. I would suggest this be migrated to Computer Science. – Aron May 17 '16 at 10:26
  • Related to many previous quesions/discussions in this community. Do you think it's productive behaviour to move things around? I believe not. –  May 17 '16 at 10:31
  • @KenOKABE: Seems to be the same case as `Date.now()`. The problem is that it does not mean the current time *at the same time*, but at different times - a program takes time to execute, and this is what makes it impure. Sure, we could devise a referentially transparent `Date.now` function/getter that always returns the time of the start of the program (as if a program execution was immediate), but that's not how `Date.now()`/`Date.Now` work. They depend on the execution state of the program. – Bergi May 17 '16 at 16:32
  • @Bergi Thanks for your comment :) I think you gave us an excellent point, and I replied in #Edit5. –  May 17 '16 at 22:26
  • @KenOKABE: Well, if you consider that "*f (==`Date.now`) is evaluated in some "event-driven" context*", then that's exactly what the `IO` monad does in purely functional languages. A function can be referentially transparent even if it uses context, as long as that context is passed in explicitly. – Bergi May 17 '16 at 22:44
  • that's exactly what the IO monad does in purely functional languages @Bergi < Exactly. So, what's the point for us/ all the programming community to mention `Date.now` or `Date.now()` or "a time-function" in FP is referential intransparent? That's my qustion. –  May 17 '16 at 22:53
  • 1
    Because `Date.now` in JS is not [`getCurrentTime`](http://hackage.haskell.org/package/time-1.6.0.1/docs/Data-Time-Clock.html#v:getCurrentTime), but rather [`unsafePerformIO getCurrentTime`](http://hackage.haskell.org/package/base-4.8.2.0/docs/System-IO-Unsafe.html#v:unsafePerformIO). It is referentially intransparent when it does use external state (context) that was not expliclty modelled in. – Bergi May 17 '16 at 22:58
  • Well, for me, it's very hard to understand why obtaining the current time in a certain context is "unsafe". Can you give us some example in what case, `Date.now` or `Date.now()` become "unsafe" in FP/declarative paradigm not in Imperative Programming? –  May 17 '16 at 23:09
  • I have doubts about the last paragraph (edit 4). Once your program concurrently tags event streams with time stamps derived from ``date.now()`` in multiple threads and then you do frp operations like merge on those event streams, the resulting stream has random order. If you then fold those streams, depending on folder function semantics, the result can differ. Much in contrast to an alternate program which tags the streams with the result of a refentrially transparent function (a value, which is always the same). – BitTickler May 20 '16 at 09:17
  • Assume a very heavy routine that uses a certain`date.now()`. The process can take up to a couple of hours, for instance. In this function semantics, `date.now()` obviously means `date.now()` at the some point, maybe invoked-time, or done-time. –  Jun 05 '16 at 07:54

1 Answers1

4

Okay, I'm going to take a stab at this. I'm not an expert on this stuff, but I've spent some time thinking about @UdayReddy's answers to this question that you linked to, and I think I've got my head wrapped around it.

Referential Transparency in Analytic Philosophy

I think you have to start where Mr. Reddy did in his answer to the other question. Mr. Reddy wrote:

The term "referent" is used in analytical philosophy to talk about the thing that an expression refers to. It is roughly the same as what we mean by "meaning" or "denotation" in programming language semantics.

Note the use of the word "denotation". Programming languages have a syntax, or grammar, but they also have a semantics, or meaning. Denotational semantics is the practice of translating a language's syntax to its mathematical meaning.

Denotational semantics, as far as I can tell, is not widely understood even though it is one of the most powerful tools around for understanding, designing, and reasoning about computer programs. I gotta spend a little time on it to lay the foundation for the answer to your question.

Denotational Semantics: Mapping Syntax to Meaning

The idea behind denotational semantics is that every syntactical element in a computer language has a corresponding mathematical meaning, or semantics. Denotational semantics is the explicit mapping between syntax and semantics. Take the syntactic numeral 1. You can map it to its mathematical meaning, which is just the mathematical number 1. The semantic function might look like this:

syntax
   ↓
  ⟦1⟧ ∷ One
        ↑ 
    semantics

Sometimes the double-square brackets are used to stand for "meaning", and in this case the number 1 on the semantic side is spelled out as One. Those are just tools for indicating when we are talking about semantics and when we are talking about syntax. You can read that function to mean, "The meaning of the syntactic symbol 1 is the number One."

The example that I used above looks trivial. Of course 1 means One. What else would it mean? It doesn't have to, however. You could do this:

⟦1⟧ ∷ Four

That would be dumb, and no-one would use such a dumb language, but it would be a valid language all the same. But the point is that denotational semantics allows us to be explicit about the mathematical meaning of the programs that we write. Here is a denotation for a function that squares the integer x using lambda notation:

⟦square x⟧ ∷ λx → x²

Now we can move on and talk about referential transparency.

Referential Transparency is About Meaning

Allow me to piggyback on Mr. Uday's answer again. He writes:

A context in a sentence is "referentially transparent" if replacing a term in that context by another term that refers to the same entity doesn't alter the meaning.

Compare that to the answer you get when you ask the average programmer what referential transparency means. They usually say something like the answer you quoted above:

Referential transparency, a term commonly used in functional programming, means that given a function and an input value, you will always receive the same output. That is to say there is no external state used in the function.

That answer defines referential transparency in terms of values and side effects, but it totally ignores meaning.

Here is a function that under the second definition is not referentially transparent:

var x = 0

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

It reads some external state, mutates it, and then returns the value. It takes no input, returns a different value every time you call it, and it relies on external state. Meh. Big deal.

Given a correct denotational semantics, it is still referentially transparent.

Why? Because you could replace it with another expression with the same semantic meaning.

Now, the semantics of that function is much more confusing. I don't know how to define it. It has something to do with state transformations, given a state s and a function that produces a new state s', the denotation might look something like this, though I have no idea if this is mathematically correct:

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

Is that right? I have don't have a clue. Strachey figured out the denotational semantics for imperative languages, but it is complicated and I don't understand it yet. By establishing the denotative semantics, however, he established that imperative languages are every bit as referentially transparent as functional languages. Why? Because the mathematical meaning can be precisely described. And once you know the precise mathematical meaning of something, you can replace it with any other term that has the same meaning. So even though I don't know what the true semantics of the changeX function is, I know that if I had another term with the same semantic meaning, I could swap one out for the other.

So What About Date.now?

I don't know anything about that function. I'm not even sure what language it is from, though I suspect it may be Javascript. But who cares. What is its denotational semantics? What does it mean? What could you insert in its place without changing the meaning of your program?

The ugly truth is, most of us don't have a clue! Denotational semantics isn't that widely used to begin with, and the denotational semantics of imperative programming languages is really complicated (at least for me - if you find it easy, I'd love to have you explain it to me). Take any imperative program consisting of more than about 20 lines of non-trivial code and tell me what its mathematical meaning is. I challenge you.

By contrast the denotational semantics of Haskell is pretty straightforward. I have very little knowledge of Haskell. I've never done any coding in it beyond messing around in the ghci, but what makes it so powerful is that the syntax tracks the semantics more closely than any other language that I know of. Being a pure, strict functional language, the semantics are right there on the surface of the syntax. The syntax is defined by the mathematical concepts that define the meaning.

In fact, the syntax and semantics are so closely related that functional programmers have begun to conflate the two. (I humbly submit this opinion and await the backlash.) That is why you get definitions of referential transparency from FPers that talk about values instead of meaning. In a language like Haskell, the two are almost indistinguishable. Since there is no mutable state and every function is a pure function, all you have to do is look at the value that is produced when the function is evaluated and you've basically determined its meaning.

It may also be that the new-age FPer's explanation of referential transparency is, in a way, more useful than the one that I summarized above. And that cannot be ignored. After all, if what I wrote above is correct then everything that has a denotational semantics is referentially transparent. There is no such thing as a non-referentially transparent function, because every function has a mathematical meaning (though it may be obscure and hard to define) and you could always replace it with another term with the same meaning. What good is that?

Well, it's good for one reason. It let's us know that we don't know jack about the mathematics behind what we do. Like I said above, I haven't a clue what the denotational semantics of Date.now is or what it means in a mathematical sense. Is it referentially transparent? Yeah, I'm sure that it is, since it could be replaced by another function with the same semantics. But I have no idea how to evaluate the semantics for that function, and therefore its referential transparency is of no use to me as a programmer.

So if there's one thing I've learned out of all of this, it is to focus a lot less on whether or not something meets some definition of "referential transparency" and a lot more on trying to make programs out of small, mathematically composable parts that have precise semantic meanings that even I can understand.

Community
  • 1
  • 1
Aaron Rasmussen
  • 12,331
  • 2
  • 38
  • 42
  • My appreciation for the answer by @Roman Sausarnes, I replied to you in #Edit4, thanks! –  May 17 '16 at 09:39
  • [Mapping Syntax to Meaning] C.S. Peirce introduced the term ‘indexical’ to suggest the idea of pointing (as in ‘index finger’). ⟦I⟧ ,[[here]],[[now]] ,etc.. Probably this is mathematically identical concept of "Monad", "functor" things in Haskell. In denotational semantics even in Haskell, [[now]] as the 'index finger' is clear. –  May 17 '16 at 21:29