2
$\begingroup$

Type inference can be done by a compiler, and in such cases, type inference is static.

Type inference can also be done by an interpreter. An interpreter does the jobs of both compilation and execution. So is type inference done by an interpreter static or dynamic?

This question is an example that motivated my question Does "static" or "dynamic" imply a compiler or interpreter implementation of a programming language? at a more general level.

$\endgroup$
  • $\begingroup$ I think it depends on the semantics of the language, not the implementation. There are interpreters for statically typed languages, and compilers for dynamic languages. $\endgroup$ – adrianN Sep 17 '16 at 18:01
  • $\begingroup$ Thanks. How does it depend on what aspects of the semantics of the language? $\endgroup$ – Tim Sep 17 '16 at 18:42
  • 2
    $\begingroup$ The terms "static" and "dynamic" are a bit slippery. You might have to define them. It's possible that in doing so, you will find the answer to your own question. $\endgroup$ – Steven Shaw Sep 18 '16 at 5:10
4
$\begingroup$

Typically , type inference in interpreted languages happens after parsing (because type inference works on ASTs) but before interpretation (= execution). Both compilers and interpreters have a phase distinction between type inference, and the execution. The former happens strictly before the latter.


BTW, it's wrong to say that an "interpreter does the jobs of both compilation and execution". An interpreter does not do compilation.

  • A compiler is a meta-program that takes as input a program and outputs another program (the executable). It is this output that is executed.

  • An interpreter is a meta-program that takes as input a program and executes it.

Maybe the following picture clarifies the issue.

enter image description here

$\endgroup$
  • $\begingroup$ Thanks. ' type inference in interpreted languages happens after parsing (because type inference works on ASTs) but before interpretation (= execution).' If I am correct, dynamic means at run time. So do you mean that type inference interpreted languages is typically done statically not dynamically? $\endgroup$ – Tim Sep 22 '16 at 5:20
  • $\begingroup$ @Tim What I meant is that type inference in statically typed interpreted languages happens before the actual interpretation, after lexing / parsing. The actual interpretation process starts only after type inference, and when the program is actually typable. This is different from interpreting dynamically typed languages. $\endgroup$ – Martin Berger Sep 22 '16 at 9:45
  • $\begingroup$ by ' The actual interpretation process', do you mean execution? So does at actual interpretation time means the same as at run time? $\endgroup$ – Tim Sep 22 '16 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.