pi/src/Core/Misc.idr

80 lines
1.6 KiB
Idris
Raw Normal View History

2022-07-23 03:38:15 +02:00
module Core.Misc
2022-04-23 15:18:06 +02:00
2022-05-13 19:46:05 +02:00
import Control.Monad.RWS
import Control.Monad.Identity
import Control.Monad.Either
2022-04-23 15:18:06 +02:00
import Data.Nat
2022-07-21 19:51:55 +02:00
import Data.Vect
2022-07-26 23:07:13 +02:00
import Data.IORef
import Data.IOArray
2022-04-23 15:18:06 +02:00
%default total
public export
Index : Type
Index = Nat
public export
Name : Type
Name = String
public export
PI : Type -> Type
2022-07-26 23:07:13 +02:00
PI = EitherT String IO
2022-05-13 19:46:05 +02:00
public export
2022-07-26 23:07:13 +02:00
data NST : Type where
public export
data DTY : Type where
public export
data DTR : Type where
public export
data RefP : Type -> Type -> Type where
MkRefP : (label : Type) -> a -> RefP label a
public export
RefA : Type -> Type -> Type
RefA label a = RefP label (IOArray a)
public export
Ref : Type -> Type -> Type
Ref label a = RefP label (IORef a)
public export
getRef : HasIO io => (label : Type) -> {auto ref : Ref label a} -> io a
getRef _ {ref = MkRefP _ ref} = readIORef ref
public export
putRef : HasIO io => (label : Type) -> {auto ref : Ref label a} -> a -> io ()
putRef _ {ref = MkRefP _ ref} = writeIORef ref
public export
getArr : HasIO io => (label : Type) -> {auto ref : RefA label a} -> Int -> io (Maybe a)
getArr _ {ref = MkRefP _ ref} = readArray ref
public export
putArr : HasIO io => (label : Type) -> {auto ref : RefA label a} -> Int -> a -> io Bool
putArr _ {ref = MkRefP _ ref} = writeArray ref
public export
resolve : PI a -> IO (Either String a)
resolve a = runEitherT a
2022-04-23 16:38:53 +02:00
public export
oops : String -> PI a
2022-05-13 19:46:05 +02:00
oops = left
2022-07-21 04:18:50 +02:00
public export
guardS : String -> Bool -> PI ()
guardS str True = pure ()
guardS str False = oops str
2022-05-13 19:46:05 +02:00
public export
2022-07-26 23:07:13 +02:00
fresh : {auto frst : Ref NST Nat} -> PI Nat
2022-05-13 19:46:05 +02:00
fresh = do
2022-07-26 23:07:13 +02:00
i <- getRef NST
putRef NST (S i)
2022-05-13 19:46:05 +02:00
pure i