TC-2 Chunks

The type checking rules of Tiger, or rather its binding rules, justify the contrived parsing of declarations. This is why this section uses -b/--bindings-compute, implemented later (see TC-3, Bindings).

In Tiger, to support recursive types and functions, continuous declarations of functions and continuous declarations of types are considered “simultaneously”. For instance in the following program, foo and bar are visible in each other’s scope, and therefore the following program is correct wrt type checking.

foo-bar.tig
let
  function foo() : int = bar()
  function bar() : int = foo()
in
  0
end
tc -b foo-bar.tig
$ tc -b foo-bar.tig

$ echo $?
0

In the following sample, because bar is not declared in the same bunch of declarations, it is not visible during the declaration of foo. The program is invalid.

foo-stop-bar.tig
let
  function foo() : int = bar()
  var stop := 0
  function bar() : int = foo()
in
  0
end
tc -b foo-stop-bar.tig
$ tc -b foo-stop-bar.tig
foo-stop-bar.tig:2.26-30: undeclared function: bar
$ echo $?
4

The same applies to types.

We shall name chunk a continuous series of type or function declaration.

A single name cannot be defined more than once in a chunk.

fbfsb.tig
let
  function foo() : int = 0
  function bar() : int = 1
  function foo() : int = 2
  var stop := 0
  function bar() : int = 3
in
  0
end
tc -b fbfsb.tig
$ tc -b fbfsb.tig
fbfsb.tig:4.3-26: redefinition: foo
fbfsb.tig:2.3-26: first definition
$ echo $?
4

It behaves exactly as if chunks were part of embedded let in end, i.e. as if the previous program was syntactic sugar for the following one (in fact, Tiger 2006 used to desugar it that way).

fbfsb-desugared.tig
let
  function foo() : int = 0
  function bar() : int = 1
in
  let
    function foo() : int = 2
  in
    let
      var stop := 0
    in
      let
        function bar() : int = 3
      in
        0
      end
    end
  end
end

Given the type checking rules for variables, whose definitions cannot be recursive, chunks of variable declarations are reduced to a single variable.