Question

It is mentioned in various sources that OCaml has a static and strong type system, and also that it is an interpreted language.

Wikepedia states that static type checking is done at compile time. Now if OCaml is not a compiled language, then how does static type checking apply to it?

It is also mentioned in some places that OCaml has a byte code compiler and a VM. Then how does OCaml figure when to behave as an interpreted language and when to behave as a compiled language?

Was it helpful?

Solution

OCaml comes with an interpreter and two compilers. The interpreter reproduces the behavior of the compiler quite faithfully as you enter one expression at a time. It does a static type analysis of each expression. If the typing is OK, it evaluates the expression.

So, OCaml doesn't have to decide when to be a compiler and when to be an interpreter. The user decides by running the interpreter (commonly called the "toplevel") when that's what they want to do. Or, they run one of the compilers if they want to produce code for running later.

One of the compilers produces native code (machine code), for a set of supported machines. The other compiler produces bytecodes for a virtual machine that works in many more environments. There are tradeoffs between the two kinds of code; generally, the native code is faster but the bytecode is more portable and supports more interesting types of debugging (including running the program in reversed time).

OTHER TIPS

Static type checking occurs before any code is run. This is the case in OCaml, its typechecker runs before the interpreter or the compiler kicks in, and you get type errors if something is wrong.

Dynamic type checking occurs during runtime, every time the variable in question is used. For example - Racket or Scheme. In those, you can get a type error during program's execution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top