Automatically transform recursive functions to dynamic programming