I thought I knew how to do that but this time I found that the syntax is a little different than what I had in mind.

I used tho think (incorrectly) that just like function types for free-standing functions, one would create a member-function type. While, it's straight-forward to create function types for free-standing functions, I think it's not possible to create member function types. Don't get me wrong. One can create pointer-to-member-function type just like pointer-to-a-function type. Here's what I mean.

Sunday, August 06, 2017

std::accumulate makes a ton of copies internally. In fact it's 2x the size of the number of elements in the iterator range. To fix, use std::ref and std::reference_wrapper for the initial state. std::shared_ptr is also a possibility if the accumulated state must be dynamically allocated for some reason. Live code on wandbox.

Sunday, July 09, 2017

While looking for some old photos, I stumbled upon my own presentation on C++ coroutines, which I never posted online to a broader audience. I presented this material in SF Bay ACCU meetup and at the DC Polyglot meetup in early 2016! Yeah, it's been a while. It's based on much longer blogpost about Asynchronous RPC using modern C++. So without further ado.

Friday, January 06, 2017

In the previous two blog posts (Understanding Fold Expressions and Folding Functions) we looked at the basic usage of C++17 fold expressions and how simple functions can be folded to create a composite one. We’ll continue our stride and see how "embellished" functions may be composed in fold expressions.

First, let me define what I mean by embellished functions. Instead of just returning a simple value, these functions are going to return a generic container of the desired value. The choice of container is very broad but not arbitrary. There are some constraints on the container and once you select a generic container, all functions must return values of the same container. Let's begin with std::vector.

The focus of this post is folding functions like get_countries, get_states, and get_cities. As you may have noted, these functions are different from what we saw in the previous post. They don’t readily compose. The return types and argument types don’t line up. But I bet you know how to get to the cities given a Continent. Don’t you? Hold on to that intuition. It will come in handy later. In essence, my attempt is going to formalize that intuition of yours while generalizing it by leaps and bounds. Possibly, beyond recognition.

Our target here is to enable folding (a.k.a. composition) of these embellished functions similar to the compose function we saw in the previous post. We’ll call it composeM though.

Now would be a good time to recall the intuition you had about going from a Continent to it's cities. Your intuition about implementing cities_from_continent is probably as follows. This algorithm goes all three levels in one function (with three nested loops).

Get a Continent from somewhere. Initialize an empty vector of cities.

Call get_countries (level 1)

Call get_states for each country (level 2)

Call get_cities for each state (level 3) and push_back in the vector of cities

Return the vector of cities

On the other hand, the overloaded >>= function implements the same intuition with only two levels at a time (as it accepts only two functions as parameters). Going three levels requires operator >>= to be called twice. C++ compiler will do that for us because we're using the fold expression in composeM. Every time compiler calls operator >>= it returns a function that's just like it received as arguments. Specifically, the returned closure is not a generic. We find out the argument type of F (FArg) and result type of G (GResult) and use them in the definition of the lambda. The returned closure is a composite because it accepts F's argument type and returns G's result type. It glues F and G together. All that ceremony is kinda important.

When operator >>= is called the first time, F is of type Continent→Vector<Countries> (i.e., get_countries) and G is of type Country→Vector<State> (i.e., get_states). The first time around, the signature of the closure is going to be Continent→Vector<State>. It's just "like" the other two. Naturally, when this closure is composed with get_cities, operator >>= works smoothly and returns a closure of type Continent→Vector<City>, which is exactly the same as cities_from_continent you intuitively had in mind.

An important difference between the direct function and the function that's the result of composition is that the composed version materializes (creates) a temporary vector every time operator >>= is called. That's an overhead. There are ways to avoid that but we may have to consider something other than std::vector. I won't discuss that here. See Range-v3.

Another Example: boost::future

So far we've looked at composition of embellished functions that accept an argument and return a vector of some type. At the beginning of the article, I suggested that there is broad set of generic containers we can choose from. Now would be a good time to select another one. How a about boost::future? We'll go through a similar exercise of compositing function that return boost::future. We'll see if there's anything common in the composition of functions that return vectors and functions that return futures.

Let's imagine there's a database that contains information about the largest countries, states, and cities in the world. As database operations are i/o bound, using futures to communicate the possibility of long execution times is a good idea. So our function look like as follows.

