diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-02-15 17:39:51 +0100 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-02-15 20:31:06 +0100 |
commit | 35e7f0ba7365059d8b7df9cacd4e180fff3179c1 (patch) | |
tree | 4b570a273b91bb0343063adf4c722cb40577c975 /src/LambdaCube/Compiler | |
parent | 6d6efec9eef6ed274a9396fbcb00e08d275949e9 (diff) |
refactoring
Diffstat (limited to 'src/LambdaCube/Compiler')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 23 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 22 |
2 files changed, 27 insertions, 18 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 1c60ae8a..5eefce9a 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -15,7 +15,7 @@ | |||
15 | {-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove | 15 | {-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove |
16 | -- {-# OPTIONS_GHC -O0 #-} | 16 | -- {-# OPTIONS_GHC -O0 #-} |
17 | module LambdaCube.Compiler.Infer | 17 | module LambdaCube.Compiler.Infer |
18 | ( Binder (..), SName, Lit(..), Visibility(..), Export(..), Module(..) | 18 | ( Binder (..), SName, Lit(..), Visibility(..) |
19 | , Exp (..), Neutral (..), ExpType, GlobalEnv | 19 | , Exp (..), Neutral (..), ExpType, GlobalEnv |
20 | , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, pattern LabelEnd | 20 | , pattern Var, pattern CaseFun, pattern TyCaseFun, pattern App_, pattern LabelEnd |
21 | , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun | 21 | , pattern Con, pattern TyCon, pattern Pi, pattern Lam, pattern Fun |
@@ -23,13 +23,13 @@ module LambdaCube.Compiler.Infer | |||
23 | , down, Subst (..), free | 23 | , down, Subst (..), free |
24 | , litType | 24 | , litType |
25 | , initEnv, Env(..), pattern EBind2 | 25 | , initEnv, Env(..), pattern EBind2 |
26 | , SI(..), Range(..) -- todo: remove | ||
26 | , Info(..), Infos, listAllInfos, listTypeInfos, listTraceInfos | 27 | , Info(..), Infos, listAllInfos, listTypeInfos, listTraceInfos |
27 | , PolyEnv(..), parseLC, joinPolyEnvs, filterPolyEnv, inference_ | 28 | , PolyEnv(..), joinPolyEnvs, filterPolyEnv, inference_ |
28 | , ImportItems (..) | ||
29 | , SI(..), Range(..) | ||
30 | , nType, conType, neutType, neutType', appTy, mkConPars, makeCaseFunPars, makeCaseFunPars' | 29 | , nType, conType, neutType, neutType', appTy, mkConPars, makeCaseFunPars, makeCaseFunPars' |
31 | , MaxDB(..), unfixlabel | 30 | , MaxDB(..), unfixlabel |
32 | , ErrorMsg, showError | 31 | , ErrorMsg, showError |
32 | , extractDesugarInfo | ||
33 | ) where | 33 | ) where |
34 | 34 | ||
35 | import Data.Monoid | 35 | import Data.Monoid |
@@ -1120,6 +1120,7 @@ initEnv = Map.fromList | |||
1120 | [ (,) "'Type" (TType, TType, (debugSI "source-of-Type", Nothing)) | 1120 | [ (,) "'Type" (TType, TType, (debugSI "source-of-Type", Nothing)) |
1121 | ] | 1121 | ] |
1122 | 1122 | ||
1123 | -- todo: eliminate | ||
1123 | extractDesugarInfo :: GlobalEnv -> DesugarInfo | 1124 | extractDesugarInfo :: GlobalEnv -> DesugarInfo |
1124 | extractDesugarInfo ge = | 1125 | extractDesugarInfo ge = |
1125 | ( Map.fromList | 1126 | ( Map.fromList |
@@ -1470,18 +1471,8 @@ instance MkDoc (CEnv Exp) where | |||
1470 | mfix' f = ExceptT (mfix (runExceptT . f . either bomb id)) | 1471 | mfix' f = ExceptT (mfix (runExceptT . f . either bomb id)) |
1471 | where bomb e = error $ "mfix (ExceptT): inner computation returned Left value:\n" ++ show e | 1472 | where bomb e = error $ "mfix (ExceptT): inner computation returned Left value:\n" ++ show e |
1472 | 1473 | ||
1473 | inference_ :: PolyEnv -> Module -> ExceptT ErrorMsg (WriterT Infos Identity) PolyEnv | 1474 | inference_ :: PolyEnv -> Extensions -> [Stmt] -> ExceptT ErrorMsg (WriterT Infos Identity) PolyEnv |
1474 | inference_ (PolyEnv pe is) m = do | 1475 | inference_ (PolyEnv pe is) exts defs = mapExceptT (ff . runWriter . flip runReaderT (exts, mempty)) $ gg (handleStmt defs) (initEnv <> pe) defs |
1475 | |||
1476 | ((defs, dns), ds) <- mfix $ \ ~(_, ds) -> do | ||
1477 | let (x, dns) = definitions m ds | ||
1478 | defs <- either (throwError' . ErrorMsg . show) return x | ||
1479 | let ds' = mkDesugarInfo defs `joinDesugarInfo` extractDesugarInfo pe | ||
1480 | return ((defs, dns), ds') | ||
1481 | |||
1482 | mapM_ (maybe (return ()) (throwError' . ErrorMsg)) dns | ||
1483 | mapExceptT (ff . runWriter . flip runReaderT (extensions m, mempty)) $ gg (handleStmt defs) (initEnv <> pe) $ sortDefs ds defs | ||
1484 | |||
1485 | where | 1476 | where |
1486 | ff (Left e, is) = do | 1477 | ff (Left e, is) = do |
1487 | tell is | 1478 | tell is |
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index c1215ee9..aa9efe28 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -15,7 +15,8 @@ module LambdaCube.Compiler.Parser | |||
15 | , pattern SVar, pattern SType, pattern Wildcard, pattern SAppV, pattern SLamV, pattern SAnn | 15 | , pattern SVar, pattern SType, pattern Wildcard, pattern SAppV, pattern SLamV, pattern SAnn |
16 | , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam | 16 | , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SLabelEnd, pattern SLam |
17 | , pattern TyType, pattern Wildcard_ | 17 | , pattern TyType, pattern Wildcard_ |
18 | , debug, LI, isPi, varDB, lowerDB, justDB, upDB, cmpDB, MaxDB (..), iterateN, traceD, parseLC | 18 | , debug, LI, isPi, varDB, lowerDB, justDB, upDB, cmpDB, MaxDB (..), iterateN, traceD |
19 | , parseLC, runDefParser | ||
19 | , getParamsS, addParamsS, getApps, apps', downToS, addForalls | 20 | , getParamsS, addParamsS, getApps, apps', downToS, addForalls |
20 | , mkDesugarInfo, joinDesugarInfo | 21 | , mkDesugarInfo, joinDesugarInfo |
21 | , Up (..), up1, up | 22 | , Up (..), up1, up |
@@ -1045,9 +1046,11 @@ data Module | |||
1045 | { extensions :: Extensions | 1046 | { extensions :: Extensions |
1046 | , moduleImports :: [(SIName, ImportItems)] | 1047 | , moduleImports :: [(SIName, ImportItems)] |
1047 | , moduleExports :: Maybe [Export] | 1048 | , moduleExports :: Maybe [Export] |
1048 | , definitions :: DesugarInfo -> (Either ParseError [Stmt], [PostponedCheck]) | 1049 | , definitions :: DefParser |
1049 | } | 1050 | } |
1050 | 1051 | ||
1052 | type DefParser = DesugarInfo -> (Either ParseError [Stmt], [PostponedCheck]) | ||
1053 | |||
1051 | parseModule :: FilePath -> String -> P Module | 1054 | parseModule :: FilePath -> String -> P Module |
1052 | parseModule f str = do | 1055 | parseModule f str = do |
1053 | exts <- concat <$> many parseExtensions | 1056 | exts <- concat <$> many parseExtensions |
@@ -1084,6 +1087,21 @@ parseLC f str | |||
1084 | . runP (error "globalenv used", Namespace (Just ExpLevel) True) f (parseModule f str) | 1087 | . runP (error "globalenv used", Namespace (Just ExpLevel) True) f (parseModule f str) |
1085 | $ str | 1088 | $ str |
1086 | 1089 | ||
1090 | --type DefParser = DesugarInfo -> (Either ParseError [Stmt], [PostponedCheck]) | ||
1091 | runDefParser :: (MonadFix m, MonadError String m) => DesugarInfo -> DefParser -> m [Stmt] | ||
1092 | runDefParser ds_ dp = do | ||
1093 | |||
1094 | ((defs, dns), ds) <- mfix $ \ ~(_, ds) -> do | ||
1095 | let (x, dns) = dp ds | ||
1096 | defs <- either (throwError . show) return x | ||
1097 | let ds' = mkDesugarInfo defs `joinDesugarInfo` ds_ | ||
1098 | return ((defs, dns), ds') | ||
1099 | |||
1100 | mapM_ (maybe (return ()) throwError) dns | ||
1101 | |||
1102 | return $ sortDefs ds defs | ||
1103 | |||
1104 | |||
1087 | -------------------------------------------------------------------------------- pretty print | 1105 | -------------------------------------------------------------------------------- pretty print |
1088 | 1106 | ||
1089 | instance Up a => PShow (SExp' a) where | 1107 | instance Up a => PShow (SExp' a) where |