summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-04-20 22:29:45 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-04-20 22:29:45 +0200
commitdd45f9928c190ae95f9e345aa5fa219321d2acba (patch)
tree920dc0370bb1e128fe8d152c600f6a1bdd4ddf5f /src
parent71e72247060237b4de5d5b8f4035505f90384028 (diff)
refactoring
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Parser.hs80
1 files changed, 36 insertions, 44 deletions
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index bd3a6286..6887c8f4 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -483,10 +483,7 @@ parseTerm_ ge = \case
483 exp <- parseTerm PrecLam 483 exp <- parseTerm PrecLam
484 return $ \e -> 484 return $ \e ->
485 SBuiltin "concatMap" 485 SBuiltin "concatMap"
486 `SAppV` SLamV (compileGuardTree id id ge $ Alts 486 `SAppV` SLamV (compileGuardTree id id ge $ compilePatts [(pat, 0)] (Right $ deBruijnify dbs e) ++ [GuardLeaf BNil])
487 [ compilePatts [(pat, 0)] $ Right $ deBruijnify dbs e
488 , GuardLeaf $ BNil
489 ])
490 `SAppV` exp 487 `SAppV` exp
491 488
492 letdecl = mkLets ge <$ reserved "let" <*> (compileFunAlts' =<< valueDef) 489 letdecl = mkLets ge <$ reserved "let" <*> (compileFunAlts' =<< valueDef)
@@ -671,18 +668,15 @@ postponedCheck dcls x = do
671 668
672-------------------------------------------------------------------------------- pattern match compilation 669-------------------------------------------------------------------------------- pattern match compilation
673 670
671type GuardTrees = [GuardTree]
672
674data GuardTree 673data GuardTree
675 = GuardNode SExp SName [ParPat] GuardTree -- _ <- _ 674 = GuardNode SExp SName [ParPat] GuardTrees -- _ <- _
676 | Alts [GuardTree] -- _ | _
677 | GuardLeaf SExp -- _ -> e 675 | GuardLeaf SExp -- _ -> e
678 deriving Show 676 deriving Show
679 677
680alts (Alts xs) = concatMap alts xs
681alts x = [x]
682
683mapGT k i = \case 678mapGT k i = \case
684 GuardNode e c pps gt -> GuardNode (i k e) c {-todo: up-}pps $ mapGT (k + sum (map varPP pps)) i gt 679 GuardNode e c pps gt -> GuardNode (i k e) c {-todo: up-}pps $ mapGT (k + sum (map varPP pps)) i <$> gt
685 Alts gts -> Alts $ map (mapGT k i) gts
686 GuardLeaf e -> GuardLeaf $ i k e 680 GuardLeaf e -> GuardLeaf $ i k e
687 681
688upGT k i = mapGT k $ \k -> up_ i k 682upGT k i = mapGT k $ \k -> up_ i k
@@ -690,20 +684,20 @@ upGT k i = mapGT k $ \k -> up_ i k
690substGT 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 684substGT 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
691 685
692-- todo: clenup 686-- todo: clenup
693compilePatts :: [(Pat, Int)] -> Either [(SExp, SExp)] SExp -> GuardTree 687compilePatts :: [(Pat, Int)] -> Either [(SExp, SExp)] SExp -> GuardTrees
694compilePatts ps gu = cp [] ps 688compilePatts ps gu = cp [] ps
695 where 689 where
696 cp ps' [] = case gu of 690 cp ps' [] = case gu of
697 Right e -> GuardLeaf $ rearrangeS 0 (f $ reverse ps') e 691 Right e -> [GuardLeaf $ rearrangeS 0 (f $ reverse ps') e]
698 Left gs -> Alts 692 Left gs ->
699 [ GuardNode (rearrangeS 0 (f $ reverse ps') ge) "True" [] $ GuardLeaf $ rearrangeS 0 (f $ reverse ps') e 693 [ GuardNode (rearrangeS 0 (f $ reverse ps') ge) "True" [] [GuardLeaf $ rearrangeS 0 (f $ reverse ps') e]
700 | (ge, e) <- gs 694 | (ge, e) <- gs
701 ] 695 ]
702 cp ps' ((p@PVar{}, i): xs) = cp (p: ps') xs 696 cp ps' ((p@PVar{}, i): xs) = cp (p: ps') xs
703 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 697 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]
704 cp ps' ((PParens p, i): xs) = cp ps' ((p, i): xs) 698 cp ps' ((PParens p, i): xs) = cp ps' ((p, i): xs)
705 cp ps' ((p@(ViewPatSimp f (PCon (si, n) ps)), i): xs) 699 cp ps' ((p@(ViewPatSimp f (PCon (si, n) ps)), i): xs)
706 = GuardNode (SAppV f $ SVar (si, n) $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs 700 = [GuardNode (SAppV f $ SVar (si, n) $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs]
707 cp _ p = error $ "cp: " ++ show p 701 cp _ p = error $ "cp: " ++ show p
708 702
709 m = length ps 703 m = length ps
@@ -725,69 +719,67 @@ compilePatts ps gu = cp [] ps
725 vs' = map (fromMaybe 0) vs_ 719 vs' = map (fromMaybe 0) vs_
726 s = sum vs 720 s = sum vs
727 721
728compileGuardTrees ulend ge alts = compileGuardTree ulend SRHS ge $ Alts alts 722compileGuardTrees ulend ge alts = compileGuardTree ulend SRHS ge alts
729compileGuardTrees' ge alts = foldr1 (SAppV2 $ SBuiltin "parEval" `SAppV` Wildcard SType) $ compileGuardTree id SRHS ge <$> alts 723compileGuardTrees' ge alts = foldr1 (SAppV2 $ SBuiltin "parEval" `SAppV` Wildcard SType) $ compileGuardTree id SRHS ge . (:[]) <$> alts
730 724
731compileGuardTree :: (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> GuardTree -> SExp 725compileGuardTree :: (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> GuardTrees -> SExp
732compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ guardTreeToCases t 726compileGuardTree ulend lend adts t = (\x -> traceD (" ! :" ++ ppShow x) x) $ guardTreeToCases t
733 where 727 where
734 guardTreeToCases :: GuardTree -> SExp 728 guardTreeToCases :: GuardTrees -> SExp
735 guardTreeToCases t = case alts t of 729 guardTreeToCases t = case {-alts-} t of
736 [] -> ulend $ SBuiltin "undefined" 730 [] -> ulend $ SBuiltin "undefined"
737 GuardLeaf e: _ -> lend e 731 GuardLeaf e: _ -> lend e
738 ts@(GuardNode f s _ _: _) -> case Map.lookup s (snd adts) of 732 ts@(GuardNode f s _ _: _) -> case Map.lookup s (snd adts) of
739 Nothing -> error $ "Constructor is not defined: " ++ s 733 Nothing -> error $ "Constructor is not defined: " ++ s
740 Just (Left ((casename, inum), cns)) -> 734 Just (Left ((casename, inum), cns)) ->
741 foldl SAppV (SGlobal (debugSI "compileGuardTree2", casename) `SAppV` iterateN (1 + inum) SLamV (Wildcard (Wildcard SType))) 735 foldl SAppV (SGlobal (debugSI "compileGuardTree2", casename) `SAppV` iterateN (1 + inum) SLamV (Wildcard (Wildcard SType)))
742 [ iterateN n SLamV $ guardTreeToCases $ Alts $ map (filterGuardTree (up n f) cn 0 n . upGT 0 n) ts 736 [ iterateN n SLamV $ guardTreeToCases $ filterGuardTree (up n f) cn 0 n $ upGT 0 n <$> ts
743 | (cn, n) <- cns 737 | (cn, n) <- cns
744 ] 738 ]
745 `SAppV` f 739 `SAppV` f
746 Just (Right n) -> SGlobal (debugSI "compileGuardTree3", MatchName s) 740 Just (Right n) -> SGlobal (debugSI "compileGuardTree3", MatchName s)
747 `SAppV` SLamV (Wildcard SType) 741 `SAppV` SLamV (Wildcard SType)
748 `SAppV` iterateN n SLamV (guardTreeToCases $ Alts $ map (filterGuardTree (up n f) s 0 n . upGT 0 n) ts) 742 `SAppV` iterateN n SLamV (guardTreeToCases $ filterGuardTree (up n f) s 0 n $ upGT 0 n <$> ts)
749 `SAppV` f 743 `SAppV` f
750 `SAppV` guardTreeToCases (Alts $ map (filterGuardTree' f s) ts) 744 `SAppV` guardTreeToCases (filterGuardTree' f s ts)
751 745
752 filterGuardTree :: SExp -> SName{-constr.-} -> Int -> Int{-number of constr. params-} -> GuardTree -> GuardTree 746 filterGuardTree :: SExp -> SName{-constr.-} -> Int -> Int{-number of constr. params-} -> GuardTrees -> GuardTrees
753 filterGuardTree f s k ns = \case 747 filterGuardTree f s k ns = concatMap $ \case
754 GuardLeaf e -> GuardLeaf e 748 GuardLeaf e -> [GuardLeaf e]
755 Alts ts -> Alts $ map (filterGuardTree f s k ns) ts
756 GuardNode f' s' ps gs 749 GuardNode f' s' ps gs
757 | f /= f' -> GuardNode f' s' ps $ filterGuardTree (up su f) s (su + k) ns gs 750 | f /= f' -> [GuardNode f' s' ps $ filterGuardTree (up su f) s (su + k) ns gs]
758 | s == s' -> filterGuardTree f s k ns $ guardNodes (zips [k+ns-1, k+ns-2..] ps) gs 751 | s == s' -> filterGuardTree f s k ns $ guardNodes (zips [k+ns-1, k+ns-2..] ps) gs
759 | otherwise -> Alts [] 752 | otherwise -> []
760 where 753 where
761 zips is ps = zip (map (sVar "30") $ zipWith (+) is $ sums $ map varPP ps) ps 754 zips is ps = zip (map (sVar "30") $ zipWith (+) is $ sums $ map varPP ps) ps
762 su = sum $ map varPP ps 755 su = sum $ map varPP ps
763 sums = scanl (+) 0 756 sums = scanl (+) 0
764 757
765 filterGuardTree' :: SExp -> SName{-constr.-} -> GuardTree -> GuardTree 758 filterGuardTree' :: SExp -> SName{-constr.-} -> GuardTrees -> GuardTrees
766 filterGuardTree' f s = \case 759 filterGuardTree' f s = concatMap $ \case
767 GuardLeaf e -> GuardLeaf e 760 GuardLeaf e -> [GuardLeaf e]
768 Alts ts -> Alts $ map (filterGuardTree' f s) ts
769 GuardNode f' s' ps gs 761 GuardNode f' s' ps gs
770 | f /= f' || s /= s' -> GuardNode f' s' ps $ filterGuardTree' (up su f) s gs 762 | f /= f' || s /= s' -> [GuardNode f' s' ps $ filterGuardTree' (up su f) s gs]
771 | otherwise -> Alts [] 763 | otherwise -> []
772 where 764 where
773 su = sum $ map varPP ps 765 su = sum $ map varPP ps
774 766
775 guardNodes :: [(SExp, ParPat)] -> GuardTree -> GuardTree 767 guardNodes :: [(SExp, ParPat)] -> GuardTrees -> GuardTrees
776 guardNodes [] l = l 768 guardNodes [] l = l
777 guardNodes ((v, ParPat ws): vs) e = guardNode v ws $ guardNodes vs e 769 guardNodes ((v, ParPat ws): vs) e = guardNode v ws $ guardNodes vs e
778 770
779 guardNode :: SExp -> [Pat] -> GuardTree -> GuardTree 771 guardNode :: SExp -> [Pat] -> GuardTrees -> GuardTrees
780 guardNode v [] e = e 772 guardNode v [] e = e
781 guardNode v [w] e = case w of 773 guardNode v [w] e = case w of
782 PVar _ -> {-todo guardNode v (subst x v ws) $ -} varGuardNode 0 v e 774 PVar _ -> {-todo guardNode v (subst x v ws) $ -} varGuardNode 0 v <$> e
783 PParens p -> guardNode v [p] e 775 PParens p -> guardNode v [p] e
784 ViewPat f (ParPat p) -> guardNode (f `SAppV` v) p {- -$ guardNode v ws -} e 776 ViewPat f (ParPat p) -> guardNode (f `SAppV` v) p {- -$ guardNode v ws -} e
785 PCon (_, s) ps' -> GuardNode v s ps' {- -$ guardNode v ws -} e 777 PCon (_, s) ps' -> [GuardNode v s ps' {- -$ guardNode v ws -} e]
786 778
787 varGuardNode v (SVar _ e) = substGT v e 779 varGuardNode v (SVar _ e) = substGT v e
788 780
789compileCase ge x cs 781compileCase ge x cs
790 = SLamV (compileGuardTree id id ge $ Alts [compilePatts [(p, 0)] e | (p, e) <- cs]) `SAppV` x 782 = SLamV (compileGuardTree id id ge $ concat [compilePatts [(p, 0)] e | (p, e) <- cs]) `SAppV` x
791 783
792 784
793-------------------------------------------------------------------------------- declaration representation 785-------------------------------------------------------------------------------- declaration representation
@@ -1046,7 +1038,7 @@ compileFunAlts' ds = fmap concat . sequence $ map (compileFunAlts (compileGuardT
1046 h _ _ = False 1038 h _ _ = False
1047 1039
1048--compileFunAlts :: forall m . Monad m => Bool -> (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> [Stmt] -> [Stmt] -> m [Stmt] 1040--compileFunAlts :: forall m . Monad m => Bool -> (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> [Stmt] -> [Stmt] -> m [Stmt]
1049compileFunAlts compilegt ds xs = dsInfo >>= \ge -> case xs of 1041compileFunAlts (compilegt :: DesugarInfo -> GuardTrees -> SExp) ds xs = dsInfo >>= \ge -> case xs of
1050 [Instance{}] -> return [] 1042 [Instance{}] -> return []
1051 [Class n ps ms] -> do 1043 [Class n ps ms] -> do
1052 cd <- compileFunAlts' $ 1044 cd <- compileFunAlts' $
@@ -1079,7 +1071,7 @@ compileFunAlts compilegt ds xs = dsInfo >>= \ge -> case xs of
1079 | otherwise -> return 1071 | otherwise -> return
1080 [ Let n 1072 [ Let n
1081 (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) 1073 (listToMaybe [t | TypeAnn n' t <- ds, n' == n])
1082 $ foldr (uncurry SLam . fst) (compilegt ge 1074 $ foldr (uncurry SLam . fst) (compilegt ge $ concat
1083 [ compilePatts (zip (map snd vs) $ reverse [0.. num - 1]) gsx 1075 [ compilePatts (zip (map snd vs) $ reverse [0.. num - 1]) gsx
1084 | FunAlt _ vs gsx <- fs 1076 | FunAlt _ vs gsx <- fs
1085 ]) vs 1077 ]) vs