summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-29 12:52:37 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-29 12:52:37 +0200
commite57768c37bc8cbc85b58bf47a71562d0d782c698 (patch)
tree471ede50cbdc347faab61667e19c2e3350f4c200 /src
parent58a479b781935155e09f565d2488693850bf21c6 (diff)
use Doc instead of String is several places
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler.hs44
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs5
-rw-r--r--src/LambdaCube/Compiler/Infer.hs55
-rw-r--r--src/LambdaCube/Compiler/Parser.hs31
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs10
5 files changed, 78 insertions, 67 deletions
diff --git a/src/LambdaCube/Compiler.hs b/src/LambdaCube/Compiler.hs
index 017d629c..4c1b7fd8 100644
--- a/src/LambdaCube/Compiler.hs
+++ b/src/LambdaCube/Compiler.hs
@@ -118,14 +118,14 @@ moduleNameToFileName n = hn n ++ ".lc"
118 h acc ('.':cs) = reverse acc </> hn cs 118 h acc ('.':cs) = reverse acc </> hn cs
119 h acc (c: cs) = h (c: acc) cs 119 h acc (c: cs) = h (c: acc) cs
120 120
121type ModuleFetcher m = Maybe FilePath -> Either FilePath MName -> m (Either String (FilePath, MName, m SourceCode)) 121type ModuleFetcher m = Maybe FilePath -> Either FilePath MName -> m (Either Doc (FilePath, MName, m SourceCode))
122 122
123ioFetch :: MonadIO m => [FilePath] -> ModuleFetcher (MMT m x) 123ioFetch :: MonadIO m => [FilePath] -> ModuleFetcher (MMT m x)
124ioFetch paths' imp n = do 124ioFetch paths' imp n = do
125 preludePath <- (</> "lc") <$> liftIO getDataDir 125 preludePath <- (</> "lc") <$> liftIO getDataDir
126 let paths = paths' ++ [preludePath] 126 let paths = paths' ++ [preludePath]
127 find ((x, mn): xs) = liftIO (readFile' x) >>= maybe (find xs) (\src -> return $ Right (x, mn, liftIO src)) 127 find ((x, mn): xs) = liftIO (readFile' x) >>= maybe (find xs) (\src -> return $ Right (x, mn, liftIO src))
128 find [] = return $ Left $ show $ "can't find " <+> either (("lc file" <+>) . text) (("module" <+>) . text) n 128 find [] = return $ Left $ "can't find" <+> either (("lc file" <+>) . text) (("module" <+>) . text) n
129 <+> "in path" <+> hsep (map text (paths' ++ ["<<installed-prelude-path>>"]{-todo-})) 129 <+> "in path" <+> hsep (map text (paths' ++ ["<<installed-prelude-path>>"]{-todo-}))
130 find $ nubBy ((==) `on` fst) $ map (first normalise . lcModuleFile) paths 130 find $ nubBy ((==) `on` fst) $ map (first normalise . lcModuleFile) paths
131 where 131 where
@@ -160,7 +160,7 @@ removeFromCache f = modify $ \m@(Modules nm im ni) -> case Map.lookup f nm of
160 Nothing -> m 160 Nothing -> m
161 Just i -> Modules (Map.delete f nm) (IM.delete i im) ni 161 Just i -> Modules (Map.delete f nm) (IM.delete i im) ni
162 162
163type Module' x = (SourceCode, Either String{-error msg-} (Module, x, Either String{-error msg-} (DesugarInfo, GlobalEnv))) 163type Module' x = (SourceCode, Either Doc{-error msg-} (Module, x, Either Doc{-error msg-} (DesugarInfo, GlobalEnv)))
164 164
165data Modules x = Modules 165data Modules x = Modules
166 { moduleIds :: !(Map FilePath Int) 166 { moduleIds :: !(Map FilePath Int)
@@ -170,7 +170,7 @@ data Modules x = Modules
170 170
171(<&>) = flip (<$>) 171(<&>) = flip (<$>)
172 172
173loadModule :: MonadMask m => ((Infos, [Stmt]) -> x) -> Maybe FilePath -> Either FilePath MName -> MMT m x (Either String (FilePath, Module' x)) 173loadModule :: MonadMask m => ((Infos, [Stmt]) -> x) -> Maybe FilePath -> Either FilePath MName -> MMT m x (Either Doc (FilePath, Module' x))
174loadModule ex imp mname_ = do 174loadModule ex imp mname_ = do
175 r <- ask >>= \fetch -> fetch imp mname_ 175 r <- ask >>= \fetch -> fetch imp mname_
176 case r of 176 case r of
@@ -184,34 +184,34 @@ loadModule ex imp mname_ = do
184 fid <- gets nextMId 184 fid <- gets nextMId
185 modify $ \(Modules nm im ni) -> Modules (Map.insert fname fid nm) im $ ni+1 185 modify $ \(Modules nm im ni) -> Modules (Map.insert fname fid nm) im $ ni+1
186 res <- case parseLC $ FileInfo fid fname src of 186 res <- case parseLC $ FileInfo fid fname src of
187 Left e -> return $ Left $ show e 187 Left e -> return $ Left $ text $ show e
188 Right e -> do 188 Right e -> do
189 modify $ \(Modules nm im ni) -> Modules nm (IM.insert fid (fname, (src, Right (e, ex mempty, Left $ show $ "cycles in module imports:" <+> pShow mname <+> pShow (fst <$> moduleImports e)))) im) ni 189 modify $ \(Modules nm im ni) -> Modules nm (IM.insert fid (fname, (src, Right (e, ex mempty, Left $ "cycles in module imports:" <+> pShow mname <+> pShow (fst <$> moduleImports e)))) im) ni
190 ms <- forM (moduleImports e) $ \(m, is) -> loadModule ex (Just fname) (Right $ sName m) <&> \r -> case r of 190 ms <- forM (moduleImports e) $ \(m, is) -> loadModule ex (Just fname) (Right $ sName m) <&> \r -> case r of
191 Left err -> Left $ sName m ++ " couldn't be found" 191 Left err -> Left $ pShow m <+> "is not found"
192 Right (fb, (src, dsge)) -> 192 Right (fb, (src, dsge)) ->
193 either (Left . const (sName m ++ " couldn't be parsed")) 193 either (Left . const (pShow m <+> "couldn't be parsed"))
194 (\(pm, x, e) -> either 194 (\(pm, x, e) -> either
195 (Left . const (sName m ++ " couldn't be typechecked")) 195 (Left . const (pShow m <+> "couldn't be typechecked"))
196 (\(ds, ge) -> Right (ds{-todo: filter-}, Map.filterWithKey (\k _ -> filterImports is k) ge)) 196 (\(ds, ge) -> Right (ds{-todo: filter-}, Map.filterWithKey (\k _ -> filterImports is k) ge))
197 e) 197 e)
198 dsge 198 dsge
199 199
200 let (res, err) = case sequence ms of 200 let (res, err) = case sequence ms of
201 Left err -> (ex mempty, Left err) 201 Left err -> (ex mempty, Left $ pShow err)
202 Right ms@(mconcat -> (ds, ge)) -> case runExcept $ runDefParser ds $ definitions e of 202 Right ms@(mconcat -> (ds, ge)) -> case runExcept $ runDefParser ds $ definitions e of
203 Left err -> (ex mempty, Left $ show err) 203 Left err -> (ex mempty, Left $ pShow err)
204 Right (defs, warnings, dsinfo) -> (,) (ex (map ParseWarning warnings ++ is, defs)) $ case res of 204 Right (defs, warnings, dsinfo) -> (,) (ex (map ParseWarning warnings ++ is, defs)) $ case res of
205 Left err -> Left (show err) 205 Left err -> Left $ pShow err
206 Right (mconcat -> newge) -> 206 Right (mconcat -> newge) ->
207 right mconcat $ forM (fromMaybe [ExportModule $ SIName mempty mname] $ moduleExports e) $ \case 207 right mconcat $ forM (fromMaybe [ExportModule $ SIName mempty mname] $ moduleExports e) $ \case
208 ExportId (sName -> d) -> case Map.lookup d newge of 208 ExportId (sName -> d) -> case Map.lookup d newge of
209 Just def -> Right (mempty{-TODO-}, Map.singleton d def) 209 Just def -> Right (mempty{-TODO-}, Map.singleton d def)
210 Nothing -> Left $ d ++ " is not defined" 210 Nothing -> Left $ text d <+> "is not defined"
211 ExportModule (sName -> m) | m == mname -> Right (dsinfo, newge) 211 ExportModule (sName -> m) | m == mname -> Right (dsinfo, newge)
212 ExportModule m -> case [ x | ((m', _), x) <- zip (moduleImports e) ms, m' == m] of 212 ExportModule m -> case [ x | ((m', _), x) <- zip (moduleImports e) ms, m' == m] of
213 [x] -> Right x 213 [x] -> Right x
214 [] -> Left $ "empty export list: " ++ show (fname, m, map fst $ moduleImports e, mname) 214 [] -> Left $ "empty export list in module" <+> text fname -- m, map fst $ moduleImports e, mname)
215 _ -> error "export list: internal error" 215 _ -> error "export list: internal error"
216 where 216 where
217 (res, is) = runWriter . flip runReaderT (extensions e, initEnv <> ge) . runExceptT $ inference defs 217 (res, is) = runWriter . flip runReaderT (extensions e, initEnv <> ge) . runExceptT $ inference defs
@@ -224,7 +224,7 @@ loadModule ex imp mname_ = do
224 filterImports (ImportJust ns) = (`elem` map sName ns) 224 filterImports (ImportJust ns) = (`elem` map sName ns)
225 225
226-- used in runTests 226-- used in runTests
227getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m Infos (Infos, Either String (FilePath, Either String (Exp, Exp))) 227getDef :: MonadMask m => FilePath -> SName -> Maybe Exp -> MMT m Infos (Infos, Either Doc (FilePath, Either Doc (Exp, Exp)))
228getDef = getDef_ fst 228getDef = getDef_ fst
229 229
230getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case 230getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case
@@ -236,37 +236,37 @@ getDef_ ex m d ty = loadModule ex Nothing (Left m) <&> \case
236 , case Map.lookup d ge of 236 , case Map.lookup d ge of
237 Just (e, thy, si) 237 Just (e, thy, si)
238 | Just False <- (== thy) <$> ty -- TODO: better type comparison 238 | Just False <- (== thy) <$> ty -- TODO: better type comparison
239 -> Left $ "type of " ++ d ++ " should be " ++ show ty ++ " instead of " ++ ppShow thy 239 -> Left $ "type of" <+> text d <+> "should be" <+> pShow ty <+> "instead of" <+> pShow thy
240 | otherwise -> Right (e, thy) 240 | otherwise -> Right (e, thy)
241 Nothing -> Left $ d ++ " is not found" 241 Nothing -> Left $ text d <+> "is not found"
242 ) 242 )
243 243
244compilePipeline' ex backend m 244compilePipeline' ex backend m
245 = second (either Left (fmap (compilePipeline backend) . snd)) <$> getDef_ ex m "main" (Just outputType) 245 = second (either Left (fmap (compilePipeline backend) . snd)) <$> getDef_ ex m "main" (Just outputType)
246 246
247-- | most commonly used interface for end users 247-- | most commonly used interface for end users
248compileMain :: [FilePath] -> IR.Backend -> MName -> IO (Either String IR.Pipeline) 248compileMain :: [FilePath] -> IR.Backend -> MName -> IO (Either Doc IR.Pipeline)
249compileMain path backend fname 249compileMain path backend fname
250 = fmap snd $ runMM (ioFetch path) $ compilePipeline' (const ()) backend fname 250 = fmap snd $ runMM (ioFetch path) $ compilePipeline' (const ()) backend fname
251 251
252parseModule :: [FilePath] -> MName -> IO (Either String String) 252parseModule :: [FilePath] -> MName -> IO (Either Doc String)
253parseModule path fname = runMM (ioFetch path) $ loadModule snd Nothing (Left fname) <&> \case 253parseModule path fname = runMM (ioFetch path) $ loadModule snd Nothing (Left fname) <&> \case
254 Left err -> Left err 254 Left err -> Left err
255 Right (fname, (src, Left err)) -> Left err 255 Right (fname, (src, Left err)) -> Left err
256 Right (fname, (src, Right (pm, infos, _))) -> Right $ pPrintStmts infos 256 Right (fname, (src, Right (pm, infos, _))) -> Right $ pPrintStmts infos
257 257
258-- used by the compiler-service of the online editor 258-- used by the compiler-service of the online editor
259preCompile :: (MonadMask m, MonadIO m) => [FilePath] -> [FilePath] -> Backend -> FilePath -> IO (String -> m (Either String IR.Pipeline, (Infos, String))) 259preCompile :: (MonadMask m, MonadIO m) => [FilePath] -> [FilePath] -> Backend -> FilePath -> IO (String -> m (Either Doc IR.Pipeline, (Infos, String)))
260preCompile paths paths' backend mod = do 260preCompile paths paths' backend mod = do
261 res <- runMM (ioFetch paths) $ loadModule ex Nothing $ Left mod 261 res <- runMM (ioFetch paths) $ loadModule ex Nothing $ Left mod
262 case res of 262 case res of
263 Left err -> error $ "Prelude could not compiled: " ++ err 263 Left err -> error $ "Prelude could not compiled:" ++ show err
264 Right (src, prelude) -> return compile 264 Right (src, prelude) -> return compile
265 where 265 where
266 compile src = runMM fetch $ do 266 compile src = runMM fetch $ do
267 let pname = "." </> "Prelude.lc" 267 let pname = "." </> "Prelude.lc"
268 modify $ \(Modules nm im ni) -> Modules (Map.insert pname ni nm) (IM.insert ni (pname, prelude) im) (ni+1) 268 modify $ \(Modules nm im ni) -> Modules (Map.insert pname ni nm) (IM.insert ni (pname, prelude) im) (ni+1)
269 ((left plainShow . snd) &&& fst) <$> compilePipeline' ex backend "Main" 269 (snd &&& fst) <$> compilePipeline' ex backend "Main"
270 where 270 where
271 fetch imp = \case 271 fetch imp = \case
272 Left "Prelude" -> return $ Right ("./Prelude.lc", "Prelude", undefined) 272 Left "Prelude" -> return $ Right ("./Prelude.lc", "Prelude", undefined)
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index c2317615..6fb1cbaa 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -136,9 +136,10 @@ instance PShow SI where
136 pShow (RangeSI r) = pShow r 136 pShow (RangeSI r) = pShow r
137 137
138-- long version 138-- long version
139-- TODO: merge with pShow
139showSI x = case sourceInfo x of 140showSI x = case sourceInfo x of
140 RangeSI r -> show $ showRange r 141 RangeSI r -> showRange r
141 x -> ppShow x 142 x -> pShow x
142 143
143hashPos :: FileInfo -> SPos -> Int 144hashPos :: FileInfo -> SPos -> Int
144hashPos fn (SPos r c) = fileId fn `shiftL` 32 .|. r `shiftL` 16 .|. c 145hashPos fn (SPos r c) = fileId fn `shiftL` 32 .|. r `shiftL` 16 .|. c
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 74e0c4d6..e7c52d9a 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -1,3 +1,4 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE LambdaCase #-} 2{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE ViewPatterns #-} 3{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE PatternSynonyms #-} 4{-# LANGUAGE PatternSynonyms #-}
@@ -881,30 +882,31 @@ appTy t x = error $ "appTy: " ++ show t
881-------------------------------------------------------------------------------- error messages 882-------------------------------------------------------------------------------- error messages
882 883
883data ErrorMsg 884data ErrorMsg
884 = ErrorMsg String 885 = ErrorMsg Doc
885 | ECantFind SName SI 886 | ECantFind SName SI
886 | ETypeError String SI 887 | ETypeError Doc SI
887 | ERedefined SName SI SI 888 | ERedefined SName SI SI
888 889
889instance NFData ErrorMsg where 890instance NFData ErrorMsg where rnf = rnf . ppShow
891{-
890 rnf = \case 892 rnf = \case
891 ErrorMsg m -> rnf m 893 ErrorMsg m -> rnf m
892 ECantFind a b -> rnf (a, b) 894 ECantFind a b -> rnf (a, b)
893 ETypeError a b -> rnf (a, b) 895 ETypeError a b -> rnf (a, b)
894 ERedefined a b c -> rnf (a, b, c) 896 ERedefined a b c -> rnf (a, b, c)
895 897-}
896errorRange_ = \case 898errorRange_ = \case
897 ErrorMsg s -> [] 899 ErrorMsg s -> []
898 ECantFind s si -> [si] 900 ECantFind s si -> [si]
899 ETypeError msg si -> [si] 901 ETypeError msg si -> [si]
900 ERedefined s si si' -> [si, si'] 902 ERedefined s si si' -> [si, si']
901 903
902instance Show ErrorMsg where 904instance PShow ErrorMsg where
903 show = \case 905 pShow = \case
904 ErrorMsg s -> s 906 ErrorMsg s -> s
905 ECantFind s si -> "can't find: " ++ s ++ " in " ++ showSI si 907 ECantFind s si -> "can't find:" <+> text s <+> "in" <+> showSI si
906 ETypeError msg si -> "type error: " ++ msg ++ "\nin " ++ showSI si ++ "\n" 908 ETypeError msg si -> "type error:" <+> msg <$$> "in" <+> showSI si
907 ERedefined s si si' -> "already defined " ++ s ++ " at " ++ showSI si ++ "\n and at " ++ showSI si' 909 ERedefined s si si' -> "already defined" <+> text s <+> "at" <+> showSI si <$$> "and at" <+> showSI si'
908 910
909 911
910-------------------------------------------------------------------------------- inference 912-------------------------------------------------------------------------------- inference
@@ -1051,7 +1053,7 @@ inferN_ tellTrace = infer where
1051 EBind2_ si BMeta tt_ te 1053 EBind2_ si BMeta tt_ te
1052 | ELabelEnd te' <- te -> refocus (ELabelEnd $ EBind2_ si BMeta tt_ te') eet 1054 | ELabelEnd te' <- te -> refocus (ELabelEnd $ EBind2_ si BMeta tt_ te') eet
1053 | Unit <- tt -> refocus te $ subst 0 TT eet 1055 | Unit <- tt -> refocus te $ subst 0 TT eet
1054 | Empty msg <- tt -> throwError' $ ETypeError msg si 1056 | Empty msg <- tt -> throwError' $ ETypeError (text msg) si
1055 | T2 x y <- tt, let te' = EBind2_ si BMeta (up 1 y) $ EBind2_ si BMeta x te 1057 | T2 x y <- tt, let te' = EBind2_ si BMeta (up 1 y) $ EBind2_ si BMeta x te
1056 -> refocus te' $ subst 2 (t2C (Var 1) (Var 0)) $ up 2 eet 1058 -> refocus te' $ subst 2 (t2C (Var 1) (Var 0)) $ up 2 eet
1057 | CstrT t a b <- tt, Just r <- cst (a, t) b -> r 1059 | CstrT t a b <- tt, Just r <- cst (a, t) b -> r
@@ -1117,8 +1119,8 @@ inferN_ tellTrace = infer where
1117 1119
1118 EGlobal{} -> return eet 1120 EGlobal{} -> return eet
1119 _ -> case eet of 1121 _ -> case eet of
1120 MEnd x -> throwError' $ ErrorMsg $ "focus todo: " ++ ppShow x 1122 MEnd x -> throwError' $ ErrorMsg $ "focus todo:" <+> pShow x
1121 _ -> throwError' $ ErrorMsg $ "focus checkMetas: " ++ ppShow env ++ "\n" ++ ppShow (fst <$> eet) 1123 _ -> throwError' $ ErrorMsg $ "focus checkMetas:" <+> pShow env <$$> pShow (fst <$> eet)
1122 where 1124 where
1123 refocus_ :: (Env -> CEnv ExpType -> IM m ExpType') -> Env -> CEnv ExpType -> IM m ExpType' 1125 refocus_ :: (Env -> CEnv ExpType -> IM m ExpType') -> Env -> CEnv ExpType -> IM m ExpType'
1124 refocus_ _ e (MEnd at) = focus_ e at 1126 refocus_ _ e (MEnd at) = focus_ e at
@@ -1255,12 +1257,13 @@ initEnv = Map.fromList
1255 1257
1256data Info 1258data Info
1257 = Info Range String 1259 = Info Range String
1258 | IType String String 1260 | IType SIName Exp
1259 | ITrace String String 1261 | ITrace String String
1260 | IError ErrorMsg 1262 | IError ErrorMsg
1261 | ParseWarning ParseWarning 1263 | ParseWarning ParseWarning
1262 1264
1263instance NFData Info 1265instance NFData Info where rnf = rnf . ppShow
1266{-
1264 where 1267 where
1265 rnf = \case 1268 rnf = \case
1266 Info r s -> rnf (r, s) 1269 Info r s -> rnf (r, s)
@@ -1268,14 +1271,14 @@ instance NFData Info
1268 ITrace i s -> rnf (i, s) 1271 ITrace i s -> rnf (i, s)
1269 IError x -> rnf x 1272 IError x -> rnf x
1270 ParseWarning w -> rnf w 1273 ParseWarning w -> rnf w
1271 1274-}
1272instance Show Info where 1275instance PShow Info where
1273 show = \case 1276 pShow = \case
1274 Info r s -> ppShow r ++ " " ++ s 1277 Info r s -> pShow r <+> "" <+> text s
1275 IType a b -> a ++ " :: " ++ b 1278 IType a b -> shAnn False (pShow a) (pShow b)
1276 ITrace i s -> i ++ ": " ++ s 1279 ITrace i s -> text i <> ": " <+> text s
1277 IError e -> "!" ++ show e 1280 IError e -> "!" <> pShow e
1278 ParseWarning w -> show w 1281 ParseWarning w -> pShow w
1279 1282
1280errorRange is = [r | IError e <- is, RangeSI r <- errorRange_ e ] 1283errorRange is = [r | IError e <- is, RangeSI r <- errorRange_ e ]
1281 1284
@@ -1288,12 +1291,12 @@ mkInfoItem _ _ = mempty
1288 1291
1289listAllInfos m = h "trace" (listTraceInfos m) 1292listAllInfos m = h "trace" (listTraceInfos m)
1290 ++ h "tooltips" [ ppShow r ++ " " ++ intercalate " | " is | (r, is) <- listTypeInfos m ] 1293 ++ h "tooltips" [ ppShow r ++ " " ++ intercalate " | " is | (r, is) <- listTypeInfos m ]
1291 ++ h "warnings" [ show w | ParseWarning w <- m ] 1294 ++ h "warnings" [ ppShow w | ParseWarning w <- m ]
1292 where 1295 where
1293 h x [] = [] 1296 h x [] = []
1294 h x xs = ("------------ " ++ x) : xs 1297 h x xs = ("------------ " ++ x) : xs
1295 1298
1296listTraceInfos m = [show i | i <- m, case i of Info{} -> False; ParseWarning{} -> False; _ -> True] 1299listTraceInfos m = [ppShow i | i <- m, case i of Info{} -> False; ParseWarning{} -> False; _ -> True]
1297listTypeInfos m = map (second Set.toList) $ Map.toList $ Map.unionsWith (<>) [Map.singleton r $ Set.singleton i | Info r i <- m] 1300listTypeInfos m = map (second Set.toList) $ Map.toList $ Map.unionsWith (<>) [Map.singleton r $ Set.singleton i | Info r i <- m]
1298 1301
1299-------------------------------------------------------------------------------- inference for statements 1302-------------------------------------------------------------------------------- inference for statements
@@ -1428,10 +1431,10 @@ inferType :: Monad m => SExp2 -> IM m Type
1428inferType t = fmap (closedExp . fst . recheck "inferType" EGlobal . flip (,) TType . replaceMetas (Pi Hidden) . fmap fst) $ inferN (CheckType_ (debugSI "inferType CheckType_") TType EGlobal) t 1431inferType t = fmap (closedExp . fst . recheck "inferType" EGlobal . flip (,) TType . replaceMetas (Pi Hidden) . fmap fst) $ inferN (CheckType_ (debugSI "inferType CheckType_") TType EGlobal) t
1429 1432
1430addToEnv :: Monad m => SIName -> ExpType -> IM m GlobalEnv 1433addToEnv :: Monad m => SIName -> ExpType -> IM m GlobalEnv
1431addToEnv (SIName si s) (x, t) = do 1434addToEnv sn@(SIName si s) (x, t) = do
1432-- maybe (pure ()) throwError_ $ ambiguityCheck s t -- TODO 1435-- maybe (pure ()) throwError_ $ ambiguityCheck s t -- TODO
1433-- b <- asks $ (TraceTypeCheck `elem`) . fst 1436-- b <- asks $ (TraceTypeCheck `elem`) . fst
1434 tell [IType s $ ppShow t] 1437 tell [IType sn t]
1435 v <- asks $ Map.lookup s . snd 1438 v <- asks $ Map.lookup s . snd
1436 case v of 1439 case v of
1437 Nothing -> return $ Map.singleton s (closedExp x, closedExp t, si) 1440 Nothing -> return $ Map.singleton s (closedExp x, closedExp t, si)
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index ce830f2a..a4dfa229 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -67,24 +67,21 @@ instance NFData ParseWarning
67 Unreachable r -> rnf r 67 Unreachable r -> rnf r
68 Uncovered si r -> rnf si -- TODO --rnf r 68 Uncovered si r -> rnf si -- TODO --rnf r
69 69
70instance Show LCParseError where 70instance PShow LCParseError where
71 show = \case 71 pShow = \case
72 MultiplePatternVars xs -> unlines $ "multiple pattern vars:": 72 MultiplePatternVars xs -> vcat $ "multiple pattern vars:":
73 concat [(sName (head ns) ++ " is defined at"): map showSI ns | ns <- xs] 73 concat [(pShow (head ns) <+> "is defined at"): map showSI ns | ns <- xs]
74 OperatorMismatch op op' -> "Operator precedences don't match:\n" ++ show (fromJust $ getFixity_ op) ++ " at " ++ showSI op ++ "\n" ++ show (fromJust $ getFixity_ op') ++ " at " ++ showSI op' 74 OperatorMismatch op op' -> "Operator precedences don't match:" <$$> pShow (fromJust $ getFixity_ op) <+> "at" <+> showSI op <$$> pShow (fromJust $ getFixity_ op') <+> "at" <+> showSI op'
75 UndefinedConstructor n -> "Constructor " ++ show n ++ " is not defined at " ++ showSI n 75 UndefinedConstructor n -> "Constructor" <+> pShow n <+> "is not defined at" <+> showSI n
76 ParseError p -> show p 76 ParseError p -> text $ show p
77 77
78instance Show ParseWarning where 78instance PShow ParseWarning where
79 show = \case 79 pShow = \case
80 Unreachable si -> "Source code is not reachable: " ++ show (showRange si) 80 Unreachable si -> "Source code is not reachable:" <+> showRange si
81 Uncovered si pss -> "Uncovered pattern(s) at " ++ showSI si ++ "\nMissing case(s):\n" ++ 81 Uncovered si pss -> "Uncovered pattern(s) at" <+> showSI si <$$> "Missing case(s):" <$$>
82 unlines [" " ++ unwords (map ppShow ps) ++ 82 vcat [" " <> hsep (map pShow ps) <+>
83 " | " +++ intercalate ", " [ppShow p ++ " <- " ++ ppShow e | (p, e) <- gs] 83 hsep [se <+> pShow p <+> "<-" <+> pShow e | (se, (p, e)) <- zip ("|": repeat ",") gs]
84 | (ps, gs) <- pss] 84 | (ps, gs) <- pss]
85 where
86 s +++ "" = ""
87 s +++ s' = s ++ s'
88 85
89trackSI p = do 86trackSI p = do
90 x <- p 87 x <- p
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index a8fdf851..42e06ad3 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -17,6 +17,7 @@ import Data.String
17--import qualified Data.Map as Map 17--import qualified Data.Map as Map
18import Control.Monad.Reader 18import Control.Monad.Reader
19import Control.Monad.State 19import Control.Monad.State
20import Control.DeepSeq
20--import Debug.Trace 21--import Debug.Trace
21 22
22import qualified Text.PrettyPrint.ANSI.Leijen as P 23import qualified Text.PrettyPrint.ANSI.Leijen as P
@@ -66,6 +67,12 @@ data Fixity
66 | InfixR !Int 67 | InfixR !Int
67 deriving (Eq, Show) 68 deriving (Eq, Show)
68 69
70instance PShow Fixity where
71 pShow = \case
72 Infix i -> "infix" `DApp` pShow i
73 InfixL i -> "infixl" `DApp` pShow i
74 InfixR i -> "infixr" `DApp` pShow i
75
69precedence, leftPrecedence, rightPrecedence :: Fixity -> Int 76precedence, leftPrecedence, rightPrecedence :: Fixity -> Int
70 77
71precedence = \case 78precedence = \case
@@ -107,6 +114,9 @@ instance Monoid Doc where
107 mempty = text "" 114 mempty = text ""
108 a `mappend` b = DDoc $ DOHCat a b 115 a `mappend` b = DDoc $ DOHCat a b
109 116
117instance NFData Doc where
118 rnf x = rnf $ show x -- TODO
119
110pattern DColor c a = DDoc (DOColor c a) 120pattern DColor c a = DDoc (DOColor c a)
111 121
112strip :: Doc -> Doc 122strip :: Doc -> Doc