pi/src/Misc.idr

55 lines
890 B
Idris

module Misc
import Control.Monad.RWS
import Control.Monad.Identity
import Control.Monad.Either
import Data.Nat
import Data.Vect
%default total
public export
Index : Type
Index = Nat
public export
Name : Type
Name = String
public export
PI : Type -> Type
PI = EitherT String (RWS () (List String) Nat)
public export
resolve : PI a -> Either String (a, List String)
resolve a = case runRWS (runEitherT a) () 0 of
(Left e, _) => Left e
(Right r, _, s) => Right (r, s)
public export
oops : String -> PI a
oops = left
public export
guardS : String -> Bool -> PI ()
guardS str True = pure ()
guardS str False = oops str
public export
fresh : PI Nat
fresh = do
i <- get
put (S i)
pure i
public export
logS : String -> PI ()
logS = tell . (`Prelude.(::)` [])
public export
headM : Vect n a -> Maybe a
headM [] = Nothing
headM (x :: _) = Just x