1

私は現在、私が履修しているコースのための小さなコンパイラを書いています。だから私はtypecheckingを処理するためにこのモナド変圧器を書いて始めましたが、非常に暗い型のエラーがありました。機能的な依存関係を含む何か、私はそれほどよく知らない。 エラーを再現することができますプログラムからの小さな抜粋:この型検査エラーは何を意味しますか?

import Control.Monad.RWS.Lazy 
import qualified Data.Map as M 
import Control.Applicative 

--Placeholders for other data types. 
data TypeError = TypeError 
data Ident = Ident String 
data Type = Type 

type Typer a = RWS FunctionTable [TypeError] IdentTable a 

type FunctionTable = M.Map Ident Type 

type IdentTable = [M.Map Ident Type] 

emptyIdents :: IdentTable 
emptyIdents = [] 

getIdent :: Ident -> Typer (Maybe Type) 
getIdent id = getIdent' id <$> get 

getIdent' :: Ident -> IdentTable -> Maybe Type 
getIdent' _ [] = Nothing 
getIdent' id (x:xs) = 
    case M.lookup id x of 
    Just t -> Just t 
    Nothing -> getIdent' id xs 

putIdent :: Ident -> Type -> Typer() 
putIdent id ty = modify $ \xs -> case xs of 
    [] -> [M.singleton id ty] 
    (x:xs) -> (M.insert id ty x) : xs 

scopeEnter :: Typer() 
scopeEnter = modify $ \ids -> emptyIdents : ids 

scopeExit :: Typer() 
scopeExit = modify tail 

そして実際のメッセージ:

[1 of 1] Compiling Main    (ErrorExample.hs, interpreted) 

ErrorExample.hs:30:22: 
    Couldn't match type `M.Map Ident Type' with `Type' 
    When using functional dependencies to combine 
     MonadState 
     [[M.Map Ident Type]] 
     (RWST 
      (M.Map Ident Type) 
      [TypeError] 
      [M.Map Ident Type] 
      transformers-0.2.2.0:Data.Functor.Identity.Identity), 
     arising from a use of `modify' at ErrorExample.hs:35:18-23 
     MonadState 
     [M.Map Ident Type] 
     (RWST 
      (M.Map Ident Type) 
      [TypeError] 
      [M.Map Ident Type] 
      transformers-0.2.2.0:Data.Functor.Identity.Identity), 
     arising from a use of `modify' at ErrorExample.hs:30:22-27 
    In the expression: modify 
    In the expression: 
     modify 
     $ \ xs 
      -> case xs of { 
       [] -> [...] 
       (x : xs) -> (M.insert id ty x) : xs } 

ErrorExample.hs:30:22: 
    Couldn't match type `[]' with `M.Map Ident' 
    When using functional dependencies to combine 
     MonadState 
     [[M.Map Ident Type]] 
     (RWST 
      (M.Map Ident Type) 
      [TypeError] 
      [M.Map Ident Type] 
      transformers-0.2.2.0:Data.Functor.Identity.Identity), 
     arising from a use of `modify' at ErrorExample.hs:35:18-23 
     MonadState 
     [M.Map Ident Type] 
     (RWST 
      (M.Map Ident Type) 
      [TypeError] 
      [M.Map Ident Type] 
      transformers-0.2.2.0:Data.Functor.Identity.Identity), 
     arising from a use of `modify' at ErrorExample.hs:30:22-27 
    In the expression: modify 
    In the expression: 
     modify 
     $ \ xs 
      -> case xs of { 
       [] -> [...] 
       (x : xs) -> (M.insert id ty x) : xs } 

ErrorExample.hs:35:18: 
    Couldn't match type `Type' with `M.Map Ident Type' 
    When using functional dependencies to combine 
     MonadState s (RWST r w s m), 
     arising from the dependency `m -> s' 
     in the instance declaration in `Control.Monad.State.Class' 
     MonadState 
     [[M.Map Ident Type]] 
     (RWST 
      (M.Map Ident Type) 
      [TypeError] 
      [M.Map Ident Type] 
      transformers-0.2.2.0:Data.Functor.Identity.Identity), 
     arising from a use of `modify' at ErrorExample.hs:35:18-23 
    In the expression: modify 
    In the expression: modify $ \ ids -> emptyIdents : ids 

ErrorExample.hs:35:18: 
    Couldn't match type `M.Map Ident' with `[]' 
    When using functional dependencies to combine 
     MonadState s (RWST r w s m), 
     arising from the dependency `m -> s' 
     in the instance declaration in `Control.Monad.State.Class' 
     MonadState 
     [[M.Map Ident Type]] 
     (RWST 
      (M.Map Ident Type) 
      [TypeError] 
      [M.Map Ident Type] 
      transformers-0.2.2.0:Data.Functor.Identity.Identity), 
     arising from a use of `modify' at ErrorExample.hs:35:18-23 
    In the expression: modify 
    In the expression: modify $ \ ids -> emptyIdents : ids 
Failed, modules loaded: none. 
Leaving GHCi. 

答えて

4

非常に悪いエラーメッセージです。原因は州のタイプの不一致によるものです。あなたのコードは[M.Map Ident Type]と[[M.Map Ident Type]]を混在させようとしています。

を手動emptyIdentsが、それはこのようになりますscopeEnterに呼び出すインライン化した場合:[M.Map Ident Type]ためあまり意味がありません

scopeEnter :: Typer() 
scopeEnter = modify $ \ids -> [] : ids 

...。タイプチェックを行っているバージョンと比較してください:

scopeEnter :: Typer() 
scopeEnter = modify $ \ids -> M.empty : ids 
関連する問題