Lecture 12: Heterogeneous Metaprogramming in F#

Metaprogramming, code that manipulates code.  Examples: macros, templates, eval; quotation, quasiquotation, antiquotation; staged computation; run-time code generation, self-modifying code; MetaOCaml, homogeneous vs. heterogeneous metaprogramming, offshoring code.  The F# programming language; background, motivation; mixture of ML and .NET; interlanguage working.  Metaprogramming with LINQ: expressions in C#; quotations in F#, strongly typed expression data; using LINQ to map F# source to SQL queries; lightweight runtime code generation; Conway’s Game of Life in F#, outsourced to run on a GPU.

Advertisements for Martín Escardo’s seminar this afternoon, and Sam Lindley’s talk on Database Programming Without Tiers as an APL guest lecture on Monday.

Links for background material now available below.

Metaprogramming

As noted in the lecture metaprogramming covers a wide variety of language features, not all equivalent in meaning, safety, power, or usefulness.

Non-syntactic macros are a popular (ab)use of the C preprocessor at the International Obfuscated C Code Contest, for example in this Morse Code translator, or this program to calculate Easter. The C++ Standard Template Library makes rather more purposeful use of macros.

Sun provide a tutorial on reflection in Java. Mozilla documents the JavaScript eval() operation, including the observation Don’t use eval!.  By constrast, the LISP eval operation has a long and respectable history, and was indeed central to the original bootstrapping of the language via its metacircular evaluator, whereby LISP is defined in terms of itself. LISP quasiquote and antiquote help in building elaborate macros.

The MetaOCaml project page gives an overview of multi-stage programming in general.

F#

See Lecture 11 for F# references. The particular application of LINQ described in the lecture is taken from this research paper:

Don Syme. Leveraging .NET Meta-programming Components from F#: Integrated Queries and Interoperable Heterogeneous Execution. In Proceedings of the 2006 ACM SIGPLAN Workshop on ML, September 2006.  DOI: 10.1145/1159876.1159884

The ACM web page to download this paper should be usable from within the University network, as the library pays a subscription to the relevant ACM journal.  From outside the University, you can try to link through the library proxy, which will require you to authenticate with EASE. You could also try using the University web proxy, or a VPN.

Comments are closed.