gas- Limit how many steps a program may take.

Safe HaskellSafe



Dynamic gas consumption for free monads.



limitGas Source #


:: Monad m 
=> (forall x. f x -> Word)

Given an action, its cost.

-> (forall x. f x -> m x)

Natural transformation.

-> Free f a

Program to run and cut off.

-> Word

Initial gas.

-> m (Maybe a) 

Similar to foldFree, but limit the number of steps the interpretation of a free monad action may take. The number of steps is determined dynamically, while the program is running. It is the cummulative result of the cost function given to limitGas. The limit is called the gas, by typical car analogy.

If you want to predict, precisely, how much gas will be used by the computation, then you cannot do this if the computation is monadic, only if it is applicative. See the Control.Applicative.Free.Gas module.

Note that while there is still gas available, the interpretation may cause effects, even if it runs out of gas later!