小编Goa*_*t16的帖子

Haskell中的Monadic类型检查器

我正在从BNFC开始在Haskell中编写解析器和类型检查器.类型检查器的主要功能实现如下:

typecheck :: Program -> Err ()
typecheck (PDefs ds) = do
    env  <- foldM (\env  (DFun typ id args _ _) -> 
               updateFun env id (argTypes args,typ) ) (emptyEnv) (ds)
    mapM_ (checkDef env) ds 
    where argTypes = map (\(ADecl _ typ _) -> typ)
Run Code Online (Sandbox Code Playgroud)

where PDefs,DFunADecl是在语言的抽象语法中定义的代数数据类型的构造函数,checkDef并且updateFun是函数.Program是语法的"起点".使用的monad是monad Err:

    data Err a = Ok a | Bad String
       deriving (Read, Show, Eq, Ord)

    instance Monad Err where
       return      = …
Run Code Online (Sandbox Code Playgroud)

monads haskell compilation typechecking

5
推荐指数
1
解决办法
381
查看次数

标签 统计

compilation ×1

haskell ×1

monads ×1

typechecking ×1