Lecture 11: Implementing Functional Programming Languages Programming Language Technology Course Aarne Ranta (aarne@chalmers.se), Krasimir Angelov (krasimir@chalmers.se) %!target:html %!postproc(html): #NEW %!postproc(html): #HR
%!postproc(html): #sub1 1 %!postproc(html): #subn1 n-1 %!postproc(html): #subn n This lecture is extra material for Masters, as is the whole Lab 4. #NEW ==Conclusion of substitutions== "Substitution is the problem, not the solution." (Xavier Leroy) In actual implementations of functional languages, substitutions are not used. Better techniques: - **de Bruijn indices**: replace variables by unique numbers - **explicit substitutions**: don't change the expression, but just glue the substitution to it - **closures**: expression with explicit substitutions Compilers of functional languages use closures. #NEW ==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. #NEW ==A tour of Lab 4== See [Lab PM ../laborations/lab4/lab4.html]