diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-04-25 09:50:55 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-04-25 09:50:55 +0200 |
commit | ccb994a7ee997723d4634ca7a6a79b62394a9296 (patch) | |
tree | 76447ceac7b705255d1314f5874be9669e556451 /src | |
parent | eb71540a59046a8c06f816af2e5f3f238f19545f (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Parser.hs | 79 |
1 files changed, 41 insertions, 38 deletions
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs index 13891d2d..bc51f386 100644 --- a/src/LambdaCube/Compiler/Parser.hs +++ b/src/LambdaCube/Compiler/Parser.hs | |||
@@ -353,10 +353,7 @@ telescope mb = fmap mkTelescope $ many $ hiddenTerm | |||
353 | tvar x = (,) <$> patVar <*> x | 353 | tvar x = (,) <$> patVar <*> x |
354 | typedId = parens $ tvar $ parseType mb | 354 | typedId = parens $ tvar $ parseType mb |
355 | 355 | ||
356 | mkTelescope = go [] | 356 | mkTelescope (unzip -> (vs, ns)) = second (zip vs) $ mkTelescope' $ first (:[]) <$> ns |
357 | where | ||
358 | go ns [] = (ns, []) | ||
359 | go ns ((v, (n, e)): ts) = second ((v, deBruijnify ns e):) $ go (n: ns) ts | ||
360 | 357 | ||
361 | mkTelescope' = go [] | 358 | mkTelescope' = go [] |
362 | where | 359 | where |
@@ -487,7 +484,7 @@ parseTerm_ = \case | |||
487 | cf <- compileGuardTree id id $ compilePatts [pat] (noGuards $ deBruijnify dbs e) `mappend` In (GTSuccess BNil) | 484 | cf <- compileGuardTree id id $ compilePatts [pat] (noGuards $ deBruijnify dbs e) `mappend` In (GTSuccess BNil) |
488 | return $ SBuiltin "concatMap" `SAppV` SLamV cf `SAppV` exp | 485 | return $ SBuiltin "concatMap" `SAppV` SLamV cf `SAppV` exp |
489 | 486 | ||
490 | letdecl = (return .) . mkLets <$ reserved "let" <*> (compileFunAlts' =<< valueDef) | 487 | letdecl = (return .) . mkLets <$ reserved "let" <*> (compileStmt' =<< valueDef) |
491 | 488 | ||
492 | boolExpression = (\pred e -> return $ SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` BNil) <$> parseTerm PrecLam | 489 | boolExpression = (\pred e -> return $ SBuiltin "primIfThenElse" `SAppV` pred `SAppV` e `SAppV` BNil) <$> parseTerm PrecLam |
493 | 490 | ||
@@ -724,7 +721,7 @@ runPMC = return . runIdentity | |||
724 | 721 | ||
725 | data Lets a | 722 | data Lets a |
726 | = LLet SIName SExp (Lets a) | 723 | = LLet SIName SExp (Lets a) |
727 | | LTypeAnn SExp (Lets a) | 724 | | LTypeAnn SExp (Lets a) -- TODO: eliminate if not used |
728 | | In a | 725 | | In a |
729 | deriving Show | 726 | deriving Show |
730 | 727 | ||
@@ -742,7 +739,6 @@ instance Rearrange a => Rearrange (Lets a) where | |||
742 | instance DeBruijnify a => DeBruijnify (Lets a) where | 739 | instance DeBruijnify a => DeBruijnify (Lets a) where |
743 | deBruijnify_ l ns = mapLets (`deBruijnify_` ns) (`deBruijnify_` ns) l | 740 | deBruijnify_ l ns = mapLets (`deBruijnify_` ns) (`deBruijnify_` ns) l |
744 | 741 | ||
745 | -- TODO: support type signature here? | ||
746 | data GuardTree | 742 | data GuardTree |
747 | = GuardNode SExp (SIName, ConsInfo) [SIName] GuardTrees GuardTrees | 743 | = GuardNode SExp (SIName, ConsInfo) [SIName] GuardTrees GuardTrees |
748 | | GTSuccess SExp | 744 | | GTSuccess SExp |
@@ -870,7 +866,10 @@ data Stmt | |||
870 | = Let SIName (Maybe SExp) SExp | 866 | = Let SIName (Maybe SExp) SExp |
871 | | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} Bool{-True:add foralls-} [(SIName, SExp)]{-constructor names and types-} | 867 | | Data SIName [(Visibility, SExp)]{-parameters-} SExp{-type-} Bool{-True:add foralls-} [(SIName, SExp)]{-constructor names and types-} |
872 | | PrecDef SIName Fixity | 868 | | PrecDef SIName Fixity |
869 | deriving (Show) | ||
873 | 870 | ||
871 | data PreStmt | ||
872 | = Stmt Stmt | ||
874 | -- eliminated during parsing | 873 | -- eliminated during parsing |
875 | | TypeAnn SIName SExp | 874 | | TypeAnn SIName SExp |
876 | | TypeFamily SIName SExp{-type-} -- type family declaration | 875 | | TypeFamily SIName SExp{-type-} -- type family declaration |
@@ -887,15 +886,18 @@ instance PShow Stmt where | |||
887 | Data (_, n) ps ty fa cs -> "data" <+> text n | 886 | Data (_, n) ps ty fa cs -> "data" <+> text n |
888 | PrecDef (_, n) i -> "precedence" <+> text n <+> text (show i) | 887 | PrecDef (_, n) i -> "precedence" <+> text n <+> text (show i) |
889 | 888 | ||
890 | instance DeBruijnify Stmt where | 889 | instance DeBruijnify PreStmt where |
891 | deBruijnify_ k v = \case | 890 | deBruijnify_ k v = \case |
892 | FunAlt n ts gue -> FunAlt n (map (second $ deBruijnify_ k v) ts) $ deBruijnify_ k v gue | 891 | FunAlt n ts gue -> FunAlt n (map (second $ deBruijnify_ k v) ts) $ deBruijnify_ k v gue |
893 | Let sn mt e -> Let sn (deBruijnify_ k v <$> mt) (deBruijnify_ k v e) | ||
894 | x -> error $ "deBruijnify @ " ++ show x | 892 | x -> error $ "deBruijnify @ " ++ show x |
895 | 893 | ||
894 | instance DeBruijnify Stmt where | ||
895 | deBruijnify_ k v = \case | ||
896 | Let sn mt e -> Let sn (deBruijnify_ k v <$> mt) (deBruijnify_ k v e) | ||
897 | x -> error $ "deBruijnify @ " ++ show x | ||
896 | -------------------------------------------------------------------------------- declaration parsing | 898 | -------------------------------------------------------------------------------- declaration parsing |
897 | 899 | ||
898 | parseDef :: BodyParser [Stmt] | 900 | parseDef :: BodyParser [PreStmt] |
899 | parseDef = | 901 | parseDef = |
900 | do reserved "data" *> do | 902 | do reserved "data" *> do |
901 | x <- typeNS upperCase | 903 | x <- typeNS upperCase |
@@ -914,7 +916,7 @@ parseDef = | |||
914 | <$> telescope Nothing | 916 | <$> telescope Nothing |
915 | ) | 917 | ) |
916 | (reservedOp "|") | 918 | (reservedOp "|") |
917 | join $ mkData <$> pure x <*> pure ts <*> pure t <*> pure af <*> pure (concatMap (\(vs, t) -> (,) <$> vs <*> pure t) cs) | 919 | mkData x ts t af $ concatMap (\(vs, t) -> (,) <$> vs <*> pure t) cs |
918 | <|> do reserved "class" *> do | 920 | <|> do reserved "class" *> do |
919 | x <- typeNS upperCase | 921 | x <- typeNS upperCase |
920 | (nps, ts) <- telescope (Just SType) | 922 | (nps, ts) <- telescope (Just SType) |
@@ -926,7 +928,7 @@ parseDef = | |||
926 | x <- upperCase | 928 | x <- upperCase |
927 | (nps, args) <- telescopePat | 929 | (nps, args) <- telescopePat |
928 | cs <- expNS $ option [] $ reserved "where" *> identation False (deBruijnify nps <$> funAltDef (Just lhsOperator) varId) | 930 | cs <- expNS $ option [] $ reserved "where" *> identation False (deBruijnify nps <$> funAltDef (Just lhsOperator) varId) |
929 | pure . Instance x ({-todo-}map snd args) (deBruijnify (nps <> [x]) <$> constraints) <$> compileFunAlts' cs | 931 | pure . Instance x ({-todo-}map snd args) (deBruijnify (nps <> [x]) <$> constraints) <$> compileStmt' cs |
930 | <|> do reserved "type" *> do | 932 | <|> do reserved "type" *> do |
931 | typeNS $ do | 933 | typeNS $ do |
932 | reserved "family" *> do | 934 | reserved "family" *> do |
@@ -936,16 +938,16 @@ parseDef = | |||
936 | option {-open type family-}[TypeFamily x $ UncurryS ts t] $ do | 938 | option {-open type family-}[TypeFamily x $ UncurryS ts t] $ do |
937 | cs <- (reserved "where" >>) $ identation True $ funAltDef Nothing $ mfilter (== x) upperCase | 939 | cs <- (reserved "where" >>) $ identation True $ funAltDef Nothing $ mfilter (== x) upperCase |
938 | -- closed type family desugared here | 940 | -- closed type family desugared here |
939 | compileFunAlts (compileGuardTrees id) [TypeAnn x $ UncurryS ts t] cs | 941 | fmap Stmt <$> compileStmt (compileGuardTrees id) [TypeAnn x $ UncurryS ts t] cs |
940 | <|> pure <$ reserved "instance" <*> funAltDef Nothing upperCase | 942 | <|> pure <$ reserved "instance" <*> funAltDef Nothing upperCase |
941 | <|> do x <- upperCase | 943 | <|> do x <- upperCase |
942 | (nps, ts) <- telescope $ Just (Wildcard SType) | 944 | (nps, ts) <- telescope $ Just (Wildcard SType) |
943 | rhs <- deBruijnify nps <$ reservedOp "=" <*> parseTerm PrecLam | 945 | rhs <- deBruijnify nps <$ reservedOp "=" <*> parseTerm PrecLam |
944 | compileFunAlts (compileGuardTrees id) | 946 | fmap Stmt <$> compileStmt (compileGuardTrees id) |
945 | [{-TypeAnn x $ UncurryS ts $ SType-}{-todo-}] | 947 | [{-TypeAnn x $ UncurryS ts $ SType-}{-todo-}] |
946 | [funAlt x (zip ts $ map PVarSimp $ reverse nps) $ noGuards rhs] | 948 | [funAlt' x ts (map PVarSimp $ reverse nps) $ noGuards rhs] |
947 | <|> do try "typed ident" $ (\(vs, t) -> TypeAnn <$> vs <*> pure t) <$> typedIds Nothing | 949 | <|> do try "typed ident" $ (\(vs, t) -> TypeAnn <$> vs <*> pure t) <$> typedIds Nothing |
948 | <|> fmap . flip PrecDef <$> parseFixity <*> commaSep1 rhsOperator | 950 | <|> fmap . (Stmt .) . flip PrecDef <$> parseFixity <*> commaSep1 rhsOperator |
949 | <|> pure <$> funAltDef (Just lhsOperator) varId | 951 | <|> pure <$> funAltDef (Just lhsOperator) varId |
950 | <|> valueDef | 952 | <|> valueDef |
951 | where | 953 | where |
@@ -953,12 +955,12 @@ parseDef = | |||
953 | telescopeDataFields = mkTelescope <$> commaSep ((,) Visible <$> ((,) <$> lowerCase <*> parseType Nothing)) | 955 | telescopeDataFields = mkTelescope <$> commaSep ((,) Visible <$> ((,) <$> lowerCase <*> parseType Nothing)) |
954 | 956 | ||
955 | mkData x ts t af cs | 957 | mkData x ts t af cs |
956 | = (Data x ts t af (second snd <$> cs):) . concat <$> traverse mkProj (nub $ concat [fs | (_, (Just fs, _)) <- cs]) | 958 | = (Stmt (Data x ts t af (second snd <$> cs)):) . concat <$> traverse mkProj (nub $ concat [fs | (_, (Just fs, _)) <- cs]) |
957 | where | 959 | where |
958 | mkProj fn = sequence | 960 | mkProj fn = sequence |
959 | [ do | 961 | [ do |
960 | cn' <- addConsInfo $ pure cn | 962 | cn' <- addConsInfo $ pure cn |
961 | return $ funAlt fn [((Visible, Wildcard SType), PConSimp cn' $ replicate (length fs) $ PVarSimp (mempty, "generated_name1"))] | 963 | return $ funAlt' fn [(Visible, Wildcard SType)] [PConSimp cn' $ replicate (length fs) $ PVarSimp (mempty, "generated_name1")] |
962 | $ noGuards $ sVar "proj" i | 964 | $ noGuards $ sVar "proj" i |
963 | | (cn, (Just fs, _)) <- cs, (i, fn') <- zip [0..] fs, fn' == fn | 965 | | (cn, (Just fs, _)) <- cs, (i, fn') <- zip [0..] fs, fn' == fn |
964 | ] | 966 | ] |
@@ -985,7 +987,7 @@ parseRHS tok = do | |||
985 | 987 | ||
986 | noGuards = In . GTSuccess | 988 | noGuards = In . GTSuccess |
987 | 989 | ||
988 | parseDefs = identation True parseDef >>= compileFunAlts' . concat | 990 | parseDefs = identation True parseDef >>= compileStmt' . concat |
989 | 991 | ||
990 | funAltDef parseOpName parseName = do | 992 | funAltDef parseOpName parseName = do |
991 | (n, (fee, tss)) <- | 993 | (n, (fee, tss)) <- |
@@ -1002,21 +1004,21 @@ funAltDef parseOpName parseName = do | |||
1002 | <|> do try "lhs" $ (,) <$> parseName <*> telescopePat <* lookAhead (reservedOp "=" <|> reservedOp "|") | 1004 | <|> do try "lhs" $ (,) <$> parseName <*> telescopePat <* lookAhead (reservedOp "=" <|> reservedOp "|") |
1003 | funAlt n tss . deBruijnify fee <$> parseRHS "=" | 1005 | funAlt n tss . deBruijnify fee <$> parseRHS "=" |
1004 | 1006 | ||
1005 | valueDef :: BodyParser [Stmt] | 1007 | valueDef :: BodyParser [PreStmt] |
1006 | valueDef = do | 1008 | valueDef = do |
1007 | (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" | 1009 | (dns, p) <- try "pattern" $ longPattern <* reservedOp "=" |
1008 | checkPattern dns | 1010 | checkPattern dns |
1009 | e <- parseTerm PrecLam | 1011 | e <- parseTerm PrecLam |
1010 | desugarValueDef p e | 1012 | desugarValueDef p e |
1011 | |||
1012 | desugarValueDef p e = sequence | ||
1013 | $ pure (funAlt n [] $ noGuards e) | ||
1014 | : [ funAlt x [] . noGuards <$> compileCase (SGlobal n) [(p, noGuards $ SVar x i)] | ||
1015 | | (i, x) <- zip [0..] dns | ||
1016 | ] | ||
1017 | where | 1013 | where |
1018 | dns = reverse $ getPVars p | 1014 | desugarValueDef p e = sequence |
1019 | n = mangleNames dns | 1015 | $ pure (FunAlt n [] $ noGuards e) |
1016 | : [ FunAlt x [] . noGuards <$> compileCase (SGlobal n) [(p, noGuards $ SVar x i)] | ||
1017 | | (i, x) <- zip [0..] dns | ||
1018 | ] | ||
1019 | where | ||
1020 | dns = reverse $ getPVars p | ||
1021 | n = mangleNames dns | ||
1020 | 1022 | ||
1021 | mangleNames xs = (foldMap fst xs, "_" ++ intercalate "_" (map snd xs)) | 1023 | mangleNames xs = (foldMap fst xs, "_" ++ intercalate "_" (map snd xs)) |
1022 | 1024 | ||
@@ -1104,25 +1106,25 @@ desugarMutual xs = xs | |||
1104 | 1106 | ||
1105 | ------------------------------------------------------------------------ | 1107 | ------------------------------------------------------------------------ |
1106 | 1108 | ||
1107 | compileFunAlts' ds = fmap concat . sequence $ map (compileFunAlts (compileGuardTrees SRHS) ds) $ groupBy h ds where | 1109 | compileStmt' ds = fmap concat . sequence $ map (compileStmt (compileGuardTrees SRHS) ds) $ groupBy h ds where |
1108 | h (FunAlt n _ _) (FunAlt m _ _) = m == n | 1110 | h (FunAlt n _ _) (FunAlt m _ _) = m == n |
1109 | h _ _ = False | 1111 | h _ _ = False |
1110 | 1112 | ||
1111 | --compileFunAlts :: forall m . Monad m => Bool -> (SExp -> SExp) -> (SExp -> SExp) -> DesugarInfo -> [Stmt] -> [Stmt] -> m [Stmt] | 1113 | compileStmt :: ([GuardTrees] -> ErrorFinder SExp) -> [PreStmt] -> [PreStmt] -> ErrorFinder [Stmt] |
1112 | compileFunAlts (compilegt :: [GuardTrees] -> ErrorFinder SExp) ds xs = case xs of | 1114 | compileStmt compilegt ds = \case |
1113 | [Instance{}] -> return [] | 1115 | [Instance{}] -> return [] |
1114 | [Class n ps ms] -> do | 1116 | [Class n ps ms] -> do |
1115 | cd <- compileFunAlts' $ | 1117 | cd <- compileStmt' $ |
1116 | [ TypeAnn n $ foldr (SPi Visible) SType ps ] | 1118 | [ TypeAnn n $ foldr (SPi Visible) SType ps ] |
1117 | ++ [ funAlt n (map noTA ps) $ noGuards $ foldr (SAppV2 $ SBuiltin "'T2") (SBuiltin "'Unit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ] | 1119 | ++ [ funAlt n (map noTA ps) $ noGuards $ foldr (SAppV2 $ SBuiltin "'T2") (SBuiltin "'Unit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ] |
1118 | ++ [ funAlt n (replicate (length ps) (noTA $ PVarSimp (debugSI "compileFunAlts1", "generated_name0"))) $ noGuards $ SBuiltin "'Empty" `SAppV` sLit (LString $ "no instance of " ++ snd n ++ " on ???")] | 1120 | ++ [ funAlt n (replicate (length ps) (noTA $ PVarSimp (debugSI "compileFunAlts1", "generated_name0"))) $ noGuards $ SBuiltin "'Empty" `SAppV` sLit (LString $ "no instance of " ++ snd n ++ " on ???")] |
1119 | cds <- sequence | 1121 | cds <- sequence |
1120 | [ compileFunAlts' | 1122 | [ compileStmt' |
1121 | $ TypeAnn m (UncurryS (map ((,) Hidden) ps) $ SPi Hidden (foldl SAppV (SGlobal n) $ downToS "a2" 0 $ length ps) $ up1 t) | 1123 | $ TypeAnn m (UncurryS (map ((,) Hidden) ps) $ SPi Hidden (foldl SAppV (SGlobal n) $ downToS "a2" 0 $ length ps) $ up1 t) |
1122 | : as | 1124 | : as |
1123 | | (m, t) <- ms | 1125 | | (m, t) <- ms |
1124 | -- , let ts = fst $ getParamsS $ up1 t | 1126 | -- , let ts = fst $ getParamsS $ up1 t |
1125 | , let as = [ funAlt m p $ noGuards {- -$ SLam Hidden (Wildcard SType) $ up1 -} $ SLet m' e $ SVar mempty 0 | 1127 | , let as = [ funAlt' m ts p $ noGuards {- -$ SLam Hidden (Wildcard SType) $ up1 -} $ SLet m' e $ SVar mempty 0 |
1126 | | Instance n' i cstrs alts <- ds, n' == n | 1128 | | Instance n' i cstrs alts <- ds, n' == n |
1127 | , Let m' ~Nothing e <- alts, m' == m | 1129 | , Let m' ~Nothing e <- alts, m' == m |
1128 | , let p = zip ((,) Hidden <$> ps) i ++ [((Hidden, Wildcard SType), PVarSimp (mempty, ""))] | 1130 | , let p = zip ((,) Hidden <$> ps) i ++ [((Hidden, Wildcard SType), PVarSimp (mempty, ""))] |
@@ -1133,8 +1135,7 @@ compileFunAlts (compilegt :: [GuardTrees] -> ErrorFinder SExp) ds xs = case xs o | |||
1133 | [TypeAnn n t] -> return [Primitive n t | snd n `notElem` [n' | FunAlt (_, n') _ _ <- ds]] | 1135 | [TypeAnn n t] -> return [Primitive n t | snd n `notElem` [n' | FunAlt (_, n') _ _ <- ds]] |
1134 | tf@[TypeFamily n t] -> case [d | d@(FunAlt n' _ _) <- ds, n' == n] of | 1136 | tf@[TypeFamily n t] -> case [d | d@(FunAlt n' _ _) <- ds, n' == n] of |
1135 | [] -> return [Primitive n t] | 1137 | [] -> return [Primitive n t] |
1136 | alts -> compileFunAlts compileGuardTrees' [TypeAnn n t] alts | 1138 | alts -> compileStmt compileGuardTrees' [TypeAnn n t] alts |
1137 | [p@PrecDef{}] -> return [p] | ||
1138 | fs@(FunAlt n vs _: _) -> case map head $ group [length vs | FunAlt _ vs _ <- fs] of | 1139 | fs@(FunAlt n vs _: _) -> case map head $ group [length vs | FunAlt _ vs _ <- fs] of |
1139 | [num] | 1140 | [num] |
1140 | | num == 0 && length fs > 1 -> fail $ "redefined " ++ snd n ++ " at " ++ ppShow (fst n) | 1141 | | num == 0 && length fs > 1 -> fail $ "redefined " ++ snd n ++ " at " ++ ppShow (fst n) |
@@ -1143,13 +1144,15 @@ compileFunAlts (compilegt :: [GuardTrees] -> ErrorFinder SExp) ds xs = case xs o | |||
1143 | cf <- compilegt [gt | FunAlt _ _ gt <- fs] | 1144 | cf <- compilegt [gt | FunAlt _ _ gt <- fs] |
1144 | return [Let n (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) $ foldr (uncurry SLam) cf vs] | 1145 | return [Let n (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) $ foldr (uncurry SLam) cf vs] |
1145 | _ -> fail $ "different number of arguments of " ++ snd n ++ " at " ++ ppShow (fst n) | 1146 | _ -> fail $ "different number of arguments of " ++ snd n ++ " at " ++ ppShow (fst n) |
1146 | x -> return x | 1147 | [Stmt x] -> return [x] |
1147 | where | 1148 | where |
1148 | noTA x = ((Visible, Wildcard SType), x) | 1149 | noTA x = ((Visible, Wildcard SType), x) |
1149 | 1150 | ||
1150 | funAlt :: SIName -> [((Visibility, SExp), ParPat)] -> GuardTrees -> Stmt | 1151 | funAlt :: SIName -> [((Visibility, SExp), ParPat)] -> GuardTrees -> PreStmt |
1151 | funAlt n pats gt = FunAlt n (fst <$> pats) $ compilePatts (map snd pats) gt | 1152 | funAlt n pats gt = FunAlt n (fst <$> pats) $ compilePatts (map snd pats) gt |
1152 | 1153 | ||
1154 | funAlt' n ts x gt = FunAlt n ts $ compilePatts x gt | ||
1155 | |||
1153 | 1156 | ||
1154 | 1157 | ||
1155 | -------------------------------------------------------------------------------- desugar info | 1158 | -------------------------------------------------------------------------------- desugar info |