free-semiring: use shared_ptr to avoid copying