Fusion Laws?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Fusion Laws?

Michael Haufe
Does the JavaScript Engine utilize anything similar to the fusion laws for methods as described in a number of academic papers in the functional programming community[1][2]?

For example:

xs.map(f).map(g) <==> xs.map(g ∘ f)


[1] <http://www.cs.nott.ac.uk/~pszgmh/fold.pdf>
[2] <https://www.cs.ox.ac.uk/ralf.hinze/publications/IFL10.pdf>
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Boris Zbarsky
On 3/30/16 5:02 PM, Michael Haufe wrote:
> For example:
>
> xs.map(f).map(g) <==> xs.map(g ∘ f)

This transformation is only valid if f and g can be proved to have no
side-effects, yes?

-Boris
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Michael Haufe
On Wednesday, March 30, 2016 at 4:48:36 PM UTC-5, Boris Zbarsky wrote:
> On 3/30/16 5:02 PM, Michael Haufe wrote:
> > For example:
> >
> > xs.map(f).map(g) <==> xs.map(g ∘ f)
>
> This transformation is only valid if f and g can be proved to have no
> side-effects, yes?

In general yes. The exceptions are of course idempotent ones like dom calls which are constant after creation (window.self perhaps?), and innocuous side-effects that are never used:

xs.map(function(x){
var d = Date.now(); //side-effect
var r = Math.random(); //side-effect
return x
});
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Boris Zbarsky
On 3/30/16 6:17 PM, Michael Haufe wrote:
> In general yes. The exceptions are of course idempotent ones like dom calls which are constant after creation (window.self perhaps?), and innocuous side-effects that are never used:
>
> xs.map(function(x){
> var d = Date.now(); //side-effect
> var r = Math.random(); //side-effect
> return x
> });

