程序代写代做代考 module Trycat where

module Trycat where

import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map

import TrycatDef

— Question 1.

instance Functor TC where
fmap f tc = tc >>= \a -> pure (f a)

instance Applicative TC where
pure a = MkTC (\s -> (s, Right a))
tf <*> ta = tf >>= \f -> ta >>= \a -> pure (f a)

instance Monad TC where
return a = MkTC (\s -> (s, Right a))

MkTC stf >>= k = error “TODO”

instance MonadTrycat TC where

putVar v x = error “TODO”

getVar v = error “TODO”

raise e = error “TODO”

tryCatch (MkTC stf1) toCatch t2 = error “TODO”

— Question 2.

run :: Stmt -> (Map String Integer, Either Exception ())
run stmt = runWith stmt Map.empty

runWith :: Stmt -> Map String Integer -> (Map String Integer, Either Exception ())
runWith stmt s0 = unTC (interp stmt) s0

interp :: MonadTrycat m => Stmt -> m ()

interp = error “TODO”

— You may find it nicer to implement and use this helper for expressions.
— eval :: MonadTrycat m => Expr -> m Integer
— eval = error “TODO”