xababafr
xababafr

Reputation: 5

Static type inferring in Ruby, what can I do?

I have a school project which aims to statically type some Ruby code. So, my input is simply a .rb file, and I should be able to type every variable that is assigned in the program.

Right now, what I'm planning to do is :

I only accept some very basic Ruby as input ( = no call to external library, just the core of ruby + defined-in-the-file methods)

My question is : what do you think of my approach? Is there any gem/existing programs that could help me?

Upvotes: 0

Views: 814

Answers (1)

Jörg W Mittag
Jörg W Mittag

Reputation: 369526

Your approach is technically correct, but it sounds very strange how you put it. This:

Right now, what I'm planning to do is :

  • get the file's AST with the Parser library
  • put each kind of nodes in container objects
  • implement the visitor pattern to recursively go through the program
  • try to infer something from there (I was thinking of somehow creating a table of possible input and output types from the core's methods)

sounds a little bit like you wanted to go to Mars like this:

Right now, what I'm planning to do is :

  • get a pencil
  • get a piece of paper
  • get a desk
  • sit down at the desk and use my pen and paper to design a space launch system and Mars lander

In other words, you list three completely trivial points that are maybe an hour of work for an experienced programmer, and then a fourth, that is multiple years of work and worth a PhD.

The most advanced work I know regarding static type inference for Ruby was Diamondback Ruby (DRuby) (not to be confused with the Distributed Ruby standard library aka dRb / dRuby). However, Diamondback Ruby is now abandoned, since the authors gave up on static type inference for Ruby.

One of the principal researchers behind Diamondback Ruby is now working on a new project called RDL. The main differences between Diamondback Ruby and RDL are:

  • RDL performs dynamic checking, not static checking
  • RDL relies on explicit annotations, not implicit inference

Steep is another similar project. It, too, relies on dynamic checking and annotations, and in addition does not actually strive for type-correctness.

Ruby Type Inference for IDEA is a complete re-think of how JetBrains plans to approach type inference for Ruby in their IDEA / RubyMine IDE. This does use type inference, but it uses dynamic type inference, not static.

So, as you can see, static type inference for Ruby is so hard that nobody is even trying it, and the guys who did try gave up on it and are now doing dynamic type checking with explicit type annotations instead.

Ruby Type Checking Roundup on Robert Mosolgo's blog is a good overview about the current state-of-the-art in Ruby typing.

Upvotes: 3

Related Questions