That would be
val bracket =
if dCntxt > dHere
then fn s => "(" ^ s ^ ")"
else fn x => x
The function compares the precedence level of your context against the precedence level of the outer operator of your expression and decides to either insert a pair of parentheses around the given string or not.