>>17182531x+1 = s(x) = union(x, {x})
{} is 0
{{}} is 1
{{},{{}}} is 2
{{}, {{}}, {{},{{}}}} is 3
{{}, {{}}, {{},{{}}}, {{}, {{}}, {{},{{}}}}} is 4
add(0,y)=y
add(s(x),y) = s(add(x,y)) = add(union(x, {x}), y) = union(add(x,y), {add(x,y)})
add({{},{{}}}, {{},{{}}})
= add(2,2)
= s(add(1,2))
= s(s(add(0,2)))
= s(s(2))
= union(s(2), {s(2)})
= union({{}, {{}}, {{},{{}}}}, {{{}, {{}}, {{},{{}}}}})
= {{}, {{}}, {{},{{}}}, {{}, {{}}, {{},{{}}}}}
= 4
You can do the following CTRL+H in a regex-capable editor to replace all, step by step, to get a little "movie" of this post where you can skip back and forward with CTRL+Z and CTRL+Y:
{} -> 0
{0, 1} -> 1
{0, 1, 2} -> 2
{0, 1, 2, 3} -> 4
s\(([^\(]+)\) -> $1+1
add\(([^,]+),([^,])\) -> $1+$2
union\(([^\(]+),[^\(\)]+\) -> $1+1
2+1 -> 3
3+1 -> 4