Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Metaprogramming examples in F#

I'm interested in metaprogramming examples written in F# that demonstrate the use of F#-specific functionality such as the TryGetReflectedDefinition function in the F# standard library.

Where might I find such examples?

like image 725
J D Avatar asked Feb 11 '12 12:02

J D


People also ask

What is metaprogramming example?

MetaProgramming gives Ruby the ability to open and modify classes, create methods on the fly and much more. A few examples of metaprogramming in Ruby are: Adding a new method to Ruby's native classes or to classes that have been declared beforehand. Using send to invoke a method by name programmatically.

Where is metaprogramming used?

Metaprogramming can be used to move computations from run-time to compile-time, to generate code using compile time computations, and to enable self-modifying code. The ability of a programming language to be its own metalanguage is called reflection.

Are macros metaprogramming?

Meta-Programming uses a program as a data type to generate code; Macros and Reflection are techniques of Meta-Programming in some sense.

Does C have metaprogramming?

We don't have many tools for metaprogramming in C. We don't have templates or constexprs C++ has; we don't have mixins like in D; and we certainly don't have homoiconicity of Lisp and Julia. All we have in C is the preprocessor that supports macros.


2 Answers

Some time ago, I wrote a project that translates F# quotations to GPU code using MSR Accelerator.

This is a fairly simple translator (compared e.g. to WebSharper, which is pretty complex), but it should demonstrate most of the features you're interested in. In particular, it translates functions marked with the ReflectedDefinition attribute. It is also from your favorite scientific computing domain :-)

  • Accelerator and F# (III.): Data-parallel programs using F# quotations
  • Accelerator and F# (IV.): Composing computations with quotations
like image 24
Tomas Petricek Avatar answered Sep 20 '22 22:09

Tomas Petricek


WebSharper and Unquote are two examples of open source F# libraries using Quotations (Unquote doesn't specifically use TryGetReflectedDefinition, but I believe WebSharper does).

FSharp.PowerPack.Linq is another example. Also, I've found the implementation of quotations in the F# compiler to be a helpful and insightful reference.

like image 190
Stephen Swensen Avatar answered Sep 20 '22 22:09

Stephen Swensen