Tomas Petricek

Searching for new ways of thinking in programming & working with data

I believe that the most interesting work is not the one solving hard problems, but the one changing how we think about the world. I follow this belief in my work on data science tools, functional programming and F# teaching, in my programming languages research and I try to understand it through philosophy of science.

The Gamma

I'm working on making data-driven storytelling easier, more open and reproducible at the Alan Turing Institute.

Consulting

I'm author of definitive F# books and open-source libraries. I offer my F# training and consulting services as part of fsharpWorks.

Academic

I published papers about theory of context-aware programming languages, type providers, but also philosophy of science.

Tomas Petricek
  • Tomas Petricek
  • Home
  • F# Trainings
  • Talks and books
  • The Gamma
  • Academic

F# metaprogramming and classes

Introduction

F# quotations allows you to easily write programs that manipulate with data representation of program source code. If you're not familiar with quotations I recommend reading my previous article [1] that contains short introduction to this topic first. Quotations can be used for example for translating subset of the F# language to another code representation or another language.

To get the quotation data of the expression you can either use <@@ .. @@> operator or resolveTopDef function. In the first case the code written between the "<@@" and "@@>" is converted to data during the compilation. The resolveTopDef function allows you to get quotation data of top-level definition (function) from compiled library at runtime (you have to use --quotation-data command line switch while compiling the library). I mentioned that quotations can be used to represent only subset of the F# language. Currently, one of the quotation limitations is that it's not possible to enclose the whole class in the quotation operators. It is also not possible to get the representation of the whole class at runtime nor the representation of class members (for example methods).

Class quotations

In one my project I wanted to be able to translate the class written in F# to another language, so I wanted to make this possible. This option will be probably implemented in the future versions of F#, so it will be possible to do this using more elegant method, but if you want to experiment with quotations and use classes, you can use my solution to implement the prototype.

In the prototype I implemented, you have to write the class and one module that contains the actual code for the methods. This makes it possible to get the structure of the class using standard .NET reflection classes and to extract quotation data for the class members using F# resolveTopDef function. Of course, you can't create instance of module, but for metaprogramming we only need to be able to get the quoted code and unless you want to use the class from code, you can leave the implementation of the class methods empty.

First I had to design the data structures for representing the class. If I were adding this feature to the F# metaprogramming, I would probably extended the expr data type to make this possible in consistent way. However I didn't want to modify the F# source code, so I designed the following types that are similar to the expr type (I reversed the order of type declarations, so it is easier to understand):

/// Structure that contains information about class -
/// consists of name, name of base class and members
type classInfo = { name:string; baseName:string option; members:classMember list; };; /// Member of the class /// For working with this type use cmfCtor, cmfMethod, cmfField, cmfProp..
type classMember;; /// Type for working with class members /// Query function tests whether member is of specified type /// Make function creates member
type 'a classMemberFamily with
member Query : classMember -> 'a option member Make : 'a -> classMember end;; /// Constructor declaration /// - parameter should be lambda expression
val cmfCtor : (expr) classMemberFamily;; /// Method delcaration /// - method name, expression (should be lambda) and return type /// (types of parameters are stored in lambda expression)
val cmfMethod : (string*expr*Type option) classMemberFamily;; /// Field declaration /// - field name, type and init expression
val cmfField : (string*Type*expr option) classMemberFamily;; /// Property declaration /// - property name, getter and setter (both should be lambda expr) and type
val cmfProp : (string*expr*expr*Type) classMemberFamily;;

This representation is still very limited - for example it isn't possible to represent polymorphic methods. In the previous code, the classMember can be used to represent any member (similarly to the F# expr that can represent any expression). The cmf[Something] values are equivalent to the ef[Something] from F# quotation library and allows you to work with classMember type.

The following example shows how to write simple class with module that can be used for extracting the quotation data:

#light

// Module with implementation of methods
module Person_Meta = begin   // Simulates field of the class   let name = ref "" // Represents constructor   let ctor (n) = name := n // Represents method   let Say (pre:string) = let s = pre^", my name is "^(!name)^"." in print_string s // Represents property   let get_Name () = (!name)   let set_Name (value) = name:=value end  

  // The real class // Members are just a placeholders and the quotations // are extracted from the previous module
type Person = class
val mutable name : string; new((n:string)) = { name = ""; } member this.Say(pre:string) = () member this.Name with get() = "" and set((v:string)) = () end

In the last code sample, I'll show how the functions I wrote can be used for working with the previous piece of code. To get the representation that I mentioned earlier of the class Person, you can use function getClassFromType. This function extracts structure of the class (from the class itself) and quoted code from the module with the _Meta suffix. The following example is quite simple. It prints basic class information (name and base name) and then iterates through all the members and prints all available information for every member. For printing the expr type (which represents the quoted code) I used printf function with the output_any formatter.

