Expressions as higher order types?

Looking at an expression type to encompass queries of the Lua state that may require impure changes to the state in order to retreive a value, but can be made pure by reverting the state.