Lecture 11: Implementing Functional Programming Languages

Programming Language Technology Course
Aarne Ranta (aarne@chalmers.se), Krasimir Angelov (krasimir@chalmers.se)

This lecture is extra material for Masters, as is the whole Lab 4.

Conclusion of substitutions

"Substitution is the problem, not the solution." (Xavier Leroy)

In actual implementations of functional languages, substitutions are not used.

Better techniques:

Compilers of functional languages use closures.

Evaluating expressions with closures

Extend the domain of values by closures

    Val   ::= Integer | Exp "[" Env "]"
    Env   ::= [Subst]
    Subst ::= Ident "=" Val

Thus values are integer constants or expressions with substitutions; the latter one are called closures.

The big-step semantics is:

    env => var = lookup env var
  
    env => const = const
  
    env => fun = (\x -> body)[env']    env => arg = val    env',x :=val => body = res
    ---------------------------------------------------------------------------------
                    env => (fun  arg) = res
  
    env => (\x -> body) = (\x -> body)[env]

Thus no substitution of the argument is made in the lambda body - instead, the body is evaluated in an extended with the argument substitution.

This is the recommended technique for Lab 4.

A tour of Lab 4

See Lab PM