Hello Andrea!
In the first example you sent, an explicit Runge-Kutta scheme is indeed used, or more precisely, the RK4 scheme. If you want to use an implicit Runge-Kutta scheme such as collocation instead, there are basically two possible approaches:
1. You can embed an "implicit function" in the expression graph. CasADi will then indeed calculate the derivatives using rules derived via the implicit function theorem. Since CasADi uses the source-code-transformation approach to AD, a new expression graph for the directional derivatives will be generated. This expression graph will contain calls to functions for calculating forward and reverse mode directional derivatives of the root-finding problem as well as calls for calculating the Jacobian as well as solving the resulting linear system (transposed for reverse mode AD). The linear solver and Jacobian functions used to calculate the directional derivatives are typically the same as what is being used internally to evaluate the implicit function (i.e. solve the root-finding problem). Higher order derivatives can be calculated by applying AD on the resulting graph, which in turn will exploit the derivative propagation rules for linear system solves. An implementation of this approach can be found in the example https://github.com/casadi/casadi/blob/master/docs/examples/python/implicit_runge-kutta.py. Note that this approach will calculate the exact derivative of the approximate solution to the initial-value problem.
2. A second approach, which will calculate an approximate derivative to the exact solution to the initial-value problem is possible if you use the "Integrator" class in CasADi. Here, CasADi will differentiate the DAE function and then generate a new integrator for integrating the augmented DAE system (original DAE + sensitivity equations). Since CasADi is using a special integrator formulation that can solve an initial-value problem in DAE _coupled_to_ a terminal value problem in DAE, this approach is possible for both forward and reverse mode derivatives to arbitrary order. You will find more details about this in my PhD thesis:
https://lirias.kuleuven.be/bitstream/123456789/418048/1/thesis_final2.pdf. An implementation of this approach can be found in
https://github.com/casadi/casadi/blob/master/docs/examples/python/sensitivity_analysis.py.
Best regards,
Joel