2
我寫簡單的解釋器,我想存儲變量。到目前爲止我有:哈斯克爾 - 存儲變量
-- MyEnv is a map from strings into integers
type MyEnv = M.Map String Int
type TCM a = ErrorT String (StateT MyEnv IO) a
我有一個定義
ms_assgn :: Assgn -> TCM()
ms_assgn (Assgn (Ident ident) exp) = do
map <- get
w1 <- ms_exp exp
put (M.insert (ident w1 map))
,我已經獲得了以下mistatkes:
Interpret.hs:118:5:
Couldn't match type `Envnt' with `a0 -> M.Map k0 a0 -> M.Map k0 a0'
When using functional dependencies to combine
MonadState s (StateT s m),
arising from the dependency `m -> s'
in the instance declaration in `Control.Monad.State.Class'
MonadState (a0 -> M.Map k0 a0 -> M.Map k0 a0) (StateT Envnt IO),
arising from a use of `put' at Interpret.hs:118:5-7
In a stmt of a 'do' block: put (M.insert (ident w1 map))
In the expression:
do { map <- get;
w1 <- ms_exp exp;
put (M.insert (ident w1 map)) }
Interpret.hs:118:20:
Couldn't match expected type `Integer -> Envnt -> k0'
with actual type `[Char]'
The function `ident' is applied to two arguments,
but its type `String' has none
In the first argument of `M.insert', namely `(ident w1 map)'
In the first argument of `put', namely `(M.insert (ident w1 map))'
當我註釋掉最後一行用放和替換它與返回()它沒有任何合理的,但至少它編譯。 的ms_assgn功能我的理解是這樣的:
- 首先,我讓我的當前狀態
- 下一個我eveluate EXP到W1
- Finanlly我想UPADTE我的狀態
什麼是錯的用它?任何提示?
+1 OMG。感謝您的快速響應!我會在幾分鐘內接受答案。編輯:並感謝更多的解釋。我真的很喜歡這個! – JosephConrad 2013-05-04 18:56:35