summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-01-21 18:09:53 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-01-21 18:14:45 +0100
commit0894e8376035fb8a0c4641124a746fdfe7cd5080 (patch)
tree12bff63edd49f92d6fa58942119cd2748ee96597
parent3474e2e50fed18d937d8df1a1f4f8cf7311f896d (diff)
wip refactoring
-rw-r--r--src/LambdaCube/Compiler/Infer.hs203
-rw-r--r--testdata/language-features/basic-list/list11.out1
2 files changed, 102 insertions, 102 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 25253199..7e6bc883 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -1615,18 +1615,18 @@ parseLC f str = either (throwError . ErrorMsg . show) return . flip runReader (e
1615 1615
1616-------------------------------------------------------------------------------- 1616--------------------------------------------------------------------------------
1617 1617
1618parseType ns mb vs = maybe id option mb $ reservedOp "::" *> parseTTerm ns PrecLam vs 1618parseType ns mb vs = maybe id option mb $ reservedOp "::" *> (dbf' vs <$> parseTTerm ns PrecLam)
1619typedIds ns mb vs = (,) <$> commaSep1 (siName (varId ns <|> patVar ns <|> upperCase ns)) 1619typedIds ns mb vs = (,) <$> commaSep1 (siName (varId ns <|> patVar ns <|> upperCase ns))
1620 <*> localIndentation Gt {-TODO-} (parseType ns mb vs) 1620 <*> localIndentation Gt {-TODO-} (parseType ns mb vs)
1621 1621
1622telescope ns mb vs = (removeSI *** id) <$> telescopeSI ns mb (dbToSINames "tel" vs) 1622telescope ns mb = (removeSI *** id) <$> telescopeSI ns mb []
1623 1623
1624telescopeSI :: Namespace -> Maybe SExp -> [SIName] -> P ([SIName], [(Visibility, SExp)]) -- todo: refactor to [(SIName, (Visibility, SExp))] 1624telescopeSI :: Namespace -> Maybe SExp -> [SIName] -> P ([SIName], [(Visibility, SExp)]) -- todo: refactor to [(SIName, (Visibility, SExp))]
1625telescopeSI ns mb vs = option (vs, []) $ do 1625telescopeSI ns mb vs = option (vs, []) $ do
1626 (si, (x, vt)) <- withSI 1626 (si, (x, vt)) <- withSI
1627 ( reservedOp "@" *> (maybe empty (\x -> flip (,) (Hidden, x) <$> patVar ns) mb <|> parens (typedId Hidden)) 1627 ( reservedOp "@" *> (maybe empty (\x -> flip (,) (Hidden, x) <$> patVar ns) mb <|> parens (typedId Hidden))
1628 <|> try (parens $ typedId Visible) 1628 <|> try (parens $ typedId Visible)
1629 <|> maybe ((,) "" . (,) Visible <$> parseTerm ns PrecAtom (removeSI vs)) 1629 <|> maybe ((,) "" . (,) Visible . dbf' (removeSI vs) <$> parseTerm ns PrecAtom)
1630 (\x -> flip (,) (Visible, x) <$> patVar ns) 1630 (\x -> flip (,) (Visible, x) <$> patVar ns)
1631 mb 1631 mb
1632 ) 1632 )
@@ -1638,13 +1638,10 @@ telescopeSI ns mb vs = option (vs, []) $ do
1638 1638
1639removeSI = DBNamesC . map snd 1639removeSI = DBNamesC . map snd
1640 1640
1641dbToSINames :: String -> DBNames -> [SIName]
1642dbToSINames d (DBNamesC xs) = map ((,) (debugSI d)) xs
1643
1644parseClause ns e = do 1641parseClause ns e = do
1645 (fe, p) <- pattern' ns e 1642 (fe, p) <- pattern' ns e
1646 localIndentation Gt $ do 1643 localIndentation Gt $ do
1647 x <- reservedOp "->" *> parseETerm ns PrecLam fe 1644 x <- reservedOp "->" *> (dbf' fe <$> parseETerm ns PrecLam)
1648 f <- parseWhereBlock ns 1645 f <- parseWhereBlock ns
1649 return (p, dbf' fe $ f x) 1646 return (p, dbf' fe $ f x)
1650 1647
@@ -1697,16 +1694,17 @@ commaSepUnfold1 p vs0 = do
1697commaSepUnfold :: (t -> P (t, a)) -> t -> P (t, [a]) 1694commaSepUnfold :: (t -> P (t, a)) -> t -> P (t, [a])
1698commaSepUnfold p vs = commaSepUnfold1 p vs <|> pure (vs, []) 1695commaSepUnfold p vs = commaSepUnfold1 p vs <|> pure (vs, [])
1699 1696
1700telescope' ns vs = option (vs, []) $ do 1697telescope' ns = go mempty where
1701 (vs', vt) <- 1698 go vs = option (vs, []) $ do
1702 reservedOp "@" *> (f Hidden <$> patternAtom ns vs) 1699 (vs', vt) <-
1703 <|> f Visible <$> patternAtom ns vs 1700 reservedOp "@" *> (f Hidden <$> patternAtom ns vs)
1704 (id *** (vt:)) <$> telescope' ns vs' 1701 <|> f Visible <$> patternAtom ns vs
1705 where 1702 (id *** (vt:)) <$> go vs'
1706 f h (vs, PatType (ParPat [p]) t) = (vs, ((h, t), p)) 1703 where
1707 f h (vs, p) = (vs, ((h, Wildcard SType), p)) 1704 f h (vs, PatType (ParPat [p]) t) = (vs, ((h, t), p))
1705 f h (vs, p) = (vs, ((h, Wildcard SType), p))
1708 1706
1709parseDefs lend ns = (asks $ \ge -> compileFunAlts' lend ge . concat) <*> many (parseDef ns mempty) 1707parseDefs lend ns = (asks $ \ge -> compileFunAlts' lend ge . concat) <*> many (parseDef ns)
1710 1708
1711compileFunAlts' lend ge ds = concatMap (compileFunAlts False lend lend ge ds) $ groupBy h ds where 1709compileFunAlts' lend ge ds = concatMap (compileFunAlts False lend lend ge ds) $ groupBy h ds where
1712 h (FunAlt n _ _ _) (FunAlt m _ _ _) = m == n 1710 h (FunAlt n _ _ _) (FunAlt m _ _ _) = m == n
@@ -1754,12 +1752,12 @@ compileFunAlts par ulend lend ge ds = \case
1754compileGuardTrees False ulend lend ge alts = compileGuardTree ulend lend ge $ Alts alts 1752compileGuardTrees False ulend lend ge alts = compileGuardTree ulend lend ge $ Alts alts
1755compileGuardTrees True ulend lend ge alts = foldr1 SParEval $ compileGuardTree ulend lend ge <$> alts 1753compileGuardTrees True ulend lend ge alts = foldr1 SParEval $ compileGuardTree ulend lend ge <$> alts
1756 1754
1757parseDef :: Namespace -> DBNames -> P [Stmt] 1755parseDef :: Namespace -> P [Stmt]
1758parseDef ns e = 1756parseDef ns =
1759 do reserved "data" 1757 do reserved "data"
1760 localIndentation Gt $ do 1758 localIndentation Gt $ do
1761 (si,x) <- siName $ upperCase (typeNS ns) 1759 (si,x) <- siName $ upperCase (typeNS ns)
1762 (nps, ts) <- telescopeSI (typeNS ns) (Just SType) (dbToSINames "parseDef data" e) 1760 (nps, ts) <- telescopeSI (typeNS ns) (Just SType) []
1763 let npsd@(DBNamesC npsd_) = removeSI nps 1761 let npsd@(DBNamesC npsd_) = removeSI nps
1764 t <- parseType (typeNS ns) (Just SType) npsd 1762 t <- parseType (typeNS ns) (Just SType) npsd
1765 let mkConTy mk (nps', ts') = 1763 let mkConTy mk (nps', ts') =
@@ -1779,7 +1777,7 @@ parseDef ns e =
1779 <|> do reserved "class" 1777 <|> do reserved "class"
1780 localIndentation Gt $ do 1778 localIndentation Gt $ do
1781 x <- siName $ upperCase (typeNS ns) 1779 x <- siName $ upperCase (typeNS ns)
1782 (nps, ts) <- telescope (typeNS ns) (Just SType) e 1780 (nps, ts) <- telescope (typeNS ns) (Just SType)
1783 cs <- 1781 cs <-
1784 do reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ many $ typedIds ns Nothing nps) 1782 do reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ many $ typedIds ns Nothing nps)
1785 <|> pure [] 1783 <|> pure []
@@ -1787,23 +1785,23 @@ parseDef ns e =
1787 <|> do reserved "instance" 1785 <|> do reserved "instance"
1788 let ns' = typeNS ns 1786 let ns' = typeNS ns
1789 localIndentation Gt $ do 1787 localIndentation Gt $ do
1790 constraints <- option [] $ try $ getTTuple' <$> parseTerm ns' PrecEq e <* reservedOp "=>" 1788 constraints <- option [] $ try $ getTTuple' <$> parseTerm ns' PrecEq <* reservedOp "=>"
1791 (si,x) <- siName $ upperCase ns' 1789 (si,x) <- siName $ upperCase ns'
1792 (nps, args) <- telescope' ns' e 1790 (nps, args) <- telescope' ns'
1793 cs <- option [] $ reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ some $ 1791 cs <- option [] $ reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ some $
1794 funAltDef (varId ns) ns nps) 1792 dbFunAlt nps <$> funAltDef (varId ns) ns)
1795 <|> pure [] 1793 <|> pure []
1796 ge <- ask 1794 ge <- ask
1797 return $ pure $ Instance (si,x) ({-todo-}map snd args) (dbf (diffDBNames nps e ++ [x]) <$> constraints) $ compileFunAlts' id{-TODO-} ge cs 1795 return $ pure $ Instance (si,x) ({-todo-}map snd args) (dbf (diffDBNames nps ++ [x]) <$> constraints) $ compileFunAlts' id{-TODO-} ge cs
1798 <|> do try (reserved "type" >> reserved "family") 1796 <|> do try (reserved "type" >> reserved "family")
1799 let ns' = typeNS ns 1797 let ns' = typeNS ns
1800 localIndentation Gt $ do 1798 localIndentation Gt $ do
1801 (si, x) <- siName $ upperCase ns' 1799 (si, x) <- siName $ upperCase ns'
1802 (nps, ts) <- telescope ns' (Just SType) e 1800 (nps, ts) <- telescope ns' (Just SType)
1803 t <- parseType ns' (Just SType) nps 1801 t <- parseType ns' (Just SType) nps
1804 option {-open type family-}[TypeFamily (si, x) ts t] $ do 1802 option {-open type family-}[TypeFamily (si, x) ts t] $ do
1805 cs <- reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ many $ 1803 cs <- reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ many $
1806 funAltDef (upperCase ns' >>= \x' -> guard (x == x') >> return x') ns' e) 1804 funAltDef (upperCase ns' >>= \x' -> guard (x == x') >> return x') ns')
1807 ge <- ask 1805 ge <- ask
1808 -- closed type family desugared here 1806 -- closed type family desugared here
1809 return $ compileFunAlts False id SLabelEnd ge [TypeAnn (si, x) $ addParamsS ts t] cs 1807 return $ compileFunAlts False id SLabelEnd ge [TypeAnn (si, x) $ addParamsS ts t] cs
@@ -1811,32 +1809,32 @@ parseDef ns e =
1811 let ns' = typeNS ns 1809 let ns' = typeNS ns
1812 localIndentation Gt $ do 1810 localIndentation Gt $ do
1813 (si, x) <- siName $ upperCase ns' 1811 (si, x) <- siName $ upperCase ns'
1814 (nps, ts) <- telescopeSI ns' (Just (Wildcard SType)) (dbToSINames "xx" e) 1812 (nps, ts) <- telescopeSI ns' (Just (Wildcard SType)) []
1815 reservedOp "=" 1813 reservedOp "="
1816 rhs <- parseTerm ns' PrecLam $ removeSI nps 1814 rhs <- dbf' (removeSI nps) <$> parseTerm ns' PrecLam
1817 ge <- ask 1815 ge <- ask
1818 return $ compileFunAlts False id SLabelEnd ge [{-TypeAnn (si, x) $ addParamsS ts $ SType-}{-todo-}] [FunAlt (si, x) (reverse $ zip (reverse ts) $ map PVar nps) Nothing rhs] 1816 return $ compileFunAlts False id SLabelEnd ge [{-TypeAnn (si, x) $ addParamsS ts $ SType-}{-todo-}] [FunAlt (si, x) (reverse $ zip (reverse ts) $ map PVar nps) Nothing rhs]
1819 <|> do try (reserved "type" >> reserved "instance") 1817 <|> do try (reserved "type" >> reserved "instance")
1820 let ns' = typeNS ns 1818 let ns' = typeNS ns
1821 pure <$> localIndentation Gt (funAltDef (upperCase ns') ns' e) 1819 pure <$> localIndentation Gt (funAltDef (upperCase ns') ns')
1822 <|> do (vs, t) <- try $ typedIds ns Nothing $ emptyDBNames 1820 <|> do (vs, t) <- try $ typedIds ns Nothing mempty
1823 return $ TypeAnn <$> vs <*> pure t 1821 return $ TypeAnn <$> vs <*> pure t
1824 <|> fixityDef 1822 <|> fixityDef
1825 <|> pure <$> funAltDef (varId ns) ns e 1823 <|> pure <$> funAltDef (varId ns) ns
1826 <|> pure . uncurry ValueDef <$> valueDef ns e 1824 <|> pure . uncurry ValueDef <$> valueDef ns
1827 where 1825 where
1828 telescopeDataFields :: Namespace -> [SIName] -> P ([SIName], [(Visibility, SExp)]) 1826 telescopeDataFields :: Namespace -> [SIName] -> P ([SIName], [(Visibility, SExp)])
1829 telescopeDataFields ns vs = option (vs, []) $ do 1827 telescopeDataFields ns vs = option (vs, []) $ do
1830 (x, vt) <- do name <- siName $ var (expNS ns) 1828 (x, vt) <- do name <- siName $ var (expNS ns)
1831 reservedOp "::" 1829 reservedOp "::"
1832 term <- parseTerm ns PrecLam (removeSI vs) 1830 term <- dbf' (removeSI vs) <$> parseTerm ns PrecLam
1833 return (name, (Visible, term)) 1831 return (name, (Visible, term))
1834 (id *** (vt:)) <$> (comma *> telescopeDataFields ns (x: vs) <|> pure (vs, [])) 1832 (id *** (vt:)) <$> (comma *> telescopeDataFields ns (x: vs) <|> pure (vs, []))
1835 1833
1836funAltDef parseName ns e = do -- todo: use ns to determine parseName 1834funAltDef parseName ns = do -- todo: use ns to determine parseName
1837 (n, (fee, tss)) <- 1835 (n, (fee, tss)) <-
1838 do try' "operator definition" $ do 1836 do try' "operator definition" $ do
1839 (e', a1) <- patternAtom ns e 1837 (e', a1) <- patternAtom ns mempty
1840 localIndentation Gt $ do 1838 localIndentation Gt $ do
1841 n <- siName operatorT 1839 n <- siName operatorT
1842 (e'', a2) <- patternAtom ns e' 1840 (e'', a2) <- patternAtom ns e'
@@ -1844,19 +1842,20 @@ funAltDef parseName ns e = do -- todo: use ns to determine parseName
1844 return (n, (e'', (,) (Visible, Wildcard SType) <$> [a1, a2])) 1842 return (n, (e'', (,) (Visible, Wildcard SType) <$> [a1, a2]))
1845 <|> do try $ do 1843 <|> do try $ do
1846 n <- siName parseName 1844 n <- siName parseName
1847 localIndentation Gt $ (,) n <$> telescope' ns e <* (lookAhead $ reservedOp "=" <|> reservedOp "|") 1845 localIndentation Gt $ (,) n <$> telescope' ns <* (lookAhead $ reservedOp "=" <|> reservedOp "|")
1848 let fe = addDBNames (diffDBNames fee e) $ addDBName (snd n) e 1846 let fe = addDBNames (diffDBNames fee) $ addDBName (snd n) mempty
1849 ts = map (id *** upP 0 1{-todo: replace n with Var 0-}) tss 1847 ts = map (id *** upP 0 1{-todo: replace n with Var 0-}) tss
1850 ni = length $ diffDBNames fee e
1851 localIndentation Gt $ do 1848 localIndentation Gt $ do
1852 gu <- option Nothing $ do 1849 gu <- option Nothing $ do
1853 reservedOp "|" 1850 reservedOp "|"
1854 Just <$> parseTerm ns PrecOp emptyDBNames 1851 Just <$> parseTerm ns PrecOp
1855 reservedOp "=" 1852 reservedOp "="
1856 rhs <- parseTerm ns PrecLam emptyDBNames 1853 rhs <- parseTerm ns PrecLam
1857 f <- parseWhereBlock ns 1854 f <- parseWhereBlock ns
1858 return $ FunAlt n ts (dbf' fe <$> gu) $ dbf' fe $ f rhs 1855 return $ FunAlt n ts (dbf' fe <$> gu) $ dbf' fe $ f rhs
1859 1856
1857dbFunAlt v (FunAlt n ts gu e) = FunAlt n (map (id *** mapP (dbf' v)) ts) (dbf' v <$> gu) $ dbf' v e
1858
1860mkData ge x ts t af cs = [Data x ts t af $ (id *** snd) <$> cs] ++ concatMap mkProj cs 1859mkData ge x ts t af cs = [Data x ts t af $ (id *** snd) <$> cs] ++ concatMap mkProj cs
1861 where 1860 where
1862 mkProj ((si,cn), (Just fs, _)) 1861 mkProj ((si,cn), (Just fs, _))
@@ -1876,87 +1875,83 @@ instance Show DBNames where show (DBNamesC x) = show x
1876instance Monoid DBNames where mempty = DBNamesC [] 1875instance Monoid DBNames where mempty = DBNamesC []
1877 mappend = error "mappend @DBNames" 1876 mappend = error "mappend @DBNames"
1878 1877
1879emptyDBNames = DBNamesC []
1880addDBNames xs (DBNamesC ys) = DBNamesC $ xs ++ ys 1878addDBNames xs (DBNamesC ys) = DBNamesC $ xs ++ ys
1881addDBName n (DBNamesC ns) = DBNamesC $ n:ns 1879addDBName n (DBNamesC ns) = DBNamesC $ n:ns
1882sVar = withRange $ \si x -> SGlobal (si, x) 1880sVar = withRange $ \si x -> SGlobal (si, x)
1883diffDBNames (DBNamesC xs) (DBNamesC ys) = take (length xs - length ys) xs 1881diffDBNames (DBNamesC xs) = xs
1884 1882
1885valueDef :: Namespace -> DBNames -> P (([SName], Pat), SExp) 1883valueDef :: Namespace -> P (([SName], Pat), SExp)
1886valueDef ns e = do 1884valueDef ns = do
1887 (e', p) <- try $ pattern' ns e <* reservedOp "=" 1885 (DBNamesC e', p) <- try $ pattern' ns mempty <* reservedOp "="
1888 localIndentation Gt $ do 1886 localIndentation Gt $ do
1889 ex <- parseETerm ns PrecLam e 1887 ex <- parseETerm ns PrecLam
1890 return ((diffDBNames e' e, p), ex) 1888 return ((e', p), ex)
1891 1889
1892sapp a (v, b) = SApp (debugSI "sapp") v a b 1890sapp a (v, b) = SApp (debugSI "sapp") v a b
1893 1891
1894parseTTerm ns = parseTerm $ typeNS ns 1892parseTTerm ns = parseTerm $ typeNS ns
1895parseETerm ns = parseTerm $ expNS ns 1893parseETerm ns = parseTerm $ expNS ns
1896 1894
1897parseTerm ns p e = dbf' e <$> parseTerm' ns p 1895parseTerm :: Namespace -> Prec -> P SExp
1898parseTerm' ns p = parseTerm_ ns p mempty 1896parseTerm ns PrecLam =
1899 1897 mkIf `withRange` ((,,) <$ reserved "if" <*> parseTerm ns PrecLam <* reserved "then" <*> parseTerm ns PrecLam <* reserved "else" <*> parseTerm ns PrecLam)
1900parseTerm_ :: Namespace -> Prec -> DBNames -> P SExp
1901parseTerm_ ns PrecLam e =
1902 mkIf `withRange` ((,,) <$ reserved "if" <*> parseTerm' ns PrecLam <* reserved "then" <*> parseTerm' ns PrecLam <* reserved "else" <*> parseTerm_ ns PrecLam e)
1903 <|> do (tok, ns) <- (SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->", typeNS ns) <$ reserved "forall" 1898 <|> do (tok, ns) <- (SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->", typeNS ns) <$ reserved "forall"
1904 (fe, ts) <- telescope ns (Just $ Wildcard SType) e 1899 (fe, ts) <- telescope ns (Just $ Wildcard SType)
1905 f <- tok 1900 f <- tok
1906 t' <- parseTerm ns PrecLam fe 1901 t' <- dbf' fe <$> parseTerm ns PrecLam
1907 return $ foldr (uncurry f) t' ts 1902 return $ foldr (uncurry f) t' ts
1908 <|> do appRange $ do 1903 <|> do appRange $ do
1909 (tok, ns) <- (asks (\a r -> patLam_ r id a) <* reservedOp "->", expNS ns) <$ reservedOp "\\" 1904 (tok, ns) <- (asks (\a r -> patLam_ r id a) <* reservedOp "->", expNS ns) <$ reservedOp "\\"
1910 (fe, ts) <- telescope' ns e 1905 (fe, ts) <- telescope' ns
1911 f <- tok 1906 f <- tok
1912 t' <- parseTerm ns PrecLam fe 1907 t' <- dbf' fe <$> parseTerm ns PrecLam
1913 return $ \r -> foldr (uncurry (f r)) t' ts 1908 return $ \r -> foldr (uncurry (f r)) t' ts
1914 <|> do (asks compileCase) <* reserved "case" <*> parseETerm ns PrecLam e 1909 <|> do (asks compileCase) <* reserved "case" <*> parseETerm ns PrecLam
1915 <* reserved "of" <*> localIndentation Ge (localAbsoluteIndentation $ some $ parseClause ns e) 1910 <* reserved "of" <*> localIndentation Ge (localAbsoluteIndentation $ some $ parseClause ns mempty)
1916 <|> do (asks $ \ge -> compileGuardTree id id ge . Alts) <*> parseSomeGuards ns (const True) e 1911 <|> do (asks $ \ge -> compileGuardTree id id ge . Alts) <*> parseSomeGuards ns (const True)
1917 <|> do t <- parseTerm ns PrecEq e 1912 <|> do t <- parseTerm ns PrecEq
1918 option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm ns PrecLam e 1913 option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm ns PrecLam
1919parseTerm_ ns PrecEq e = parseTerm' ns PrecAnn >>= \t -> option t $ SCstr t <$ reservedOp "~" <*> parseTTerm ns PrecAnn e 1914parseTerm ns PrecEq = parseTerm ns PrecAnn >>= \t -> option t $ SCstr t <$ reservedOp "~" <*> parseTTerm ns PrecAnn
1920parseTerm_ ns PrecAnn e = parseTerm' ns PrecOp >>= \t -> option t $ SAnn t <$> parseType ns Nothing e 1915parseTerm ns PrecAnn = parseTerm ns PrecOp >>= \t -> option t $ SAnn t <$> parseType ns Nothing mempty
1921parseTerm_ ns PrecOp e = asks calculatePrecs <*> p' where 1916parseTerm ns PrecOp = asks calculatePrecs <*> p' where
1922 p' = ((\si (t, xs) -> (mkNat ns si 0, (SGlobal (debugSI "12", "-"), t): xs)) `withRange` (reservedOp "-" *> p_)) 1917 p' = ((\si (t, xs) -> (mkNat ns si 0, (SGlobal (debugSI "12", "-"), t): xs)) `withRange` (reservedOp "-" *> p_))
1923 <|> p_ 1918 <|> p_
1924 p_ = (,) <$> parseTerm' ns PrecApp <*> (option [] $ sVar operatorT >>= p) 1919 p_ = (,) <$> parseTerm ns PrecApp <*> (option [] $ sVar operatorT >>= p)
1925 p op = do (exp, op') <- try ((,) <$> parseTerm' ns PrecApp <*> sVar operatorT) 1920 p op = do (exp, op') <- try ((,) <$> parseTerm ns PrecApp <*> sVar operatorT)
1926 ((op, exp):) <$> p op' 1921 ((op, exp):) <$> p op'
1927 <|> pure . (,) op <$> parseTerm' ns PrecLam 1922 <|> pure . (,) op <$> parseTerm ns PrecLam
1928parseTerm_ ns PrecApp e = 1923parseTerm ns PrecApp =
1929 try {- TODO: adjust try for better error messages e.g. don't use braces -} 1924 try {- TODO: adjust try for better error messages e.g. don't use braces -}
1930 (foldl sapp <$> sVar (upperCase ns) <*> braces (commaSep $ lowerCase ns *> reservedOp "=" *> ((,) Visible <$> parseTerm' ns PrecLam))) <|> 1925 (foldl sapp <$> sVar (upperCase ns) <*> braces (commaSep $ lowerCase ns *> reservedOp "=" *> ((,) Visible <$> parseTerm ns PrecLam))) <|>
1931 (foldl sapp <$> parseTerm ns PrecSwiz e <*> many 1926 (foldl sapp <$> parseTerm ns PrecSwiz <*> many
1932 ( (,) Visible <$> parseTerm ns PrecSwiz e 1927 ( (,) Visible <$> parseTerm ns PrecSwiz
1933 <|> (,) Hidden <$ reservedOp "@" <*> parseTTerm ns PrecSwiz e)) 1928 <|> (,) Hidden <$ reservedOp "@" <*> parseTTerm ns PrecSwiz))
1934parseTerm_ ns PrecSwiz e = do 1929parseTerm ns PrecSwiz = do
1935 t <- parseTerm' ns PrecProj 1930 t <- parseTerm ns PrecProj
1936 try (mkSwizzling t `withRange` (char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: [Char]))) <* whiteSpace)) <|> pure t 1931 try (mkSwizzling t `withRange` (char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: [Char]))) <* whiteSpace)) <|> pure t
1937parseTerm_ ns PrecProj e = do 1932parseTerm ns PrecProj = do
1938 t <- parseTerm' ns PrecAtom 1933 t <- parseTerm ns PrecAtom
1939 try (mkProjection t <$ char '.' <*> (sepBy1 (sLit `withRange` (LString <$> lowerCase ns)) (char '.'))) <|> pure t 1934 try (mkProjection t <$ char '.' <*> (sepBy1 (sLit `withRange` (LString <$> lowerCase ns)) (char '.'))) <|> pure t
1940parseTerm_ ns PrecAtom e = 1935parseTerm ns PrecAtom =
1941 sLit `withRange` (LChar <$> try charLiteral) 1936 sLit `withRange` (LChar <$> try charLiteral)
1942 <|> sLit `withRange` (LString <$> stringLiteral) 1937 <|> sLit `withRange` (LString <$> stringLiteral)
1943 <|> sLit `withRange` (LFloat <$> try float) 1938 <|> sLit `withRange` (LFloat <$> try float)
1944 <|> sLit `withRange` (LInt . fromIntegral <$ char '#' <*> natural) 1939 <|> sLit `withRange` (LInt . fromIntegral <$ char '#' <*> natural)
1945 <|> mkNat ns `withRange` natural 1940 <|> mkNat ns `withRange` natural
1946 <|> Wildcard (Wildcard SType) <$ reserved "_" 1941 <|> Wildcard (Wildcard SType) <$ reserved "_"
1947 <|> char '\'' *> parseTerm' (switchNS ns) PrecAtom 1942 <|> char '\'' *> parseTerm (switchNS ns) PrecAtom
1948 <|> sVar (try (varId ns) <|> upperCase ns) 1943 <|> sVar (try (varId ns) <|> upperCase ns)
1949 <|> mkDotDot <$> try (reservedOp "[" *> parseTerm' ns PrecLam <* reservedOp ".." ) <*> parseTerm' ns PrecLam <* reservedOp "]" 1944 <|> mkDotDot <$> try (reservedOp "[" *> parseTerm ns PrecLam <* reservedOp ".." ) <*> parseTerm ns PrecLam <* reservedOp "]"
1950 <|> listCompr ns e 1945 <|> listCompr ns
1951 <|> mkList ns `withRange` brackets (commaSep $ parseTerm' ns PrecLam) 1946 <|> mkList ns `withRange` brackets (commaSep $ parseTerm ns PrecLam)
1952 <|> mkLeftSection `withRange` try{-todo: better try-} (parens $ (,) <$> siName (guardM (/= "-") operatorT) <*> parseTerm' ns PrecLam) 1947 <|> mkLeftSection `withRange` try{-todo: better try-} (parens $ (,) <$> siName (guardM (/= "-") operatorT) <*> parseTerm ns PrecLam)
1953 <|> mkRightSection `withRange` try{-todo: better try!-} (parens $ (,) <$> parseTerm' ns PrecApp <*> siName operatorT) 1948 <|> mkRightSection `withRange` try{-todo: better try!-} (parens $ (,) <$> parseTerm ns PrecApp <*> siName operatorT)
1954 <|> mkTuple ns `withRange` parens (commaSep $ parseTerm' ns PrecLam) 1949 <|> mkTuple ns `withRange` parens (commaSep $ parseTerm ns PrecLam)
1955 <|> mkRecord `withRange` braces (commaSep $ ((,) <$> lowerCase ns <* colon <*> parseTerm' ns PrecLam)) 1950 <|> mkRecord `withRange` braces (commaSep $ ((,) <$> lowerCase ns <* colon <*> parseTerm ns PrecLam))
1956 <|> do reserved "let" 1951 <|> do reserved "let"
1957 dcls <- localIndentation Ge (localAbsoluteIndentation $ parseDefs id ns) 1952 dcls <- localIndentation Ge (localAbsoluteIndentation $ parseDefs id ns)
1958 ge <- ask 1953 ge <- ask
1959 mkLets ge dcls <$ reserved "in" <*> parseTerm ns PrecLam mempty 1954 mkLets ge dcls <$ reserved "in" <*> parseTerm ns PrecLam
1960 1955
1961guardM p m = m >>= \x -> if p x then return x else fail "guardM" 1956guardM p m = m >>= \x -> if p x then return x else fail "guardM"
1962 1957
@@ -2026,7 +2021,7 @@ manyNM n m p = do
2026generator, letdecl, boolExpression :: Namespace -> DBNames -> P (DBNames, SExp -> SExp) 2021generator, letdecl, boolExpression :: Namespace -> DBNames -> P (DBNames, SExp -> SExp)
2027generator ns dbs = do 2022generator ns dbs = do
2028 (dbs', pat) <- try $ pattern' ns dbs <* reservedOp "<-" 2023 (dbs', pat) <- try $ pattern' ns dbs <* reservedOp "<-"
2029 exp <- parseTerm ns PrecLam dbs 2024 exp <- dbf' dbs <$> parseTerm ns PrecLam
2030 ge <- ask 2025 ge <- ask
2031 return $ (,) dbs' $ \e -> application 2026 return $ (,) dbs' $ \e -> application
2032 [ SGlobal (noSI, "concatMap") 2027 [ SGlobal (noSI, "concatMap")
@@ -2037,23 +2032,24 @@ generator ns dbs = do
2037 , exp 2032 , exp
2038 ] 2033 ]
2039 2034
2040letdecl ns dbs = ask >>= \ge -> reserved "let" *> ((\((dbs', p), e) -> (dbs, \exp -> mkLets ge [ValueDef (dbs', p) e] exp)) <$> valueDef ns dbs) 2035letdecl ns dbs = ask >>= \ge -> reserved "let" *> ((\((dbs', p), e) -> (dbs, \exp -> mkLets ge [ValueDef (dbs', mapP (dbf' dbs) p) $ dbf' dbs e] exp)) <$> valueDef ns)
2041 2036
2042boolExpression ns dbs = do 2037boolExpression ns dbs = do
2043 pred <- parseTerm ns PrecLam dbs 2038 pred <- dbf' dbs <$> parseTerm ns PrecLam
2044 return (dbs, \e -> application [SGlobal (noSI,"primIfThenElse"), pred, e, SGlobal (noSI,"Nil")]) 2039 return (dbs, \e -> application [SGlobal (noSI,"primIfThenElse"), pred, e, SGlobal (noSI,"Nil")])
2045 2040
2046application = foldl1 SAppV 2041application = foldl1 SAppV
2047 2042
2048listCompr :: Namespace -> DBNames -> P SExp 2043listCompr :: Namespace -> P SExp
2049listCompr ns dbs = (\e (dbs', fs) -> foldr ($) (dbf (diffDBNames dbs' dbs) e) fs) 2044listCompr ns = (\e (dbs', fs) -> foldr ($) (dbf (diffDBNames dbs') e) fs)
2050 <$> try' "List comprehension" ((SGlobal (noSI, "singleton") `SAppV`) <$ reservedOp "[" <*> parseTerm ns PrecLam dbs <* reservedOp "|") 2045 <$> try' "List comprehension" ((SGlobal (noSI, "singleton") `SAppV`) <$ reservedOp "[" <*> parseTerm ns PrecLam <* reservedOp "|")
2051 <*> commaSepUnfold (liftA2 (<|>) (generator ns) $ liftA2 (<|>) (letdecl ns) (boolExpression ns)) dbs <* reservedOp "]" 2046 <*> commaSepUnfold (liftA2 (<|>) (generator ns) $ liftA2 (<|>) (letdecl ns) (boolExpression ns)) mempty <* reservedOp "]"
2052 2047
2053-- todo: make it more efficient 2048-- todo: make it more efficient
2054diffDBNames' xs ys = take (length xs - length ys) xs 2049diffDBNames' xs ys = take (length xs - length ys) xs
2055 2050
2056dbf' (DBNamesC xs) e = foldl (\e (i, n) -> substSG n (\si -> SVar (si, n) i) e) e $ zip [0..] xs 2051dbf' = dbf_ 0
2052dbf_ j (DBNamesC xs) e = foldl (\e (i, n) -> substSG n (\si -> SVar (si, n) i) e) e $ zip [j..] xs
2057 2053
2058dbf :: [String] -> SExp -> SExp 2054dbf :: [String] -> SExp -> SExp
2059dbf [] e = e 2055dbf [] e = e
@@ -2157,17 +2153,17 @@ patLam si f ge = patLam_ si f ge (Visible, Wildcard SType)
2157patLam_ :: SI -> (SExp -> SExp) -> GlobalEnv' -> (Visibility, SExp) -> Pat -> SExp -> SExp 2153patLam_ :: SI -> (SExp -> SExp) -> GlobalEnv' -> (Visibility, SExp) -> Pat -> SExp -> SExp
2158patLam_ si f ge (v, t) p e = SLam_ si v t $ compileGuardTree f f ge $ compilePatts [(p, 0)] Nothing e 2154patLam_ si f ge (v, t) p e = SLam_ si v t $ compileGuardTree f f ge $ compilePatts [(p, 0)] Nothing e
2159 2155
2160parseSomeGuards ns f e = do 2156parseSomeGuards ns f = do
2161 pos <- sourceColumn <$> getPosition <* reservedOp "|" 2157 pos <- sourceColumn <$> getPosition <* reservedOp "|"
2162 guard $ f pos 2158 guard $ f pos
2163 (e', f) <- 2159 (e', f) <-
2164 do (e', PCon (_, p) vs) <- try $ pattern' ns e <* reservedOp "<-" 2160 do (e', PCon (_, p) vs) <- try $ pattern' ns mempty <* reservedOp "<-"
2165 x <- parseETerm ns PrecEq e 2161 x <- parseETerm ns PrecEq
2166 return (e', \gs' gs -> GuardNode x p vs (Alts gs'): gs) 2162 return (e', \gs' gs -> GuardNode x p vs (Alts gs'): gs)
2167 <|> do x <- parseETerm ns PrecEq e 2163 <|> do x <- parseETerm ns PrecEq
2168 return (e, \gs' gs -> [GuardNode x "True" [] $ Alts gs', GuardNode x "False" [] $ Alts gs]) 2164 return (mempty, \gs' gs -> [GuardNode x "True" [] $ Alts gs', GuardNode x "False" [] $ Alts gs])
2169 f <$> (parseSomeGuards ns (> pos) e' <|> (:[]) . GuardLeaf <$ reservedOp "->" <*> parseETerm ns PrecLam e') 2165 f <$> ((map (dbfGT e') <$> parseSomeGuards ns (> pos)) <|> (:[]) . GuardLeaf <$ reservedOp "->" <*> (dbf' e' <$> parseETerm ns PrecLam))
2170 <*> (parseSomeGuards ns (== pos) e <|> pure []) 2166 <*> (parseSomeGuards ns (== pos) <|> pure [])
2171 2167
2172toNat 0 = SGlobal (debugSI "toNat","Zero") 2168toNat 0 = SGlobal (debugSI "toNat","Zero")
2173toNat n | n > 0 = SAppV (SGlobal (debugSI "toNat","Succ")) $ toNat (n-1) 2169toNat n | n > 0 = SAppV (SGlobal (debugSI "toNat","Succ")) $ toNat (n-1)
@@ -2243,6 +2239,9 @@ upGT k i = mapGT k $ \k -> upS__ k i
2243 2239
2244substGT i j = mapGT 0 $ \k -> rearrangeS $ \r -> if r == k + i then k + j else if r > k + i then r - 1 else r 2240substGT i j = mapGT 0 $ \k -> rearrangeS $ \r -> if r == k + i then k + j else if r > k + i then r - 1 else r
2245 2241
2242dbfGT :: DBNames -> GuardTree -> GuardTree
2243dbfGT v = mapGT 0 $ \k -> dbf_ k v
2244
2246mapPP f = \case 2245mapPP f = \case
2247 ParPat ps -> ParPat (mapP f <$> ps) 2246 ParPat ps -> ParPat (mapP f <$> ps)
2248 2247
diff --git a/testdata/language-features/basic-list/list11.out b/testdata/language-features/basic-list/list11.out
index 31fbcae7..7736ade0 100644
--- a/testdata/language-features/basic-list/list11.out
+++ b/testdata/language-features/basic-list/list11.out
@@ -1,4 +1,5 @@
1main is not found 1main is not found
2tooltips: 2tooltips:
3testdata/language-features/basic-list/list11.lc 1:10-1:11 {a} -> a -> 'List a -> 'List a
3testdata/language-features/basic-list/list11.lc 1:9-1:10 V0 4testdata/language-features/basic-list/list11.lc 1:9-1:10 V0
4testdata/language-features/basic-list/list11.lc 1:9-1:10 'Int 5testdata/language-features/basic-list/list11.lc 1:9-1:10 'Int