diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Infer.hs | 140 |
1 files changed, 73 insertions, 67 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs index f0e2e9de..83111855 100644 --- a/src/LambdaCube/Compiler/Infer.hs +++ b/src/LambdaCube/Compiler/Infer.hs | |||
@@ -99,6 +99,10 @@ noSI = NoSI (Set.empty) | |||
99 | instance Show SI where show _ = "SI" | 99 | instance Show SI where show _ = "SI" |
100 | instance Eq SI where _ == _ = True | 100 | instance Eq SI where _ == _ = True |
101 | 101 | ||
102 | newtype SData a = SData a | ||
103 | instance Show (SData a) where show _ = "SData" | ||
104 | instance Eq (SData a) where _ == _ = True | ||
105 | |||
102 | siElim | 106 | siElim |
103 | noSI | 107 | noSI |
104 | range | 108 | range |
@@ -133,17 +137,19 @@ data SExp | |||
133 | | SBind SI Binder SIName{-parameter's name-} SExp SExp | 137 | | SBind SI Binder SIName{-parameter's name-} SExp SExp |
134 | | SApp SI Visibility SExp SExp | 138 | | SApp SI Visibility SExp SExp |
135 | | SLet SExp SExp -- let x = e in f --> SLet e f{-x is Var 0-} | 139 | | SLet SExp SExp -- let x = e in f --> SLet e f{-x is Var 0-} |
136 | | SVar SI !Int | 140 | | SVar_ (SData SIName) !Int |
137 | | STyped SI ExpType | 141 | | STyped SI ExpType |
138 | deriving (Eq, Show) | 142 | deriving (Eq, Show) |
139 | 143 | ||
144 | pattern SVar a b = SVar_ (SData a) b | ||
145 | |||
140 | sexpSI :: SExp -> SI | 146 | sexpSI :: SExp -> SI |
141 | sexpSI = \case | 147 | sexpSI = \case |
142 | SGlobal (si, _) -> si | 148 | SGlobal (si, _) -> si |
143 | SBind si _ (si', _) e1 e2 -> si <> si' <> sexpSI e1 <> sexpSI e2 -- TODO: just si | 149 | SBind si _ (si', _) e1 e2 -> si <> si' <> sexpSI e1 <> sexpSI e2 -- TODO: just si |
144 | SApp si _ e1 e2 -> si <> sexpSI e1 <> sexpSI e2 -- TODO: just si | 150 | SApp si _ e1 e2 -> si <> sexpSI e1 <> sexpSI e2 -- TODO: just si |
145 | SLet e1 e2 -> sexpSI e1 <> sexpSI e2 | 151 | SLet e1 e2 -> sexpSI e1 <> sexpSI e2 |
146 | SVar si _ -> si | 152 | SVar (si, _) _ -> si |
147 | STyped si _ -> si | 153 | STyped si _ -> si |
148 | 154 | ||
149 | type FixityMap = Map.Map SName Fixity | 155 | type FixityMap = Map.Map SName Fixity |
@@ -170,10 +176,10 @@ pattern Primitive n mf t <- Let n mf (Just t) _ (SGlobal (si, "undefined")) wher | |||
170 | pattern SType <- STyped _ (TType, TType) where SType = STyped (debugSI "pattern SType") (TType, TType) | 176 | pattern SType <- STyped _ (TType, TType) where SType = STyped (debugSI "pattern SType") (TType, TType) |
171 | pattern SPi h a b <- SBind _ (BPi h) _ a b where SPi h a b = SBind (debugSI "pattern SPi1") (BPi h) (debugSI "patternSPi2", "pattern_spi_name") a b | 177 | pattern SPi h a b <- SBind _ (BPi h) _ a b where SPi h a b = SBind (debugSI "pattern SPi1") (BPi h) (debugSI "patternSPi2", "pattern_spi_name") a b |
172 | pattern SLam h a b <- SBind _ (BLam h) _ a b where SLam h a b = SBind (debugSI "pattern SLam1") (BLam h) (debugSI "patternSLam2", "pattern_slam_name") a b | 178 | pattern SLam h a b <- SBind _ (BLam h) _ a b where SLam h a b = SBind (debugSI "pattern SLam1") (BLam h) (debugSI "patternSLam2", "pattern_slam_name") a b |
173 | pattern Wildcard t <- SBind _ BMeta _ t (SVar _ 0) where Wildcard t = SBind (debugSI "pattern Wildcard1") BMeta (debugSI "pattern Wildcard2", "pattern_wildcard_name") t (SVar (debugSI "pattern Wildcard2") 0) | 179 | pattern Wildcard t <- SBind _ BMeta _ t (SVar _ 0) where Wildcard t = SBind (debugSI "pattern Wildcard1") BMeta (debugSI "pattern Wildcard2", "pattern_wildcard_name") t (SVar (debugSI "pattern Wildcard2", ".wc") 0) |
174 | pattern SPi_ si h a b <- SBind si (BPi h) _ a b where SPi_ si h a b = SBind si (BPi h) (debugSI "16", "generated_name3") a b | 180 | pattern SPi_ si h a b <- SBind si (BPi h) _ a b where SPi_ si h a b = SBind si (BPi h) (debugSI "16", "generated_name3") a b |
175 | pattern SLam_ si h a b <- SBind si (BLam h) _ a b where SLam_ si h a b = SBind si (BLam h) (debugSI "17", "generated_name4") a b | 181 | pattern SLam_ si h a b <- SBind si (BLam h) _ a b where SLam_ si h a b = SBind si (BLam h) (debugSI "17", "generated_name4") a b |
176 | pattern Wildcard_ si t <- SBind _ BMeta _ t (SVar si 0) where Wildcard_ si t = SBind si BMeta (debugSI "pattern Wildcard_", "pattern_wildcard__name") t (SVar si 0) | 182 | pattern Wildcard_ si t <- SBind _ BMeta _ t (SVar (si, _) 0) where Wildcard_ si t = SBind si BMeta (debugSI "pattern Wildcard_", "pattern_wildcard__name") t (SVar (si, ".wc2") 0) |
177 | pattern SAppH a b <- SApp _ Hidden a b where SAppH a b = SApp (debugSI "pattern SAppH") Hidden a b | 183 | pattern SAppH a b <- SApp _ Hidden a b where SAppH a b = SApp (debugSI "pattern SAppH") Hidden a b |
178 | pattern SAppV a b <- SApp _ Visible a b where SAppV a b = SApp (debugSI "pattern SAppV") Visible a b | 184 | pattern SAppV a b <- SApp _ Visible a b where SAppV a b = SApp (debugSI "pattern SAppV") Visible a b |
179 | pattern SAppHSI si a b = SApp si Hidden a b | 185 | pattern SAppHSI si a b = SApp si Hidden a b |
@@ -490,7 +496,7 @@ foldS g f i = \case | |||
490 | SLet a b -> foldS g f i a <> foldS g f (i+1) b | 496 | SLet a b -> foldS g f i a <> foldS g f (i+1) b |
491 | SBind _ _ _ a b -> foldS g f i a <> foldS g f (i+1) b | 497 | SBind _ _ _ a b -> foldS g f i a <> foldS g f (i+1) b |
492 | STyped si (e, t) -> f si i e <> f si i t | 498 | STyped si (e, t) -> f si i e <> f si i t |
493 | SVar si j -> f si i (Var j) | 499 | SVar (si, _) j -> f si i (Var j) |
494 | SGlobal (si, x) -> g si i x | 500 | SGlobal (si, x) -> g si i x |
495 | 501 | ||
496 | foldE f i = \case | 502 | foldE f i = \case |
@@ -517,8 +523,8 @@ usedS = (getAny .) . foldS mempty (const $ (Any .) . usedE) | |||
517 | usedE = (getAny .) . foldE ((Any .) . (==)) | 523 | usedE = (getAny .) . foldE ((Any .) . (==)) |
518 | 524 | ||
519 | mapS = mapS_ (\si _ x -> SGlobal (si, x)) | 525 | mapS = mapS_ (\si _ x -> SGlobal (si, x)) |
520 | mapS_ gg ff = mapS__ gg ff $ \si i j -> case ff i $ Var j of | 526 | mapS_ gg ff = mapS__ gg ff $ \sn i j -> case ff i $ Var j of |
521 | Var k -> SVar si k | 527 | Var k -> SVar sn k |
522 | -- x -> STyped x -- todo | 528 | -- x -> STyped x -- todo |
523 | mapS__ gg f1 f2 h e = g e where | 529 | mapS__ gg f1 f2 h e = g e where |
524 | g i = \case | 530 | g i = \case |
@@ -526,11 +532,11 @@ mapS__ gg f1 f2 h e = g e where | |||
526 | SLet a b -> SLet (g i a) (g (h i) b) | 532 | SLet a b -> SLet (g i a) (g (h i) b) |
527 | SBind si k si' a b -> SBind si k si' (g i a) (g (h i) b) | 533 | SBind si k si' a b -> SBind si k si' (g i a) (g (h i) b) |
528 | STyped si (x, t) -> STyped si (f1 i x, f1 i t) | 534 | STyped si (x, t) -> STyped si (f1 i x, f1 i t) |
529 | SVar si j -> f2 si i j | 535 | SVar sn j -> f2 sn i j |
530 | SGlobal (si, x) -> gg si i x | 536 | SGlobal (si, x) -> gg si i x |
531 | 537 | ||
532 | rearrangeS :: (Int -> Int) -> SExp -> SExp | 538 | rearrangeS :: (Int -> Int) -> SExp -> SExp |
533 | rearrangeS f = mapS__ (\si _ x -> SGlobal (si,x)) (const id) (\si i j -> SVar si $ if j < i then j else i + f (j - i)) (+1) 0 | 539 | rearrangeS f = mapS__ (\si _ x -> SGlobal (si,x)) (const id) (\sn i j -> SVar sn $ if j < i then j else i + f (j - i)) (+1) 0 |
534 | 540 | ||
535 | upS__ i n = mapS (\i -> upE i n) (+1) i | 541 | upS__ i n = mapS (\i -> upE i n) (+1) i |
536 | upS = upS__ 0 1 | 542 | upS = upS__ 0 1 |
@@ -554,14 +560,14 @@ up1E i = \case | |||
554 | upE i n e = iterateN n (up1E i) e | 560 | upE i n e = iterateN n (up1E i) e |
555 | 561 | ||
556 | substSS :: Int -> SExp -> SExp -> SExp | 562 | substSS :: Int -> SExp -> SExp -> SExp |
557 | substSS k x = mapS__ (\si _ x -> SGlobal (si, x)) (error "substSS") (\si (i, x) j -> case compare j i of | 563 | substSS k x = mapS__ (\si _ x -> SGlobal (si, x)) (error "substSS") (\sn (i, x) j -> case compare j i of |
558 | EQ -> x | 564 | EQ -> x |
559 | GT -> SVar si $ j - 1 | 565 | GT -> SVar sn $ j - 1 |
560 | LT -> SVar si j | 566 | LT -> SVar sn j |
561 | ) ((+1) *** upS) (k, x) | 567 | ) ((+1) *** upS) (k, x) |
562 | substS j x = mapS (uncurry $ substE "substS") ((+1) *** up1E 0) (j, x) | 568 | substS j x = mapS (uncurry $ substE "substS") ((+1) *** up1E 0) (j, x) |
563 | substSG j x = mapS_ (\si x i -> if i == j then x else SGlobal (si, i)) (const id) upS x | 569 | substSG j x = mapS_ (\si x i -> if i == j then x else SGlobal (si, i)) (const id) upS x |
564 | substSG0 n e = substSG n (SVar (sexpSI e) 0) $ upS e | 570 | substSG0 n e = substSG n (SVar (sexpSI e, n) 0) $ upS e |
565 | 571 | ||
566 | substE err = substE_ (error $ "substE: todo: environment required in " ++ err) -- todo: remove | 572 | substE err = substE_ (error $ "substE: todo: environment required in " ++ err) -- todo: remove |
567 | 573 | ||
@@ -885,7 +891,7 @@ inferN tracelevel = infer where | |||
885 | infer te exp = (if tracelevel >= 1 then trace_ ("infer: " ++ showEnvSExp te exp) else id) $ (if debug then fmap (recheck' "infer" te *** id) else id) $ case exp of | 891 | infer te exp = (if tracelevel >= 1 then trace_ ("infer: " ++ showEnvSExp te exp) else id) $ (if debug then fmap (recheck' "infer" te *** id) else id) $ case exp of |
886 | SAnn x t -> checkN (CheckIType x te) t TType | 892 | SAnn x t -> checkN (CheckIType x te) t TType |
887 | SLabelEnd x -> infer (ELabelEnd te) x | 893 | SLabelEnd x -> infer (ELabelEnd te) x |
888 | SVar si i -> focus_' te si (Var i, expType_ "1" te (Var i)) | 894 | SVar (si, _) i -> focus_' te si (Var i, expType_ "1" te (Var i)) |
889 | STyped si et -> focus_' te si et | 895 | STyped si et -> focus_' te si et |
890 | SGlobal (si, s) -> focus_' te si =<< getDef te si s | 896 | SGlobal (si, s) -> focus_' te si =<< getDef te si s |
891 | SApp si h a b -> infer (EApp1 h te b) a | 897 | SApp si h a b -> infer (EApp1 h te b) a |
@@ -1262,7 +1268,7 @@ addType x = (x, expType x) | |||
1262 | addType_ te x = (x, expType_ "6" te x) | 1268 | addType_ te x = (x, expType_ "6" te x) |
1263 | 1269 | ||
1264 | downTo n m = map Var [n+m-1, n+m-2..n] | 1270 | downTo n m = map Var [n+m-1, n+m-2..n] |
1265 | downToS n m = map (SVar (debugSI "20")) [n+m-1, n+m-2..n] | 1271 | downToS n m = map (SVar (debugSI "20", ".ds")) [n+m-1, n+m-2..n] |
1266 | 1272 | ||
1267 | defined' = Map.keys | 1273 | defined' = Map.keys |
1268 | 1274 | ||
@@ -1324,7 +1330,7 @@ handleStmt exs = \case | |||
1324 | addToEnv exs (si, cn) (Con conn [], cty) | 1330 | addToEnv exs (si, cn) (Con conn [], cty) |
1325 | return ( conn | 1331 | return ( conn |
1326 | , addParamsS pars | 1332 | , addParamsS pars |
1327 | $ foldl SAppV (SVar (debugSI "22") $ j + length pars) $ drop pnum' xs ++ [apps' cn (zip acts $ downToS (j+1+length pars) (length ps) ++ downToS 0 (act- length ps))] | 1333 | $ foldl SAppV (SVar (debugSI "22", ".cs") $ j + length pars) $ drop pnum' xs ++ [apps' cn (zip acts $ downToS (j+1+length pars) (length ps) ++ downToS 0 (act- length ps))] |
1328 | ) | 1334 | ) |
1329 | | otherwise = throwError $ "illegal data definition (parameters are not uniform) " -- ++ show (c, cn, take pnum' xs, act) | 1335 | | otherwise = throwError $ "illegal data definition (parameters are not uniform) " -- ++ show (c, cn, take pnum' xs, act) |
1330 | where | 1336 | where |
@@ -1345,7 +1351,7 @@ handleStmt exs = \case | |||
1345 | ++ replicate inum (Hidden, Wildcard SType) | 1351 | ++ replicate inum (Hidden, Wildcard SType) |
1346 | ++ [(Visible, apps' s $ zip (map fst ps) (downToS (inum + length cs + 1) $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS 0 inum))] | 1352 | ++ [(Visible, apps' s $ zip (map fst ps) (downToS (inum + length cs + 1) $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS 0 inum))] |
1347 | ) | 1353 | ) |
1348 | $ foldl SAppV (SVar (debugSI "23") $ length cs + inum + 1) $ downToS 1 inum ++ [SVar (debugSI "24") 0] | 1354 | $ foldl SAppV (SVar (debugSI "23", ".ct") $ length cs + inum + 1) $ downToS 1 inum ++ [SVar (debugSI "24", ".24") 0] |
1349 | ) | 1355 | ) |
1350 | addToEnv exs (si, caseName s) (lamify ct $ CaseFun (CaseFunName s ct $ length ps), ct) | 1356 | addToEnv exs (si, caseName s) (lamify ct $ CaseFun (CaseFunName s ct $ length ps), ct) |
1351 | let ps' = fst $ getParams vty | 1357 | let ps' = fst $ getParams vty |
@@ -1595,7 +1601,7 @@ parseLC f str = either (throwError . ErrorMsg . show) return . flip runReader (e | |||
1595 | where | 1601 | where |
1596 | p = do | 1602 | p = do |
1597 | setParserState st | 1603 | setParserState st |
1598 | parseDefs SLabelEnd ns [] <* eof | 1604 | parseDefs SLabelEnd ns (emptyDBNames) <* eof |
1599 | return $ Module | 1605 | return $ Module |
1600 | { extensions = exts | 1606 | { extensions = exts |
1601 | , moduleImports = if NoImplicitPrelude `elem` exts | 1607 | , moduleImports = if NoImplicitPrelude `elem` exts |
@@ -1612,26 +1618,27 @@ parseType ns mb vs = maybe id option mb $ reservedOp "::" *> parseTTerm ns PrecL | |||
1612 | typedIds ns mb vs = (,) <$> commaSep1 (siName (varId ns <|> patVar ns <|> upperCase ns)) | 1618 | typedIds ns mb vs = (,) <$> commaSep1 (siName (varId ns <|> patVar ns <|> upperCase ns)) |
1613 | <*> localIndentation Gt {-TODO-} (parseType ns mb vs) | 1619 | <*> localIndentation Gt {-TODO-} (parseType ns mb vs) |
1614 | 1620 | ||
1615 | telescope ns mb vs = (map removeSI *** id) <$> telescopeSI ns mb (map addSI vs) | 1621 | telescope ns mb vs = (removeSI *** id) <$> telescopeSI ns mb (dbToSINames "tel" vs) |
1616 | where | ||
1617 | addSI n = (debugSI "telescope", n) | ||
1618 | removeSI = snd | ||
1619 | 1622 | ||
1620 | telescopeSI :: Namespace -> Maybe SExp -> [SIName] -> P ([SIName], [(Visibility, SExp)]) -- todo: refactor to [(SIName, (Visibility, SExp))] | 1623 | telescopeSI :: Namespace -> Maybe SExp -> [SIName] -> P ([SIName], [(Visibility, SExp)]) -- todo: refactor to [(SIName, (Visibility, SExp))] |
1621 | telescopeSI ns mb vs = option (vs, []) $ do | 1624 | telescopeSI ns mb vs = option (vs, []) $ do |
1622 | (si, (x, vt)) <- withSI | 1625 | (si, (x, vt)) <- withSI |
1623 | ( reservedOp "@" *> (maybe empty (\x -> flip (,) (Hidden, x) <$> patVar ns) mb <|> parens (typedId Hidden)) | 1626 | ( reservedOp "@" *> (maybe empty (\x -> flip (,) (Hidden, x) <$> patVar ns) mb <|> parens (typedId Hidden)) |
1624 | <|> try (parens $ typedId Visible) | 1627 | <|> try (parens $ typedId Visible) |
1625 | <|> maybe ((,) "" . (,) Visible <$> parseTerm ns PrecAtom (map removeSI vs)) | 1628 | <|> maybe ((,) "" . (,) Visible <$> parseTerm ns PrecAtom (removeSI vs)) |
1626 | (\x -> flip (,) (Visible, x) <$> patVar ns) | 1629 | (\x -> flip (,) (Visible, x) <$> patVar ns) |
1627 | mb | 1630 | mb |
1628 | ) | 1631 | ) |
1629 | (second (vt:)) <$> telescopeSI ns mb ((si, x): vs) | 1632 | (second (vt:)) <$> telescopeSI ns mb ((si, x): vs) |
1630 | where | 1633 | where |
1631 | removeSI = snd | ||
1632 | typedId v = (\f s -> (f,(v,s))) | 1634 | typedId v = (\f s -> (f,(v,s))) |
1633 | <$> patVar ns | 1635 | <$> patVar ns |
1634 | <*> localIndentation Gt {-TODO-} (parseType ns mb (map removeSI vs)) | 1636 | <*> localIndentation Gt {-TODO-} (parseType ns mb (removeSI vs)) |
1637 | |||
1638 | removeSI = DBNamesC . map snd | ||
1639 | |||
1640 | dbToSINames :: String -> DBNames -> [SIName] | ||
1641 | dbToSINames d (DBNamesC xs) = map ((,) (debugSI d)) xs | ||
1635 | 1642 | ||
1636 | parseClause ns e = do | 1643 | parseClause ns e = do |
1637 | (fe, p) <- pattern' ns e | 1644 | (fe, p) <- pattern' ns e |
@@ -1645,7 +1652,7 @@ patternAtom ns vs = | |||
1645 | <|> (,) vs . mkNatPat ns <$> (withSI natural) | 1652 | <|> (,) vs . mkNatPat ns <$> (withSI natural) |
1646 | <|> (,) vs . flip PCon [] <$> (siName $ upperCase ns) | 1653 | <|> (,) vs . flip PCon [] <$> (siName $ upperCase ns) |
1647 | <|> char '\'' *> patternAtom (switchNS ns) vs | 1654 | <|> char '\'' *> patternAtom (switchNS ns) vs |
1648 | <|> (\sn@(si, n) -> (n:vs, PVar sn)) <$> withSI (patVar ns) | 1655 | <|> (\sn@(si, n) -> (addDBName n vs, PVar sn)) <$> withSI (patVar ns) |
1649 | <|> (id *** (pConSI . mkListPat ns)) <$> brackets (patlist ns vs <|> pure (vs, [])) | 1656 | <|> (id *** (pConSI . mkListPat ns)) <$> brackets (patlist ns vs <|> pure (vs, [])) |
1650 | <|> (id *** (pConSI . mkTupPat ns)) <$> parens (patlist ns vs) | 1657 | <|> (id *** (pConSI . mkTupPat ns)) <$> parens (patlist ns vs) |
1651 | where | 1658 | where |
@@ -1752,10 +1759,10 @@ parseDef ns e = | |||
1752 | localIndentation Gt $ do | 1759 | localIndentation Gt $ do |
1753 | (si,x) <- siName $ upperCase (typeNS ns) | 1760 | (si,x) <- siName $ upperCase (typeNS ns) |
1754 | (nps, ts) <- telescopeSI (typeNS ns) (Just SType) (dbToSINames "parseDef data" e) | 1761 | (nps, ts) <- telescopeSI (typeNS ns) (Just SType) (dbToSINames "parseDef data" e) |
1755 | let npsd = siToDBNames nps | 1762 | let npsd@(DBNamesC npsd_) = removeSI nps |
1756 | t <- parseType (typeNS ns) (Just SType) npsd | 1763 | t <- parseType (typeNS ns) (Just SType) npsd |
1757 | let mkConTy mk (nps', ts') = | 1764 | let mkConTy mk (nps', ts') = |
1758 | ( if mk then Just $ diffDBNames nps' npsd else Nothing | 1765 | ( if mk then Just $ diffDBNames' nps' npsd_ else Nothing |
1759 | , foldr (uncurry SPi) (foldl SAppV (SGlobal (si,x)) $ downToS (length ts') $ length ts) ts') | 1766 | , foldr (uncurry SPi) (foldl SAppV (SGlobal (si,x)) $ downToS (length ts') $ length ts) ts') |
1760 | (af, cs) <- | 1767 | (af, cs) <- |
1761 | do (,) True <$ reserved "where" <*> localIndentation Ge (localAbsoluteIndentation $ many $ | 1768 | do (,) True <$ reserved "where" <*> localIndentation Ge (localAbsoluteIndentation $ many $ |
@@ -1803,54 +1810,54 @@ parseDef ns e = | |||
1803 | let ns' = typeNS ns | 1810 | let ns' = typeNS ns |
1804 | localIndentation Gt $ do | 1811 | localIndentation Gt $ do |
1805 | (si, x) <- siName $ upperCase ns' | 1812 | (si, x) <- siName $ upperCase ns' |
1806 | let addSI n = (debugSI "type telescope", n) | 1813 | (nps, ts) <- telescopeSI ns' (Just (Wildcard SType)) (dbToSINames "xx" e) |
1807 | (nps, ts) <- telescopeSI ns' (Just (Wildcard SType)) (map addSI{-todo: remove-} e) | ||
1808 | reservedOp "=" | 1814 | reservedOp "=" |
1809 | rhs <- parseTerm ns' PrecLam $ map snd nps | 1815 | rhs <- parseTerm ns' PrecLam $ removeSI nps |
1810 | ge <- ask | 1816 | ge <- ask |
1811 | 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] | 1817 | 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] |
1812 | <|> do try (reserved "type" >> reserved "instance") | 1818 | <|> do try (reserved "type" >> reserved "instance") |
1813 | let ns' = typeNS ns | 1819 | let ns' = typeNS ns |
1814 | pure <$> localIndentation Gt (funAltDef (upperCase ns') ns' e) | 1820 | pure <$> localIndentation Gt (funAltDef (upperCase ns') ns' e) |
1815 | <|> do (vs, t) <- try $ typedIds ns Nothing [] | 1821 | <|> do (vs, t) <- try $ typedIds ns Nothing $ emptyDBNames |
1816 | return $ TypeAnn <$> vs <*> pure t | 1822 | return $ TypeAnn <$> vs <*> pure t |
1817 | <|> fixityDef | 1823 | <|> fixityDef |
1818 | <|> pure <$> funAltDef (varId ns) ns e | 1824 | <|> pure <$> funAltDef (varId ns) ns e |
1819 | <|> pure . uncurry ValueDef <$> valueDef ns e | 1825 | <|> pure . uncurry ValueDef <$> valueDef ns e |
1820 | where | 1826 | where |
1827 | telescopeDataFields :: Namespace -> [SIName] -> P ([SIName], [(Visibility, SExp)]) | ||
1821 | telescopeDataFields ns vs = option (vs, []) $ do | 1828 | telescopeDataFields ns vs = option (vs, []) $ do |
1822 | (x, vt) <- do name <- siName $ var (expNS ns) | 1829 | (x, vt) <- do name <- siName $ var (expNS ns) |
1823 | reservedOp "::" | 1830 | reservedOp "::" |
1824 | term <- parseTerm ns PrecLam (siToDBNames vs) | 1831 | term <- parseTerm ns PrecLam (removeSI vs) |
1825 | return (name, (Visible, term)) | 1832 | return (name, (Visible, term)) |
1826 | (id *** (vt:)) <$> (comma *> telescopeDataFields ns (x: vs) <|> pure (vs, [])) | 1833 | (id *** (vt:)) <$> (comma *> telescopeDataFields ns (x: vs) <|> pure (vs, [])) |
1827 | 1834 | ||
1828 | funAltDef parseName ns e = do -- todo: use ns to determine parseName | 1835 | funAltDef parseName ns e = do -- todo: use ns to determine parseName |
1829 | (n, (fe, ts)) <- | 1836 | (n, (fe@(DBNamesC fe_), ts)) <- |
1830 | do try' "operator definition" $ do | 1837 | do try' "operator definition" $ do |
1831 | (e', a1) <- patternAtom ns ("": e) | 1838 | (e', a1) <- patternAtom ns (addDBName "" e) |
1832 | localIndentation Gt $ do | 1839 | localIndentation Gt $ do |
1833 | (si,n) <- siName $ operatorT | 1840 | (si,n) <- siName $ operatorT |
1834 | (e'', a2) <- patternAtom ns $ init (diffDBNames e' e) ++ n: e | 1841 | (e'', a2) <- patternAtom ns $ addDBNames (init (diffDBNames e' e) ++ [n]) e |
1835 | lookAhead $ reservedOp "=" <|> reservedOp "|" | 1842 | lookAhead $ reservedOp "=" <|> reservedOp "|" |
1836 | return ((si, n), (e'', (,) (Visible, Wildcard SType) <$> [a1, a2])) | 1843 | return ((si, n), (e'', (,) (Visible, Wildcard SType) <$> [a1, a2])) |
1837 | <|> do try $ do | 1844 | <|> do try $ do |
1838 | (si,n) <- siName $ parseName | 1845 | (si,n) <- siName $ parseName |
1839 | localIndentation Gt $ (,) (si, n) <$> telescope' ns (n: e) <* (lookAhead $ reservedOp "=" <|> reservedOp "|") | 1846 | localIndentation Gt $ (,) (si, n) <$> telescope' ns (addDBName n e) <* (lookAhead $ reservedOp "=" <|> reservedOp "|") |
1840 | localIndentation Gt $ do | 1847 | localIndentation Gt $ do |
1841 | gu <- option Nothing $ do | 1848 | gu <- option Nothing $ do |
1842 | reservedOp "|" | 1849 | reservedOp "|" |
1843 | Just <$> parseTerm ns PrecOp fe | 1850 | Just <$> parseTerm ns PrecOp fe |
1844 | reservedOp "=" | 1851 | reservedOp "=" |
1845 | rhs <- parseTerm ns PrecLam []--fe | 1852 | rhs <- parseTerm ns PrecLam $ emptyDBNames --fe |
1846 | f <- parseWhereBlock ns fe | 1853 | f <- parseWhereBlock ns fe |
1847 | return $ FunAlt n ts gu $ deBruinify' fe {-hack-} $ f rhs | 1854 | return $ FunAlt n ts gu $ deBruinify' fe_ {-hack-} $ f rhs |
1848 | 1855 | ||
1849 | mkData ge x ts t af cs = [Data x ts t af $ (id *** snd) <$> cs] ++ concatMap mkProj cs | 1856 | mkData ge x ts t af cs = [Data x ts t af $ (id *** snd) <$> cs] ++ concatMap mkProj cs |
1850 | where | 1857 | where |
1851 | mkProj ((si,cn), (Just fs, _)) | 1858 | mkProj ((si,cn), (Just fs, _)) |
1852 | = [ Let fn Nothing Nothing [Visible] | 1859 | = [ Let fn Nothing Nothing [Visible] |
1853 | $ upS{-non-rec-} $ patLam si SLabelEnd ge (PCon (si,cn) $ replicate (length fs) $ ParPat [PVar (si, "generated_name1")]) $ SVar si i | 1860 | $ upS{-non-rec-} $ patLam si SLabelEnd ge (PCon (si,cn) $ replicate (length fs) $ ParPat [PVar (si, "generated_name1")]) $ SVar (si, ".proj") i |
1854 | | (i, fn) <- zip [0..] fs] | 1861 | | (i, fn) <- zip [0..] fs] |
1855 | mkProj _ = [] | 1862 | mkProj _ = [] |
1856 | 1863 | ||
@@ -1860,15 +1867,16 @@ parseWhereBlock ns fe = option id $ do | |||
1860 | ge <- ask | 1867 | ge <- ask |
1861 | return $ mkLets ge dcls | 1868 | return $ mkLets ge dcls |
1862 | 1869 | ||
1863 | siToDBNames :: [SIName] -> DBNames | 1870 | newtype DBNames = DBNamesC [SName] -- De Bruijn variable names |
1864 | siToDBNames = map snd | 1871 | instance Show DBNames where show (DBNamesC x) = show x |
1865 | |||
1866 | dbToSINames :: String -> DBNames -> [SIName] | ||
1867 | dbToSINames d = map ((,) (debugSI d)) | ||
1868 | 1872 | ||
1869 | type DBNames = [SName] -- De Bruijn variable names | 1873 | emptyDBNames = DBNamesC [] |
1874 | addDBNames xs (DBNamesC ys) = DBNamesC $ xs ++ ys | ||
1875 | addDBName n (DBNamesC ns) = DBNamesC $ n:ns | ||
1876 | sVar (DBNamesC e) si x = maybe (SGlobal (si, x)) (SVar (si, x)) $ elemIndex' x e | ||
1877 | diffDBNames (DBNamesC xs) (DBNamesC ys) = take (length xs - length ys) xs | ||
1870 | 1878 | ||
1871 | valueDef :: Namespace -> DBNames -> P ((DBNames, Pat), SExp) | 1879 | valueDef :: Namespace -> DBNames -> P (([SName], Pat), SExp) |
1872 | valueDef ns e = do | 1880 | valueDef ns e = do |
1873 | (e', p) <- try $ pattern' ns e <* reservedOp "=" | 1881 | (e', p) <- try $ pattern' ns e <* reservedOp "=" |
1874 | localIndentation Gt $ do | 1882 | localIndentation Gt $ do |
@@ -1901,11 +1909,11 @@ parseTerm ns PrecLam e = | |||
1901 | option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm ns PrecLam e | 1909 | option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm ns PrecLam e |
1902 | parseTerm ns PrecEq e = parseTerm ns PrecAnn e >>= \t -> option t $ SCstr t <$ reservedOp "~" <*> parseTTerm ns PrecAnn e | 1910 | parseTerm ns PrecEq e = parseTerm ns PrecAnn e >>= \t -> option t $ SCstr t <$ reservedOp "~" <*> parseTTerm ns PrecAnn e |
1903 | parseTerm ns PrecAnn e = parseTerm ns PrecOp e >>= \t -> option t $ SAnn t <$> parseType ns Nothing e | 1911 | parseTerm ns PrecAnn e = parseTerm ns PrecOp e >>= \t -> option t $ SAnn t <$> parseType ns Nothing e |
1904 | parseTerm ns PrecOp e = (asks $ \dcls -> calculatePrecs dcls e) <*> p' where | 1912 | parseTerm ns PrecOp e = (asks $ \dcls -> calculatePrecs dcls) <*> p' where |
1905 | p' = ((\si (t, xs) -> (mkNat ns si 0, ("-!", t): xs)) `withRange` (reservedOp "-" *> p_)) | 1913 | p' = ((\si (t, xs) -> (mkNat ns si 0, (sVar e (debugSI "12") "-", t): xs)) `withRange` (reservedOp "-" *> p_)) |
1906 | <|> p_ | 1914 | <|> p_ |
1907 | p_ = (,) <$> parseTerm ns PrecApp e <*> (option [] $ operatorT >>= p) | 1915 | p_ = (,) <$> parseTerm ns PrecApp e <*> (option [] $ (sVar e (debugSI "12b") <$> operatorT) >>= p) |
1908 | p op = do (exp, op') <- try ((,) <$> parseTerm ns PrecApp e <*> operatorT) | 1916 | p op = do (exp, op') <- try ((,) <$> parseTerm ns PrecApp e <*> (sVar e (debugSI "12c") <$> operatorT)) |
1909 | ((op, exp):) <$> p op' | 1917 | ((op, exp):) <$> p op' |
1910 | <|> pure . (,) op <$> parseTerm ns PrecLam e | 1918 | <|> pure . (,) op <$> parseTerm ns PrecLam e |
1911 | parseTerm ns PrecApp e = | 1919 | parseTerm ns PrecApp e = |
@@ -1943,8 +1951,8 @@ parseTerm ns PrecAtom e = | |||
1943 | 1951 | ||
1944 | guardM p m = m >>= \x -> if p x then return x else fail "guardM" | 1952 | guardM p m = m >>= \x -> if p x then return x else fail "guardM" |
1945 | 1953 | ||
1946 | mkLeftSection si (op, e) = SLam_ si Visible (Wildcard SType) $ SGlobal op `SAppV` SVar si 0 `SAppV` upS e | 1954 | mkLeftSection si (op, e) = SLam_ si Visible (Wildcard SType) $ SGlobal op `SAppV` SVar (si, ".ls") 0 `SAppV` upS e |
1947 | mkRightSection si (e, op) = SLam_ si Visible (Wildcard SType) $ SGlobal op `SAppV` upS e `SAppV` SVar si 0 | 1955 | mkRightSection si (e, op) = SLam_ si Visible (Wildcard SType) $ SGlobal op `SAppV` upS e `SAppV` SVar (si, ".rs") 0 |
1948 | 1956 | ||
1949 | mkSwizzling term si = swizzcall | 1957 | mkSwizzling term si = swizzcall |
1950 | where | 1958 | where |
@@ -1993,8 +2001,6 @@ siName = withSI | |||
1993 | 2001 | ||
1994 | withSI = withRange (,) | 2002 | withSI = withRange (,) |
1995 | 2003 | ||
1996 | sVar e si x = maybe (SGlobal (si, x)) (SVar si) $ elemIndex' x e | ||
1997 | |||
1998 | mkIf si (b,t,f) = SGlobal (si,"primIfThenElse") `SAppV` b `SAppV` t `SAppV` f | 2004 | mkIf si (b,t,f) = SGlobal (si,"primIfThenElse") `SAppV` b `SAppV` t `SAppV` f |
1999 | 2005 | ||
2000 | mkDotDot e f = SGlobal (noSI,"fromTo") `SAppV` e `SAppV` f | 2006 | mkDotDot e f = SGlobal (noSI,"fromTo") `SAppV` e `SAppV` f |
@@ -2036,22 +2042,22 @@ listCompr ns dbs = (\e (dbs', fs) -> foldr ($) (deBruinify (diffDBNames dbs' dbs | |||
2036 | <*> commaSepUnfold (liftA2 (<|>) (generator ns) $ liftA2 (<|>) (letdecl ns) (boolExpression ns)) dbs <* reservedOp "]" | 2042 | <*> commaSepUnfold (liftA2 (<|>) (generator ns) $ liftA2 (<|>) (letdecl ns) (boolExpression ns)) dbs <* reservedOp "]" |
2037 | 2043 | ||
2038 | -- todo: make it more efficient | 2044 | -- todo: make it more efficient |
2039 | diffDBNames xs ys = take (length xs - length ys) xs | 2045 | diffDBNames' xs ys = take (length xs - length ys) xs |
2040 | 2046 | ||
2041 | deBruinify' xs e = foldl (\e (i, n) -> substSG n (SVar (debugSI "26") i) e) e $ zip [0..] xs | 2047 | deBruinify' xs e = foldl (\e (i, n) -> substSG n (SVar (debugSI "26", n) i) e) e $ zip [0..] xs |
2042 | 2048 | ||
2043 | deBruinify :: DBNames -> SExp -> SExp | 2049 | deBruinify :: [String] -> SExp -> SExp |
2044 | deBruinify [] e = e | 2050 | deBruinify [] e = e |
2045 | deBruinify (n: ns) e = substSG0 n $ deBruinify ns e | 2051 | deBruinify (n: ns) e = substSG0 n $ deBruinify ns e |
2046 | 2052 | ||
2047 | -------------------------------------------------------------------------------- | 2053 | -------------------------------------------------------------------------------- |
2048 | 2054 | ||
2049 | calculatePrecs :: GlobalEnv' -> [SName] -> (SExp, [(SName, SExp)]) -> SExp | 2055 | --calculatePrecs :: GlobalEnv' -> DBNames -> (SExp, [(SName, SExp)]) -> SExp |
2050 | calculatePrecs _ vs (e, []) = e | 2056 | calculatePrecs _ (e, []) = e |
2051 | calculatePrecs dcls vs (e, xs) = calcPrec (\op x y -> op `SAppV` x `SAppV` y) (getFixity dcls . gf) e $ (sVar vs (debugSI "12"){-todo-} *** id) <$> xs | 2057 | calculatePrecs dcls (e, xs) = calcPrec (\op x y -> op `SAppV` x `SAppV` y) (getFixity dcls . gf) e xs |
2052 | where | 2058 | where |
2053 | gf (SGlobal (si, n)) = n | 2059 | gf (SGlobal (si, n)) = n |
2054 | gf (SVar si i) = vs !! i | 2060 | gf (SVar (_, n) i) = n |
2055 | 2061 | ||
2056 | getFixity :: GlobalEnv' -> SName -> Fixity | 2062 | getFixity :: GlobalEnv' -> SName -> Fixity |
2057 | getFixity (fm, _) n = fromMaybe (InfixL, 9) $ Map.lookup n fm | 2063 | getFixity (fm, _) n = fromMaybe (InfixL, 9) $ Map.lookup n fm |
@@ -2283,7 +2289,7 @@ compileGuardTree unode node adts t = (\x -> traceD (" ! :" ++ showSExp x) x) $ | |||
2283 | | s == s' -> filterGuardTree f s k ns $ guardNodes (zips [k+ns-1, k+ns-2..] ps) gs | 2289 | | s == s' -> filterGuardTree f s k ns $ guardNodes (zips [k+ns-1, k+ns-2..] ps) gs |
2284 | | otherwise -> Alts [] | 2290 | | otherwise -> Alts [] |
2285 | where | 2291 | where |
2286 | zips is ps = zip (map (SVar (debugSI "30")) $ zipWith (+) is $ sums $ map varPP ps) ps | 2292 | zips is ps = zip (map (SVar (debugSI "30", ".30")) $ zipWith (+) is $ sums $ map varPP ps) ps |
2287 | su = sum $ map varPP ps | 2293 | su = sum $ map varPP ps |
2288 | sums = scanl (+) 0 | 2294 | sums = scanl (+) 0 |
2289 | 2295 | ||
@@ -2308,7 +2314,7 @@ compileGuardTree unode node adts t = (\x -> traceD (" ! :" ++ showSExp x) x) $ | |||
2308 | ViewPat f (ParPat p) -> guardNode (f `SAppV` v) p {- $ guardNode v ws -} e | 2314 | ViewPat f (ParPat p) -> guardNode (f `SAppV` v) p {- $ guardNode v ws -} e |
2309 | PCon (_, s) ps' -> GuardNode v s ps' {- $ guardNode v ws -} e | 2315 | PCon (_, s) ps' -> GuardNode v s ps' {- $ guardNode v ws -} e |
2310 | 2316 | ||
2311 | varGuardNode v (SVar si e) gt = substGT v e gt | 2317 | varGuardNode v (SVar _ e) gt = substGT v e gt |
2312 | 2318 | ||
2313 | compileCase ge x cs | 2319 | compileCase ge x cs |
2314 | = SLamV (compileGuardTree id id ge $ Alts [compilePatts [(p, 0)] Nothing e | (p, e) <- cs]) `SAppV` x | 2320 | = SLamV (compileGuardTree id id ge $ Alts [compilePatts [(p, 0)] Nothing e | (p, e) <- cs]) `SAppV` x |
@@ -2322,9 +2328,9 @@ compilePatts ps gu = cp [] ps | |||
2322 | Just ge -> GuardNode (rearrangeS (f $ reverse ps') ge) "True" [] rhs | 2328 | Just ge -> GuardNode (rearrangeS (f $ reverse ps') ge) "True" [] rhs |
2323 | where rhs = GuardLeaf $ rearrangeS (f $ reverse ps') e | 2329 | where rhs = GuardLeaf $ rearrangeS (f $ reverse ps') e |
2324 | cp ps' ((p@PVar{}, i): xs) e = cp (p: ps') xs e | 2330 | cp ps' ((p@PVar{}, i): xs) e = cp (p: ps') xs e |
2325 | cp ps' ((p@(PCon (si, n) ps), i): xs) e = GuardNode (SVar si $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs e | 2331 | cp ps' ((p@(PCon (si, n) ps), i): xs) e = GuardNode (SVar (si, n) $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs e |
2326 | cp ps' ((p@(ViewPat f (ParPat [PCon (si, n) ps])), i): xs) e | 2332 | cp ps' ((p@(ViewPat f (ParPat [PCon (si, n) ps])), i): xs) e |
2327 | = GuardNode (SAppV f $ SVar si $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs e | 2333 | = GuardNode (SAppV f $ SVar (si, n) $ i + sum (map (fromMaybe 0 . ff) ps')) n ps $ cp (p: ps') xs e |
2328 | 2334 | ||
2329 | m = length ps | 2335 | m = length ps |
2330 | 2336 | ||
@@ -2376,7 +2382,7 @@ showDoc = str . flip runReader [] . flip evalStateT (flip (:) <$> iterate ('\'': | |||
2376 | type FreshVars = [String] | 2382 | type FreshVars = [String] |
2377 | 2383 | ||
2378 | -- name De Bruijn indices | 2384 | -- name De Bruijn indices |
2379 | type NameDB a = StateT FreshVars (Reader DBNames) a | 2385 | type NameDB a = StateT FreshVars (Reader [String]) a |
2380 | 2386 | ||
2381 | type Doc = NameDB PrecString | 2387 | type Doc = NameDB PrecString |
2382 | {- | 2388 | {- |