A Foundation for Metareasoning Part I: The Proof Theory