1

I am currently working on a "plugable", augmented run-time type system for Javascript to facilitate type-directed functional programming in untyped environments. It is essentially based on Proxy virtualization. Proxies are great, though they are not always transparent:

const xs = [1,2,3];
const xs_ = new Proxy(xs, {});
const s = new WeakSet([xs]);

s.has(xs); // true
s.has(xs_); // false

The same object may appear with and without a Proxy contract during run-time. Since Javascript defines object identity by reference, proxies are opaque when it comes to comparison of objects with their contracted counterparts.

As a result I'd lose WeakMap/WeakSet and the === operator, when working with proxies along with object types. Apart from that I would have to compare their properties recursively, which may be a rather expensive operation.

Is there a straightforward workaround in vanilla Javascript to keep proxies transparent?


Since I guess the answer to my question is no, here is an additional, more general one:

With purely functional languages the identity of data structures seem to be defined by state rather than by reference. While I know that deep cloning doesn't matter much in the presence of persistent data structures / structural sharing, recursive value comparison seems to be as expensive as with ordinary Javascript.

How do purely functional languages reduce the complexity of extensive data structure comparison?

  • Is inquiry how to determine if an object is a `Proxy`? – guest271314 Aug 05 '17 at 18:46
  • Have you looked at immutable.js ? – Karthik Aug 05 '17 at 18:54
  • @ftor Not certain what you are trying to achieve? What is requirement? – guest271314 Aug 05 '17 at 18:55
  • functional languages use a number of techniques, e.g. immutable data can reduce value comparisons to pointer comparisons.. – thebjorn Aug 05 '17 at 19:00
  • @thebjorn Hm, pointers aren't particularly functional. What do you exactly mean? –  Aug 05 '17 at 19:03
  • @ftor Are you trying to create an object that a) is unique; singular within a scope; b) cannot be changed? – guest271314 Aug 05 '17 at 19:08
  • The implementation of functional languages need not be functional, indeed sooner or later it has to run on hardware.. The idea is that if a data structure cannot be changed after creation, and this datastructure is stored in memory, then all variables containing that value can point to the same address and equality testing is trivial. – thebjorn Aug 05 '17 at 19:08
  • @thebjorn So you mean that e.g. in Haskell, two immutable lists equal in their values share the same memory address and hence a value comparison is just a comparison of references behind the scene? That would be terrible since I am not able to reproduce such behavior in Javascript. –  Aug 05 '17 at 19:13
  • A workaround would be New Map([xs.length,xs]); if xs.length is a random value... – Jonas Wilms Aug 05 '17 at 19:16
  • 1
    I'm not saying anything at all about Haskell (since I haven't looked at its implementation). Have you looked at http://matthias-keil.de/papers/techrep2015-proxy.pdf (sounds similar to what you're attempting). If you haven't read https://www.microsoft.com/en-us/research/publication/the-implementation-of-functional-programming-languages/ I would suggest starting there.. – thebjorn Aug 05 '17 at 19:16
  • 1
    They were working on [something for JS for that matter](https://github.com/nikomatsakis/typed-objects-explainer/blob/master/valuetypes.md) but that became inactive and I have no idea what's the progress like. – MinusFour Aug 05 '17 at 19:17
  • *How do purely functional languages reduce the complexity of extensive data structure comparison?* -> hashtables? – Jonas Wilms Aug 05 '17 at 19:18

1 Answers1

1

The same object may appear with and without a Proxy contract during run-time.

Just avoid that wherever you can.

With purely functional languages the identity of data structures seem to be defined by state rather than by reference.

Purely functional data structures have no state, they only have contents :-)

And unlike in OOP, there's no concept of "identity" - there's only equality, which you usually have to define yourself (like the Eq typeclass in Haskell, though the compiler can automatically derive an implementation of a structural equality algorithm).

How do purely functional languages reduce the complexity of extensive data structure comparison?

Hash consing is a popular approach - if the structures are constructed from the same elements, they can share their representation in memory. Two values that point to the same memory location are obviously the same, for everything else the default check needs to be done.

Some languages don't do this because of rather unpredictable performance, other languages (not purely functional ones) allow the programmers to implement hash consing using side effects for their own data structures.

Apart from that I would have to compare their properties recursively, which may be a rather expensive operation.

Yes. Comparisons are not cheap.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @guest271314 I'm confident in ftor's ability to implement my advise, yes. – Bergi Aug 05 '17 at 21:31
  • @Bergi Yes, the _state_ term was a poor choice. I like the idea that equality is a concept that provides something similar to identity. This is of course a bit misleading, because identity cannot be shared. Anyway, although you could clear up the issue, it remains the critical point in my project. I think I have to take the plunge and just try it out in practice... Thanks for your time - again! –  Aug 05 '17 at 21:57
  • 1
    @guest271314 just try not to get suspended again ^_^ – Mulan Aug 07 '17 at 08:14