F# function call dependency graphs

f#, metaprogramming

Solution

Scott Wlaschin has done similar things in his 'Cycles and modularity in the wild' article. His approach uses Mono.Cecil as a metadata reader and spit out dependency graphs in SVG. A recent article, 'Analysis of Roslyn vs. the F# compiler' uses the same method.

You might be able to modify these scripts for your purpose. The downside is that the approach isn't very precise; some F#-specific information might be lost when the code is compiled down to IL.

Another approach is to use FSharp.Compiler.Service as a metadata reader; its advantage is flexibility, however, it might take some time to get used to FSharp.Compiler.Service API. You could get started by traversing the whole hierarchy starting from an assembly signature. Here is a relevant example and documentation.

In the same spirit, if functions are annotated with ReflectedDefinitionAttribute, then Reflection, Quotations and Pattern modules can be used to retrieve the dependency graph. This approach is explained and detailed in FSharp Journal.

Problem

We have an application in F# with a deep function call dependency graph. It performs financial calculations in a hierarchical fashion. How could we extract the graph of dependency calls? We are not interested in the full AST, just the dependency between modules/functions. The purpose is to have a simplified version of the code which can be used in conversations with domain experts.

Original source