+from parse import :: ParserOutput, :: Error
+
+:: Gamma :== 'Map'.Map String Type
+:: Env a :== (State Gamma (Either SemError a))
+
+get = state $ \s -> (s,s)
+
+putIdent :: String Type -> Env Void
+putIdent i t = gets ('Map'.get i) >>= \mt -> case mt of
+ Nothing = pure <$> modify ('Map'.put i t)
+ Just t2 = unify t t2 >>= \r -> case r of
+ Left e = pure $ Left e
+ Right t3 = pure <$> modify ('Map'.put i t3)
+
+instance toString SemError where
+ toString (ParseError p e) = concat [
+ toString p,"SemError: ParseError: ", e]
+ toString (Error e) = "SemError: " +++ e
+ toString (UnifyError p t1 t2) = concat [
+ toString p,
+ "SemError: Cannot unify types. Expected: ",
+ toString t1, ". Given: ", toString t2]
+
+sem :: AST -> SemOutput
+sem (AST vd fd)
+# (eithervds, gamma) = runState (mapM semVarDecl vd) 'Map'.newMap
+# (eitherfds, gamma) = runState (mapM semFunDecl fd) gamma
+= case splitEithers eithervds of
+ (Left errs) = Left $ errs ++ [x\\(Left x)<-eitherfds]
+ (Right vds) = case splitEithers eitherfds of
+ (Left errs) = Left errs
+ (Right fds) = Right $ AST vds fds
+
+splitEithers :: [Either a b] -> Either [a] [b]
+splitEithers [] = Right []
+splitEithers [Right x:xs] = splitEithers xs >>= \rest->Right [x:rest]
+splitEithers xs = Left $ [x\\(Left x)<-xs]
+
+semFunDecl :: FunDecl -> Env FunDecl
+semFunDecl f = pure $ Right f