If we need the largest city in the largest state of the largest country of a given continent we'll need all the three functions. As before, you have an intuition behind how to do that. May be it's something like below.

Get a Continent from somewhere.

Call get_largest_country (level 1). Wait to retrieve the result because db ops could take long.

This implementation is fully synchronous and isn't optimized to use nice properties of boost::future (yet). It may remind you of function composition we saw in the previous post. However, the .get() calls in between completely mess things for us. We can't use simple function composition because it does not call .get().

Let's rewrite operator >>= that would work with functions that return boost::future. We want to compare it with that of the Vector type.

Just like before, this function operates only two levels at a time because we pass only two functions to it. The implementation is much simpler than before because the argument functions return a boost::future and it may contain at most one value. We retrieve it using .get() and pass it on to function g.

Needless to say this implementation of >>= does not look much like the earlier (Vector) implementation of operator >>=. Is there anything common/reusable at all? Can we refactor these two functions into something reusable? Let's observe them closely under a special microscope--generic programming microscope.

Refactoring To a Generic operator >>=

Disclaimer: This section may appear a little too abstract and hand-wavy. As I already know how the refactoring is going to look like, I may skip ahead too fast and may be too hasty about some generalizations. Let me know what you think. Here we go.

Here's what I see in the implementations of operator >>=

Both functions are higher-order. I.e. they take functions as arguments and return a new composite function.

The type of the composite function depends on the types of the arguments. Specifically, the composite function is "just like" the argument functions.

In both cases, the argument type of the composite function is the same as that of F and the return type is the same as that of G.

Function f and g accept an argument of a simple type and return a value "wrapped" in a generic container. The notable difference is of the container type (Vector and boost:future, respectively). Hmm, template template parameter?

There's a dependency between f and g. f is invoked before g. Always. That's generic. This is key.

The argument passed to the composite function is passed to f (i.e., f(a)). As the return value of f is a generic container, the contained value(s) are somehow extracted and passed to g. In the case of Vector we use a for loop and in the case of boost::future, we use .get(). The specific way to get to the guts of the container is dependent on the container type. The composite function "collapses" two nested levels into one. In the case of Vector, two nested loops. In the case of boost::future, two calls to the database appear as one from outside. These things are very very specific to the container. There's no hope to generalize it. This is key too and the most hand-wavy observation.

What in the world all this means? And how we might express these observations in C++ in a reusable manner? Not all is reusable as we observed. Items #1 to #5 appear something like we can write reusable code for. #6 does not as the details depend on the container type---a template (as opposed to an instantiation of it).

Sounds like we need a template template parameter. Fine. But a template template parameter of what?

The Genius of Monad

So what we are looking for is known as a monad. That's the name it gets because mathematicians reached here first--long before we programmers existed. Let's just accept the name and move on.

Based on the commonalities listed above, we need something with nearly the same capabilities except for #6. It should allow to plug an implementation that is dependent on the container type. We are in pursuit of a monad-aware implementation of operator >>=. As #1 through #5 are reusable pieces of code, let's just copy-paste them.

As you can see, this implementation has the same shape but with more flexibility. It's higher-order. Returns a closure that accepts argument of the same type as F and returns the type as G. It invokes f(a). From this point on things look different. The pluggable "interface" we use is called bind. The result of f(a) is passed to bind which is dependent on type GResult. Further function g is also passed to bind because a generic implementation won't know how to peek into the value of f(a) and invoke g on it. That's the job of bind. So there must be specializations of bind for each container template. But first, we need to identify the container template. We use a template template parameter.

It is a simple wrapper over the real deal: monad. The job of get_monad is to pattern-match on GResult and extract the template-name and template arguments out. It simply forwards the template-name to the monad template. I.e., In case of get_monad<Vector<City>>, monad is instantiated with just Vector as in monad<Vector>. Similarly, for the function returning boost::future, monad<boost::future> is instantiated.

That brings us to the monad template, which accepts a template template parameter.

This definition only serves as an "interface" and does no work. It's completely optional. Note the signature of bind though. It accepts an argument of type M<T>, whatever M is. We'll look at two examples shortly. It also accepts a function, which must also return M<T>. Bind calls one embellished function at a time. At this point, what we really need are the specializations of this template. Here's a monad specialization for Vector.

