Next: , Previous: , Up: TC-2 Samples   [Contents][Index]


4.4.2.2 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).

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.

let function foo() : int = bar()
    function bar() : int = foo()
in
  0
end

File 4.13: foo-bar.tig

$ tc -b foo-bar.tig

Example 4.17: tc -b foo-bar.tig

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.

let function foo() : int = bar()
    var stop := 0
    function bar() : int = foo()
in
  0
end

File 4.14: foo-stop-bar.tig

$ tc -b foo-stop-bar.tig
error→foo-stop-bar.tig:1.28-32: undeclared function: bar
⇒4

Example 4.18: tc -b foo-stop-bar.tig

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.

let function foo() : int = 0
    function bar() : int = 1
    function foo() : int = 2
    var stop := 0
    function bar() : int = 3
in
  0
end

File 4.15: fbfsb.tig

$ tc -b fbfsb.tig
error→fbfsb.tig:3.5-28: redefinition: foo
error→fbfsb.tig:1.5-28: first definition
⇒4

Example 4.19: tc -b fbfsb.tig

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, in 2006-tc used to desugar it that way).

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

File 4.16: fbfsb-desugared.tig

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


Next: , Previous: , Up: TC-2 Samples   [Contents][Index]