janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.52k stars 227 forks source link

Feature request: gradual typing #488

Closed elimisteve closed 4 years ago

elimisteve commented 4 years ago

I'd love to see gradual typing in Janet! You already have typed arrays, which is great.

Any interest in adding a more general, optional type system that empowers Janet users with the very best of both dynamic and statically typed languages?!

elimisteve commented 4 years ago

I'd think such type information could be used to increase Janet's runtime performance!

bakpakin commented 4 years ago

While possible, might be a lot of work to improve performance. However, it would be possible to add some gradual typing as a set of macros to Janet (at least to start) - there are already some implementations for this in other lisps. Such macros could simply strip out typing information and type check while expanding forms.

As for the core compiler, I would put gradual typing down as "unlikely in the near future". I just don't think you can add it so easily and have a good result - personally I don't really like such systems like Typescript and MyPy, they feel incredibly inadequate compared to other static type systems. Julia sounds like what you want, except Julia is anything but simple and lightweight. However, there are other ideas which I think are easier and more practical for increasing performance.

If you think about it, it is pretty easy to make "any" language into a lisp, basically you just map S expression to the native syntax you are targeting somehow. This property makes it easy to use Lisps (and Janet) for DSLs, which can be implemented as a native compiler, an interpreter, etc. This is how Janet does it's PEG engine. Many dynamic languages create "fast" DSL for heavy lifting, such as Lua + Pallene, RED + RED System, Any dynamic language with a regex engine, etc. Of course you can use a language like C, but the strength of these DSLs is you can usually write much less code to great effect

I'd think such type information could be used to increase Janet's runtime performance!

Well yes, but that's like saying if Janet was C, it would be faster. Doing something to use that information would basically require a rewrite of the whole language, or a JIT compiler.

andrewchambers commented 4 years ago

I think gradual typing might be great as an external linter tool.

bakpakin commented 4 years ago

I will close this as I am unlikely to add it built into the compiler as is.

elimisteve commented 3 years ago

I think gradual typing might be great as an external linter tool.

Interesting idea, @andrewchambers! I see https://janet-lang.org/docs/linting.html , but is there a Janet library that will tokenize Janet code for me so I can walk the AST and strip out type information I insert? Thanks.

andrewchambers commented 3 years ago

No but the spork formatter is probably a good place to start.

elimisteve commented 3 years ago

Thanks. This sure looks handy indeed: https://github.com/janet-lang/spork/blob/master/spork/fmt.janet#L12

pepe commented 3 years ago

@elimisteve I have my linter for Kakoune, it may be to your interest too https://git.sr.ht/~pepe/jlnt.kak/tree/master/item/jlnt

sogaiu commented 3 years ago

@elimisteve Not quite sure what you are looking for, but there is this sort of thing for going from Janet source to a hiccup-like representation:

   (peg/match jg-capture-ast "|(+ $ 2)")
   # =>
   '@[(:fn
        (:tuple
          (:symbol "+") (:whitespace " ")
          (:symbol "$") (:whitespace " ")
          (:number "2")))])

In addition, the reverse direction is also possible:

  (code
    '(:fn
       (:tuple
         (:symbol "-") (:whitespace " ")
         (:symbol "$") (:whitespace " ")
         (:number "8"))))
  # => "|(- $ 8)"