Skip to content
This repository has been archived by the owner on Feb 2, 2021. It is now read-only.

Latest commit

 

History

History
37 lines (26 loc) · 1.22 KB

README.md

File metadata and controls

37 lines (26 loc) · 1.22 KB

purescript-safely

A combinator for making monadic controls operator stack-safe (using MonadRec).

For example, purescript-lists defines replicateM_, which is not stack-safe. It gives a stack overflow when applied to large inputs using, for example, the Eff monad.

> replicateM_ 100000 (log "Testing...")

RangeError: Maximum call stack size exceeded

However, we can make it stack-safe by using the safely combinator.

For any MonadRec, we can form the Monad FreeT Identity m in which arbitrarily-associated binds are stack-safe.

The safely function performs the wrapping and unwrapping for us, and provides a pair of natural transformations to lift and lower actions to and from the safe monad.

This means that we can write stack-safe monadic code for any MonadRec by simply writing the naive implementation, and wrapping it in a call to safely:

replicateS_ :: forall m a. MonadRec m => Int -> m a -> m Unit
replicateS_ n x = safely \lift _ ->
  let go i | i <= 0 = pure unit
           | otherwise = do lift x
                            go (i - 1)
  in go n

Documentation

Module documentation is published on Pursuit.