-
-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward function declarations are not needed for simple recursion #408
Comments
Nice! The only reason the forward declarations were in there is that I couldn't figure out how to include them on the fly using the Spirit Qi parser because of the way I had scoped semantic actions. |
They're still useful if you need mutual recursion (I think that any C-like language will require them for that case, only when you have something like OCaml's |
I don't recall Java requiring forward declarations for mutual recursion. Isn't it just a matter of how long we wait before resolving function signature well formedness? |
I suppose you could scan through once and treat each function as if it was a forward declaration, and then scan a second time and verify they are well-typed in their definitions (I think this is what let-and ends up doing), forward declarations just allow you to do it in a single typechecking pass. |
I realize the constraints mean you can't resolve mutual recursion in a single pass without forward declarations. I was trying to ask how they're resolved and if a multi-pass approach would be possible for stanc3. It'd be nice to remove the recursion-oriented forward declarations altogether if possible. |
I can't find anything explicitly describing how it is done in a language like Java, I'm just assuming they do this sort of preliminary pass. There's certainly no reason we couldn't do it in Stan if you'd like to add it to the wishlist next to function overloading |
Actually, we already do a second pass over the functions block to make sure that all functions which were declared eventually received definitions. So we could definitely have another |
Do we have some equivalent of this vignette on external functions in the docs somewhere? It's worth noting that standalone declarations are useful for this context to whatever extent it is supported |
|
Created stan-dev/stanc3#976 for the first bullet and #410 for the second bullet. |
Cool! And thanks, @nhuurre ---I've been wanting to modify the way user-defined functions work to do this and allow overloading ever since they were introduced. |
Summary:
In the reference manual, both here and here it is stated that a forward declaration is needed for recursion. This is not true in stanc3 -- function signatures are added to the symbol table before the body is checked, so they are optional unless you need complicated mutual recursion or are defining an external function.
This can be verified by compiling the fibonacci example with and without the forward declaration.
Current Version:
v2.27.0
The text was updated successfully, but these errors were encountered: