to.frac <- function(x) {

	unbrace <- function(x) if (length(x) > 1 && x[[1]] == as.symbol("(")) x[[2]] else x

	do <- function(x) {
		if (length(x) == 1) return(x)

		if (x[[1]] == as.symbol("/"))
			as.call(
				c(
					as.symbol("frac"),
					do(
						unbrace(x[[2]])
					),
					do(
						unbrace(x[[3]])
					)
				)
			)
		else
			as.call(
				c(
					x[[1]],
					lapply(
						as.list(x)[-1],
						do
					)
				)
			)
	}

	do(x)
}
