TC-1 Chunks

In Tiger, to support recursive types, functions and methods, continuous declarations are considered “simultaneously”. To ease the treatments of such series of declarations, we wrapp them into chunks from on the parsing.

This section uses --bindings-compute, implemented later (see TC-3, Bindings) as this is when we detect bad declaration referencing.

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 --bindings-compute foo-bar.tig
$ tc --bindings-compute 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 --bindings-compute foo-stop-bar.tig
$ tc --bindings-compute foo-stop-bar.tig
foo-stop-bar.tig:2.26-30: undeclared function: bar
$ echo $?
4

The same applies to types.

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 --bindings-compute fbfsb.tig
$ tc --bindings-compute 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.

var-chunks.tig
let
  var x := y
  var y := x
in
end
tc --bindings-compute var-chunks.tig
$ tc --bindings-compute var-chunks.tig
var-chunks.tig:2.12: undeclared variable: y
$ echo $?
4