git.iko.soy/rowtype-yoga/purescript-native
on 100d787 help sign in
46 lines 1.3 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : PureScript.TypeChecker.Monad
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE GeneralizedNewtypeDeriving, FlexibleInstances #-}
17module PureScript.TypeChecker.Monad where
19import PureScript.Types
20import PureScript.Kinds
22import Control.Applicative
23import Control.Monad.State
24import Control.Monad.Error
26import qualified Data.Map as M
28data Environment = Environment
29 { names :: M.Map String Type
30 , types :: M.Map String Kind
31 } deriving (Show)
33emptyEnvironment :: Environment
34emptyEnvironment = Environment M.empty M.empty
36newtype Check a = Check { unCheck :: StateT Environment (Either String) a } deriving (Functor, Monad, Applicative, MonadPlus, MonadState Environment, MonadError String)
38check :: Check a -> Either String a
39check = flip evalStateT emptyEnvironment . unCheck
41guardWith :: (MonadError e m) => e -> Bool -> m ()
42guardWith _ True = return ()
43guardWith e False = throwError e
45rethrow :: (MonadError e m) => (e -> e) -> m a -> m a
46rethrow f = flip catchError $ \e -> throwError (f e)