type Context = Map.Map VarName (Type, Integer)
type Memory = Map.Map Integer LitVal
type Functions = Map.Map VarName (Stmt, Context)
data InterpreterM stmts a = ExeInterpreter stmts a | PropInterpreter stmts a
newtype InterpreterMT stmts m a = InterpreterMT { runInterpreterMT :: stmts -> m (InterpreterM stmts a) }
type Interpreter = InterpreterMT Stmts (StateT (Memory, Functions) (ReaderT (Context, Context) (ErrorT String IO)))
instance (Monad m) => Monad (InterpreterMT s m) where
return x = InterpreterMT $ \stmts -> return (ExeInterpreter stmts x)
x >>= f = InterpreterMT $ \stmts -> do
m <- runInterpreterMT x stmts
case m of
(ExeInterpreter ss a) -> (runInterpreterMT (f a) ss)
data Stmts = Statements Stmt Stmts | EmptyStmts
nextStatement :: <HERE>
こんにちは、 ご覧のとおり、Stmts
には再帰的な定義があります。今、私はnextStatement
の機能を実装したいと思いますが、私はモナドでそれを行う方法を想像できません。 モナドを超えて、それは簡単ですが、その後、文は私が意味する、提供する必要があります。モナドのフィールド。 Haskell
nextStatement :: Stmts -> Stmt
nextStatement (Statements s ss) = s
nextStatement EmptyStatement = EmptyStatement
を書くことができるのだろうか?私はあなたが 'nextStatement'を定義するのと同じ問題は、空のリストに' head :: [a] - > a'を定義しようとするのと同じだと思います。あなたはすることはできません、そして、それは 'head []'がエラーを投げる理由です。 – ErikR
少なくともあなたの問題を再現できる十分なコードを含めることは丁寧です。これは、インポートと、必要なすべての型定義を意味します。 'VarName'、' Type'、 'LitVal'、' Stmt'とは何ですか?私たちはこのことを突き詰めることができますが、将来的には最小の実例を含めてください。 –
@DanielWagnerあまりにも多くのコードを置かないようにしたいと思っていました。私は今すぐ添付することができます。 – Gilgamesz