diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-21 08:58:32 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-21 08:58:32 +0200 |
commit | 4575f9eea5db5b53b11a551b89d6195caf4ea88a (patch) | |
tree | 898290a85ecf49083b99397d222712e81c7ff183 | |
parent | 3e2821c6dc119ae116d5a92bdfc7d5d0983e4cde (diff) |
simplification
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index 793d31ec..a0ed25bd 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -263,6 +263,12 @@ instance Up a => Up (SExp' a) where | |||
263 | class Rearrange a where | 263 | class Rearrange a where |
264 | rearrange :: Int -> (Int -> Int) -> a -> a | 264 | rearrange :: Int -> (Int -> Int) -> a -> a |
265 | 265 | ||
266 | rSubst :: Rearrange a => Int -> Int -> a -> a | ||
267 | rSubst i j = rearrange 0 $ \k -> if k == i then j else if k > i then k - 1 else k | ||
268 | |||
269 | rUp :: Rearrange a => Int -> Int -> a -> a | ||
270 | rUp n l = rearrange l $ \k -> if k >= 0 then k + n else k | ||
271 | |||
266 | instance Rearrange SExp where | 272 | instance Rearrange SExp where |
267 | rearrange i f = mapS (\_ -> elimVoid) (const . SGlobal) (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) i | 273 | rearrange i f = mapS (\_ -> elimVoid) (const . SGlobal) (\sn j i -> SVar sn $ if j < i then j else i + f (j - i)) i |
268 | 274 | ||
@@ -568,12 +574,6 @@ mapPP f i = \case | |||
568 | upPats f g k [] = [] | 574 | upPats f g k [] = [] |
569 | upPats f g k (p: ps) = g k p: upPats f g (k + f p) ps | 575 | upPats f g k (p: ps) = g k p: upPats f g (k + f p) ps |
570 | 576 | ||
571 | instance Up Pat where | ||
572 | up_ = mapP . up_ | ||
573 | |||
574 | instance Up ParPat where | ||
575 | up_ = mapPP . up_ | ||
576 | |||
577 | instance Rearrange Pat where | 577 | instance Rearrange Pat where |
578 | rearrange k f = mapP (`rearrange` f) k | 578 | rearrange k f = mapP (`rearrange` f) k |
579 | 579 | ||
@@ -689,6 +689,8 @@ postponedCheck dcls x = do | |||
689 | 689 | ||
690 | -------------------------------------------------------------------------------- pattern match compilation | 690 | -------------------------------------------------------------------------------- pattern match compilation |
691 | 691 | ||
692 | -- TODO: support let | ||
693 | -- TODO: support type signature? | ||
692 | data GuardTree | 694 | data GuardTree |
693 | = GuardNode SExp SName [ParPat] GuardTrees | 695 | = GuardNode SExp SName [ParPat] GuardTrees |
694 | | GuardLeaf SExp | 696 | | GuardLeaf SExp |
@@ -701,25 +703,19 @@ mapGT f h k = \case | |||
701 | GuardNode e c pps gt -> GuardNode (h k e) c (upPats varPP f k pps) $ mapGT f h (k + sum (map varPP pps)) <$> gt | 703 | GuardNode e c pps gt -> GuardNode (h k e) c (upPats varPP f k pps) $ mapGT f h (k + sum (map varPP pps)) <$> gt |
702 | GuardLeaf e -> GuardLeaf $ h k e | 704 | GuardLeaf e -> GuardLeaf $ h k e |
703 | 705 | ||
704 | instance Up GuardTree where | ||
705 | up_ n = mapGT (up_ n) (up_ n) | ||
706 | |||
707 | instance Rearrange GuardTree where | 706 | instance Rearrange GuardTree where |
708 | rearrange l f = mapGT (`rearrange` f) (`rearrange` f) l | 707 | rearrange l f = mapGT (`rearrange` f) (`rearrange` f) l |
709 | 708 | ||
710 | substGT :: Int -> Int -> GuardTree -> GuardTree | 709 | instance Rearrange a => Rearrange [a] where |
711 | substGT i j = rearrange 0 $ \k -> if k == i then j else if k > i then k - 1 else k | 710 | rearrange l f = map $ rearrange l f |
712 | 711 | ||
713 | -- todo: clenup | 712 | -- todo: clenup |
714 | compilePatts :: [(Pat, Int)] -> Either [(SExp, SExp)] SExp -> GuardTrees | 713 | compilePatts :: [(Pat, Int)] -> Either [(SExp, SExp)] SExp -> GuardTrees |
715 | compilePatts ps gu = cp [] ps | 714 | compilePatts ps gu = cp [] ps |
716 | where | 715 | where |
717 | cp ps' [] = case gu of | 716 | cp ps' [] = rearrange 0 (f $ reverse ps') $ case gu of |
718 | Right e -> [GuardLeaf $ rearrange 0 (f $ reverse ps') e] | 717 | Right e -> [GuardLeaf e] |
719 | Left gs -> | 718 | Left gs -> [GuardNode ge "True" [] [GuardLeaf e] | (ge, e) <- gs] |
720 | [ GuardNode (rearrange 0 (f $ reverse ps') ge) "True" [] [GuardLeaf $ rearrange 0 (f $ reverse ps') e] | ||
721 | | (ge, e) <- gs | ||
722 | ] | ||
723 | cp ps' ((p@PVar{}, i): xs) = cp (p: ps') xs | 719 | cp ps' ((p@PVar{}, i): xs) = cp (p: ps') xs |
724 | cp ps' ((p@(PCon (si, n) ps), i): xs) = [GuardNode (SVar (si, n) $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs] | 720 | cp ps' ((p@(PCon (si, n) ps), i): xs) = [GuardNode (SVar (si, n) $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs] |
725 | cp ps' ((PParens p, i): xs) = cp ps' ((p, i): xs) | 721 | cp ps' ((PParens p, i): xs) = cp ps' ((p, i): xs) |
@@ -757,13 +753,13 @@ compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ g | |||
757 | Nothing -> error $ "Constructor is not defined: " ++ s | 753 | Nothing -> error $ "Constructor is not defined: " ++ s |
758 | Just (Left ((casename, inum), cns)) -> | 754 | Just (Left ((casename, inum), cns)) -> |
759 | foldl SAppV (SGlobal (debugSI "compileGuardTree2", casename) `SAppV` iterateN (1 + inum) SLamV (Wildcard (Wildcard SType))) | 755 | foldl SAppV (SGlobal (debugSI "compileGuardTree2", casename) `SAppV` iterateN (1 + inum) SLamV (Wildcard (Wildcard SType))) |
760 | [ iterateN n SLamV $ guardTreeToCases $ filterGuardTree (up n f) cn 0 n $ up n ts | 756 | [ iterateN n SLamV $ guardTreeToCases $ filterGuardTree (up n f) cn 0 n $ rUp n 0 ts |
761 | | (cn, n) <- cns | 757 | | (cn, n) <- cns |
762 | ] | 758 | ] |
763 | `SAppV` f | 759 | `SAppV` f |
764 | Just (Right n) -> SGlobal (debugSI "compileGuardTree3", MatchName s) | 760 | Just (Right n) -> SGlobal (debugSI "compileGuardTree3", MatchName s) |
765 | `SAppV` SLamV (Wildcard SType) | 761 | `SAppV` SLamV (Wildcard SType) |
766 | `SAppV` iterateN n SLamV (guardTreeToCases $ filterGuardTree (up n f) s 0 n $ up n ts) | 762 | `SAppV` iterateN n SLamV (guardTreeToCases $ filterGuardTree (up n f) s 0 n $ rUp n 0 ts) |
767 | `SAppV` f | 763 | `SAppV` f |
768 | `SAppV` guardTreeToCases (filterGuardTree' f s ts) | 764 | `SAppV` guardTreeToCases (filterGuardTree' f s ts) |
769 | 765 | ||
@@ -800,7 +796,7 @@ compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ g | |||
800 | ViewPat f (ParPat p) -> guardNode (f `SAppV` v) p {- -$ guardNode v ws -} e | 796 | ViewPat f (ParPat p) -> guardNode (f `SAppV` v) p {- -$ guardNode v ws -} e |
801 | PCon (_, s) ps' -> [GuardNode v s ps' {- -$ guardNode v ws -} e] | 797 | PCon (_, s) ps' -> [GuardNode v s ps' {- -$ guardNode v ws -} e] |
802 | 798 | ||
803 | varGuardNode v (SVar _ e) = substGT v e | 799 | varGuardNode v (SVar _ e) = rSubst v e |
804 | 800 | ||
805 | compileGuardTrees ulend = compileGuardTree ulend SRHS | 801 | compileGuardTrees ulend = compileGuardTree ulend SRHS |
806 | 802 | ||