Let-In

ML family languages provide the let-in construct:

let a = 1 + 2 in a * a;;

This construct evaluates a value, then binds it to a name that is available within the expression that follows. Aside from a little type system requirement (let-in binding is required in order to achieve universal polymorphism safely) this construct can be completely simulated using an anonymous function:

(fun a -> a * a) (1 + 2);;

In some cases, reverting to the original definition can have advantages. One such advantage is the ability to control the lifetime of the binding: a let-in construct defines a name that is evaluated right before the expression is evaluated, but nothing happens, or can be set up to happen automatically, after the expression has been evaluated. For instance, if the program opens a file, an explicit closing operation must be present:

let out = open_out "foo.txt" in output_string out "hello" ; close_out out

When using a function, the caller can be another function which takes care of the removal. For instance:

let on_file filename f =
  let out = open_out filename in
  f out ;
  close_out out 

on_file "foo.txt" (fun out -> output_string out "hello")

Also, when designing a meta-language (that is, a library which helps construct a program in another language) it’s impossible to achieve the level of reflection required to notice that two expressions in the program are identical just by looking at the meta-program:

let result = Lang.call func arg in Lang.multiply result result

This code cannot notice that “result” is being used twice. By contrast, one can use an approach such as:

Lang.let_in (Lang.call func arg) (fun var -> Lang.multiply var var)

Here, the let_in function can create an expression which pushes the expression on a stack or otherwise binds it to a variable, and then generate a pop instruction or read-variable instruction on which the user-provided function is called.  For instance:

let let_in a b =
  let uid = get_uid () in
  LetIn(uid, a, b (GetValue uid))

0 Responses to “Let-In”


  1. No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



1170 feed subscribers
(readers who polled a feed this week)