caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] flambda for bytecode backend
@ 2018-01-12 18:36 Alexey Egorov
  2018-01-14 18:29 ` [Caml-list] <DKIM> " Pierre Chambart
  0 siblings, 1 reply; 5+ messages in thread
From: Alexey Egorov @ 2018-01-12 18:36 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 143 bytes --]

Hello,

I'm curious why flambda doesn't supported in bytecode compiler? It seems that js_of_ocaml can greatly benefit from such possibility.



[-- Attachment #2: Type: text/html, Size: 186 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] <DKIM> flambda for bytecode backend
  2018-01-12 18:36 [Caml-list] flambda for bytecode backend Alexey Egorov
@ 2018-01-14 18:29 ` Pierre Chambart
  2018-01-15 10:34   ` Leo White
  0 siblings, 1 reply; 5+ messages in thread
From: Pierre Chambart @ 2018-01-14 18:29 UTC (permalink / raw)
  To: Alexey Egorov, caml-list

On 12/01/2018 19:36, Alexey Egorov wrote:
> Hello,
>
> I'm curious why flambda doesn't supported in bytecode compiler? It
> seems that js_of_ocaml can greatly benefit from such possibility.
>
It has been considered, and in fact Grégoire Henry did a version of the
bytecode compiler after flambda. But it was discarded as it was not very
useful. It couldn't work with js_of_ocaml, and there was no obvious way
how to do it properly.

The reason being that flambda takes place after closure conversion.
Hence after flambda, functions takes an explicit environment value. But
on the other hand, js_of_ocaml represents OCaml functions directly as
javascript function since the closures are working correctly.

The best we could do (without doing some magic) would look like:

let f x =
  let y = x + 1 in
  let g z = z + y in
  g

compiled to:

function f_code(x, env) {
  let y = x + 1;
  function g_code(y, env) {
    return(z + env.y);
  }
  let g = { code : g_code, env : { y: y };
  return(g);
}

let f = { code : f_code, env : { } }

This would probably be far slower than what js_of_ocaml currently does.
-- 
Pierre



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] <DKIM> flambda for bytecode backend
  2018-01-14 18:29 ` [Caml-list] <DKIM> " Pierre Chambart
@ 2018-01-15 10:34   ` Leo White
  2018-01-15 10:51     ` Leandro Ostera
  2018-01-29 16:26     ` [Caml-list] OCaml <-> Computer Algebra Nicolas Ratier
  0 siblings, 2 replies; 5+ messages in thread
From: Leo White @ 2018-01-15 10:34 UTC (permalink / raw)
  To: caml-list

> The best we could do (without doing some magic) would look like:
> 
> let f x =
>   let y = x + 1 in
>   let g z = z + y in
>   g
> 
> compiled to:
> 
> function f_code(x, env) {
>   let y = x + 1;
>   function g_code(y, env) {
>     return(z + env.y);
>   }
>   let g = { code : g_code, env : { y: y };
>   return(g);
> }
> 
> let f = { code : f_code, env : { } }

Pierre is right about the reason we don't use flambda with
bytecode, although I don't think that is the best JavaScript we
could produce.

It would not be to difficult to undo closure conversion within
the function body itself. This still leaves references to the
closure outside of the function body.  These aren't shown in
Pierre's example but they are produced during inlining and are
the main thing that flambda uses which bytecode doesn't
support. For these we can take advantage of the fact that
JavaScript functions are also ordinary objects, attaching the
environment to the function as fields:

    function f(x) {
      let y = x + 1;
      function g(z) {
        return(z + y);
      }
      g.y = y;
      return(g);
    }

Of course, this could still have an adverse effect on how the JIT
of different browsers deals with these functions, so some
benchmarking would be needed to confirm the viability of this
approach.

Regards,

Leo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] <DKIM> flambda for bytecode backend
  2018-01-15 10:34   ` Leo White
@ 2018-01-15 10:51     ` Leandro Ostera
  2018-01-29 16:26     ` [Caml-list] OCaml <-> Computer Algebra Nicolas Ratier
  1 sibling, 0 replies; 5+ messages in thread
From: Leandro Ostera @ 2018-01-15 10:51 UTC (permalink / raw)
  To: Leo White; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 2078 bytes --]

You could also use the binding mechanism that js functions have to swap the
context they are ran in, although I’m not sure if it’s going to be faster
than the two options already provided above.

function f_unbound(x) { return (x + this.y); }

const env = { y: 1 };
const f = f_unbound.bind(env);
f(1) // 2
mån 15 jan. 2018 kl. 11:35 skrev Leo White <leo@lpw25.net>:

> > The best we could do (without doing some magic) would look like:
> >
> > let f x =
> >   let y = x + 1 in
> >   let g z = z + y in
> >   g
> >
> > compiled to:
> >
> > function f_code(x, env) {
> >   let y = x + 1;
> >   function g_code(y, env) {
> >     return(z + env.y);
> >   }
> >   let g = { code : g_code, env : { y: y };
> >   return(g);
> > }
> >
> > let f = { code : f_code, env : { } }
>
> Pierre is right about the reason we don't use flambda with
> bytecode, although I don't think that is the best JavaScript we
> could produce.
>
> It would not be to difficult to undo closure conversion within
> the function body itself. This still leaves references to the
> closure outside of the function body.  These aren't shown in
> Pierre's example but they are produced during inlining and are
> the main thing that flambda uses which bytecode doesn't
> support. For these we can take advantage of the fact that
> JavaScript functions are also ordinary objects, attaching the
> environment to the function as fields:
>
>     function f(x) {
>       let y = x + 1;
>       function g(z) {
>         return(z + y);
>       }
>       g.y = y;
>       return(g);
>     }
>
> Of course, this could still have an adverse effect on how the JIT
> of different browsers deals with these functions, so some
> benchmarking would be needed to confirm the viability of this
> approach.
>
> Regards,
>
> Leo
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 2795 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] OCaml <-> Computer Algebra
  2018-01-15 10:34   ` Leo White
  2018-01-15 10:51     ` Leandro Ostera
@ 2018-01-29 16:26     ` Nicolas Ratier
  1 sibling, 0 replies; 5+ messages in thread
From: Nicolas Ratier @ 2018-01-29 16:26 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 416 bytes --]

Hello,

We develop an OCaml code to automate repetitive calculations and to 
assemble proofs in the mathematical domain of PDE homogenization (to 
summarize in one sentence).

We now need to embed elementary symbolic calculations.

Do you know a library of "Computer Algebra" which interfaces well with 
OCaml?
Does COQ need symbolic calculus? If so, is there a core of CA in COQ?

Thank you for your help,
Nicolas


[-- Attachment #2: Type: text/html, Size: 1240 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-01-29 16:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-12 18:36 [Caml-list] flambda for bytecode backend Alexey Egorov
2018-01-14 18:29 ` [Caml-list] <DKIM> " Pierre Chambart
2018-01-15 10:34   ` Leo White
2018-01-15 10:51     ` Leandro Ostera
2018-01-29 16:26     ` [Caml-list] OCaml <-> Computer Algebra Nicolas Ratier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).