Bob has suggested that it might be desirable to add support for first-class functions, higher-order functions and closures to Stan. In particular, he notes that these would be useful for the following purposes:
Mixture modeling like simple mixtures or HMMs. We can write a general
HMM forward algorithm if we could somehow abstract the emission probability
Our ODE integrator is based on a system function which would hugely benefit
from closure-based binding of data and parameter variables.
Mapping a unary function over a collection (we want to do this all the time)
Additionally, wds15 raised the issue in the context of the Stan 3 language:
I haven’t seen any news on functional programming… is that planned as well?
I would like to Curry my functions, pass functions around and wrap them into closures. I know this is asking for a lot, but if this is a thread for wishes, then those are mine.
Personally, I am interested in the issue from a more theoretical programming language semantics point of view. (I am working on the topic as a postdoc with Luke Ong in Oxford.) I am hoping this would close some of the gap with some of the work on probabilistic programming in the academic PL community. Moreover, the implementation of these constructs seems more feasible with the move to C++ 11. I am keen to give it a shot, if this is indeed something that people are interested in.
Therefore, I thought I’d open a thread on the issue.
I’d be really interested in hearing your thoughts on:
- use cases for the above language constructs and their desirability from a practitioners point of view;
- issues to do with a possible implementation;
- related suggestions;
- volunteers to join me in this endeavour :-) .