This works at least for simple examples, although it's probably a bit restrictive on the array indexes you're allowed; it should attempt to constant-fold them.