The problem is that it's hard to prove functions are side-effect-free in
JS, in even this weak sense.  For example, your function there might do
absolutely anything, depending on what's been done to the Date and Math
properties on the global.  :(

-Boris

_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Michael Haufe
On Wednesday, March 30, 2016 at 5:26:17 PM UTC-5, Boris Zbarsky wrote:

> On 3/30/16 6:17 PM, Michael Haufe wrote:
> > In general yes. The exceptions are of course idempotent ones like dom calls which are constant after creation (window.self perhaps?), and innocuous side-effects that are never used:
> >
> > xs.map(function(x){
> > var d = Date.now(); //side-effect
> > var r = Math.random(); //side-effect
> > return x
> > });
>
> The problem is that it's hard to prove functions are side-effect-free in
> JS, in even this weak sense.  For example, your function there might do
> absolutely anything, depending on what's been done to the Date and Math
> properties on the global.  :(

It's a shame there isn't an Attribute Grammar definition for the semantics of ECMAScript, it might make this a bit more tractable...

I found a related thread on es-discuss:
<https://mail.mozilla.org/pipermail/es-discuss/2012-November/thread.html#26657>

I think Brendan said it best. To paraphrase: we'd be eaten by a grue if we tried.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Nicolas B. Pierron
In reply to this post by Michael Haufe
On 03/30/2016 09:02 PM, Michael Haufe wrote:
> Does the JavaScript Engine utilize anything similar to the fusion laws for methods as described in a number of academic papers in the functional programming community[1][2]?
>
> For example:
>
> xs.map(f).map(g) <==> xs.map(g ∘ f)

I would love to have our Jit implement deforestation optimizations, but the
problem is worse than what you might expect.

This would hold only if you can prove that you have no side effects, both in
the functions, and also in the data of xs.

For example:

xs.map((x, i) => { if (i % 2 == 0) return x + 1; return x; })
   .map((x, i) => { if (i % 2 == 1) return x + 1; return x; })

This sounds like this would be side-effect free, but if two of the value
contained in the array are objects, with a coercion function that I would
not name, then you can no longer guarantee that the order of the side-effect
is maintained.

Then if we were to optimize this in the Jit, we might have a bigger problem
being that IonMonkey has to resume to Baseline when we face unexpected
cases.  These unexpected cases could be things from a mismatch type, or a
shrinking GC.  These implies that we would have to reconstruct the temporary
array and discard half of the result we computed so far.

Typed and Pure languages have good guarantees.  Unfortunately, JavaScript
has none of that.

--
Nicolas B. Pierron
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Jason Orendorff-2
In reply to this post by Michael Haufe
On Wed, Mar 30, 2016 at 10:32 PM, Michael Haufe <
[hidden email]> wrote:

> It's a shame there isn't an Attribute Grammar definition for the semantics
> of ECMAScript, it might make this a bit more tractable...
>

Believe it or not, the specification itself is all attribute grammars,
including the parts that define Evaluation. It's not written in a real
formal language, and it's a weird mash-up of declarative and procedural
style, but it's close enough to attribute grammars that you can see exactly
how you would go about formalizing it.

The specification technique isn't the problem. It's the semantics being
specified. They are the semantics of a dynamic language. Reasoning about
such code is an exercise in hand-waving.

-j
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Michael Haufe
In reply to this post by Michael Haufe
On Thursday, March 31, 2016 at 1:22:14 PM UTC-5, Jason Orendorff wrote:

> On Wed, Mar 30, 2016 at 10:32 PM, Michael Haufe wrote:
>
> > It's a shame there isn't an Attribute Grammar definition for the semantics
> > of ECMAScript, it might make this a bit more tractable...
> >
>
> Believe it or not, the specification itself is all attribute grammars,
> including the parts that define Evaluation. It's not written in a real
> formal language, and it's a weird mash-up of declarative and procedural
> style, but it's close enough to attribute grammars that you can see exactly
> how you would go about formalizing it.

Yes, the formal language was what I was referring to. It's a fair point. My off-side comment was more towards being able to poke at those semantics as easily as one might poke at a BNF grammar and see what goes wrong when it's run through a verifier (like specifying isTailPosition or isPure from earlier in the thread).

> The specification technique isn't the problem. It's the semantics being
> specified. They are the semantics of a dynamic language. Reasoning about
> such code is an exercise in hand-waving.

Perhaps I'm misreading your statement here, but are you saying that the current spec is hand-waving the runtime semantics of the language, or are you instead saying that one can't formalize the semantics of a dynamic language?
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Jason Orendorff-2
On Thu, Mar 31, 2016 at 1:45 PM, Michael Haufe <
[hidden email]> wrote:

> On Thursday, March 31, 2016 at 1:22:14 PM UTC-5, Jason Orendorff wrote:
> > The specification technique isn't the problem. It's the semantics being
> > specified. They are the semantics of a dynamic language. Reasoning about
> > such code is an exercise in hand-waving.
>
> Perhaps I'm misreading your statement here, but are you saying that the
> current spec is hand-waving the runtime semantics of the language, or are
> you instead saying that one can't formalize the semantics of a dynamic
> language?
>

No and no. (The current spec is very detailed. The semantics of JS could
certainly be formalized; I'm not sure you'd learn anything new though.)

I'm saying that the way JS programmers reason about their code---when we
reason about it at all---hand-waves away all the complicating factors that
JS engines are forced to treat seriously. We take for granted that many
possible things that *could* theoretically happen, won't happen. We assume
(rather than prove or propagate) a bunch of antecedents like "well, x and y
are both gonna be numbers here, and neither one can be NaN, and this
addition won't overflow, and this object won't be a proxy..."

JS implementations (and the spec itself) don't have that luxury.

-j
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine
Reply | Threaded
Open this post in threaded view
|

Re: Fusion Laws?

Michael Haufe
In reply to this post by Michael Haufe
On Thursday, March 31, 2016 at 2:45:43 PM UTC-5, Jason Orendorff wrote:
> On Thu, Mar 31, 2016 at 1:45 PM, Michael Haufe
>
> > Perhaps I'm misreading your statement here, but are you saying that the
> > current spec is hand-waving the runtime semantics of the language, or are
> > you instead saying that one can't formalize the semantics of a dynamic
> > language?
>
> No and no. (The current spec is very detailed. The semantics of JS could
> certainly be formalized; I'm not sure you'd learn anything new though.)

Understood. Minor spec issues might be caught early, but from what I've seen in es-discuss, early implementations seem to do plenty well in catching these anyway.

> I'm saying that the way JS programmers reason about their code---when we
> reason about it at all---hand-waves away all the complicating factors that
> JS engines are forced to treat seriously. We take for granted that many
> possible things that *could* theoretically happen, won't happen. We assume
> (rather than prove or propagate) a bunch of antecedents like "well, x and y
> are both gonna be numbers here, and neither one can be NaN, and this
> addition won't overflow, and this object won't be a proxy..."
>
> JS implementations (and the spec itself) don't have that luxury.

Understood.
_______________________________________________
dev-tech-js-engine mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-js-engine