summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-01-22 21:38:39 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-01-22 21:42:00 +0100
commitd2afbccb36a784174b6f0b3aca0510a8c052a74a (patch)
tree65db71048b14b991f4365ccb6d11639b062db878 /src
parentaab775ccb7f7498a0128bf9839dd7ccde4926afb (diff)
reject function alternatives with different arity
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Infer.hs29
1 files changed, 15 insertions, 14 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 8ef9704e..8d3ee968 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -1833,15 +1833,13 @@ telescopePat ns = go mempty where
1833 f h (PatType (ParPat [p]) t) = ((h, t), p) 1833 f h (PatType (ParPat [p]) t) = ((h, t), p)
1834 f h p = ((h, Wildcard SType), p) 1834 f h p = ((h, Wildcard SType), p)
1835 1835
1836parseDefs lend ns = (asks $ \ge -> compileFunAlts' lend ge . concat) <*> many (parseDef ns) 1836compileFunAlts' lend ge ds = fmap concat . sequence $ map (compileFunAlts False lend lend ge ds) $ groupBy h ds where
1837
1838compileFunAlts' lend ge ds = concatMap (compileFunAlts False lend lend ge ds) $ groupBy h ds where
1839 h (FunAlt n _ _) (FunAlt m _ _) = m == n 1837 h (FunAlt n _ _) (FunAlt m _ _) = m == n
1840 h _ _ = False 1838 h _ _ = False
1841 1839
1842compileFunAlts :: Bool -> (SExp -> SExp) -> (SExp -> SExp) -> GlobalEnv' -> [Stmt] -> [Stmt] -> [Stmt] 1840compileFunAlts :: forall m . Monad m => Bool -> (SExp -> SExp) -> (SExp -> SExp) -> GlobalEnv' -> [Stmt] -> [Stmt] -> m [Stmt]
1843compileFunAlts par ulend lend ge ds = \case 1841compileFunAlts par ulend lend ge ds = \case
1844 [Instance{}] -> [] 1842 [Instance{}] -> return []
1845 [Class n ps ms] -> compileFunAlts' SLabelEnd ge $ 1843 [Class n ps ms] -> compileFunAlts' SLabelEnd ge $
1846 [ TypeAnn n $ foldr (SPi Visible) SType ps ] 1844 [ TypeAnn n $ foldr (SPi Visible) SType ps ]
1847 ++ [ FunAlt n (map noTA ps) $ Right $ foldr ST2 (SBuiltin "'Unit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ] 1845 ++ [ FunAlt n (map noTA ps) $ Right $ foldr ST2 (SBuiltin "'Unit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ]
@@ -1857,14 +1855,15 @@ compileFunAlts par ulend lend ge ds = \case
1857 | (m, t) <- ms 1855 | (m, t) <- ms
1858 , let ts = fst $ getParamsS $ upS t 1856 , let ts = fst $ getParamsS $ upS t
1859 ] 1857 ]
1860 [TypeAnn n t] -> [Primitive n Nothing t | all (/= snd n) [n' | FunAlt (_, n') _ _ <- ds]] 1858 [TypeAnn n t] -> return [Primitive n Nothing t | all (/= snd n) [n' | FunAlt (_, n') _ _ <- ds]]
1861 tf@[TypeFamily n ps t] -> case [d | d@(FunAlt n' _ _) <- ds, n' == n] of 1859 tf@[TypeFamily n ps t] -> case [d | d@(FunAlt n' _ _) <- ds, n' == n] of
1862 [] -> tf -- builtin type family 1860 [] -> return tf -- builtin type family
1863 alts -> compileFunAlts True id SLabelEnd ge [TypeAnn n $ addParamsS ps t] alts 1861 alts -> compileFunAlts True id SLabelEnd ge [TypeAnn n $ addParamsS ps t] alts
1864 [p@PrecDef{}] -> [p] 1862 [p@PrecDef{}] -> return [p]
1865 fs@((FunAlt n vs _): _) 1863 fs@((FunAlt n vs _): _)
1866 | any (== n) [n' | TypeFamily n' _ _ <- ds] -> [] 1864 | ls@(_:_:_) <- map head $ group [length vs | FunAlt _ vs _ <- fs] -> fail $ "different number of arguments of " ++ snd n ++ " at " ++ showSIRange (fst n)
1867 | otherwise -> 1865 | any (== n) [n' | TypeFamily n' _ _ <- ds] -> return []
1866 | otherwise -> return
1868 [ Let n 1867 [ Let n
1869 (listToMaybe [t | PrecDef n' t <- ds, n' == n]) 1868 (listToMaybe [t | PrecDef n' t <- ds, n' == n])
1870 (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) 1869 (listToMaybe [t | TypeAnn n' t <- ds, n' == n])
@@ -1874,13 +1873,15 @@ compileFunAlts par ulend lend ge ds = \case
1874 | FunAlt _ vs gsx <- fs 1873 | FunAlt _ vs gsx <- fs
1875 ]) (map fst vs)) 1874 ]) (map fst vs))
1876 ] 1875 ]
1877 x -> x 1876 x -> return x
1878 where 1877 where
1879 noTA x = ((Visible, Wildcard SType), x) 1878 noTA x = ((Visible, Wildcard SType), x)
1880 1879
1881compileGuardTrees False ulend lend ge alts = compileGuardTree ulend lend ge $ Alts alts 1880compileGuardTrees False ulend lend ge alts = compileGuardTree ulend lend ge $ Alts alts
1882compileGuardTrees True ulend lend ge alts = foldr1 SParEval $ compileGuardTree ulend lend ge <$> alts 1881compileGuardTrees True ulend lend ge alts = foldr1 SParEval $ compileGuardTree ulend lend ge <$> alts
1883 1882
1883parseDefs lend ns = join $ (asks $ \ge -> compileFunAlts' lend ge . concat) <*> many (parseDef ns)
1884
1884parseDef :: Namespace -> P [Stmt] 1885parseDef :: Namespace -> P [Stmt]
1885parseDef ns = 1886parseDef ns =
1886 do reserved "data" 1887 do reserved "data"
@@ -1921,7 +1922,7 @@ parseDef ns =
1921 dbFunAlt nps <$> funAltDef (varId ns) ns) 1922 dbFunAlt nps <$> funAltDef (varId ns) ns)
1922 <|> pure [] 1923 <|> pure []
1923 ge <- ask 1924 ge <- ask
1924 return $ pure $ Instance x ({-todo-}map snd args) (dbff (diffDBNames nps ++ [snd x]) <$> constraints) $ compileFunAlts' id{-TODO-} ge cs 1925 pure . Instance x ({-todo-}map snd args) (dbff (diffDBNames nps ++ [snd x]) <$> constraints) <$> compileFunAlts' id{-TODO-} ge cs
1925 <|> do try (reserved "type" >> reserved "family") 1926 <|> do try (reserved "type" >> reserved "family")
1926 let ns' = typeNS ns 1927 let ns' = typeNS ns
1927 localIndentation Gt $ do 1928 localIndentation Gt $ do
@@ -1933,7 +1934,7 @@ parseDef ns =
1933 funAltDef (upperCase ns' >>= \x' -> guard (snd x == x') >> return x') ns') 1934 funAltDef (upperCase ns' >>= \x' -> guard (snd x == x') >> return x') ns')
1934 ge <- ask 1935 ge <- ask
1935 -- closed type family desugared here 1936 -- closed type family desugared here
1936 return $ compileFunAlts False id SLabelEnd ge [TypeAnn x $ addParamsS ts t] cs 1937 compileFunAlts False id SLabelEnd ge [TypeAnn x $ addParamsS ts t] cs
1937 <|> do reserved "type" 1938 <|> do reserved "type"
1938 let ns' = typeNS ns 1939 let ns' = typeNS ns
1939 localIndentation Gt $ do 1940 localIndentation Gt $ do
@@ -1942,7 +1943,7 @@ parseDef ns =
1942 reservedOp "=" 1943 reservedOp "="
1943 rhs <- dbf' (DBNamesC nps) <$> parseTerm ns' PrecLam 1944 rhs <- dbf' (DBNamesC nps) <$> parseTerm ns' PrecLam
1944 ge <- ask 1945 ge <- ask
1945 return $ compileFunAlts False id SLabelEnd ge 1946 compileFunAlts False id SLabelEnd ge
1946 [{-TypeAnn x $ addParamsS ts $ SType-}{-todo-}] 1947 [{-TypeAnn x $ addParamsS ts $ SType-}{-todo-}]
1947 [FunAlt x (reverse $ zip (reverse ts) $ map PVar nps) $ Right rhs] 1948 [FunAlt x (reverse $ zip (reverse ts) $ map PVar nps) $ Right rhs]
1948 <|> do try (reserved "type" >> reserved "instance") 1949 <|> do try (reserved "type" >> reserved "instance")