Monadic templating in C# - Part 1

10/27/2015 By Laszlo 0 comments


When one is to generate formatted output from a complex hierarchical set of data, in our example, generating source code from an abstract syntax tree (AST), the task must be or practical to be divided into smaller subtasks, small generator functions which are responsible for producing output of a specific part of the data.

The output of the generator functions must be collected, then concatenated or combined together in a complex way using some templating technique. Speaking about string outputs, concatenation also must be done in an efficient manner using helper classes e.g. StringBuilder . In addition, the individual generator functions probably share some generation context with non-trivial scoping. They may generate warnings or errors along with the formatted output, which also must be collected. And the requirements can go on...

All of these requirements affect the calling convention between the individual generator functions, and it can be very complex even in a single case. The generation context must be passed, multiple return values must be received: generated output, warnings, modified context... It is also very hard to change the calling convention later as it easily pervades the whole source code.

In this post we explain a framework for generating such formatted output using distributed generator functions. The framework is based on a technique heavily exploited in the functional programming world, called monads . With this technique arbitrary complex calling convention can be hidden in a small library, which can be extended later without affecting the actual generation logic.

In this first part of the post, the monadic generator functions are explained in details. The generator functions will be very lightweight in usage while can efficiently collect and concatenate the output of the generator functions substasks are delegated to. In our explanation example, an AST based source code generator, the generator functions share a generator context. The context is immutable and/thus the scope of the changes can be freely managed.

In the second part of the tutorial, the framework will be extended to provide lightweight monadic templating using the string interpolation feature of C# 6.0.

Throughout the tutorial, we stick with the immutable approach as we believe that it results in less error prone software in complex tasks. As Eric Lippert phrases in his blog post:

"ASIDE: Immutable data structures are the way of the future in C#. It is much easier to reason about a data structure if you know that it will never change. Since they cannot be modified, they are automatically threadsafe. Since they cannot be modified, you can maintain a stack of past “snapshots” of the structure, and suddenly undo-redo implementations become trivial. On the down side, they do tend to chew up memory, but hey, that’s what garbage collection was invented for, so don’t sweat it."

The source code of the example used throughout this post can be downloaded from the following GitHub repository: The example is based on C# 6.0 features as string interpolation and static imports, thus one needs VisualStudio 2015 to be able to compile it.

Generator functions

In the following, let us suppose that we have the task already mentioned in the introduction, namely, generating source code given a hierarchical AST. We will use this example throughout the post.

First, let us see how a generator function would look developed in a naive way. We are basically doing string concatenations, so it must be done carefully to be efficient enough. Using StringBuilder is a trivial choice, the first approach could be to pass around a StringBuilder instance. Unfortunately this approach is not compositional, the result is messy, hard to read and maintain; templating cannot be easily achieved either, because of the lack of compositionality:

1 public void GenerateMethod(StringBuilder o, Method method) 2 { 3 o.Append("function "); 4 GenerateIdentifier(o, method.Name); 5 o.Append("("); 6 GenerateArgumentList(o, method.Arguments); 7 o.Append("){"); 8 GenerateBlock(o, method.Body); 9 o.Append("}"); 10 }

Having a string return type results in more readable code and it comes only with a small price of managing a local StringBuilder. Templating is also possible with this approach:

1 public string GenerateMethods(List<Method> methods) 2 var o = new StringBuilder(); 3 4 foreach(var method in methods) 5 { 6 var name = GenerateIdentifier(method.Name); 7 var arguments = GenerateArgumentList(method.Arguments); 8 var body = GenerateBlock(method.Body); 9 10 o.AppendFormat("function {0}({1}){{{2}}}", name, arguments, body); 11 } 12 13 return o.ToString(); 14 }

It is getting more complicated though if generation context is introduced. To keep the solution compositional, the context is passed around in a "ref" parameter (another design could be to return a tuple of values from the generator functions as it would be done in a functional language):

1 private string GenerateMethods(ref Context ctx, List<Method> methods) 2 { 3 var o = new StringBuilder(); 4 5 foreach(var method in methods) 6 { 7 var name = GenerateIdentifier(ref ctx, method.Name); 8 var arguments = GenerateArgumentList(ref ctx, method.Arguments); 9 var body = GenerateBlock(ref ctx, method.Body); 10 11 o.Append($"function {name}({arguments}){{{body}}}"); 12 } 13 14 return o.ToString(); 15 }

Implementation details are getting pervades the source code now. If more features are added, e.g. generated warnings, writing generator functions are getting very tiresome and error prone. This is the point where functional programming "design patterns" come to the rescue.

Monadic generators

The technique we will use, called monad,was introduced by Phillip Wadler in his seminal paper Monads for functional programming in 1992.

According to Wikipedia: "In functional programming, a monad is a structure that represents computations defined as sequences of steps: a type with a monad structure defines what it means to chain operations, or nest functions of that type together"."

In other words, a monad is a data structure which helps to define steps of computations in a compositional way: basic computation steps can be composed together to an indistinguishable new step. A monad usually has two operations associated with it:

1. return lifts a constant value into a monad;

2. bind or >>= composes to computations into a new computation in a sequential manner. a >>= b means that a computation precedes b computation.

In our case the monad type will be the Generator<T> class. The is a bit more general than it was in the introductory example, but we want to enable the generator functions to be able to produce any value, not just strings. For practical reasons the T type variable must be covariant, thus an interface also must be utilized. It contains only one function to execute the monad (we will discuss this concept later):

1 interface IGenerator<out T> 2 { 3 ICovariantTuple<T, Context> Run(Context ctx); 4 }