#light
do
// Get the class info for class 'Person'
let clsInfo = getClassFromType ((typeof() : Person typ).result) // Print name and optional base name Console.WriteLine("Class '{0}':", clsInfo.name) if (clsInfo.baseName <> None) then Console.WriteLine(" base: {0}", clsInfo.baseName) // Iterate through class members clsInfo.members |> List.iter ( fun m -> match cmfMethod.Query m with | Some (name, expr, ret) -> Method "printf ( %s : %a ) = " name output_any ret | _ -> match cmfField.Query m with | Some (name, typ, init) -> printf "Field ( %s : %a) " name output_any typ | _ -> match cmfProp.Query m with | Some (name, getter, setter, typ) -> printf "Property ( %s : %a )\n" name output_any typ printf "get = %a\n" output_any getter printf "set = %a\n\n" output_any setter | _ -> match cmfCtor.Query m with | Some (expr) -> printf "Ctor = %a\n\n" output_any expr | _ -> failwith "Error!" )

Conclusion

The aim of this article isn't to present fully working solution, but to suggest a few enhancements to the F# metaprogramming that I think would be useful. You can use the source code attached to this article to find (and experiment with) some interesting use cases of metaprogramming that require working with classes, for example translating classes written in F# to another language. The biggest limitation of the solution I presented is, that you have to write every implementation twice - it occurs in the body of the class and in the module used for metaprogramming too (however the code looks very similar). If you want to translate F# code, you don't need to implement methods/properties of the real class, but if the program creates instances of the class at runtime (and uses the quoted code to perform some analysis etc.), you'll need to write both implementations.

Downloads

  • Download the source code and examples (6.77kB)
  • Download the article (pdf, 204kB)

Links and references

  • [1] F# - Simple quotations transformation [^] - My F# Notes

Published: Saturday, 14 October 2006, 1:36 AM
Author: Tomas Petricek
Typos: Send me pull request!
Tags: meta-programming, f#

Contact & about

This site is hosted on GitHub and is generated using F# Formatting and DotLiquid. For more info, see the website source on GitHub.

Please submit issues & corrections on GitHub. Use pull requests for minor corrections only.

  • Twitter: @tomaspetricek
  • GitHub: @tpetricek
  • Email me: tomas@tomasp.net

Blog archives

February 2019 (1),  November 2018 (1),  October 2018 (1),  May 2018 (1),  September 2017 (1),  June 2017 (1),  April 2017 (1),  March 2017 (2),  January 2017 (1),  October 2016 (1),  September 2016 (2),  August 2016 (1),  July 2016 (1),  May 2016 (2),  April 2016 (1),  December 2015 (2),  November 2015 (1),  September 2015 (3),  July 2015 (1),  June 2015 (1),  May 2015 (2),  April 2015 (3),  March 2015 (2),  February 2015 (1),  January 2015 (2),  December 2014 (1),  May 2014 (3),  April 2014 (2),  March 2014 (1),  January 2014 (2),  December 2013 (1),  November 2013 (1),  October 2013 (1),  September 2013 (1),  August 2013 (2),  May 2013 (1),  April 2013 (1),  March 2013 (1),  February 2013 (1),  January 2013 (1),  December 2012 (2),  October 2012 (1),  August 2012 (3),  June 2012 (2),  April 2012 (1),  March 2012 (4),  February 2012 (5),  January 2012 (2),  November 2011 (5),  August 2011 (3),  July 2011 (2),  June 2011 (2),  May 2011 (2),  March 2011 (4),  December 2010 (1),  November 2010 (6),  October 2010 (6),  September 2010 (4),  July 2010 (3),  June 2010 (2),  May 2010 (1),  February 2010 (2),  January 2010 (3),  December 2009 (3),  July 2009 (1),  June 2009 (3),  May 2009 (2),  April 2009 (1),  March 2009 (2),  February 2009 (1),  December 2008 (1),  November 2008 (5),  October 2008 (1),  September 2008 (1),  June 2008 (1),  March 2008 (3),  February 2008 (1),  December 2007 (2),  November 2007 (6),  October 2007 (1),  September 2007 (1),  August 2007 (1),  July 2007 (2),  April 2007 (2),  March 2007 (2),  February 2007 (3),  January 2007 (2),  November 2006 (1),  October 2006 (3),  August 2006 (2),  July 2006 (1),  June 2006 (3),  May 2006 (2),  April 2006 (2),  December 2005 (1),  July 2005 (4),  June 2005 (5),  May 2005 (1),  April 2005 (3),  March 2005 (3),  January 2005 (1),  December 2004 (3),  November 2004 (2), 

License

Unless explicitly mentioned, all articles on this site are licensed under Creative Commons Attribution Share Alike. All source code samples are licensed under the MIT License.

CC License logo