The bind function accepts a Vector object and a function as arguments. The job of bind is to unwrap the Vector, get the values, and pass them to the function. That's just half the story. The function itself returns a Vector for every call. bind can return only one Vector. Therefore, bind needs to combine/flatten all the Vectors into one. The outer Vector is that flat Vector, which contains all the elements from the individual Vectors received from func. Note that if if the input vector is empty, the resulting vector is also empty. Likewise, if the function func returns empty Vectors every-time, the resulting Vector is also empty. This behavior is exactly same as commonly discussed list monad.

Like the Vector version, future version also accepts an object of future type (i.e., the container type) and a function. The bind function extracts the value out from the future by calling get. Only bind would know how to do that. It passes it on to the function. The function itself returns a future. We simply return it. This version is fully synchronous and wait for the result of the first function to arrive before calling the next function. Recall that fut is the result of calling f(a) in operator >>=.

The bind function is not unique and it may be implemented in multiple ways. A better alternative with boost::future is to chain the result one future to a subsequent function. In fact, that's what we've been doing all along. boost::future provides a nice api called .then to chain embellished functions that return futures. Therefore, an alternative implementation of monad<boost::future>::bind could be as follows.

The boost::future::then function is very similar to bind. The main difference is that the argument lambda to .then accepts a boost::future object as opposed the value in it. This complication is to support the possibility of futures that complete with an exception. If the first method (f) returns a future that resolves into an error, we may need to know about it. If a future resolves to an error, .get() rethrows the exception skipping all the subsequent chained functions. That's exactly what we want.

There's one more wrinkle that isn't clear in the code. .then wraps the return type of the lambda in a future. In our case the lambda returns a future itself. (e.g., future<State>). Therefore, the resulting type of .then is future<future<T>>. That does not match with that of bind, which is just future<T>. There're two ways out provided by boost::future. Either you can call .unwrap on future<future<T>> or rely on the unwrapping constructor of boost::future that auto converts future<future<T>> to future<T>. I opted for the former for clarity.

Putting It All Together

With this refactoring in place, our composeM function that does a left fold of embellished functions works just fine.

That's pretty neat. The type of the container does not matter. We successfully abstracted the details of the container type in to it's respective bind functions. For new generic containers, all we need to do is to implement a specialization of bind. Bind is the function that allows us to compose (a.k.a. chain) a set of embellished functions belonging to the same container type (monad). As long as an implementation of bind satisfying the signature exists for a container, we can use it in fold expressions. Clearly, C++ fold expressions are quite versatile. As an exercise, try implementing bind for std::optional. It's fun! Find out what monad that is.

Something's Amiss?

If you are familiar with the concept of a monad from Haskell or other languages, you know already that the monad abstraction presented above is not complete. It's missing a function called return (Haskell lingo), that accepts a unwrapped value and puts it into the chosen generic container (monad). Obviously, it is highly specific to the container used. Putting a value in a future differs significantly from putting a value in a Vector.

I'm unsure if we really need a function like return because we just have constructors in C++ to do the job. For the sake of completeness, however, I will include it.

Note that the type of return_ matches with that of our embellished functions. As return_ does not do anything but "put a value into the monad", we can use it as the identity for monadic function composition. Let's use a binary left fold as a demonstration.

This implementation of composeM works as long as there's at least one function is passed to it. That's because composeM won't know which monad you are talking about without looking at at least one of them in the return type of the passed function. identity simply forwards its argument to the return_ function. It cheats a little in the process. It finds out the argument_type of Func (FArg). That's not strictly necessary---especially if we use generic lambdas and simply forward the argument to bind. If we do that, we need a few tweaks in operator >>=.

On a closer examination, it's clear that operator >>= does not depend on the argument type at all. It only needs to figure out the container type---the template template parameter to use to pull-in the right monad. So, GResult is all we need.

Here's the final revised version of operator >>= for folding monadic functions.

Oh, I almost forgot. The choice of operator >>= is not an accident. In Haskell >>= works the same way as our bind function. On the other hand, operator >>= is like Haskell's fish operators (>=> for right fish and <=< for left fish).

That's it! If you are still here, thank you for reading. Here's live code if you take it for a spin. This concludes the three part series on C++17 Fold Expressions. See previous posts: #1 and #2.

