### Formal implicit differentiation

I am not perfectly sure that the following code ~~evaluate ~~evaluates the implicit differentiation of the function `UU`

. In ~~all ~~any case what I expect is to find $\partial w_2/ \partial w_1$ and then $\partial^2 w_2/ \partial ~~w_1^2$~~w_1^2$.

~~U=function('U')(x)
w2=function('w2')(w1)
U=U(x)
UU=p*U(w1)+(1-p)*U(w2)
~~U = function('U')(x)
w2 = function('w2')(w1)
U = U(x)
UU = p*U(w1) + (1 - p)*U(w2)
diff(UU, w1)

then I would like to substitute ~~$U(x) ~~`U(x) `

`= `~~\log(x)$ ~~log(x)

or ~~$U(x) ~~`U(x) `

`= `~~x^a$ ~~x^a

or ~~$U(x) ~~`U(x) `

`= `~~-\exp(-a x)$. ~~-exp(-a*x)

. And as ~~ ~~an implied question, I would like to know how to select part of an expression like for ~~instance $\exp(a~~*x **instance*`exp(a*x `

`+ `~~b)+ f(x)$ ~~b) + f(x)

, ~~$a$, ~~`a`

, or `a*x`

or ~~ $a~~x$ or $f(x)$. `f(x)`

. As every expression in Mathematica is a ~~three, ~~tree, is not difficult to saw a branch. But in ~~Sagemath ~~SageMath I wonder how to do.