Code Monkey home page Code Monkey logo

Comments (3)

LPTK avatar LPTK commented on May 28, 2024

@chengluyu being a bit busy this week, maybe someone else wants to take this one up. Cc @Meowcolm024.
(Though we need to wait for #53 to be merged.)

from mlscript.

LPTK avatar LPTK commented on May 28, 2024

Ah and also: in the future the semantics of recursive definitions will be changed, so that all the rec defs in a block can see each other, as in:

rec def foo() = bar()
rec def baz() = 1
rec def bar() = baz()

So it would be good to preemptively account for that future semantics in the code-gen.

Finally, we don't yet have a check that recursive defs are "compilable", but eventually we'll need one. For instance:

rec def foo = { bar: () => foo } // this is fine
rec def foo = { bar: foo } // this is NOT fine

It's fine for the JS code to either loop forever, stack-overflow, or return undefined in the latter case.

from mlscript.

LPTK avatar LPTK commented on May 28, 2024

It seems #81 solves this for top-level defs but not for recursive let bindings. Cc @chengluyu:

:js
rec def pow0 n x =
  if n > 0 then pow0 (n - 1) x * x
  else 1
//│ // Prelude
//│ let res;
//│ // Query 1
//│ globalThis.pow0 = function pow0(n) {
//│   return (x) => n > 0 ? pow0(n - 1)(x) * x : 1;
//│ };
//│ res = pow0;
//│ // End of generated code
//│ pow0: int -> int -> int
//│     = [Function: pow0]

pow0 3 4
//│ res: int
//│    = 64

:js
def pow1 x =
  let rec go n =
    if n > 0 then go (n - 1) * x
    else 1
  in go
//│ // Query 1
//│ globalThis.pow1 = function pow1(x) {
//│   return ((function (go) {
//│     return go;
//│   })((n) => n > 0 ? go(n - 1) * x : 1));
//│ };
//│ res = pow1;
//│ // End of generated code
//│ pow1: int -> int -> int
//│     = [Function: pow1]

// FIXME support recursive let bindings
pow1 3 4
//│ res: int
//│ Runtime error:
//│   ReferenceError: go is not defined

from mlscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.