diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-20 11:39:57 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-20 11:39:57 +0200 |
commit | f57422af1b60ddab1339138472de3aa83cc1b2a3 (patch) | |
tree | c45a556b82aa54136ceb46b35a715924dc263833 /src | |
parent | 2150cf6ecaa227696d2fc7ff9e2258c76a309ee7 (diff) |
tweak pattern source infos
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Lexer.hs | 3 | ||||
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 122 |
2 files changed, 64 insertions, 61 deletions
diff --git a/src/LambdaCube/Compiler/Lexer.hs b/src/LambdaCube/Compiler/Lexer.hs index 066e156a..65d3c454 100644 --- a/src/LambdaCube/Compiler/Lexer.hs +++ b/src/LambdaCube/Compiler/Lexer.hs | |||
@@ -337,10 +337,9 @@ switchNamespace = \case ExpNS -> TypeNS; TypeNS -> ExpNS | |||
337 | 337 | ||
338 | modifyLevel f = local $ \e -> e {namespace = f $ namespace e} | 338 | modifyLevel f = local $ \e -> e {namespace = f $ namespace e} |
339 | 339 | ||
340 | typeNS, expNS, switchNS :: Parse r w a -> Parse r w a | 340 | typeNS, expNS :: Parse r w a -> Parse r w a |
341 | typeNS = modifyLevel $ const TypeNS | 341 | typeNS = modifyLevel $ const TypeNS |
342 | expNS = modifyLevel $ const ExpNS | 342 | expNS = modifyLevel $ const ExpNS |
343 | switchNS = modifyLevel switchNamespace | ||
344 | 343 | ||
345 | -------------------------------------------------------------------------------- identifiers | 344 | -------------------------------------------------------------------------------- identifiers |
346 | 345 | ||
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index 433a2181..db538f1b 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -376,7 +376,7 @@ parseTerm_ ge = \case | |||
376 | <|> compileCase ge <$ reserved "case" <*> parseTerm PrecLam <* reserved "of" <*> do | 376 | <|> compileCase ge <$ reserved "case" <*> parseTerm PrecLam <* reserved "of" <*> do |
377 | identation False $ do | 377 | identation False $ do |
378 | (fe, p) <- longPattern | 378 | (fe, p) <- longPattern |
379 | (,) p <$> parseRHS (deBruijnify fe) "->" | 379 | (,) p . deBruijnify fe <$> parseRHS "->" |
380 | PrecAnn -> level PrecOp $ \t -> SAnn t <$> parseType Nothing | 380 | PrecAnn -> level PrecOp $ \t -> SAnn t <$> parseType Nothing |
381 | PrecOp -> (notOp False <|> notExp) >>= calculatePrecs ge where | 381 | PrecOp -> (notOp False <|> notExp) >>= calculatePrecs ge where |
382 | notExp = (++) <$> ope <*> notOp True | 382 | notExp = (++) <$> ope <*> notOp True |
@@ -398,7 +398,7 @@ parseTerm_ ge = \case | |||
398 | <|> Wildcard (Wildcard SType) <$ reserved "_" | 398 | <|> Wildcard (Wildcard SType) <$ reserved "_" |
399 | <|> mkLets ge <$ reserved "let" <*> parseDefs <* reserved "in" <*> parseTerm PrecLam | 399 | <|> mkLets ge <$ reserved "let" <*> parseDefs <* reserved "in" <*> parseTerm PrecLam |
400 | <|> SGlobal <$> lowerCase | 400 | <|> SGlobal <$> lowerCase |
401 | <|> SGlobal <$> upperCase_ -- todo: move under ppa? | 401 | <|> SGlobal <$> upperCase_ |
402 | <|> braces (mkRecord <$> commaSep ((,) <$> lowerCase <* symbol ":" <*> parseTerm PrecLam)) | 402 | <|> braces (mkRecord <$> commaSep ((,) <$> lowerCase <* symbol ":" <*> parseTerm PrecLam)) |
403 | <|> char '\'' *> ppa switchNamespace | 403 | <|> char '\'' *> ppa switchNamespace |
404 | <|> ppa id | 404 | <|> ppa id |
@@ -519,11 +519,18 @@ patLam f ge (v, t) p e = SLam v t $ compileGuardTree f f ge $ compilePatts [(p, | |||
519 | 519 | ||
520 | data Pat | 520 | data Pat |
521 | = PVar SIName -- Int | 521 | = PVar SIName -- Int |
522 | | PCon SIName [ParPat] | 522 | | PCon_ SI SIName [ParPat] |
523 | | ViewPat SExp ParPat | 523 | | ViewPat_ SI SExp ParPat |
524 | | PatType ParPat SExp | 524 | | PatType_ SI ParPat SExp |
525 | deriving Show | 525 | deriving Show |
526 | 526 | ||
527 | pattern PCon n pp <- PCon_ _ n pp | ||
528 | where PCon n pp = PCon_ (fst n <> sourceInfo pp) n pp | ||
529 | pattern ViewPat e pp <- ViewPat_ _ e pp | ||
530 | where ViewPat e pp = ViewPat_ (sourceInfo e <> sourceInfo pp) e pp | ||
531 | pattern PatType pp e <- PatType_ _ pp e | ||
532 | where PatType pp e = PatType_ (sourceInfo e <> sourceInfo pp) pp e | ||
533 | |||
527 | pattern PParens p = ViewPat (SBuiltin "parens") (ParPat [p]) | 534 | pattern PParens p = ViewPat (SBuiltin "parens") (ParPat [p]) |
528 | 535 | ||
529 | -- parallel patterns like v@(f -> [])@(Just x) | 536 | -- parallel patterns like v@(f -> [])@(Just x) |
@@ -536,30 +543,21 @@ mapPP f = \case | |||
536 | mapP :: (SExp -> SExp) -> Pat -> Pat | 543 | mapP :: (SExp -> SExp) -> Pat -> Pat |
537 | mapP f = \case | 544 | mapP f = \case |
538 | PVar n -> PVar n | 545 | PVar n -> PVar n |
539 | PCon n pp -> PCon n (mapPP f <$> pp) | 546 | PCon_ si n pp -> PCon_ si n (mapPP f <$> pp) |
540 | PParens p -> PParens (mapP f p) | 547 | ViewPat_ si e pp -> ViewPat_ si (f e) (mapPP f pp) |
541 | ViewPat e pp -> ViewPat (f e) (mapPP f pp) | 548 | PatType_ si pp e -> PatType_ si (mapPP f pp) (f e) |
542 | PatType pp e -> PatType (mapPP f pp) (f e) | ||
543 | |||
544 | --upP i j = mapP (up_ j i) | ||
545 | |||
546 | varPP = length . getPPVars_ | ||
547 | varP = length . getPVars_ | ||
548 | 549 | ||
549 | getPVars :: Pat -> [SIName] | 550 | getPVars = \case |
550 | getPVars = reverse . getPVars_ | ||
551 | |||
552 | getPPVars = reverse . getPPVars_ | ||
553 | |||
554 | getPVars_ = \case | ||
555 | PVar n -> [n] | 551 | PVar n -> [n] |
556 | PCon _ pp -> foldMap getPPVars_ pp | 552 | PCon _ pp -> foldMap getPPVars pp |
557 | PParens p -> getPVars_ p | 553 | ViewPat e pp -> getPPVars pp |
558 | ViewPat e pp -> getPPVars_ pp | 554 | PatType pp e -> getPPVars pp |
559 | PatType pp e -> getPPVars_ pp | 555 | |
556 | getPPVars = \case | ||
557 | ParPat pp -> foldMap getPVars pp | ||
560 | 558 | ||
561 | getPPVars_ = \case | 559 | varPP = length . getPPVars |
562 | ParPat pp -> foldMap getPVars_ pp | 560 | varP = length . getPVars |
563 | 561 | ||
564 | instance SourceInfo ParPat where | 562 | instance SourceInfo ParPat where |
565 | sourceInfo (ParPat ps) = sourceInfo ps | 563 | sourceInfo (ParPat ps) = sourceInfo ps |
@@ -567,34 +565,46 @@ instance SourceInfo ParPat where | |||
567 | instance SourceInfo Pat where | 565 | instance SourceInfo Pat where |
568 | sourceInfo = \case | 566 | sourceInfo = \case |
569 | PVar (si,_) -> si | 567 | PVar (si,_) -> si |
570 | PCon (si,_) ps -> si <> sourceInfo ps | 568 | PCon_ si _ _ -> si |
571 | ViewPat e ps -> sourceInfo e <> sourceInfo ps | 569 | ViewPat_ si _ _ -> si |
572 | PatType ps e -> sourceInfo ps <> sourceInfo e | 570 | PatType_ si _ _ -> si |
571 | |||
572 | instance SetSourceInfo Pat where | ||
573 | setSI si = \case | ||
574 | PVar (_, n) -> PVar (si, n) | ||
575 | PCon_ _ a b -> PCon_ si a b | ||
576 | ViewPat_ _ a b -> ViewPat_ si a b | ||
577 | PatType_ _ a b -> PatType_ si a b | ||
573 | 578 | ||
574 | -------------------------------------------------------------------------------- pattern parsing | 579 | -------------------------------------------------------------------------------- pattern parsing |
575 | 580 | ||
576 | parsePat :: Prec -> BodyParser Pat | 581 | parsePat p = appRange $ flip setSI <$> parsePat_ p |
577 | parsePat = \case | 582 | |
578 | PrecAnn -> | 583 | parsePat_ :: Prec -> BodyParser Pat |
584 | parsePat_ = \case | ||
585 | PrecAnn -> | ||
579 | patType <$> parsePat PrecOp <*> parseType (Just $ Wildcard SType) | 586 | patType <$> parsePat PrecOp <*> parseType (Just $ Wildcard SType) |
580 | PrecOp -> | 587 | PrecOp -> |
581 | join $ calculatePatPrecs <$> dsInfo <*> p_ | 588 | join $ calculatePatPrecs <$> dsInfo <*> p_ |
582 | where | 589 | where |
583 | p_ = (,) <$> parsePat PrecApp <*> option [] (colonSymbols >>= p) | 590 | p_ = (,) <$> parsePat PrecApp <*> option [] (colonSymbols >>= p) |
584 | p op = do (exp, op') <- try "pattern" ((,) <$> parsePat PrecApp <*> colonSymbols) | 591 | p op = do (exp, op') <- try "pattern" ((,) <$> parsePat PrecApp <*> colonSymbols) |
585 | ((op, exp):) <$> p op' | 592 | ((op, exp):) <$> p op' |
586 | <|> pure . (,) op <$> parsePat PrecAnn | 593 | <|> pure . (,) op <$> parsePat PrecAnn |
587 | PrecApp -> | 594 | PrecApp -> |
588 | PCon <$> upperCase_ <*> many (ParPat . pure <$> parsePat PrecAtom) | 595 | PCon <$> upperCase_ <*> many (ParPat . pure <$> parsePat PrecAtom) |
589 | <|> parsePat PrecAtom | 596 | <|> parsePat_ PrecAtom |
590 | PrecAtom -> | 597 | PrecAtom -> |
591 | mkLit <$> asks namespace <*> try "literal" parseLit | 598 | mkLit <$> asks namespace <*> try "literal" parseLit |
592 | <|> flip PCon [] <$> upperCase_ | 599 | <|> flip PCon [] <$> upperCase_ |
593 | <|> char '\'' *> switchNS (parsePat PrecAtom) | ||
594 | <|> PVar <$> patVar | 600 | <|> PVar <$> patVar |
595 | <|> (\ns -> pConSI . mkListPat ns) <$> asks namespace <*> brackets patlist | 601 | <|> char '\'' *> ppa switchNamespace |
596 | <|> (\ns -> pConSI . mkTupPat ns) <$> asks namespace <*> parens patlist | 602 | <|> ppa id |
597 | where | 603 | where |
604 | ppa tick = | ||
605 | brackets (mkListPat . tick <$> asks namespace <*> patlist) | ||
606 | <|> parens (mkTupPat . tick <$> asks namespace <*> patlist) | ||
607 | |||
598 | litP = flip ViewPat (ParPat [PCon (mempty, "True") []]) . SAppV (SBuiltin "==") | 608 | litP = flip ViewPat (ParPat [PCon (mempty, "True") []]) . SAppV (SBuiltin "==") |
599 | 609 | ||
600 | mkLit TypeNS (LInt n) = toNatP n -- todo: elim this alternative | 610 | mkLit TypeNS (LInt n) = toNatP n -- todo: elim this alternative |
@@ -605,9 +615,6 @@ parsePat = \case | |||
605 | run 0 = PCon (mempty, "Zero") [] | 615 | run 0 = PCon (mempty, "Zero") [] |
606 | run n | n > 0 = PCon (mempty, "Succ") [ParPat [run $ n-1]] | 616 | run n | n > 0 = PCon (mempty, "Succ") [ParPat [run $ n-1]] |
607 | 617 | ||
608 | pConSI (PCon (_, n) ps) = PCon (sourceInfo ps, n) ps | ||
609 | pConSI p = p | ||
610 | |||
611 | patlist = commaSep $ parsePat PrecAnn | 618 | patlist = commaSep $ parsePat PrecAnn |
612 | 619 | ||
613 | mkListPat TypeNS [p] = PCon (debugSI "mkListPat4", "'List") [ParPat [p]] | 620 | mkListPat TypeNS [p] = PCon (debugSI "mkListPat4", "'List") [ParPat [p]] |
@@ -615,22 +622,22 @@ parsePat = \case | |||
615 | mkListPat _ [] = PCon (debugSI "mkListPat3", "Nil") [] | 622 | mkListPat _ [] = PCon (debugSI "mkListPat3", "Nil") [] |
616 | 623 | ||
617 | --mkTupPat :: [Pat] -> Pat | 624 | --mkTupPat :: [Pat] -> Pat |
618 | mkTupPat ns [PParens x] = ff [x] | 625 | -- TODO: tup type pattern in type namespace |
619 | mkTupPat ns [x] = PParens x | 626 | mkTupPat ns [PParens x] = mkTup [x] |
620 | mkTupPat ns ps = ff ps | 627 | mkTupPat ns [x] = PParens x |
628 | mkTupPat ns ps = mkTup ps | ||
621 | 629 | ||
622 | ff ps = foldr (\a b -> PCon (mempty, "HCons") (ParPat . (:[]) <$> [a, b])) (PCon (mempty, "HNil") []) ps | 630 | mkTup ps = foldr (\a b -> PCon (mempty, "HCons") (ParPat . (:[]) <$> [a, b])) (PCon (mempty, "HNil") []) ps |
623 | 631 | ||
624 | patType p (Wildcard SType) = p | 632 | patType p (Wildcard SType) = p |
625 | patType p t = PatType (ParPat [p]) t | 633 | patType p t = PatType (ParPat [p]) t |
626 | 634 | ||
627 | calculatePatPrecs dcls (e, xs) = postponedCheck dcls $ calcPrec (\op x y -> PCon op $ ParPat . (:[]) <$> [x, y]) (getFixity dcls . snd) e xs | 635 | calculatePatPrecs dcls (e, xs) = postponedCheck dcls $ calcPrec (\op x y -> PCon op $ ParPat . (:[]) <$> [x, y]) (getFixity dcls . snd) e xs |
628 | 636 | ||
629 | longPattern = parsePat PrecAnn <&> (getPVars &&& id) | 637 | longPattern = parsePat PrecAnn <&> (reverse . getPVars &&& id) |
630 | --patternAtom = parsePat PrecAtom <&> (getPVars &&& id) | ||
631 | 638 | ||
632 | telescopePat = do | 639 | telescopePat = do |
633 | (a, b) <- fmap (getPPVars . ParPat . map snd &&& id) $ many $ uncurry f <$> hiddenTerm (parsePat PrecAtom) (parsePat PrecAtom) | 640 | (a, b) <- fmap (reverse . foldMap (getPVars . snd) &&& id) $ many $ uncurry f <$> hiddenTerm (parsePat PrecAtom) (parsePat PrecAtom) |
634 | checkPattern a | 641 | checkPattern a |
635 | return (a, b) | 642 | return (a, b) |
636 | where | 643 | where |
@@ -668,10 +675,7 @@ mapGT k i = \case | |||
668 | upGT k i = mapGT k $ \k -> up_ i k | 675 | upGT k i = mapGT k $ \k -> up_ i k |
669 | 676 | ||
670 | substGT i j = mapGT 0 $ \k -> rearrangeS 0 $ \r -> if r == k + i then k + j else if r > k + i then r - 1 else r | 677 | substGT i j = mapGT 0 $ \k -> rearrangeS 0 $ \r -> if r == k + i then k + j else if r > k + i then r - 1 else r |
671 | {- | 678 | |
672 | dbfGT :: [SIName] -> GuardTree -> GuardTree | ||
673 | dbfGT v = mapGT 0 $ \k -> deBruijnify_ k v | ||
674 | -} | ||
675 | -- todo: clenup | 679 | -- todo: clenup |
676 | compilePatts :: [(Pat, Int)] -> Either [(SExp, SExp)] SExp -> GuardTree | 680 | compilePatts :: [(Pat, Int)] -> Either [(SExp, SExp)] SExp -> GuardTree |
677 | compilePatts ps gu = cp [] ps | 681 | compilePatts ps gu = cp [] ps |
@@ -866,7 +870,7 @@ parseDef = | |||
866 | ] | 870 | ] |
867 | 871 | ||
868 | 872 | ||
869 | parseRHS fe tok = fmap (fmap (fe *** fe) +++ fe) $ do | 873 | parseRHS tok = do |
870 | fmap Left . some $ (,) <$ reservedOp "|" <*> parseTerm PrecOp <* reservedOp tok <*> parseTerm PrecLam | 874 | fmap Left . some $ (,) <$ reservedOp "|" <*> parseTerm PrecOp <* reservedOp tok <*> parseTerm PrecLam |
871 | <|> do | 875 | <|> do |
872 | reservedOp tok | 876 | reservedOp tok |
@@ -889,7 +893,7 @@ funAltDef parseOpName parseName = do | |||
889 | checkPattern fee | 893 | checkPattern fee |
890 | return (n, (fee, (,) (Visible, Wildcard SType) <$> [a1, mapP (deBruijnify e') a2])) | 894 | return (n, (fee, (,) (Visible, Wildcard SType) <$> [a1, mapP (deBruijnify e') a2])) |
891 | <|> do try "lhs" $ (,) <$> parseName <*> telescopePat <* lookAhead (reservedOp "=" <|> reservedOp "|") | 895 | <|> do try "lhs" $ (,) <$> parseName <*> telescopePat <* lookAhead (reservedOp "=" <|> reservedOp "|") |
892 | FunAlt n tss <$> parseRHS (deBruijnify fee) "=" | 896 | FunAlt n tss . deBruijnify fee <$> parseRHS "=" |
893 | 897 | ||
894 | valueDef :: BodyParser [Stmt] | 898 | valueDef :: BodyParser [Stmt] |
895 | valueDef = do | 899 | valueDef = do |
@@ -905,7 +909,7 @@ desugarValueDef ds p e | |||
905 | | (i, x) <- zip [0..] dns | 909 | | (i, x) <- zip [0..] dns |
906 | ] | 910 | ] |
907 | where | 911 | where |
908 | dns = getPVars p | 912 | dns = reverse $ getPVars p |
909 | n = mangleNames dns | 913 | n = mangleNames dns |
910 | 914 | ||
911 | mangleNames xs = (foldMap fst xs, "_" ++ intercalate "_" (map snd xs)) | 915 | mangleNames xs = (foldMap fst xs, "_" ++ intercalate "_" (map snd xs)) |
@@ -981,7 +985,7 @@ desugarMutual ds xs = xs | |||
981 | | (i, x) <- zip [0..] dns | 985 | | (i, x) <- zip [0..] dns |
982 | ] | 986 | ] |
983 | where | 987 | where |
984 | dns = getPVars p | 988 | dns = reverse $ getPVars p |
985 | n = mangleNames dns | 989 | n = mangleNames dns |
986 | (ps, es) = unzip [(n, e) | Let n ~Nothing ~Nothing [] e <- xs] | 990 | (ps, es) = unzip [(n, e) | Let n ~Nothing ~Nothing [] e <- xs] |
987 | tup = "Tuple" ++ show (length xs) | 991 | tup = "Tuple" ++ show (length xs) |