Search code examples
f#quotations

F# quotations object graph


In C# I could create a string representation of an object graph fairly easily with expression trees.

public static string GetGraph<TModel, T>(TModel model, Expression<Func<TModel, T>> action) where TModel : class
{
            var method = action.Body as MethodCallExpression;
            var body = method != null ? method.Object != null ? method.Object as MemberExpression : method.Arguments.Any() ? method.Arguments.First() as MemberExpression : null : action.Body as MemberExpression;
            if (body != null)
            {
                string graph = GetObjectGraph(body, typeof(TModel))
                return graph;
            }
            throw new Exception("Could not create object graph");
}

In F# I've been looking at Quotations to attempt to do the same thing, and can't quite figure it out. I've attempted converting the quotation into an Expression using the PowerPack libraries, but have had no luck so far, and the information on the internet seems fairly sparse on this topic.

If the input is:

let result = getGraph myObject <@ myObject.MyProperty @>

the output should be "myobject.MyProperty"


Solution

  • You can see what you get from quotation expression in fsi session:

    > let v = "abc"
    > <@ v.Length @>;;
    val it : Expr<int>
    = PropGet (Some (PropGet (None, System.String v, [])), Int32 Length, [])
    
    > <@ "abc".Length @>;;
    val it : Expr<int>
    = PropGet (Some (Value ("abc")), Int32 Length, [])
    

    You can find description of all active patterns available to parse qoutations into

    manual\FSharp.Core\Microsoft.FSharp.Quotations.Patterns.html

    under your F# installation directory or at msdn site

    There is nice Chris Smith's book "Programming F#" with chapter named "Quotations" :)

    So, after all, just try to write simple parser:

    open Microsoft.FSharp.Quotations
    open Microsoft.FSharp.Quotations.Patterns
    open Microsoft.FSharp.Quotations.DerivedPatterns
    
    let rec getGraph (expr: Expr) =
      let parse args =
        List.fold_left (fun acc v -> acc ^ (if acc.Length > 0 then "," else "") ^ getGraph v) "" args
      let descr s = function
        | Some v -> "(* instance " ^ s ^ "*) " ^ getGraph v
        | _ -> "(* static " ^ s ^ "*)"
      match expr with
      | Int32 i -> string i
      | String s -> sprintf "\"%s\"" s
      | Value (o,t) -> sprintf "%A" o
      | Call (e, methodInfo, av) ->
        sprintf "%s.%s(%s)" (descr "method" e) methodInfo.Name (parse av)
      | PropGet(e, methodInfo, av) ->
        sprintf "%s.%s(%s)" (descr "property" e) methodInfo.Name (parse av)
      | _ -> failwithf "I'm don't understand such expression's form yet: %A" expr
    

    P.S. And of course you will need some code to translate AST to human readable format.