Suppose I have three “library” stan files providing functionality I would like to reuse across different models. They are
-
common.stan
,
-
library1.stan
which includes common.stan
,
-
library2.stan
which includes common.stan
.
Now in my model.stan
I can only ever include either library1.stan
or library2.stan
but not both because the definitions from common.stan
are repeated. In C++, we get around that issue with include guards. Does stan support these?
The short answer is no, the only preprocessor instruction is #include
. Stan hardly has a preprocessor (we don’t use M4
or anything external), it is just implemented directly in the file reader of the compiler. When #include
is encountered, we swap which file buffer we are reading from before parsing the next token.
That being said, unless you for some reason need library1.stan
to compile on its own, there is no hard requirement that you include common
into it. Instead you could keep the hierarchy of includes very flat, and then in the final model simply include common, library1, and library2 in order.
This is slightly annoying, since it is possible to forget common
now, and library1
will make reference to things which won’t be obvious where they come from, but it’s a reasonable way to solve this problem.
Great, thank you for the clarification. Sounds like I just need to include the files in the right order starting with common.stan
.