diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-19 10:36:13 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-19 10:36:13 +0200 |
commit | 445da179fab22a16cd68aa9f1139d77a2e121543 (patch) | |
tree | 772983bf71a2ef70a26c40e90b667befd7a9d77f /src | |
parent | 94963d9fedafa68481dc0c1b9c0e14f2b4f233b5 (diff) |
refactoring & fix switching to type namespace after @
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 4 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 128 |
2 files changed, 74 insertions, 58 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index 7e41aaf2..c1964205 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -928,7 +928,7 @@ inferN_ tellTrace = infer where | |||
928 | infer te exp = tellTrace "infer" (showEnvSExp te exp) $ (if debug then fmap (fmap{-todo-} $ recheck' "infer" te) else id) $ case exp of | 928 | infer te exp = tellTrace "infer" (showEnvSExp te exp) $ (if debug then fmap (fmap{-todo-} $ recheck' "infer" te) else id) $ case exp of |
929 | Parens x -> infer te x | 929 | Parens x -> infer te x |
930 | SAnn x t -> checkN (CheckIType x te) t TType | 930 | SAnn x t -> checkN (CheckIType x te) t TType |
931 | SLabelEnd x -> infer (ELabelEnd te) x | 931 | SRHS x -> infer (ELabelEnd te) x |
932 | SVar (si, _) i -> focus_' te exp (Var i, snd $ varType "C2" i te) | 932 | SVar (si, _) i -> focus_' te exp (Var i, snd $ varType "C2" i te) |
933 | SLit si l -> focus_' te exp (ELit l, litType l) | 933 | SLit si l -> focus_' te exp (ELit l, litType l) |
934 | STyped si et -> focus_' te exp et | 934 | STyped si et -> focus_' te exp et |
@@ -958,7 +958,7 @@ inferN_ tellTrace = infer where | |||
958 | , TVec (Var n') _ <- snd $ varType "xx" v te | 958 | , TVec (Var n') _ <- snd $ varType "xx" v te |
959 | = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v | 959 | = infer te $ x `SAppV` SLamV (SLamV (STyped mempty (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v |
960 | -} | 960 | -} |
961 | | SLabelEnd x <- e = checkN (ELabelEnd te) x t | 961 | | SRHS x <- e = checkN (ELabelEnd te) x t |
962 | | SApp si h a b <- e = infer (CheckAppType si h t te b) a | 962 | | SApp si h a b <- e = infer (CheckAppType si h t te b) a |
963 | | SLam h a b <- e, Pi h' x y <- t, h == h' = do | 963 | | SLam h a b <- e, Pi h' x y <- t, h == h' = do |
964 | tellType e t | 964 | tellType e t |
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index ceee43d0..0a0e64ae 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -13,7 +13,7 @@ module LambdaCube.Compiler.Parser | |||
13 | , sourceInfo, SI(..), debugSI | 13 | , sourceInfo, SI(..), debugSI |
14 | , Module(..), Visibility(..), Binder(..), SExp'(..), Extension(..), Extensions | 14 | , Module(..), Visibility(..), Binder(..), SExp'(..), Extension(..), Extensions |
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, pattern Parens | 16 | , pattern SBuiltin, pattern SPi, pattern Primitive, pattern SRHS, pattern SLam, pattern Parens |
17 | , pattern TyType, pattern SLet | 17 | , pattern TyType, pattern SLet |
18 | , debug, isPi, iterateN, traceD | 18 | , debug, isPi, iterateN, traceD |
19 | , parseLC, runDefParser | 19 | , parseLC, runDefParser |
@@ -41,7 +41,7 @@ import Control.Arrow hiding ((<+>)) | |||
41 | import Control.Applicative | 41 | import Control.Applicative |
42 | --import Debug.Trace | 42 | --import Debug.Trace |
43 | 43 | ||
44 | import qualified LambdaCube.Compiler.Pretty as P | 44 | import qualified LambdaCube.Compiler.Pretty as BodyParser |
45 | import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) | 45 | import LambdaCube.Compiler.Pretty hiding (Doc, braces, parens) |
46 | import LambdaCube.Compiler.Lexer | 46 | import LambdaCube.Compiler.Lexer |
47 | 47 | ||
@@ -58,6 +58,9 @@ data Void | |||
58 | instance Show Void where show _ = error "show @Void" | 58 | instance Show Void where show _ = error "show @Void" |
59 | instance Eq Void where _ == _ = error "(==) @Void" | 59 | instance Eq Void where _ == _ = error "(==) @Void" |
60 | 60 | ||
61 | elimVoid :: Void -> a | ||
62 | elimVoid _ = error "impossible" | ||
63 | |||
61 | -- supplementary data: data with no semantic relevance | 64 | -- supplementary data: data with no semantic relevance |
62 | newtype SData a = SData a | 65 | newtype SData a = SData a |
63 | 66 | ||
@@ -122,7 +125,7 @@ infixl 2 `SAppV`, `SAppH` | |||
122 | pattern SBuiltin s <- SGlobal (_, s) | 125 | pattern SBuiltin s <- SGlobal (_, s) |
123 | where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s) | 126 | where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s) |
124 | 127 | ||
125 | pattern SLabelEnd a = SBuiltin "^rhs" `SAppV` a | 128 | pattern SRHS a = SBuiltin "^rhs" `SAppV` a |
126 | pattern Section e = SBuiltin "^section" `SAppV` e | 129 | pattern Section e = SBuiltin "^section" `SAppV` e |
127 | pattern SType = SBuiltin "'Type" | 130 | pattern SType = SBuiltin "'Type" |
128 | pattern Parens e = SBuiltin "parens" `SAppV` e | 131 | pattern Parens e = SBuiltin "parens" `SAppV` e |
@@ -197,6 +200,7 @@ instance (Up a, Up b) => Up (a, b) where | |||
197 | up n = up_ n 0 | 200 | up n = up_ n 0 |
198 | up1 = up1_ 0 | 201 | up1 = up1_ 0 |
199 | 202 | ||
203 | substS :: Up a => Int -> a -> SExp' a -> SExp' a | ||
200 | substS j x = mapS' f2 ((+1) *** up 1) (j, x) | 204 | substS j x = mapS' f2 ((+1) *** up 1) (j, x) |
201 | where | 205 | where |
202 | f2 sn i (j, x) = case compare i j of | 206 | f2 sn i (j, x) = case compare i j of |
@@ -204,6 +208,14 @@ substS j x = mapS' f2 ((+1) *** up 1) (j, x) | |||
204 | LT -> SVar sn i | 208 | LT -> SVar sn i |
205 | EQ -> STyped (fst sn) x | 209 | EQ -> STyped (fst sn) x |
206 | 210 | ||
211 | foldS | ||
212 | :: Monoid m | ||
213 | => (Int -> SI -> t -> m) | ||
214 | -> (SIName -> Int -> m) | ||
215 | -> (SIName -> Int -> Int -> m) | ||
216 | -> Int | ||
217 | -> SExp' t | ||
218 | -> m | ||
207 | foldS h g f = fs | 219 | foldS h g f = fs |
208 | where | 220 | where |
209 | fs i = \case | 221 | fs i = \case |
@@ -215,11 +227,23 @@ foldS h g f = fs | |||
215 | SGlobal sn -> g sn i | 227 | SGlobal sn -> g sn i |
216 | x@SLit{} -> mempty | 228 | x@SLit{} -> mempty |
217 | 229 | ||
230 | freeS :: SExp' t -> [SIName] | ||
218 | freeS = nub . foldS (\_ _ _ -> error "freeS") (\sn _ -> [sn]) mempty 0 | 231 | freeS = nub . foldS (\_ _ _ -> error "freeS") (\sn _ -> [sn]) mempty 0 |
219 | 232 | ||
220 | usedS n = getAny . foldS (\_ _ _ -> error "usedS") (\sn _ -> Any $ n == sn) mempty 0 | 233 | usedS :: SIName -> SExp -> Bool |
234 | usedS n = getAny . foldS (\_ _ -> elimVoid) (\sn _ -> Any $ n == sn) mempty 0 | ||
221 | 235 | ||
236 | mapS' :: (SIName -> Int -> t -> SExp' a) -> (t -> t) -> t -> SExp' a -> SExp' a | ||
222 | mapS' = mapS__ (\_ _ _ -> error "mapS'") (const . SGlobal) | 237 | mapS' = mapS__ (\_ _ _ -> error "mapS'") (const . SGlobal) |
238 | |||
239 | mapS__ | ||
240 | :: (t -> SI -> a -> SExp' a) | ||
241 | -> (SIName -> t -> SExp' a) | ||
242 | -> (SIName -> Int -> t -> SExp' a) | ||
243 | -> (t -> t) | ||
244 | -> t | ||
245 | -> SExp' a | ||
246 | -> SExp' a | ||
223 | mapS__ hh gg f2 h = g where | 247 | mapS__ hh gg f2 h = g where |
224 | g i = \case | 248 | g i = \case |
225 | SApp si v a b -> SApp si v (g i a) (g i b) | 249 | SApp si v a b -> SApp si v (g i a) (g i b) |
@@ -232,17 +256,12 @@ mapS__ hh gg f2 h = g where | |||
232 | 256 | ||
233 | rearrangeS :: (Int -> Int) -> SExp -> SExp | 257 | rearrangeS :: (Int -> Int) -> SExp -> SExp |
234 | rearrangeS f = mapS' (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0 | 258 | rearrangeS f = mapS' (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0 |
235 | {- | 259 | |
236 | substS'' :: Int -> Int -> SExp' a -> SExp' a | 260 | substSG :: SIName -> Int -> SExp' a -> SExp' a |
237 | substS'' j' x = mapS' f2 (+1) j' | ||
238 | where | ||
239 | f2 sn j i | ||
240 | | j < i = SVar sn j | ||
241 | | j == i = SVar sn $ x + (j - j') | ||
242 | | j > i = SVar sn $ j - 1 | ||
243 | -} | ||
244 | substSG j = mapS__ (\_ _ _ -> error "substSG") (\sn x -> if sn == j then SVar sn x else SGlobal sn) (\sn j -> const $ SVar sn j) (+1) | 261 | substSG j = mapS__ (\_ _ _ -> error "substSG") (\sn x -> if sn == j then SVar sn x else SGlobal sn) (\sn j -> const $ SVar sn j) (+1) |
245 | substSG0 n = substSG n 0 . up1 | 262 | |
263 | substSG0 :: Up a => SIName -> SExp' a -> SExp' a | ||
264 | substSG0 n = substSG n 0 . up1 -- is up1 needed here? | ||
246 | 265 | ||
247 | instance Up Void where | 266 | instance Up Void where |
248 | up_ n i = error "up_ @Void" | 267 | up_ n i = error "up_ @Void" |
@@ -256,16 +275,13 @@ instance Up a => Up (SExp' a) where | |||
256 | fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i | 275 | fold f = foldS (\i si x -> fold f i x) mempty $ \sn j i -> f j i |
257 | 276 | ||
258 | dbf' = dbf_ 0 | 277 | dbf' = dbf_ 0 |
278 | dbf_ :: Int -> DBNames -> SExp -> SExp | ||
259 | dbf_ j xs e = foldl (\e (i, sn) -> substSG sn i e) e $ zip [j..] xs | 279 | dbf_ j xs e = foldl (\e (i, sn) -> substSG sn i e) e $ zip [j..] xs |
260 | 280 | ||
281 | -- is this needed, or can be replaced by dbf' ? | ||
261 | dbff :: DBNames -> SExp -> SExp | 282 | dbff :: DBNames -> SExp -> SExp |
262 | dbff ns e = foldr substSG0 e ns | 283 | dbff ns e = foldr substSG0 e ns |
263 | 284 | ||
264 | trSExp' = trSExp elimVoid | ||
265 | |||
266 | elimVoid :: Void -> a | ||
267 | elimVoid _ = error "impossible" | ||
268 | |||
269 | trSExp :: (a -> b) -> SExp' a -> SExp' b | 285 | trSExp :: (a -> b) -> SExp' a -> SExp' b |
270 | trSExp f = g where | 286 | trSExp f = g where |
271 | g = \case | 287 | g = \case |
@@ -277,9 +293,14 @@ trSExp f = g where | |||
277 | SLit si l -> SLit si l | 293 | SLit si l -> SLit si l |
278 | STyped si a -> STyped si $ f a | 294 | STyped si a -> STyped si $ f a |
279 | 295 | ||
296 | trSExp' :: SExp -> SExp' a | ||
297 | trSExp' = trSExp elimVoid | ||
298 | |||
280 | -------------------------------------------------------------------------------- parser type | 299 | -------------------------------------------------------------------------------- parser type |
281 | 300 | ||
282 | type P = Parse DesugarInfo PostponedCheck | 301 | type BodyParser = Parse DesugarInfo PostponedCheck |
302 | |||
303 | type PostponedCheck = Maybe LCParseError | ||
283 | 304 | ||
284 | data LCParseError | 305 | data LCParseError |
285 | = MultiplePatternVars [[SIName]] | 306 | = MultiplePatternVars [[SIName]] |
@@ -292,18 +313,16 @@ instance Show LCParseError where | |||
292 | OperatorMismatch (op, f) (op', f') -> "Operator precedences don't match:\n" ++ show f ++ " at " ++ showSI (fst op) ++ "\n" ++ show f' ++ " at " ++ showSI (fst op') | 313 | OperatorMismatch (op, f) (op', f') -> "Operator precedences don't match:\n" ++ show f ++ " at " ++ showSI (fst op) ++ "\n" ++ show f' ++ " at " ++ showSI (fst op') |
293 | ParseError p -> show p | 314 | ParseError p -> show p |
294 | 315 | ||
295 | type PostponedCheck = Maybe LCParseError | ||
296 | |||
297 | getFixity :: DesugarInfo -> SName -> Fixity | ||
298 | getFixity (fm, _) n = fromMaybe (InfixL, 9) $ Map.lookup n fm | ||
299 | |||
300 | type DesugarInfo = (FixityMap, ConsMap) | 316 | type DesugarInfo = (FixityMap, ConsMap) |
301 | 317 | ||
302 | type ConsMap = Map.Map SName{-constructor name-} | 318 | type ConsMap = Map.Map SName{-constructor name-} |
303 | (Either ((SName{-case eliminator name-}, Int{-num of indices-}), [(SName, Int)]{-constructors with arities-}) | 319 | (Either ((SName{-case eliminator name-}, Int{-num of indices-}), [(SName, Int)]{-constructors with arities-}) |
304 | Int{-arity-}) | 320 | Int{-arity-}) |
305 | 321 | ||
306 | dsInfo :: P DesugarInfo | 322 | getFixity :: DesugarInfo -> SName -> Fixity |
323 | getFixity (fm, _) n = fromMaybe (InfixL, 9) $ Map.lookup n fm | ||
324 | |||
325 | dsInfo :: BodyParser DesugarInfo | ||
307 | dsInfo = asks desugarInfo | 326 | dsInfo = asks desugarInfo |
308 | 327 | ||
309 | -------------------------------------------------------------------------------- builtin precedences | 328 | -------------------------------------------------------------------------------- builtin precedences |
@@ -324,14 +343,14 @@ data Prec | |||
324 | 343 | ||
325 | -------------------------------------------------------------------------------- expression parsing | 344 | -------------------------------------------------------------------------------- expression parsing |
326 | 345 | ||
327 | parseType mb = maybe id option mb (reservedOp "::" *> parseTTerm PrecLam) | 346 | parseType mb = maybe id option mb (reservedOp "::" *> typeNS (parseTerm PrecLam)) |
328 | typedIds mb = (,) <$> commaSep1 upperLower <*> parseType mb | 347 | typedIds mb = (,) <$> commaSep1 upperLower <*> parseType mb |
329 | 348 | ||
330 | hiddenTerm p q = (,) Hidden <$ reservedOp "@" <*> p <|> (,) Visible <$> q | 349 | hiddenTerm p q = (,) Hidden <$ reservedOp "@" <*> (typeNS p) <|> (,) Visible <$> q |
331 | 350 | ||
332 | telescope mb = fmap dbfi $ many $ hiddenTerm | 351 | telescope mb = fmap dbfi $ many $ hiddenTerm |
333 | (typedId <|> maybe empty (tvar . pure) mb) | 352 | (typedId <|> maybe empty (tvar . pure) mb) |
334 | (try "::" typedId <|> maybe ((,) <$> pure (mempty, "") <*> parseTTerm PrecAtom) (tvar . pure) mb) | 353 | (try "::" typedId <|> maybe ((,) (mempty, "") <$> typeNS (parseTerm PrecAtom)) (tvar . pure) mb) |
335 | where | 354 | where |
336 | tvar x = (,) <$> patVar <*> x | 355 | tvar x = (,) <$> patVar <*> x |
337 | typedId = parens $ tvar $ parseType mb | 356 | typedId = parens $ tvar $ parseType mb |
@@ -341,24 +360,21 @@ dbfi = first reverse . unzip . go [] | |||
341 | go _ [] = [] | 360 | go _ [] = [] |
342 | go vs ((v, (n, e)): ts) = (n, (v, dbf' vs e)): go (n: vs) ts | 361 | go vs ((v, (n, e)): ts) = (n, (v, dbf' vs e)): go (n: vs) ts |
343 | 362 | ||
344 | parseTTerm = typeNS . parseTerm | ||
345 | parseETerm = expNS . parseTerm | ||
346 | |||
347 | indentation p q = p >> q | 363 | indentation p q = p >> q |
348 | 364 | ||
349 | setSI' p = appRange $ flip setSI <$> p | 365 | setSI' p = appRange $ flip setSI <$> p |
350 | 366 | ||
351 | parseTerm = setSI' . parseTerm_ | 367 | parseTerm = setSI' . parseTerm_ |
352 | 368 | ||
353 | parseTerm_ :: Prec -> P SExp | 369 | parseTerm_ :: Prec -> BodyParser SExp |
354 | parseTerm_ prec = case prec of | 370 | parseTerm_ = \case |
355 | PrecLam -> | 371 | PrecLam -> |
356 | do level PrecAnn $ \t -> mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm PrecLam | 372 | do level PrecAnn $ \t -> mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTerm PrecLam |
357 | <|> mkIf <$ reserved "if" <*> parseTerm PrecLam <* reserved "then" <*> parseTerm PrecLam <* reserved "else" <*> parseTerm PrecLam | 373 | <|> mkIf <$ reserved "if" <*> parseTerm PrecLam <* reserved "then" <*> parseTerm PrecLam <* reserved "else" <*> parseTerm PrecLam |
358 | <|> do reserved "forall" | 374 | <|> do reserved "forall" |
359 | (fe, ts) <- telescope (Just $ Wildcard SType) | 375 | (fe, ts) <- telescope (Just $ Wildcard SType) |
360 | f <- SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->" | 376 | f <- SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->" |
361 | t' <- dbf' fe <$> parseTTerm PrecLam | 377 | t' <- dbf' fe <$> parseTerm PrecLam |
362 | return $ foldr (uncurry f) t' ts | 378 | return $ foldr (uncurry f) t' ts |
363 | <|> do expNS $ do | 379 | <|> do expNS $ do |
364 | (fe, ts) <- reservedOp "\\" *> telescopePat <* reservedOp "->" | 380 | (fe, ts) <- reservedOp "\\" *> telescopePat <* reservedOp "->" |
@@ -366,7 +382,7 @@ parseTerm_ prec = case prec of | |||
366 | t' <- dbf' fe <$> parseTerm PrecLam | 382 | t' <- dbf' fe <$> parseTerm PrecLam |
367 | ge <- dsInfo | 383 | ge <- dsInfo |
368 | return $ foldr (uncurry (patLam id ge)) t' ts | 384 | return $ foldr (uncurry (patLam id ge)) t' ts |
369 | <|> compileCase <$ reserved "case" <*> dsInfo <*> parseETerm PrecLam <* reserved "of" <*> do | 385 | <|> compileCase <$ reserved "case" <*> dsInfo <*> parseTerm PrecLam <* reserved "of" <*> do |
370 | identation False $ do | 386 | identation False $ do |
371 | (fe, p) <- longPattern | 387 | (fe, p) <- longPattern |
372 | (,) p <$> parseRHS (dbf' fe) "->" | 388 | (,) p <$> parseRHS (dbf' fe) "->" |
@@ -380,7 +396,7 @@ parseTerm_ prec = case prec of | |||
380 | ex pr = pure . Right <$> parseTerm pr | 396 | ex pr = pure . Right <$> parseTerm pr |
381 | PrecApp -> | 397 | PrecApp -> |
382 | AppsS <$> try "record" ((SGlobal <$> upperCase) <* symbol "{") <*> commaSep (lowerCase *> reservedOp "=" *> ((,) Visible <$> parseTerm PrecLam)) <* symbol "}" | 398 | AppsS <$> try "record" ((SGlobal <$> upperCase) <* symbol "{") <*> commaSep (lowerCase *> reservedOp "=" *> ((,) Visible <$> parseTerm PrecLam)) <* symbol "}" |
383 | <|> AppsS <$> parseTerm PrecSwiz <*> many (hiddenTerm (parseTTerm PrecSwiz) $ parseTerm PrecSwiz) | 399 | <|> AppsS <$> parseTerm PrecSwiz <*> many (hiddenTerm (typeNS $ parseTerm PrecSwiz) $ parseTerm PrecSwiz) |
384 | PrecSwiz -> level PrecProj $ \t -> mkSwizzling t <$> lexeme (try "swizzling" $ char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: String)))) | 400 | PrecSwiz -> level PrecProj $ \t -> mkSwizzling t <$> lexeme (try "swizzling" $ char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: String)))) |
385 | PrecProj -> level PrecAtom $ \t -> try "projection" $ mkProjection t <$ char '.' <*> sepBy1 (uncurry SLit . second LString <$> lowerCase) (char '.') | 401 | PrecProj -> level PrecAtom $ \t -> try "projection" $ mkProjection t <$ char '.' <*> sepBy1 (uncurry SLit . second LString <$> lowerCase) (char '.') |
386 | PrecAtom -> | 402 | PrecAtom -> |
@@ -453,7 +469,7 @@ parseTerm_ prec = case prec of | |||
453 | 469 | ||
454 | mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f | 470 | mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f |
455 | 471 | ||
456 | calculatePrecs :: DesugarInfo -> [Either SIName SExp] -> P SExp | 472 | calculatePrecs :: DesugarInfo -> [Either SIName SExp] -> BodyParser SExp |
457 | calculatePrecs dcls = f where | 473 | calculatePrecs dcls = f where |
458 | f [] = error "impossible" | 474 | f [] = error "impossible" |
459 | f (Right t: xs) = join $ either (\(op, xs) -> calcPrec' t xs <&> \z -> Section $ SLamV $ SGlobal op `SAppV` up1 z `SAppV` sVar "calcPrec" 0) (calcPrec' t) <$> cont xs | 475 | f (Right t: xs) = join $ either (\(op, xs) -> calcPrec' t xs <&> \z -> Section $ SLamV $ SGlobal op `SAppV` up1 z `SAppV` sVar "calcPrec" 0) (calcPrec' t) <$> cont xs |
@@ -470,7 +486,7 @@ parseTerm_ prec = case prec of | |||
470 | 486 | ||
471 | calcPrec' = (postponedCheck dcls .) . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (getFixity dcls . snd) | 487 | calcPrec' = (postponedCheck dcls .) . calcPrec (\op x y -> SGlobal op `SAppV` x `SAppV` y) (getFixity dcls . snd) |
472 | 488 | ||
473 | generator, letdecl, boolExpression :: P (SExp -> SExp) | 489 | generator, letdecl, boolExpression :: BodyParser (SExp -> SExp) |
474 | generator = do | 490 | generator = do |
475 | ge <- dsInfo | 491 | ge <- dsInfo |
476 | (dbs, pat) <- try "generator" $ longPattern <* reservedOp "<-" | 492 | (dbs, pat) <- try "generator" $ longPattern <* reservedOp "<-" |
@@ -489,16 +505,16 @@ parseTerm_ prec = case prec of | |||
489 | boolExpression = (\pred e -> SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` SBuiltin "Nil") <$> parseTerm PrecLam | 505 | boolExpression = (\pred e -> SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` SBuiltin "Nil") <$> parseTerm PrecLam |
490 | 506 | ||
491 | 507 | ||
492 | mkPi Hidden (getTTuple' -> xs) b = foldr (sNonDepPi Hidden) b xs | 508 | mkPi Hidden xs b = foldr (sNonDepPi Hidden) b $ getTTuple' xs |
493 | mkPi h a b = sNonDepPi h a b | 509 | mkPi h a b = sNonDepPi h a b |
494 | 510 | ||
495 | sNonDepPi h a b = SPi h a $ up1 b | 511 | sNonDepPi h a b = SPi h a $ up1 b |
496 | 512 | ||
497 | getTTuple' (SBuiltin "'HList" `SAppV` (getTTuple -> Just (n, xs))) | n == length xs = xs | 513 | getTTuple' (SBuiltin "'HList" `SAppV` (getTTuple -> Just xs)) = xs |
498 | getTTuple' x = [x] | 514 | getTTuple' x = [x] |
499 | 515 | ||
500 | getTTuple (SBuiltin "Nil") = Just (0, []) | 516 | getTTuple (SBuiltin "Nil") = Just [] |
501 | getTTuple (SBuiltin "Cons" `SAppV` x `SAppV` (getTTuple -> Just (n, y))) = Just (n+1, x:y) | 517 | getTTuple (SBuiltin "Cons" `SAppV` x `SAppV` (getTTuple -> Just y)) = Just (x:y) |
502 | getTTuple _ = Nothing | 518 | getTTuple _ = Nothing |
503 | 519 | ||
504 | patLam :: (SExp -> SExp) -> DesugarInfo -> (Visibility, SExp) -> Pat -> SExp -> SExp | 520 | patLam :: (SExp -> SExp) -> DesugarInfo -> (Visibility, SExp) -> Pat -> SExp -> SExp |
@@ -564,7 +580,7 @@ instance SourceInfo Pat where | |||
564 | 580 | ||
565 | -------------------------------------------------------------------------------- pattern parsing | 581 | -------------------------------------------------------------------------------- pattern parsing |
566 | 582 | ||
567 | parsePat :: Prec -> P Pat | 583 | parsePat :: Prec -> BodyParser Pat |
568 | parsePat = \case | 584 | parsePat = \case |
569 | PrecAnn -> | 585 | PrecAnn -> |
570 | patType <$> parsePat PrecOp <*> parseType (Just $ Wildcard SType) | 586 | patType <$> parsePat PrecOp <*> parseType (Just $ Wildcard SType) |
@@ -626,7 +642,7 @@ telescopePat = fmap (getPPVars . ParPat . map snd &&& id) $ many $ uncurry f <$> | |||
626 | f h (PatType (ParPat [p]) t) = ((h, t), p) | 642 | f h (PatType (ParPat [p]) t) = ((h, t), p) |
627 | f h p = ((h, Wildcard SType), p) | 643 | f h p = ((h, Wildcard SType), p) |
628 | 644 | ||
629 | checkPattern :: DBNames -> P () | 645 | checkPattern :: DBNames -> BodyParser () |
630 | checkPattern ns = lift $ tell $ pure $ | 646 | checkPattern ns = lift $ tell $ pure $ |
631 | case [ns' | ns' <- group . sort . filter (not . null . snd) $ ns | 647 | case [ns' | ns' <- group . sort . filter (not . null . snd) $ ns |
632 | , not . null . tail $ ns'] of | 648 | , not . null . tail $ ns'] of |
@@ -696,8 +712,8 @@ compilePatts ps gu = cp [] ps | |||
696 | vs' = map (fromMaybe 0) vs_ | 712 | vs' = map (fromMaybe 0) vs_ |
697 | s = sum vs | 713 | s = sum vs |
698 | 714 | ||
699 | compileGuardTrees ulend ge alts = compileGuardTree ulend SLabelEnd ge $ Alts alts | 715 | compileGuardTrees ulend ge alts = compileGuardTree ulend SRHS ge $ Alts alts |
700 | compileGuardTrees' ge alts = foldr1 (SAppV2 $ SBuiltin "parEval" `SAppV` Wildcard SType) $ compileGuardTree id SLabelEnd ge <$> alts | 716 | compileGuardTrees' ge alts = foldr1 (SAppV2 $ SBuiltin "parEval" `SAppV` Wildcard SType) $ compileGuardTree id SRHS ge <$> alts |
701 | 717 | ||
702 | compileGuardTree :: (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> GuardTree -> SExp | 718 | compileGuardTree :: (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> GuardTree -> SExp |
703 | compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ guardTreeToCases t | 719 | compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ guardTreeToCases t |
@@ -786,7 +802,7 @@ instance PShow Stmt where | |||
786 | 802 | ||
787 | -------------------------------------------------------------------------------- declaration parsing | 803 | -------------------------------------------------------------------------------- declaration parsing |
788 | 804 | ||
789 | parseDef :: P [Stmt] | 805 | parseDef :: BodyParser [Stmt] |
790 | parseDef = | 806 | parseDef = |
791 | do reserved "data" *> do | 807 | do reserved "data" *> do |
792 | x <- typeNS upperCase | 808 | x <- typeNS upperCase |
@@ -839,7 +855,7 @@ parseDef = | |||
839 | <|> pure <$> funAltDef varId | 855 | <|> pure <$> funAltDef varId |
840 | <|> valueDef | 856 | <|> valueDef |
841 | where | 857 | where |
842 | telescopeDataFields :: P ([SIName], [(Visibility, SExp)]) | 858 | telescopeDataFields :: BodyParser ([SIName], [(Visibility, SExp)]) |
843 | telescopeDataFields = dbfi <$> commaSep ((,) Visible <$> ((,) <$> lowerCase <*> parseType Nothing)) | 859 | telescopeDataFields = dbfi <$> commaSep ((,) Visible <$> ((,) <$> lowerCase <*> parseType Nothing)) |
844 | 860 | ||
845 | mkData ge x ts t af cs = Data x ts t af (second snd <$> cs): concatMap mkProj (nub $ concat [fs | (_, (Just fs, _)) <- cs]) | 861 | mkData ge x ts t af cs = Data x ts t af (second snd <$> cs): concatMap mkProj (nub $ concat [fs | (_, (Just fs, _)) <- cs]) |
@@ -874,11 +890,11 @@ funAltDef parseName = do -- todo: use ns to determine parseName | |||
874 | checkPattern fee | 890 | checkPattern fee |
875 | FunAlt n tss <$> parseRHS (dbf' fee) "=" | 891 | FunAlt n tss <$> parseRHS (dbf' fee) "=" |
876 | 892 | ||
877 | valueDef :: P [Stmt] | 893 | valueDef :: BodyParser [Stmt] |
878 | valueDef = do | 894 | valueDef = do |
879 | (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" | 895 | (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" |
880 | checkPattern dns | 896 | checkPattern dns |
881 | e <- parseETerm PrecLam | 897 | e <- parseTerm PrecLam |
882 | ds <- dsInfo | 898 | ds <- dsInfo |
883 | return $ desugarValueDef ds p e | 899 | return $ desugarValueDef ds p e |
884 | 900 | ||
@@ -971,7 +987,7 @@ desugarMutual ds xs = xs | |||
971 | -} | 987 | -} |
972 | 988 | ||
973 | 989 | ||
974 | compileFunAlts' ds = fmap concat . sequence $ map (compileFunAlts (compileGuardTrees SLabelEnd) ds) $ groupBy h ds where | 990 | compileFunAlts' ds = fmap concat . sequence $ map (compileFunAlts (compileGuardTrees SRHS) ds) $ groupBy h ds where |
975 | h (FunAlt n _ _) (FunAlt m _ _) = m == n | 991 | h (FunAlt n _ _) (FunAlt m _ _) = m == n |
976 | h _ _ = False | 992 | h _ _ = False |
977 | 993 | ||
@@ -1044,7 +1060,7 @@ mkDesugarInfo ss = | |||
1044 | 1060 | ||
1045 | data Export = ExportModule SIName | ExportId SIName | 1061 | data Export = ExportModule SIName | ExportId SIName |
1046 | 1062 | ||
1047 | --parseExport :: P Export | 1063 | --parseExport :: BodyParser Export |
1048 | parseExport = | 1064 | parseExport = |
1049 | ExportModule <$ reserved "module" <*> moduleName | 1065 | ExportModule <$ reserved "module" <*> moduleName |
1050 | <|> ExportId <$> varId | 1066 | <|> ExportId <$> varId |
@@ -1069,7 +1085,7 @@ data Extension | |||
1069 | extensionMap :: Map.Map String Extension | 1085 | extensionMap :: Map.Map String Extension |
1070 | extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] | 1086 | extensionMap = Map.fromList $ map (show &&& id) [toEnum 0 .. ] |
1071 | 1087 | ||
1072 | --parseExtensions :: P [Extension] | 1088 | --parseExtensions :: BodyParser [Extension] |
1073 | parseExtensions | 1089 | parseExtensions |
1074 | = try "pragma" (symbol "{-#") *> symbol "LANGUAGE" *> commaSep (lexeme ext) <* symbolWithoutSpace "#-}" <* simpleSpace | 1090 | = try "pragma" (symbol "{-#") *> symbol "LANGUAGE" *> commaSep (lexeme ext) <* symbolWithoutSpace "#-}" <* simpleSpace |
1075 | where | 1091 | where |
@@ -1153,7 +1169,7 @@ type NameDB a = StateT [String] (Reader [String]) a | |||
1153 | showDoc :: Doc -> String | 1169 | showDoc :: Doc -> String |
1154 | showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) | 1170 | showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) |
1155 | 1171 | ||
1156 | showDoc_ :: Doc -> P.Doc | 1172 | showDoc_ :: Doc -> BodyParser.Doc |
1157 | showDoc_ = text . str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) | 1173 | showDoc_ = text . str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'':) "" <*> ['a'..'z']) |
1158 | 1174 | ||
1159 | sExpDoc :: Up a => SExp' a -> Doc | 1175 | sExpDoc :: Up a => SExp' a -> Doc |