Dynamic programming by polymorphic semiring algebraic shortcut fusion