let unwind_protect thunk after = let result = try thunk () with e -> after (); raise e in after (); result