Nerodia Reference Manual

9. Monads🔗

The core monads of Nerodia are PyIO and its error-less variant PyBaseIO, which equip BaseIO with a Python context. Each provides a utility function to convert it back to its Python-less equivalent by allocating a temporary Python context for the call. However, where possible, they should be run within another Python-equipped monad to efficiently reuse its context.

🔗def
Nerodia.PyIO (α : Type) : Type
Nerodia.PyIO (α : Type) : Type

The primary monad for code using Python.

🔗def
Nerodia.PyBaseIO (α : Type) : Type
Nerodia.PyBaseIO (α : Type) : Type

A PyIO monad that cannot throw exceptions.

🔗def
Nerodia.PyIO.toIO {α : Type} (x : PyIO α) : IO α
Nerodia.PyIO.toIO {α : Type} (x : PyIO α) : IO α

Runs the PyIO function in IO.

If an exception is raised, it will be formatted in the standard Python manner (see PyBaseException.sprint) and reported as an IO.userError.

This creates a new temporary Python context for the call. As such, it should only be used when a Python context is not available. For instance, this can be used in main to run a PyIO function. It is also used to run PyIO in #eval.

Example

def main : IO Unit := do let pyVer ← Nerodia.PyIO.toIO do let sys ← Nerodia.import "sys" let ver ← sys.getAttrByString "version" return (← ver.str).toString IO.println pyVer
🔗def
Nerodia.PyIO.toEIO {α : Type} (x : PyIO α) : EIO PyBaseException α
Nerodia.PyIO.toEIO {α : Type} (x : PyIO α) : EIO PyBaseException α

Runs the PyIO function in EIO.

This creates a new temporary Python context for the call. As such, it should only be used when another Python context is not available. Otherwise, lift x into a supporting monad.

🔗def
Nerodia.PyBaseIO.toBaseIO {α : Type} (x : PyBaseIO α) : BaseIO α
Nerodia.PyBaseIO.toBaseIO {α : Type} (x : PyBaseIO α) : BaseIO α

Runs the PyBaseIO action in BaseIO, using a new Python context.

9.1. C Returns🔗

Return contexts used for optimized C functions. They are not monads themselves, but they automatically lift into their corresponding PyIO-style monad (i.e., CPyIO to PyIO, CPyBaseIO to PyBaseIO).

🔗def
Nerodia.CPyIO (α : Type) : Type
Nerodia.CPyIO (α : Type) : Type

Return context for external CPython functions that return an object and may raise an exception.

Not a monad itself, but lifts into monads equipped with a Python context.

🔗def
Nerodia.CPyBaseIO (α : Type) : Type
Nerodia.CPyBaseIO (α : Type) : Type

Return context for external CPython functions that return an object and cannot raise an exception.

Not a monad itself, but lifts into monads equipped with a Python context.

🔗def

Return type for external CPython functions that may error but do not return a Python object.

Not a monad itself, but lifts into monads equipped with a Python context.