less prototype, less bad code implementation of CCHM type theory
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

  1. {-# LANGUAGE DeriveAnyClass #-}
  2. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  3. {-# LANGUAGE DerivingVia #-}
  4. module Elab.Monad where
  5. import Control.Monad.Reader
  6. import Control.Exception
  7. import qualified Data.Map.Strict as Map
  8. import Data.Map.Strict (Map)
  9. import Data.Typeable
  10. import Syntax
  11. newtype ElabEnv = ElabEnv { getEnv :: Map Name (NFType, Value) }
  12. newtype ElabM a = ElabM { runElab :: ElabEnv -> IO a }
  13. deriving (Functor, Applicative, Monad, MonadReader ElabEnv, MonadIO)
  14. via ReaderT ElabEnv IO
  15. newtype NotInScope = NotInScope { getName :: Name }
  16. deriving (Show, Typeable)
  17. deriving anyclass (Exception)
  18. emptyEnv :: ElabEnv
  19. emptyEnv = ElabEnv mempty
  20. assume :: Name -> Value -> ElabM a -> ElabM a
  21. assume nm ty = local go where
  22. go = ElabEnv . Map.insert nm (ty, VVar nm) . getEnv
  23. define :: Name -> Value -> Value -> ElabM a -> ElabM a
  24. define nm ty vl = local go where
  25. go = ElabEnv . Map.insert nm (ty, vl) . getEnv
  26. getValue :: Name -> ElabM Value
  27. getValue nm = do
  28. vl <- asks (Map.lookup nm . getEnv)
  29. case vl of
  30. Just v -> pure (snd v)
  31. Nothing -> liftIO . throwIO $ NotInScope nm
  32. getNfType :: Name -> ElabM NFType
  33. getNfType nm = do
  34. vl <- asks (Map.lookup nm . getEnv)
  35. case vl of
  36. Just v -> pure (fst v)
  37. Nothing -> liftIO . throwIO $ NotInScope nm