Appendix

Here're the templates to extract argument and result types of lambdas. They don't work with generic lambdas though.

Tuesday, December 27, 2016

In the last post we looked at basic usage of C++17 Fold Expressions. I found that many posts on this topic discuss simple types and ignore how folds may be applicable to more complex types as well. [Edit: Please see the comments section for some examples elsewhere in the blogosphere.] In this post I'm going to describe folding over functions.

Composing Functions

Function composition is a powerful way of creating complex functions from simple ones. Functions that accept a single argument and return a value are easily composable. Consider the following example to compose two std::functions.

Function compose accepts two std::function arguments and returns another one. The types of these std::function arguments are important. f is a function from A->B where as g is a function from B->C. Therefore, it makes sense that compose can generate another function of type A->C. The output f goes to the input of g. The implementation of the lambda confirms that.

As std::function is kind of verbose and not very idiomatic in C++ when you want to pass functions around. I'll try to use C++11 lambdas initially. I want to stay away from generic lambdas because argument and return types are kinda important. In generic lambdas, however, it's impossible to find their argument and return types without knowing an actual argument or its type. Note that in compose function we have access to functions only and no arguments.

F and G are generic arguments, which we expect to be non-generic lambdas. We extract the argument type of F and result type of G and return a composition of two lambdas satisfying the type signature.

This implementation is not very idiomatic. Extracting the argument and return types of functions in this style is falling out of favor. std::function::argument_type and std::function::result_type have been deprecated in C++17. A more idiomatic way would have been to return a generic lambda without bothering the argument type. C++ clearly wants to favor duck-typing at compile-time. Until we've concepts in the language, of course.

I'll skip the implementation of the detail namespace. It's in the same vein as this stackoverflow answer.

Folding Functions

Folding functions is a generalization of function composition applied to fold expressions. First, we need to pick up an operator to use fold expressions with. I like >> as it's quite intuitive. Here's the earlier function implemented as an overloaded operator.

Interestingly, this compose function works fine with a single argument as it simply returns the argument as discussed in the previous post. It does not work with empty parameter pack however. What could we return when we get an empty parameter pack? In other words what would be the identity for the function type? Well, it's just a function that returns its argument. Let's see it in action using a binary fold.

Only problem, however, is that it does not compile. Not that anything is wrong with binary folds but the overloaded >> for generic functions cannot digest Identity. Identity has a generic function call operator. There's no way to get it's argument_type and result_type without knowing the type of the argument. The compose function does not have it.

We're therefore forced to use a generic implementation of operator >>.

With this final variation, functions can be folded over in a binary fold expression.

I will conclude this blog post with a bit of monoid theory.
You might wanna ask yourself if function composition is another monoid? As it turns out, it is. It makes sense intuitively. Composition of two functions give rise to another function. The composition is also associative. It does not matter if we call compose(f, compose(g,h)) or compose(compose(f,g),h). The end result is the same. Squint a little and you will realize that they are just left and right folds. Finally, there's also an identity function, which when combined with any other function makes no observable difference. Therefore, we can say that function form a monoid under composition.

Next time we'll look at even more interesting functions---those return values wrapped a generic "container".

This particular example is a unary left fold. It's equivalent to ((((1+2)+3)+4)+5). It reduces/folds the parameter pack of integers into a single integer by applying the binary operator successively. It's unary because it does not explicitly specify an init (a.k.a. identity) argument. So, let add it.

This version of addall is a binary left fold. The init argument is 0 and it's redundant (in this case). That's because this fold expression is equivalent to (((((0+1)+2)+3)+4)+5). Explicit identity elements will come in handy a little later---when we have empty parameter packs or if we use user-defined types in fold expressions.

Unary fold expressions do not like empty parameter packs except for && || and comma operators. In fact, the P0036 document describes what happens when empty parameter packs are used with these operators and why it's illegal for other operators. In short, empty parameter packs result into true, false, and void() respectively. In that sense, binary folds appear significantly superior because you can specify the identity element for fundamental and user-defined types and for all the operators.

Single element parameter packs result into the value of the element type. This may be ok for some types and operators but it's very confusing for operators such as > < == != <= >= && ||. These operators return boolean result in general but not when the parameter pack has only one element. The type of the expression changes when the size of the parameter pack is greater than 1. For example, lte(1) returns a int but lte(1,3) return a boolean. That's bizarre.

