from Control.Applicative import class Applicative
from Control.Monad import class Monad
+from Control.Monad.Trans import class MonadTrans
from Data.Functor import class Functor
from Data.Functor.Identity import :: Identity
from Data.Monoid import class Monoid, class Semigroup
instance Functor (RWST r w s m) | Monad m & Monoid w
instance Applicative (RWST r w s m) | Monad m & Monoid w
instance Monad (RWST r w s m) | Monad m & Monoid w
+instance MonadTrans (RWST r w s) | Monoid w
runRWST :: (RWST r w s m a) r s -> m (a, s, w)
evalRWST :: (RWST r w s m a) r s -> m (a, w) | Monad m
import Data.Functor
import Data.Monoid
import Control.Monad
+import Control.Monad.Trans
import Control.Applicative
// The RWS monad
>>= \(a, s`, w)->runRWST (k a) r s`
>>= \(b, s``,w`)->pure (b, s``, mappend w w`)
+instance MonadTrans (RWST r w s) | Monoid w where
+ liftT m = RWST \_ s->m >>= \a->pure (a, s, mempty)
+
runRWST :: (RWST r w s m a) r s -> m (a, s, w)
runRWST (RWST f) r s = f r s
from StdFunc import o, flip, const, id
import Control.Monad
+import Control.Monad.Trans
import Data.Either
import Data.Maybe
import Data.Monoid
class infer a :: a -> Infer Type
op2Type :: Op2 -> Infer Type
-op2Type op | elem op [BiPlus, BiMinus, BiTimes, BiDivide, BiMod]
- = pure (IntType ->> IntType ->> IntType)
- | elem op [BiEquals, BiUnEqual]
- = fresh >>= \t1-> fresh >>= \t2-> pure (t1 ->> t2 ->> BoolType)
- | elem op [BiLesser, BiGreater, BiLesserEq, BiGreaterEq]
- = pure (IntType ->> IntType ->> BoolType)
- | elem op [BiAnd, BiOr]
- = pure (BoolType ->> BoolType ->> BoolType)
- | op == BiCons
- = fresh >>= \t1-> pure (t1 ->> ListType t1 ->> ListType t1)
+op2Type op
+| elem op [BiPlus, BiMinus, BiTimes, BiDivide, BiMod]
+ = pure (IntType ->> IntType ->> IntType)
+| elem op [BiEquals, BiUnEqual]
+ = fresh >>= \t1-> fresh >>= \t2-> pure (t1 ->> t2 ->> BoolType)
+| elem op [BiLesser, BiGreater, BiLesserEq, BiGreaterEq]
+ = pure (IntType ->> IntType ->> BoolType)
+| elem op [BiAnd, BiOr]
+ = pure (BoolType ->> BoolType ->> BoolType)
+| op == BiCons
+ = fresh >>= \t1-> pure (t1 ->> ListType t1 ->> ListType t1)
op1Type :: Op1 -> Infer Type
op1Type UnNegation = pure $ (BoolType ->> BoolType)
op1Type UnMinus = pure $ (IntType ->> IntType)
+//instantiate :: Scheme -> Infer Type
+//instantiate (Forall as t) = mapM (const fresh) as
+
+lookupEnv :: String -> Infer Type
+lookupEnv ident = asks ('Map'.get ident)
+ >>= \m->case m of
+ Nothing = liftT $ Left $ UndeclaredVariableError zero ident
+ Just (Forall as t) = pure t //instantiate ???
+
instance infer Expr where
- infer (VarExpr _ vd) = undef
+ infer (VarExpr _ (VarDef ident fs)) = lookupEnv ident
infer (Op2Expr _ e1 op e2) = case op of
BiPlus = pure IntType
BiMinus = pure IntType