The actual Generator<T> class simply wraps a generator function, a delegate called GFun<T>. The wrapping can be done using the Wrap function. An arbitrary value can be lifted into the monadic context using the Return function. The bind operation is basically the + operator. This latter is a bit limited as it always return Generator<string>; this is enough for our purposes, the generic nature of the Generator<T> class will be exploited in the templating code.

1 public delegate ICovariantTuple<T, Context> GFun<T>(Context ctx); 2 3 public class Generator<T> : IGenerator<T> 4 { 5 private readonly GFun<T> f; 6 7 private Generator(GFun<T> f) 8 { 9 this.f = f; 10 } 11 12 private static Generator<T> Wrap<T>(GFun<T> f) 13 { 14 return new Generator<T>(f); 15 } 16 17 public ICovariantTuple<T, Context> Run(Context ctx) 18 { 19 return f(ctx); 20 } 21 22 public static Generator<T> Return(T val) 23 { 24 return Generator<T>.Wrap(delegate (Context ctx) 25 { 26 return CovariantTuple<T, Context>.Create(val, ctx); 27 }); 28 } 29 30 public static Generator<string> operator +(Generator<T> a, Generator<T> b) 31 { 32 return Wrap(delegate (Context ctx) 33 { 34 StringBuilder o = new StringBuilder(); 35 36 var Res1 = a.f(ctx); 37 o.Append(Res1.Item1); 38 39 var Res2 = b.f(Res1.Item2); 40 o.Append(Res2.Item1); 41 42 return CovariantTuple<String, Context>.Create(o.ToString(), Res2.Item2); 43 }); 44 } 45 46 }

Basic examples

And this is how to use our monad (in the following we skip the class name from method invocations, e.g. Return("string") instead of Generator<string>.Return("string");; this can be done anyway using static imports). The simplest possible example is lifting a constant to the Generator monad:

1 Generator<string> res = Return("str");

Let us contemplate a bit on this. Return("str") has the type of Generator<string>which is just a wrapped delegate having an argument of type Context. That is, when a Generator class is created, only a closure is created, a kind of suspended computation which is not executed until the context argument is finally provided. This is where the Run;method of the IGenerator<T> interface comes into the picture:

1 string str = res.Run(new Context()).Item1; // Item2 contains the modified context

Then again, when two generators are composed using the bind operation (+) the result is also a suspended computation, none of the parts are executed until the generation context is finally applied to the compound generator:

1 Generator<string> res = Return("str1") + Return("str2");

Running resresults in a cascade of recursive executions of the embedded generators. The delegate related to the (+) operator takes the first generator, executes it using the provided context then executes the second computation using the context returned by the first computation (the values are concatenated using an internal StringBuilder).

1 string str = res.Run(new Context()).Item1; // == "str1str2"

Accessing to the context

For a more complex example we need to introduce two more methods for the Generator<T> class. These methods enable to access to the current context:

1 public delegate Generator<T> GContextFun<T>(Context ctx); 2 3 public class Generator<T> : IGenerator<T> 4 { 5 ... 6 7 public static Generator<T> WithContext(GContextFun<T> gfun) 8 { 9 return Generator<T>.Wrap(delegate (Context ctx) 10 { 11 return gfun(ctx).Run(ctx); 12 }); 13 } 14 15 public static Generator<T> SetContext(Generator<T> gen, Context localctx) 16 { 17 return Generator<T>.Wrap(delegate (Context ctx) 18 { 19 var ret = gen.Run(localctx); 20 return CovariantTuple<T, Context>.Create(ret.Item1, ctx); 21 }); 22 } 23 }

The WithContext method wraps a GContextFun<T> into a generator GContextFun<T> enable read-only access to the current generation context. The SetContext method enables to change the context with a local scope (for a given generator; the context returned by the generator is dropped in line 20). The following utility functions demonstrate their usage. It is supposed that the Context class contains the pair of GetProperty/SetProperty methods:

1 public Generator<string> GetContextProperty(string property) 2 { 3 return WithContext(delegate (Context ctx) 4 { 5 return ctx.GetProperty(property); 6 }); 7 } 8 9 public Generator<T> SetContextProperty<T>(Generator<T> gen, string property, Generator<string> value) 10 { 11 return WithContext(delegate (Context ctx) 12 { 13 return SetContext(gen, ctx.SetProperty(property, value)); 14 }); 15 }

A more realistic example

Now we can have a quick glimpse into how real code, based on the Generator<T> class, would look like. The Template method used in the following example is still a debt of us. It is already uses a monadic extension of the string interpolation feature of C# 6.0, this will be introduced in the second part of this blog post series. Still, you can see how much easier to read it than the String.Format based version used in the introductory examples:

1 public Generator<string> GenerateBlock(Block block) 2 { 3 return R("Method body for ") + GetContextProperty("method"); 4 } 5 6 public Generator<string> GenerateMethod(Method method) 7 { 8 var name = GenerateName(method); 9 var arguments = GenerateArguments(method); 10 var body = SetContextProperty(GenerateBody(method), "method", name); 11 12 return Template($"function {name}({arguments}){{{body}}}"); 13 }

Much nicer, isn't it?


As the previous example shows, using the introduced framework, we can concentrate on the pure "business" logic, the code is very readable and easy to extend. It is also efficient enough; F# also runs on CLR, it is prepared to deal with higher order functions efficiently. These are pros, but what are the cons?

First of all, it increases the conceptual complexity of the code a great deal. Implementation details are now hidden behind higher order functions; that can make the inexperienced hard to see what happens under the hood. Further disadvantage is that with this monadic approach we introduced a kind of lazyness . When a generator function is executed it just "prescribes" the computation steps, the actual execution is postponed until the monad is executed. This can make debugging a challenge in some cases.


We already used templating in the latest example. We continue with its explanation in the next part of this blog post series.