@@ -110,3 +110,101 @@ end
110110f = g ()
111111@show f (1 )
112112```
113+
114+ ## Retrieving Expressions
115+
116+ From a constructed RuntimeGeneratedFunction, you can retrieve the expressions using the
117+ ` RuntimeGeneratedFunctions.get_expression ` command. For example:
118+
119+ ``` julia
120+ ex = :((x) -> x^ 2 )
121+ rgf = @RuntimeGeneratedFunction (ex)
122+ julia> RuntimeGeneratedFunctions. get_expression (rgf)
123+ #=
124+ quote
125+ #= c:\Users\accou\OneDrive\Computer\Desktop\test.jl:39 =#
126+ x ^ 2
127+ end
128+ =#
129+ ```
130+
131+ This can be used to get the expression even if ` drop_expr ` has been performed.
132+
133+ ### Example: Retrieving Expressions from ModelingToolkit.jl
134+
135+ [ ModelingToolkit.jl] ( https://github.com/SciML/ModelingToolkit.jl ) uses
136+ RuntimeGeneratedFunctions.jl for the construction of its functions to avoid issues of
137+ world-age. Take for example its tutorial:
138+
139+ ``` julia
140+ using ModelingToolkit, RuntimeGeneratedFunctions
141+ using ModelingToolkit: t_nounits as t, D_nounits as D
142+
143+ @mtkmodel FOL begin
144+ @parameters begin
145+ τ # parameters
146+ end
147+ @variables begin
148+ x (t) # dependent variables
149+ end
150+ @equations begin
151+ D (x) ~ (1 - x) / τ
152+ end
153+ end
154+
155+ using DifferentialEquations: solve
156+ @mtkbuild fol = FOL ()
157+ prob = ODEProblem (fol, [fol. x => 0.0 ], (0.0 , 10.0 ), [fol. τ => 3.0 ])
158+ ```
159+
160+ If we check the function:
161+
162+ ``` julia
163+ julia> prob. f
164+ (:: ODEFunction{true, SciMLBase.AutoSpecialize, ModelingToolkit.var"#f#697"{RuntimeGeneratedFunction{(:ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x2cce5cf2, 0xd20b0d73, 0xd14ed8a6, 0xa4d56c4f, 0x72958ea1), Nothing}, RuntimeGeneratedFunction{(:ˍ₋out, :ˍ₋arg1, :ˍ₋arg2, :t), ModelingToolkit.var"#_RGF_ModTag", ModelingToolkit.var"#_RGF_ModTag", (0x7f3c227e, 0x8f116bb1, 0xb3528ad5, 0x9c57c605, 0x60f580c3), Nothing}}, UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, ModelingToolkit.var"#852#generated_observed#706"{Bool, ODESystem, Dict{Any, Any}, Vector{Any}}, Nothing, ODESystem, Nothing, Nothing} ) (generic function with 1 method)
165+ ```
166+
167+ It's a RuntimeGeneratedFunction. We can find the code for this system using the retrieval
168+ command on the function we want. For example, for the in-place function:
169+
170+ ``` julia
171+ julia> RuntimeGeneratedFunctions. get_expression (prob. f. f. f_iip)
172+
173+ :((ˍ₋out, ˍ₋arg1, ˍ₋arg2, t)-> begin
174+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:373 =#
175+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:374 =#
176+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:375 =#
177+ begin
178+ begin
179+ begin
180+ #= C:\Users\accou\.julia\packages\Symbolics\HIg7O\src\build_function.jl:546 =#
181+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:422 =# @inbounds begin
182+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:418 =#
183+ ˍ₋out[1 ] = (/ )((+ )(1 , (* )(- 1 , ˍ₋arg1[1 ])), ˍ₋arg2[1 ])
184+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:420 =#
185+ nothing
186+ end
187+ end
188+ end
189+ end
190+ end )
191+ ```
192+
193+ or the out-of-place function:
194+
195+ ``` julia
196+ julia> RuntimeGeneratedFunctions. get_expression (prob. f. f. f_oop)
197+ :((ˍ₋arg1, ˍ₋arg2, t)-> begin
198+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:373 =#
199+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:374 =#
200+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:375 =#
201+ begin
202+ begin
203+ begin
204+ #= C:\Users\accou\.julia\packages\SymbolicUtils\c0xQb\src\code.jl:468 =#
205+ (SymbolicUtils. Code. create_array)(typeof (ˍ₋arg1), nothing , Val {1} (), Val {(1,)} (), (/ )((+ )(1 , (* )(- 1 , ˍ₋arg1[1 ])), ˍ₋arg2[1 ]))
206+ end
207+ end
208+ end
209+ end )
210+ ```
0 commit comments