Multiple element parameter packs work as expected with a twist. Consider gt example on line #73. gt(3,2,0) expands to (3>2)>0, which is true>0, which is true. Similarly, lt(1,0,-1) is (1<0)

The assign function is curious too. Assigning to a variable makes sense. For example, assign(a,2,4) expands to (a=2)=4, which assigns 2 to a and later 4 to a. So there're two assignments. The result type is int&. The funny thing is that if you replace a with an rvalue, it still works. I don't know what the compiler is thinking at that point.

Operator associativity has no consequence. For example, <<= and >>= are right-associative operators but left folds still fold from left to right. I.e., Nominally, a <<= b <<= c is equivalent to a <<= (b <<= c). With left unary fold you get (a <<= b) <<= c. If you want the former, use a unary right fold.

Finally, consider the folds expressions containing pointer to members. Line #103 and below. A single, initialized pointer to member just a decays to true in a boolean context (like any other pointer). The weird thing though is that, there's no way to make sense of two or more pointers to members. I can't think of a way where they fold (a.k.a. compose) and return something meaningful. An object (of the same class as that of the member pointer) is required as the left most element in the parameter pack to deference a list of member pointers. For example, objptrmem(p,phoneptr,extptr) is the same as p.*phoneptr.*extptr. Without p, just phoneptr and extptr make no sense together.

Binary Folds

This example uses a user-defined Int type in a left binary fold. We'll also specify our own identity for our Int-based binary folds.

Things are very much as expected in this example. For user-defined types, the operator you wish to use fold expression with must be overloaded. Int overloads binary + and *. addInts uses Int{0} as the identity element whereas mulInts uses Int{1}. Identity element is special. It's special because in case of Int addition, adding with identity element make no difference. Similarly, in Integer multiplication, multiplying with the identity element makes no difference.

I'll wrap with a quick theory about monoids.

Formally, (Int,+) is monoid with Int{0} as identity and (Int,*) is also a (different) monoid with Int{1} as identity. Two instances of the same monoid can be combined to produce a third one. In fact, Monoids can be combined arbitrarily to produce other instances of the same monoid. Left and right folds provide just 2 possible ways in which any monoid may be combined.

In the following posts, we'll create more interesting monoids and see how well fold expressions can exploit their properties.

Saturday, November 05, 2016

Just a few days ago I came across an intriguing blog-post about type-safe printf using dependent typing. The blog-post has since become inaccessible and therefore, I've copied an excerpt here. I want to thank Zesen Qian for publishing this blog-post.

.... printf originated from the C programming language and has been a headache since then because a proper call of printf requires the number and types of arguments to match the format string; otherwise, it may visit a wild memory or a wrong register. In recent versions of GCC this issue is addressed by type checks hard coded into the compiler itself, which is ugly because the compiler should not be caring about the safety of applications of a specific function....

The key issue here is that, considering the curried version, the type of the returned function of applying the format string to printf, actually depends on the value of that format string. That is to say, printf "%s" : String -> String, and printf "%d%s%d" : Int -> String -> Int -> String, and so on. This is where dependent type comes in: in dependently typed language, the type of returned values of functions can be dependent on value of the arguments; .... ---- Zesen Qian (ICFP'2106)

I thought it might be possible to achieve the same effect in C++.
.

Currying

Currying is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions, each with a single argument. I've talked about currying from very basics in a previous post. I'll jump straight to an example this time.

Function multiple takes both arguments at the same time. Function mul, which is a curried version, takes one argument at a time. Intermediate results, such as a, are themselves functions that take one of the remaining arguments. When all arguments are available, the original function evaluates producing a result.

Currying printf--dependently

Currying printf poses an extra challenge because (1) printf accepts a variable number of arguments and (2) the order of the types of the arguments is not fixed (past the first argument). More accurately, the order of the types of the arguments is determined by the format string. The format string of printf is a value---usually, a literal string. We want to make the types of the rest of the arguments dependent on the value of the first argument. That's pretty intriguing, imo. In effect, we need a way to codify the format string literal into a type and that's where the dependent-typing comes into play.

To codify a string literal into a type, we are going to use the C++ language feature proposed in N3599. This proposal includes an example of dependently-typed printf that accepts all arguments at once. We're going to twist it a little bit to accept one argument at a time.

The magic lies in the operator "" that converts a string literal into a type. Here's the code without further ado. Both clang and gcc support this extension. Perhaps it will be in C++17 standard soon or it already is.

The CharSeq type is a synonym for std::integer_sequence<char, ...>. _lift is a function that uses C++11 user-defined literals syntax convert a string literal to an equivalent CharSeq at compile-time. For example, "cpptruths"_lift returns std::integer_sequence<char,'c','p','p','t','r','u','t','h','s'>. Check this code out.

Once a string is encoded as a type, a lot of things begin to fall into place using some additional template meta-programming. First, we need to codify the type-level CharSeq into a tuple of types that directly specify the types expected by printf. For instance, "%d" expects an int and "%s" expects and const char * etc.
We implement a meta-function called StringToTuple.

StringToTuple meta-function uses a pattern-matching. Consider the %s specialization. When the beginning of the CharSeq is '%' followed by 's', the specialization matches recursively computes the type of the tail, which is tuple<int> in this case. The Append meta-function simply concatenates the types in a tuple at the head.

If the beginning of the CharSeq is not a '%', the first most generic version with char Any matches, which simply ignores the leading character.

Fun does not end here though. We still need to curry printf. All we have at this stage is a sequence of types and that's big leap forward.

Let's assume you have a function curried_printf_impl that accepts a format string and a CharSeq as follows.

We've not talked about the curry template yet. Of course, it's going to use the FormatType tuple and turn it into a sequence of curried functions. The curried_printf macro helps us cleanly separate the string literal from the compile-time character sequence into two separate arguments. ## is token-pasting operator in the C preprocessor.

The general case of the curry template has an apply function that accepts arbitrary number of arguments and returns a closure that captures all those arguments (from apply) and takes exactly one more argument of Head type. As soon as it has the Head argument, it forwards it with all previous arguments to the subsequent curry<Tail...>::apply to accept and retain remaining arguments one by one. The single argument curry (the one with just Head), terminates the recursion and returns a lambda that upon receiving the last argument calls printf. Note that the format string literal is always at the beginning of args... as curried_printf_impl passes it along. If format string is the only argument, curry::apply calls printf right-away in the last no-argument specialization.

If you mess up with the argument types, the error is short and relatively direct.

Avoiding Copying Arguments

The previous example makes a massive assumption that all arguments are fundamental types. That they are cheap to copy. The lambda inside the apply function captures the arguments by value and passes them on by value. The arguments are copied O(N*N) times approximately. That's gonna hurt for large types that are expensive to copy.

The Remedy is to std::move the arguments as much as possible. However, forwarding variadic arguments requires us to take some library help: std::tuple.

It got complicated real fast. For each argument, we'll have to wrap them in a tuple and unwrap them before passing to curry::apply. Wrapping is easy. There's the code. Unwrapping is rather complicated because all arguments are not together in a tuple. Head comes separately. std::apply and std::invoke did not appear particularly useful in this case. We perhaps need a direct syntax to expand tuple into function arguments. Secondly, there's at least one copy of each Head argument anyway because the function should be type-safe and accept only Head type argument in the lambda. I thought this is more trouble than it's worth.

Currying Arbitrary Functions

To work around this problem I'm simply going to use a dynamically allocated tuple that will store the arguments as they come in. As curried function may be copied multiple times, this scheme should work out quite efficiently in such cases.

There are three main differences in this more general implementation than the previous example.

This implementation uses an explicit compile-time index to copy arguments in to the right slot in the tuple of arguments.

There's more type related noise here because each call to apply passes the shared_ptr of the tuple type to the inner lambda.

The final dispatch to the function is implemented in the execute function that expands all the arguments in the tuple as function arguments. In C++17, std::experimental::apply can replace the execute function.

While currying C++ functions is fun, lifting C++ string literals to type-level opens up a whole new level of meta-programming in C++. constexpr functions can operate on string literals and compute integral results at compile-time. See this for an example. With constexpr function, however, we can't construct new types at compile-time depending upon the argument value. N3599 allows us to cross the string-to-type barrier at compile-time. That's pretty neat. I can already think of some intriguing applications of N3599 in serialization/deserialization of user-defined types.