summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-01-21 23:50:27 +0100
committerPéter Diviánszky <divipp@gmail.com>2016-01-21 23:50:27 +0100
commit23bbb0006279533d7f99c3cb7873cfed677ca608 (patch)
treeec0455c38b348cfd6aa9d2b540ef2ab7971f867b /src
parent2c5495fe76d2315ff241064d77a7a97014a7f898 (diff)
better source range handling
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Infer.hs362
1 files changed, 185 insertions, 177 deletions
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 405bb06a..6341b9f9 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -143,15 +143,6 @@ data SExp
143 143
144pattern SVar a b = SVar_ (SData a) b 144pattern SVar a b = SVar_ (SData a) b
145 145
146sexpSI :: SExp -> SI
147sexpSI = \case
148 SGlobal (si, _) -> si
149 SBind si _ (si', _) e1 e2 -> si <> si' <> sexpSI e1 <> sexpSI e2 -- TODO: just si
150 SApp si _ e1 e2 -> si <> sexpSI e1 <> sexpSI e2 -- TODO: just si
151 SLet e1 e2 -> sexpSI e1 <> sexpSI e2
152 SVar (si, _) _ -> si
153 STyped si _ -> si
154
155type FixityMap = Map.Map SName Fixity 146type FixityMap = Map.Map SName Fixity
156type ConsMap = Map.Map SName{-constructor name-} 147type ConsMap = Map.Map SName{-constructor name-}
157 (Either ((SName{-type name-}, Int{-num of indices-}), [(SName, Int)]{-constructors with arities-}) 148 (Either ((SName{-type name-}, Int{-num of indices-}), [(SName, Int)]{-constructors with arities-})
@@ -171,30 +162,32 @@ data Binder
171data Visibility = Hidden | Visible 162data Visibility = Hidden | Visible
172 deriving (Eq, Show) 163 deriving (Eq, Show)
173 164
174sLit si a = STyped si (ELit a, litType a) 165sLit a = STyped noSI (ELit a, litType a)
175pattern Primitive n mf t <- Let n mf (Just t) _ (SGlobal (si, "undefined")) where Primitive n mf t = Let n mf (Just t) (map fst $ fst $ getParamsS t) $ SGlobal (debugSI "pattern Primitive", "undefined") 166pattern Primitive n mf t <- Let n mf (Just t) _ (SBuiltin "undefined") where Primitive n mf t = Let n mf (Just t) (map fst $ fst $ getParamsS t) $ SBuiltin "undefined"
176pattern SType <- STyped _ (TType, TType) where SType = STyped (debugSI "pattern SType") (TType, TType) 167pattern SType <- STyped _ (TType, TType) where SType = STyped (debugSI "pattern SType") (TType, TType)
177pattern 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 168pattern SPi h a b <- SBind _ (BPi h) _ a b where SPi h a b = SBind (sourceInfo a <> sourceInfo b) (BPi h) (debugSI "patternSPi2", "pattern_spi_name") a b
178pattern 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 169pattern SLam h a b <- SBind _ (BLam h) _ a b where SLam h a b = SBind (sourceInfo a <> sourceInfo b) (BLam h) (debugSI "patternSLam2", "pattern_slam_name") a b
179pattern 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) 170pattern 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)
180pattern 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 171pattern SPi_ si h a b <- SBind si (BPi h) _ a b where SPi_ si h a b = SBind si (BPi h) (sourceInfo a <> sourceInfo b, "generated_name3") a b
181pattern 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 172pattern SLam_ si h a b <- SBind si (BLam h) _ a b where SLam_ si h a b = SBind si (BLam h) (sourceInfo a <> sourceInfo b, "generated_name4") a b
182pattern 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) 173pattern 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)
183pattern SAppH a b <- SApp _ Hidden a b where SAppH a b = SApp (debugSI "pattern SAppH") Hidden a b 174pattern SAppH a b <- SApp _ Hidden a b where SAppH a b = sApp Hidden a b
184pattern SAppV a b <- SApp _ Visible a b where SAppV a b = SApp (debugSI "pattern SAppV") Visible a b 175pattern SAppV a b <- SApp _ Visible a b where SAppV a b = sApp Visible a b
185pattern SAppHSI si a b = SApp si Hidden a b
186pattern SAppVSI si a b = SApp si Visible a b
187pattern SLamV a = SLam Visible (Wildcard SType) a 176pattern SLamV a = SLam Visible (Wildcard SType) a
188pattern SLamV_ si a <- SLam_ si Visible (Wildcard_ _ SType) a where SLamV_ si a = SLam_ si Visible (Wildcard_ si SType) a 177pattern SLamV_ si a <- SLam_ si Visible (Wildcard_ _ SType) a where SLamV_ si a = SLam_ si Visible (Wildcard_ si SType) a
189pattern SAnn a t <- STyped _ (Lam Visible TType (Lam Visible (Var 0) (Var 0)), TType :~> Var 0 :~> Var 1) `SAppV` t `SAppV` a -- a :: t ~~> id t a 178pattern SAnn a t <- STyped _ (Lam Visible TType (Lam Visible (Var 0) (Var 0)), TType :~> Var 0 :~> Var 1) `SAppV` t `SAppV` a -- a :: t ~~> id t a
190 where SAnn a t = STyped (debugSI "pattern SAnn") (Lam Visible TType (Lam Visible (Var 0) (Var 0)), TType :~> Var 0 :~> Var 1) `SAppV` t `SAppV` a 179 where SAnn a t = STyped (debugSI "pattern SAnn") (Lam Visible TType (Lam Visible (Var 0) (Var 0)), TType :~> Var 0 :~> Var 1) `SAppV` t `SAppV` a
191pattern TyType a <- STyped _ (Lam Visible TType (Var 0), TType :~> TType) `SAppV` a 180pattern TyType a <- STyped _ (Lam Visible TType (Var 0), TType :~> TType) `SAppV` a
192 where TyType a = STyped (debugSI "pattern TyType") (Lam Visible TType (Var 0), TType :~> TType) `SAppV` a 181 where TyType a = STyped (sourceInfo a) (Lam Visible TType (Var 0), TType :~> TType) `SAppV` a
193 -- same as (a :: TType) -- a :: TType ~~> (\(x :: TType) -> x) a 182 -- same as (a :: TType) -- a :: TType ~~> (\(x :: TType) -> x) a
194pattern SCstr a b <- SGlobal (_, "'EqCT") `SAppV` SType `SAppV` a `SAppV` b where SCstr a b = SGlobal (debugSI "pattern SCstr", "'EqCT") `SAppV` SType `SAppV` a `SAppV` b -- a ~ b 183pattern SCstr a b = SBuiltin "'EqCT" `SAppV` SType `SAppV` a `SAppV` b -- a ~ b
195pattern SParEval a b <- SGlobal (_, "parEval") `SAppV` Wildcard SType `SAppV` a `SAppV` b where SParEval a b = SGlobal (debugSI "pattern SParEval","parEval") `SAppV` Wildcard SType `SAppV` a `SAppV` b 184pattern SParEval a b = SBuiltin "parEval" `SAppV` Wildcard SType `SAppV` a `SAppV` b
196pattern SLabelEnd a <- SGlobal (_, "labelend") `SAppV` a where SLabelEnd a = SGlobal (debugSI "pattern SLabelEnd","labelend") `SAppV` a 185pattern SLabelEnd a = SBuiltin "labelend" `SAppV` a
197pattern ST2 a b <- SGlobal (_, "'T2") `SAppV` a `SAppV` b where ST2 a b = SGlobal (debugSI "pattern ST2","'T2") `SAppV` a `SAppV` b 186pattern ST2 a b = SBuiltin "'T2" `SAppV` a `SAppV` b
187
188pattern SBuiltin s <- SGlobal (_, s) where SBuiltin s = SGlobal (debugSI $ "builtin " ++ s, s)
189
190sApp v a b = SApp (sourceInfo a <> sourceInfo b) v a b
198 191
199isPi (BPi _) = True 192isPi (BPi _) = True
200isPi _ = False 193isPi _ = False
@@ -897,11 +890,11 @@ inferN tracelevel = infer where
897 SGlobal (si, s) -> focus_' te si =<< getDef te si s 890 SGlobal (si, s) -> focus_' te si =<< getDef te si s
898 SApp si h a b -> infer (EApp1 h te b) a 891 SApp si h a b -> infer (EApp1 h te b) a
899 SLet a b -> infer (ELet1 te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a) 892 SLet a b -> infer (ELet1 te b{-in-}) a{-let-} -- infer te SLamV b `SAppV` a)
900 SBind _ h _ a b -> infer ((if h /= BMeta then CheckType_ (sexpSI exp) TType else id) $ EBind1 h te $ (if isPi h then TyType else id) b) a 893 SBind _ h _ a b -> infer ((if h /= BMeta then CheckType_ (sourceInfo exp) TType else id) $ EBind1 h te $ (if isPi h then TyType else id) b) a
901 894
902 checkN :: Env -> SExp -> Exp -> TCM m ExpType 895 checkN :: Env -> SExp -> Exp -> TCM m ExpType
903 checkN te x t = do 896 checkN te x t = do
904 tellType te (sexpSI x) t 897 tellType te (sourceInfo x) t
905 (if tracelevel >= 1 then trace_ $ "check: " ++ showEnvSExpType te x t else id) $ checkN_ te x t 898 (if tracelevel >= 1 then trace_ $ "check: " ++ showEnvSExpType te x t else id) $ checkN_ te x t
906 899
907 checkN_ te e t 900 checkN_ te e t
@@ -925,7 +918,7 @@ inferN tracelevel = infer where
925 same <- return $ checkSame te a x 918 same <- return $ checkSame te a x
926 if same then checkN (EBind2 (BLam h) x te) b y else error $ "checkSame:\n" ++ show a ++ "\nwith\n" ++ showEnvExp te x 919 if same then checkN (EBind2 (BLam h) x te) b y else error $ "checkSame:\n" ++ show a ++ "\nwith\n" ++ showEnvExp te x
927 | Pi Hidden a b <- t, notHiddenLam e = checkN (EBind2 (BLam Hidden) a te) (upS e) b 920 | Pi Hidden a b <- t, notHiddenLam e = checkN (EBind2 (BLam Hidden) a te) (upS e) b
928 | otherwise = infer (CheckType_ (sexpSI e) t te) e 921 | otherwise = infer (CheckType_ (sourceInfo e) t te) e
929 where 922 where
930 -- todo 923 -- todo
931 notHiddenLam = \case 924 notHiddenLam = \case
@@ -959,17 +952,17 @@ inferN tracelevel = infer where
959 CheckSame x te -> focus_ (EBind2_ (debugSI "focus_ CheckSame") BMeta (cstr x e) te) (up1E 0 e, up1E 0 et) 952 CheckSame x te -> focus_ (EBind2_ (debugSI "focus_ CheckSame") BMeta (cstr x e) te) (up1E 0 e, up1E 0 et)
960 CheckAppType h t te b -- App1 h (CheckType t te) b 953 CheckAppType h t te b -- App1 h (CheckType t te) b
961 | Pi h' x (downE 0 -> Just y) <- et, h == h' -> case t of 954 | Pi h' x (downE 0 -> Just y) <- et, h == h' -> case t of
962 Pi Hidden t1 t2 | h == Visible -> focus_ (EApp1 h (CheckType_ (sexpSI b) t te) b) (e, et) -- <<e>> b : {t1} -> {t2} 955 Pi Hidden t1 t2 | h == Visible -> focus_ (EApp1 h (CheckType_ (sourceInfo b) t te) b) (e, et) -- <<e>> b : {t1} -> {t2}
963 _ -> focus_ (EBind2_ (sexpSI b) BMeta (cstr t y) $ EApp1 h te b) (up1E 0 e, up1E 0 et) 956 _ -> focus_ (EBind2_ (sourceInfo b) BMeta (cstr t y) $ EApp1 h te b) (up1E 0 e, up1E 0 et)
964 | otherwise -> focus_ (EApp1 h (CheckType_ (sexpSI b) t te) b) (e, et) 957 | otherwise -> focus_ (EApp1 h (CheckType_ (sourceInfo b) t te) b) (e, et)
965 EApp1 h te b 958 EApp1 h te b
966 | Pi h' x y <- et, h == h' -> checkN (EApp2 h e te) b x 959 | Pi h' x y <- et, h == h' -> checkN (EApp2 h e te) b x
967 | Pi Hidden x y <- et, h == Visible -> focus_ (EApp1 Hidden env $ Wildcard $ Wildcard SType) (e, et) -- e b --> e _ b 960 | Pi Hidden x y <- et, h == Visible -> focus_ (EApp1 Hidden env $ Wildcard $ Wildcard SType) (e, et) -- e b --> e _ b
968-- | CheckType (Pi Hidden _ _) te' <- te -> error "ok" 961-- | CheckType (Pi Hidden _ _) te' <- te -> error "ok"
969-- | CheckAppType Hidden _ te' _ <- te -> error "ok" 962-- | CheckAppType Hidden _ te' _ <- te -> error "ok"
970 | otherwise -> infer (CheckType_ (sexpSI b) (Var 2) $ cstr' h (upE 0 2 et) (Pi Visible (Var 1) (Var 1)) (upE 0 2 e) $ EBind2_ (sexpSI b) BMeta TType $ EBind2_ (sexpSI b) BMeta TType te) (upS__ 0 3 b) 963 | otherwise -> infer (CheckType_ (sourceInfo b) (Var 2) $ cstr' h (upE 0 2 et) (Pi Visible (Var 1) (Var 1)) (upE 0 2 e) $ EBind2_ (sourceInfo b) BMeta TType $ EBind2_ (sourceInfo b) BMeta TType te) (upS__ 0 3 b)
971 where 964 where
972 cstr' h x y e = EApp2 h (eval (error "cstr'") $ Coe (up1E 0 x) (up1E 0 y) (Var 0) (up1E 0 e)) . EBind2_ (sexpSI b) BMeta (cstr x y) 965 cstr' h x y e = EApp2 h (eval (error "cstr'") $ Coe (up1E 0 x) (up1E 0 y) (Var 0) (up1E 0 e)) . EBind2_ (sourceInfo b) BMeta (cstr x y)
973 ELet1 te b{-in-} -> replaceMetas "let" Lam e >>= \e' -> infer (ELet2 (addType_ te e'{-let-}) te) b{-in-} 966 ELet1 te b{-in-} -> replaceMetas "let" Lam e >>= \e' -> infer (ELet2 (addType_ te e'{-let-}) te) b{-in-}
974 ELet2 (x{-let-}, xt) te -> focus_ te (substE "app2" 0 (x{-let-}) ( e{-in-}), et) 967 ELet2 (x{-let-}, xt) te -> focus_ te (substE "app2" 0 (x{-let-}) ( e{-in-}), et)
975 CheckIType x te -> checkN te x e 968 CheckIType x te -> checkN te x e
@@ -980,7 +973,7 @@ inferN tracelevel = infer where
980 -> focus_ (CheckType_ si t2 $ EBind2 (BLam Hidden) t1 te) (e, et) 973 -> focus_ (CheckType_ si t2 $ EBind2 (BLam Hidden) t1 te) (e, et)
981 | otherwise -> focus_ (EBind2_ si BMeta (cstr t et) te) (up1E 0 e, up1E 0 et) 974 | otherwise -> focus_ (EBind2_ si BMeta (cstr t et) te) (up1E 0 e, up1E 0 et)
982 EApp2 h a te -> focus te $ app_ a e -- h?? 975 EApp2 h a te -> focus te $ app_ a e -- h??
983 EBind1 h te b -> infer (EBind2_ (sexpSI b) h e te) b 976 EBind1 h te b -> infer (EBind2_ (sourceInfo b) h e te) b
984 EBind2_ si BMeta tt te 977 EBind2_ si BMeta tt te
985 | Unit <- tt -> refocus te $ both (substE_ te 0 TT) (e, et) 978 | Unit <- tt -> refocus te $ both (substE_ te 0 TT) (e, et)
986 | Empty msg <- tt -> throwError $ "type error: " ++ msg ++ "\nin " ++ showSI te si ++ "\n"-- todo: better error msg 979 | Empty msg <- tt -> throwError $ "type error: " ++ msg ++ "\nin " ++ showSI te si ++ "\n"-- todo: better error msg
@@ -1156,8 +1149,8 @@ getParams x = ([], x)
1156getLams (Lam h a b) = ((h, a):) *** id $ getLams b 1149getLams (Lam h a b) = ((h, a):) *** id $ getLams b
1157getLams x = ([], x) 1150getLams x = ([], x)
1158 1151
1159apps a b = foldl SAppV{-SI todo-} (SGlobal a) b 1152apps a b = foldl SAppV (SGlobal a) b
1160apps' a b = foldl sapp{-SI todo-} (SGlobal a) b 1153apps' = foldl $ \a (v, b) -> sApp v a b
1161 1154
1162replaceMetas err bind = \case 1155replaceMetas err bind = \case
1163 Meta a t -> bind Hidden a <$> replaceMetas err bind t 1156 Meta a t -> bind Hidden a <$> replaceMetas err bind t
@@ -1287,18 +1280,18 @@ tellStmtType exs si t = getGEnv exs $ \te -> tellType te si t
1287handleStmt :: MonadFix m => Extensions -> Stmt -> ElabStmtM m () 1280handleStmt :: MonadFix m => Extensions -> Stmt -> ElabStmtM m ()
1288handleStmt exs = \case 1281handleStmt exs = \case
1289 PrecDef{} -> return () 1282 PrecDef{} -> return ()
1290 Primitive (si, n) mf t_ -> do 1283 Primitive n mf t_ -> do
1291 t <- inferType exs tr =<< ($ t_) <$> addF exs 1284 t <- inferType exs tr =<< ($ t_) <$> addF exs
1292 tellStmtType exs si t 1285 tellStmtType exs (fst n) t
1293 addToEnv exs (si, n) $ flip (,) t $ lamify t $ Fun (FunName n mf t) 1286 addToEnv exs n $ flip (,) t $ lamify t $ Fun (FunName (snd n) mf t)
1294 Let (si, n) mf mt ar t_ -> do 1287 Let n mf mt ar t_ -> do
1295 af <- addF exs 1288 af <- addF exs
1296 let t__ = maybe id (flip SAnn . af) mt t_ 1289 let t__ = maybe id (flip SAnn . af) mt t_
1297 (x, t) <- inferTerm exs n tr id $ fromMaybe (SGlobal (si, "primFix") `SAppV` SLamV t__) $ downS 0 t__ 1290 (x, t) <- inferTerm exs (snd n) tr id $ fromMaybe (SBuiltin "primFix" `SAppV` SLamV t__) $ downS 0 t__
1298 let 1291 let
1299 term = label LabelPM (addLams' ar t 0) $ par ar t x 0 1292 term = label LabelPM (addLams' ar t 0) $ par ar t x 0
1300 1293
1301 addLams' [] _ i = Fun (FunName n mf t) $ downTo 0 i 1294 addLams' [] _ i = Fun (FunName (snd n) mf t) $ downTo 0 i
1302 addLams' (h: ar) (Pi h' d t) i | h == h' = Lam h d $ addLams' ar t (i+1) 1295 addLams' (h: ar) (Pi h' d t) i | h == h' = Lam h d $ addLams' ar t (i+1)
1303 addLams' ar@(Visible: _) (Pi h@Hidden d t) i = Lam h d $ addLams' ar t (i+1) 1296 addLams' ar@(Visible: _) (Pi h@Hidden d t) i = Lam h d $ addLams' ar t (i+1)
1304 1297
@@ -1308,41 +1301,41 @@ handleStmt exs = \case
1308 dropHidden (Hidden: ar) = ar 1301 dropHidden (Hidden: ar) = ar
1309 dropHidden ar = ar 1302 dropHidden ar = ar
1310 par ar t x _ = x 1303 par ar t x _ = x
1311 tellStmtType exs si t 1304 tellStmtType exs (fst n) t
1312 addToEnv exs (si, n) (term, t) 1305 addToEnv exs n (term, t)
1313 TypeFamily s ps t -> handleStmt exs $ Primitive s Nothing $ addParamsS ps t 1306 TypeFamily s ps t -> handleStmt exs $ Primitive s Nothing $ addParamsS ps t
1314 Data (si,s) ps t_ addfa cs -> do 1307 Data s ps t_ addfa cs -> do
1315 af <- if addfa then gets $ addForalls exs . (s:) . defined' else return id 1308 af <- if addfa then gets $ addForalls exs . (snd s:) . defined' else return id
1316 vty <- inferType exs tr $ addParamsS ps t_ 1309 vty <- inferType exs tr $ addParamsS ps t_
1317 tellStmtType exs si vty 1310 tellStmtType exs (fst s) vty
1318 let 1311 let
1319 pnum' = length $ filter ((== Visible) . fst) ps 1312 pnum' = length $ filter ((== Visible) . fst) ps
1320 inum = arity vty - length ps 1313 inum = arity vty - length ps
1321 1314
1322 mkConstr j ((si, cn), af -> ct) 1315 mkConstr j (cn, af -> ct)
1323 | c == SGlobal (si,s) && take pnum' xs == downToS (length . fst . getParamsS $ ct) pnum' 1316 | c == SGlobal s && take pnum' xs == downToS (length . fst . getParamsS $ ct) pnum'
1324 = do 1317 = do
1325 cty <- removeHiddenUnit <$> inferType exs tr (addParamsS [(Hidden, x) | (Visible, x) <- ps] ct) 1318 cty <- removeHiddenUnit <$> inferType exs tr (addParamsS [(Hidden, x) | (Visible, x) <- ps] ct)
1326 tellStmtType exs si cty 1319 tellStmtType exs (fst cn) cty
1327 let pars = zipWith (\x -> id *** (STyped (debugSI "mkConstr1")) . flip (,) TType . upE x (1+j)) [0..] $ drop (length ps) $ fst $ getParams cty 1320 let pars = zipWith (\x -> id *** (STyped (debugSI "mkConstr1")) . flip (,) TType . upE x (1+j)) [0..] $ drop (length ps) $ fst $ getParams cty
1328 act = length . fst . getParams $ cty 1321 act = length . fst . getParams $ cty
1329 acts = map fst . fst . getParams $ cty 1322 acts = map fst . fst . getParams $ cty
1330 conn = ConName cn Nothing j cty 1323 conn = ConName (snd cn) Nothing j cty
1331 addToEnv exs (si, cn) (Con conn [], cty) 1324 addToEnv exs cn (Con conn [], cty)
1332 return ( conn 1325 return ( conn
1333 , addParamsS pars 1326 , addParamsS pars
1334 $ foldl SAppV (SVar (debugSI "22", ".cs") $ j + length pars) $ drop pnum' xs ++ [apps' (si, cn) (zip acts $ downToS (j+1+length pars) (length ps) ++ downToS 0 (act- length ps))] 1327 $ foldl SAppV (SVar (debugSI "22", ".cs") $ j + length pars) $ drop pnum' xs ++ [apps' (SGlobal cn) (zip acts $ downToS (j+1+length pars) (length ps) ++ downToS 0 (act- length ps))]
1335 ) 1328 )
1336 | otherwise = throwError $ "illegal data definition (parameters are not uniform) " -- ++ show (c, cn, take pnum' xs, act) 1329 | otherwise = throwError $ "illegal data definition (parameters are not uniform) " -- ++ show (c, cn, take pnum' xs, act)
1337 where 1330 where
1338 (c, map snd -> xs) = getApps $ snd $ getParamsS ct 1331 (c, map snd -> xs) = getApps $ snd $ getParamsS ct
1339 1332
1340 motive = addParamsS (replicate inum (Visible, Wildcard SType)) $ 1333 motive = addParamsS (replicate inum (Visible, Wildcard SType)) $
1341 SPi Visible (apps' (si, s) $ zip (map fst ps) (downToS inum $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS 0 inum)) SType 1334 SPi Visible (apps' (SGlobal s) $ zip (map fst ps) (downToS inum $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS 0 inum)) SType
1342 1335
1343 mdo 1336 mdo
1344 let tcn = TyConName s Nothing inum vty (map fst cons) ct 1337 let tcn = TyConName (snd s) Nothing inum vty (map fst cons) ct
1345 addToEnv exs (si, s) (TyCon tcn [], vty) 1338 addToEnv exs s (TyCon tcn [], vty)
1346 cons <- zipWithM mkConstr [0..] cs 1339 cons <- zipWithM mkConstr [0..] cs
1347 ct <- inferType exs tr 1340 ct <- inferType exs tr
1348 ( (\x -> traceD ("type of case-elim before elaboration: " ++ showSExp x) x) $ addParamsS 1341 ( (\x -> traceD ("type of case-elim before elaboration: " ++ showSExp x) x) $ addParamsS
@@ -1350,18 +1343,18 @@ handleStmt exs = \case
1350 ++ (Visible, motive) 1343 ++ (Visible, motive)
1351 : map ((,) Visible . snd) cons 1344 : map ((,) Visible . snd) cons
1352 ++ replicate inum (Hidden, Wildcard SType) 1345 ++ replicate inum (Hidden, Wildcard SType)
1353 ++ [(Visible, apps' (si, s) $ zip (map fst ps) (downToS (inum + length cs + 1) $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS 0 inum))] 1346 ++ [(Visible, apps' (SGlobal s) $ zip (map fst ps) (downToS (inum + length cs + 1) $ length ps) ++ zip (map fst $ fst $ getParamsS t_) (downToS 0 inum))]
1354 ) 1347 )
1355 $ foldl SAppV (SVar (debugSI "23", ".ct") $ length cs + inum + 1) $ downToS 1 inum ++ [SVar (debugSI "24", ".24") 0] 1348 $ foldl SAppV (SVar (debugSI "23", ".ct") $ length cs + inum + 1) $ downToS 1 inum ++ [SVar (debugSI "24", ".24") 0]
1356 ) 1349 )
1357 addToEnv exs (si, caseName s) (lamify ct $ CaseFun (CaseFunName s ct $ length ps), ct) 1350 addToEnv exs (fst s, caseName (snd s)) (lamify ct $ CaseFun (CaseFunName (snd s) ct $ length ps), ct)
1358 let ps' = fst $ getParams vty 1351 let ps' = fst $ getParams vty
1359 t = (TType :~> TType) 1352 t = (TType :~> TType)
1360 :~> addParams ps' (Var (length ps') `app_` TyCon tcn (downTo 0 $ length ps')) 1353 :~> addParams ps' (Var (length ps') `app_` TyCon tcn (downTo 0 $ length ps'))
1361 :~> TType 1354 :~> TType
1362 :~> Var 2 `app_` Var 0 1355 :~> Var 2 `app_` Var 0
1363 :~> Var 3 `app_` Var 1 1356 :~> Var 3 `app_` Var 1
1364 addToEnv exs (si, matchName s) (lamify t $ TyCaseFun (TyCaseFunName s t), t) 1357 addToEnv exs (fst s, matchName (snd s)) (lamify t $ TyCaseFun (TyCaseFunName (snd s) t), t)
1365 1358
1366 stmt -> error $ "handleStmt: " ++ show stmt 1359 stmt -> error $ "handleStmt: " ++ show stmt
1367 1360
@@ -1656,7 +1649,7 @@ parseClause ns = do
1656 1649
1657patternAtom ns = patternAtom' ns <&> \p -> (getPVars p, p) 1650patternAtom ns = patternAtom' ns <&> \p -> (getPVars p, p)
1658patternAtom' ns = 1651patternAtom' ns =
1659 flip ViewPat eqPP . SAppV (SGlobal (debugSI "patternAtom1","primCompareFloat")) <$> sLit `withRange` (LFloat <$> try float) 1652 flip ViewPat eqPP . SAppV (SBuiltin "primCompareFloat") <$> (sLit . LFloat <$> try float)
1660 <|> mkNatPat ns <$> (withSI natural) 1653 <|> mkNatPat ns <$> (withSI natural)
1661 <|> flip PCon [] <$> (withSI $ upperCase ns) 1654 <|> flip PCon [] <$> (withSI $ upperCase ns)
1662 <|> char '\'' *> patternAtom' (switchNS ns) 1655 <|> char '\'' *> patternAtom' (switchNS ns)
@@ -1664,7 +1657,7 @@ patternAtom' ns =
1664 <|> pConSI . mkListPat ns <$> brackets (patlist ns <|> pure []) 1657 <|> pConSI . mkListPat ns <$> brackets (patlist ns <|> pure [])
1665 <|> pConSI . mkTupPat ns <$> parens (patlist ns) 1658 <|> pConSI . mkTupPat ns <$> parens (patlist ns)
1666 where 1659 where
1667 mkNatPat (Namespace ExpLevel _) (si, n) = flip ViewPat eqPP . SAppV (SGlobal (debugSI "patternAtom2","primCompareInt")) . sLit si . LInt $ fromIntegral n 1660 mkNatPat (Namespace ExpLevel _) (si, n) = flip ViewPat eqPP . SAppV (SBuiltin "primCompareInt") . sLit . LInt $ fromIntegral n
1668 mkNatPat _ (si, n) = toNatP si n 1661 mkNatPat _ (si, n) = toNatP si n
1669 pConSI (PCon (_, n) ps) = PCon (sourceInfo ps, n) ps 1662 pConSI (PCon (_, n) ps) = PCon (sourceInfo ps, n) ps
1670 pConSI p = p 1663 pConSI p = p
@@ -1730,8 +1723,8 @@ compileFunAlts par ulend lend ge ds = \case
1730 [Instance{}] -> [] 1723 [Instance{}] -> []
1731 [Class n ps ms] -> compileFunAlts' SLabelEnd ge $ 1724 [Class n ps ms] -> compileFunAlts' SLabelEnd ge $
1732 [ TypeAnn n $ foldr (SPi Visible) SType ps ] 1725 [ TypeAnn n $ foldr (SPi Visible) SType ps ]
1733 ++ [ FunAlt n (map noTA ps) Nothing $ foldr ST2 (SGlobal (fst n', "'Unit")) cstrs | Instance n' ps cstrs _ <- ds, n == n' ] 1726 ++ [ FunAlt n (map noTA ps) Nothing $ foldr ST2 (SBuiltin "'Unit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ]
1734 ++ [ FunAlt n (map noTA $ replicate (length ps) (PVar (debugSI "compileFunAlts1", "generated_name0"))) Nothing $ SGlobal (debugSI "compileFunAlts2","'Empty") `SAppV` sLit noSI (LString $ "no instance of " ++ snd n ++ " on ???")] 1727 ++ [ FunAlt n (map noTA $ replicate (length ps) (PVar (debugSI "compileFunAlts1", "generated_name0"))) Nothing $ SBuiltin "'Empty" `SAppV` sLit (LString $ "no instance of " ++ snd n ++ " on ???")]
1735 ++ concat 1728 ++ concat
1736 [ TypeAnn m (addParamsS (map ((,) Hidden) ps) $ SPi Hidden (foldl SAppV (SGlobal n) $ downToS 0 $ length ps) $ upS t) 1729 [ TypeAnn m (addParamsS (map ((,) Hidden) ps) $ SPi Hidden (foldl SAppV (SGlobal n) $ downToS 0 $ length ps) $ upS t)
1737 : [ FunAlt m p Nothing $ {- SLam Hidden (Wildcard SType) $ upS $ -} substS 0 (Var ic) $ upS__ (ic+1) 1 e 1730 : [ FunAlt m p Nothing $ {- SLam Hidden (Wildcard SType) $ upS $ -} substS 0 (Var ic) $ upS__ (ic+1) 1 e
@@ -1771,12 +1764,12 @@ parseDef :: Namespace -> P [Stmt]
1771parseDef ns = 1764parseDef ns =
1772 do reserved "data" 1765 do reserved "data"
1773 localIndentation Gt $ do 1766 localIndentation Gt $ do
1774 (si,x) <- withSI $ upperCase (typeNS ns) 1767 x <- withSI $ upperCase (typeNS ns)
1775 (npsd, ts) <- telescope (typeNS ns) (Just SType) 1768 (npsd, ts) <- telescope (typeNS ns) (Just SType)
1776 t <- dbf' npsd <$> parseType (typeNS ns) (Just SType) 1769 t <- dbf' npsd <$> parseType (typeNS ns) (Just SType)
1777 let mkConTy mk (nps', ts') = 1770 let mkConTy mk (nps', ts') =
1778 ( if mk then Just nps' else Nothing 1771 ( if mk then Just nps' else Nothing
1779 , foldr (uncurry SPi) (foldl SAppV (SGlobal (si,x)) $ downToS (length ts') $ length ts) ts') 1772 , foldr (uncurry SPi) (foldl SAppV (SGlobal x) $ downToS (length ts') $ length ts) ts')
1780 (af, cs) <- 1773 (af, cs) <-
1781 do (,) True <$ reserved "where" <*> localIndentation Ge (localAbsoluteIndentation $ many $ 1774 do (,) True <$ reserved "where" <*> localIndentation Ge (localAbsoluteIndentation $ many $
1782 (id *** (,) Nothing . dbf' npsd) <$> typedIds ns Nothing) 1775 (id *** (,) Nothing . dbf' npsd) <$> typedIds ns Nothing)
@@ -1787,7 +1780,7 @@ parseDef ns =
1787 (reservedOp "|") 1780 (reservedOp "|")
1788 <|> pure (True, []) 1781 <|> pure (True, [])
1789 ge <- ask 1782 ge <- ask
1790 return $ mkData ge (si,x) ts t af $ concatMap (\(vs, t) -> (,) <$> vs <*> pure t) $ cs 1783 return $ mkData ge x ts t af $ concatMap (\(vs, t) -> (,) <$> vs <*> pure t) $ cs
1791 <|> do reserved "class" 1784 <|> do reserved "class"
1792 localIndentation Gt $ do 1785 localIndentation Gt $ do
1793 x <- withSI $ upperCase (typeNS ns) 1786 x <- withSI $ upperCase (typeNS ns)
@@ -1800,36 +1793,36 @@ parseDef ns =
1800 let ns' = typeNS ns 1793 let ns' = typeNS ns
1801 localIndentation Gt $ do 1794 localIndentation Gt $ do
1802 constraints <- option [] $ try $ getTTuple' <$> parseTerm ns' PrecEq <* reservedOp "=>" 1795 constraints <- option [] $ try $ getTTuple' <$> parseTerm ns' PrecEq <* reservedOp "=>"
1803 (si,x) <- withSI $ upperCase ns' 1796 x <- withSI $ upperCase ns'
1804 (nps, args) <- telescope' ns' 1797 (nps, args) <- telescope' ns'
1805 cs <- option [] $ reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ some $ 1798 cs <- option [] $ reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ some $
1806 dbFunAlt nps <$> funAltDef (varId ns) ns) 1799 dbFunAlt nps <$> funAltDef (varId ns) ns)
1807 <|> pure [] 1800 <|> pure []
1808 ge <- ask 1801 ge <- ask
1809 return $ pure $ Instance (si,x) ({-todo-}map snd args) (dbff (diffDBNames nps ++ [x]) <$> constraints) $ compileFunAlts' id{-TODO-} ge cs 1802 return $ pure $ Instance x ({-todo-}map snd args) (dbff (diffDBNames nps ++ [snd x]) <$> constraints) $ compileFunAlts' id{-TODO-} ge cs
1810 <|> do try (reserved "type" >> reserved "family") 1803 <|> do try (reserved "type" >> reserved "family")
1811 let ns' = typeNS ns 1804 let ns' = typeNS ns
1812 localIndentation Gt $ do 1805 localIndentation Gt $ do
1813 (si, x) <- withSI $ upperCase ns' 1806 x <- withSI $ upperCase ns'
1814 (nps, ts) <- telescope ns' (Just SType) 1807 (nps, ts) <- telescope ns' (Just SType)
1815 t <- dbf' nps <$> parseType ns' (Just SType) 1808 t <- dbf' nps <$> parseType ns' (Just SType)
1816 option {-open type family-}[TypeFamily (si, x) ts t] $ do 1809 option {-open type family-}[TypeFamily x ts t] $ do
1817 cs <- reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ many $ 1810 cs <- reserved "where" *> localIndentation Ge (localAbsoluteIndentation $ many $
1818 funAltDef (upperCase ns' >>= \x' -> guard (x == x') >> return x') ns') 1811 funAltDef (upperCase ns' >>= \x' -> guard (snd x == x') >> return x') ns')
1819 ge <- ask 1812 ge <- ask
1820 -- closed type family desugared here 1813 -- closed type family desugared here
1821 return $ compileFunAlts False id SLabelEnd ge [TypeAnn (si, x) $ addParamsS ts t] cs 1814 return $ compileFunAlts False id SLabelEnd ge [TypeAnn x $ addParamsS ts t] cs
1822 <|> do reserved "type" 1815 <|> do reserved "type"
1823 let ns' = typeNS ns 1816 let ns' = typeNS ns
1824 localIndentation Gt $ do 1817 localIndentation Gt $ do
1825 (si, x) <- withSI $ upperCase ns' 1818 x <- withSI $ upperCase ns'
1826 (nps, ts) <- telescopeSI ns' (Just (Wildcard SType)) 1819 (nps, ts) <- telescopeSI ns' (Just (Wildcard SType))
1827 reservedOp "=" 1820 reservedOp "="
1828 rhs <- dbf' (DBNamesC nps) <$> parseTerm ns' PrecLam 1821 rhs <- dbf' (DBNamesC nps) <$> parseTerm ns' PrecLam
1829 ge <- ask 1822 ge <- ask
1830 return $ compileFunAlts False id SLabelEnd ge 1823 return $ compileFunAlts False id SLabelEnd ge
1831 [{-TypeAnn (si, x) $ addParamsS ts $ SType-}{-todo-}] 1824 [{-TypeAnn x $ addParamsS ts $ SType-}{-todo-}]
1832 [FunAlt (si, x) (reverse $ zip (reverse ts) $ map PVar nps) Nothing rhs] 1825 [FunAlt x (reverse $ zip (reverse ts) $ map PVar nps) Nothing rhs]
1833 <|> do try (reserved "type" >> reserved "instance") 1826 <|> do try (reserved "type" >> reserved "instance")
1834 let ns' = typeNS ns 1827 let ns' = typeNS ns
1835 pure <$> localIndentation Gt (funAltDef (upperCase ns') ns') 1828 pure <$> localIndentation Gt (funAltDef (upperCase ns') ns')
@@ -1866,9 +1859,9 @@ dbFunAlt v (FunAlt n ts gu e) = FunAlt n (map (id *** mapP (dbf' v)) ts) (dbf' v
1866 1859
1867mkData ge x ts t af cs = [Data x ts t af $ (id *** snd) <$> cs] ++ concatMap mkProj cs 1860mkData ge x ts t af cs = [Data x ts t af $ (id *** snd) <$> cs] ++ concatMap mkProj cs
1868 where 1861 where
1869 mkProj ((si,cn), (Just fs, _)) 1862 mkProj (cn, (Just fs, _))
1870 = [ Let fn Nothing Nothing [Visible] 1863 = [ Let fn Nothing Nothing [Visible]
1871 $ upS{-non-rec-} $ patLam si SLabelEnd ge (PCon (si,cn) $ replicate (length fs) $ ParPat [PVar (si, "generated_name1")]) $ SVar (si, ".proj") i 1864 $ upS{-non-rec-} $ patLam (fst cn) SLabelEnd ge (PCon cn $ replicate (length fs) $ ParPat [PVar (fst cn, "generated_name1")]) $ SVar (fst cn, ".proj") i
1872 | (i, fn) <- zip [0..] fs] 1865 | (i, fn) <- zip [0..] fs]
1873 mkProj _ = [] 1866 mkProj _ = []
1874 1867
@@ -1895,82 +1888,81 @@ valueDef ns = do
1895 ex <- parseETerm ns PrecLam 1888 ex <- parseETerm ns PrecLam
1896 return ((e', p), ex) 1889 return ((e', p), ex)
1897 1890
1898sapp a (v, b) = SApp (debugSI "sapp") v a b
1899
1900parseTTerm ns = parseTerm $ typeNS ns 1891parseTTerm ns = parseTerm $ typeNS ns
1901parseETerm ns = parseTerm $ expNS ns 1892parseETerm ns = parseTerm $ expNS ns
1902 1893
1903parseTerm :: Namespace -> Prec -> P SExp 1894parseTerm :: Namespace -> Prec -> P SExp
1904parseTerm ns PrecLam = 1895parseTerm ns prec = withRange setSI $ case prec of
1905 mkIf `withRange` ((,,) <$ reserved "if" <*> parseTerm ns PrecLam <* reserved "then" <*> parseTerm ns PrecLam <* reserved "else" <*> parseTerm ns PrecLam) 1896 PrecLam ->
1906 <|> do (tok, ns) <- (SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->", typeNS ns) <$ reserved "forall" 1897 mkIf <$ reserved "if" <*> parseTerm ns PrecLam <* reserved "then" <*> parseTerm ns PrecLam <* reserved "else" <*> parseTerm ns PrecLam
1907 (fe, ts) <- telescope ns (Just $ Wildcard SType) 1898 <|> do (tok, ns) <- (SPi . const Hidden <$ reservedOp "." <|> SPi . const Visible <$ reservedOp "->", typeNS ns) <$ reserved "forall"
1908 f <- tok 1899 (fe, ts) <- telescope ns (Just $ Wildcard SType)
1909 t' <- dbf' fe <$> parseTerm ns PrecLam
1910 return $ foldr (uncurry f) t' ts
1911 <|> do appRange $ do
1912 (tok, ns) <- (asks (\a r -> patLam_ r id a) <* reservedOp "->", expNS ns) <$ reservedOp "\\"
1913 (fe, ts) <- telescope' ns
1914 f <- tok 1900 f <- tok
1915 t' <- dbf' fe <$> parseTerm ns PrecLam 1901 t' <- dbf' fe <$> parseTerm ns PrecLam
1916 return $ \r -> foldr (uncurry (f r)) t' ts 1902 return $ foldr (uncurry f) t' ts
1917 <|> do (asks compileCase) <* reserved "case" <*> parseETerm ns PrecLam 1903 <|> do appRange $ do
1918 <* reserved "of" <*> localIndentation Ge (localAbsoluteIndentation $ some $ parseClause ns) 1904 (tok, ns) <- (asks (\a r -> patLam_ r id a) <* reservedOp "->", expNS ns) <$ reservedOp "\\"
1919 <|> do (asks $ \ge -> compileGuardTree id id ge . Alts) <*> parseSomeGuards ns (const True) 1905 (fe, ts) <- telescope' ns
1920 <|> do t <- parseTerm ns PrecEq 1906 f <- tok
1921 option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm ns PrecLam 1907 t' <- dbf' fe <$> parseTerm ns PrecLam
1922parseTerm ns PrecEq = parseTerm ns PrecAnn >>= \t -> option t $ SCstr t <$ reservedOp "~" <*> parseTTerm ns PrecAnn 1908 return $ \r -> foldr (uncurry (f r)) t' ts
1923parseTerm ns PrecAnn = parseTerm ns PrecOp >>= \t -> option t $ SAnn t <$> parseType ns Nothing 1909 <|> do (asks compileCase) <* reserved "case" <*> parseETerm ns PrecLam
1924parseTerm ns PrecOp = asks calculatePrecs <*> p' where 1910 <* reserved "of" <*> localIndentation Ge (localAbsoluteIndentation $ some $ parseClause ns)
1925 p' = ((\si (t, xs) -> (mkNat ns si 0, (SGlobal (debugSI "12", "-"), t): xs)) `withRange` (reservedOp "-" *> p_)) 1911 <|> do (asks $ \ge -> compileGuardTree id id ge . Alts) <*> parseSomeGuards ns (const True)
1926 <|> p_ 1912 <|> do t <- parseTerm ns PrecEq
1927 p_ = (,) <$> parseTerm ns PrecApp <*> (option [] $ sVar operatorT >>= p) 1913 option t $ mkPi <$> (Visible <$ reservedOp "->" <|> Hidden <$ reservedOp "=>") <*> pure t <*> parseTTerm ns PrecLam
1928 p op = do (exp, op') <- try ((,) <$> parseTerm ns PrecApp <*> sVar operatorT) 1914 PrecEq -> parseTerm ns PrecAnn >>= \t -> option t $ SCstr t <$ reservedOp "~" <*> parseTTerm ns PrecAnn
1929 ((op, exp):) <$> p op' 1915 PrecAnn -> parseTerm ns PrecOp >>= \t -> option t $ SAnn t <$> parseType ns Nothing
1930 <|> pure . (,) op <$> parseTerm ns PrecLam 1916 PrecOp -> asks calculatePrecs <*> p' where
1931parseTerm ns PrecApp = 1917 p' = ((\si (t, xs) -> (mkNat ns 0, (SBuiltin "-", t): xs)) `withRange` (reservedOp "-" *> p_))
1932 try {- TODO: adjust try for better error messages e.g. don't use braces -} 1918 <|> p_
1933 (foldl sapp <$> sVar (upperCase ns) <*> braces (commaSep $ lowerCase ns *> reservedOp "=" *> ((,) Visible <$> parseTerm ns PrecLam))) <|> 1919 p_ = (,) <$> parseTerm ns PrecApp <*> (option [] $ sVar operatorT >>= p)
1934 (foldl sapp <$> parseTerm ns PrecSwiz <*> many 1920 p op = do (exp, op') <- try ((,) <$> parseTerm ns PrecApp <*> sVar operatorT)
1935 ( (,) Visible <$> parseTerm ns PrecSwiz 1921 ((op, exp):) <$> p op'
1936 <|> (,) Hidden <$ reservedOp "@" <*> parseTTerm ns PrecSwiz)) 1922 <|> pure . (,) op <$> parseTerm ns PrecLam
1937parseTerm ns PrecSwiz = do 1923 PrecApp ->
1938 t <- parseTerm ns PrecProj 1924 try {- TODO: adjust try for better error messages e.g. don't use braces -}
1939 try (mkSwizzling t `withRange` (char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: [Char]))) <* whiteSpace)) <|> pure t 1925 (apps' <$> sVar (upperCase ns) <*> braces (commaSep $ lowerCase ns *> reservedOp "=" *> ((,) Visible <$> parseTerm ns PrecLam))) <|>
1940parseTerm ns PrecProj = do 1926 (apps' <$> parseTerm ns PrecSwiz <*> many
1941 t <- parseTerm ns PrecAtom 1927 ( (,) Visible <$> parseTerm ns PrecSwiz
1942 try (mkProjection t <$ char '.' <*> (sepBy1 (sLit `withRange` (LString <$> lowerCase ns)) (char '.'))) <|> pure t 1928 <|> (,) Hidden <$ reservedOp "@" <*> parseTTerm ns PrecSwiz))
1943parseTerm ns PrecAtom = 1929 PrecSwiz -> do
1944 sLit `withRange` (LChar <$> try charLiteral) 1930 t <- parseTerm ns PrecProj
1945 <|> sLit `withRange` (LString <$> stringLiteral) 1931 try (mkSwizzling t `withRange` (char '%' *> manyNM 1 4 (satisfy (`elem` ("xyzwrgba" :: [Char]))) <* whiteSpace)) <|> pure t
1946 <|> sLit `withRange` (LFloat <$> try float) 1932 PrecProj -> do
1947 <|> sLit `withRange` (LInt . fromIntegral <$ char '#' <*> natural) 1933 t <- parseTerm ns PrecAtom
1948 <|> mkNat ns `withRange` natural 1934 try (mkProjection t <$ char '.' <*> (sepBy1 (sLit . LString <$> lowerCase ns) (char '.'))) <|> pure t
1949 <|> Wildcard (Wildcard SType) <$ reserved "_" 1935 PrecAtom ->
1950 <|> char '\'' *> parseTerm (switchNS ns) PrecAtom 1936 sLit . LChar <$> try charLiteral
1951 <|> sVar (try (varId ns) <|> upperCase ns) 1937 <|> sLit . LString <$> stringLiteral
1952 <|> mkDotDot <$> try (reservedOp "[" *> parseTerm ns PrecLam <* reservedOp ".." ) <*> parseTerm ns PrecLam <* reservedOp "]" 1938 <|> sLit . LFloat <$> try float
1953 <|> listCompr ns 1939 <|> sLit . LInt . fromIntegral <$ char '#' <*> natural
1954 <|> mkList ns `withRange` brackets (commaSep $ parseTerm ns PrecLam) 1940 <|> mkNat ns <$> natural
1955 <|> mkLeftSection `withRange` try{-todo: better try-} (parens $ (,) <$> withSI (guardM (/= "-") operatorT) <*> parseTerm ns PrecLam) 1941 <|> Wildcard (Wildcard SType) <$ reserved "_"
1956 <|> mkRightSection `withRange` try{-todo: better try!-} (parens $ (,) <$> parseTerm ns PrecApp <*> withSI operatorT) 1942 <|> char '\'' *> parseTerm (switchNS ns) PrecAtom
1957 <|> mkTuple ns `withRange` parens (commaSep $ parseTerm ns PrecLam) 1943 <|> sVar (try (varId ns) <|> upperCase ns)
1958 <|> mkRecord `withRange` braces (commaSep $ ((,) <$> lowerCase ns <* colon <*> parseTerm ns PrecLam)) 1944 <|> mkDotDot <$> try (reservedOp "[" *> parseTerm ns PrecLam <* reservedOp ".." ) <*> parseTerm ns PrecLam <* reservedOp "]"
1959 <|> do reserved "let" 1945 <|> listCompr ns
1960 dcls <- localIndentation Ge (localAbsoluteIndentation $ parseDefs id ns) 1946 <|> mkList ns <$> brackets (commaSep $ parseTerm ns PrecLam)
1961 ge <- ask 1947 <|> mkLeftSection <$> try{-todo: better try-} (parens $ (,) <$> withSI (guardM (/= "-") operatorT) <*> parseTerm ns PrecLam)
1962 mkLets ge dcls <$ reserved "in" <*> parseTerm ns PrecLam 1948 <|> mkRightSection <$> try{-todo: better try!-} (parens $ (,) <$> parseTerm ns PrecApp <*> withSI operatorT)
1949 <|> mkTuple ns <$> parens (commaSep $ parseTerm ns PrecLam)
1950 <|> mkRecord <$> braces (commaSep $ ((,) <$> lowerCase ns <* colon <*> parseTerm ns PrecLam))
1951 <|> do reserved "let"
1952 dcls <- localIndentation Ge (localAbsoluteIndentation $ parseDefs id ns)
1953 ge <- ask
1954 mkLets ge dcls <$ reserved "in" <*> parseTerm ns PrecLam
1963 1955
1964guardM 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"
1965 1957
1966mkLeftSection si (op, e) = SLam_ si Visible (Wildcard SType) $ SGlobal op `SAppV` SVar (si, ".ls") 0 `SAppV` upS e 1958mkLeftSection (op, e) = SLam Visible (Wildcard SType) $ SGlobal op `SAppV` SVar (noSI, ".ls") 0 `SAppV` upS e
1967mkRightSection si (e, op) = SLam_ si Visible (Wildcard SType) $ SGlobal op `SAppV` upS e `SAppV` SVar (si, ".rs") 0 1959mkRightSection (e, op) = SLam Visible (Wildcard SType) $ SGlobal op `SAppV` upS e `SAppV` SVar (noSI, ".rs") 0
1968 1960
1969mkSwizzling term si = swizzcall 1961mkSwizzling term si = swizzcall
1970 where 1962 where
1971 sc c = SGlobal (si,'S':c:[]) 1963 sc c = SGlobal (si,'S':c:[])
1972 swizzcall [x] = (SAppVSI si (SGlobal (si,"swizzscalar")) term) `SAppV` (sc . synonym) x 1964 swizzcall [x] = SBuiltin "swizzscalar" `SAppV` term `SAppV` (sc . synonym) x
1973 swizzcall xs = (SAppVSI si (SGlobal (si,"swizzvector")) term) `SAppV` swizzparam xs 1965 swizzcall xs = SBuiltin "swizzvector" `SAppV` term `SAppV` swizzparam xs
1974 swizzparam xs = foldl (\exp s -> exp `SAppV` s) (vec xs) $ map (sc . synonym) xs 1966 swizzparam xs = foldl (\exp s -> exp `SAppV` s) (vec xs) $ map (sc . synonym) xs
1975 vec xs = SGlobal $ (,) si $ case length xs of 1967 vec xs = SGlobal $ (,) si $ case length xs of
1976 0 -> error "impossible: swizzling parsing returned empty pattern" 1968 0 -> error "impossible: swizzling parsing returned empty pattern"
@@ -1982,37 +1974,37 @@ mkSwizzling term si = swizzcall
1982 synonym 'a' = 'w' 1974 synonym 'a' = 'w'
1983 synonym c = c 1975 synonym c = c
1984 1976
1985mkProjection term = foldl (\exp field -> SGlobal (debugSI "mkProjection", "project") `SAppV` field `SAppV` exp) term 1977mkProjection term = foldl (\exp field -> SBuiltin "project" `SAppV` field `SAppV` exp) term
1986 1978
1987-- Creates: RecordCons @[("x", _), ("y", _), ("z", _)] (1.0, (2.0, (3.0, ()))) 1979-- Creates: RecordCons @[("x", _), ("y", _), ("z", _)] (1.0, (2.0, (3.0, ())))
1988mkRecord si xs = SAppVSI si (SGlobal (noSI, "RecordCons") `SAppH` names) values 1980mkRecord xs = SBuiltin "RecordCons" `SAppH` names `SAppV` values
1989 where 1981 where
1990 (names, values) = mkNames *** mkValues $ unzip xs 1982 (names, values) = mkNames *** mkValues $ unzip xs
1991 1983
1992 mkNameTuple v = SGlobal (noSI,"Tuple2") `SAppV` (sLit noSI $ LString v) `SAppV` Wildcard SType 1984 mkNameTuple v = SBuiltin "Tuple2" `SAppV` (sLit $ LString v) `SAppV` Wildcard SType
1993 mkNames = foldr (\n ns -> SGlobal (noSI,"Cons") `SAppV` (mkNameTuple n) `SAppV` ns) 1985 mkNames = foldr (\n ns -> SBuiltin "Cons" `SAppV` (mkNameTuple n) `SAppV` ns)
1994 (SGlobal (noSI,"Nil")) 1986 (SBuiltin "Nil")
1995 1987
1996 mkValues = foldr (\x xs -> SGlobal (noSI,"Tuple2") `SAppV` x `SAppV` xs) 1988 mkValues = foldr (\x xs -> SBuiltin "Tuple2" `SAppV` x `SAppV` xs)
1997 (SGlobal (noSI,"Tuple0")) 1989 (SBuiltin "Tuple0")
1998 1990
1999mkTuple _ si [x] = x 1991mkTuple _ [x] = x
2000mkTuple (Namespace level _) si xs = foldl SAppV (SGlobal (si, tuple ++ show (length xs))) xs 1992mkTuple (Namespace level _) xs = foldl SAppV (SBuiltin (tuple ++ show (length xs))) xs
2001 where tuple = levelElim "'Tuple" "Tuple" level 1993 where tuple = levelElim "'Tuple" "Tuple" level
2002mkTuple _ si xs = error "mkTuple" 1994mkTuple _ xs = error "mkTuple"
2003 1995
2004mkList (Namespace TypeLevel _) si [x] = SAppVSI si (SGlobal (noSI,"'List")) x 1996mkList (Namespace TypeLevel _) [x] = SBuiltin "'List" `SAppV` x
2005mkList (Namespace ExpLevel _) si xs = foldr (\x l -> SAppVSI si (SGlobal (noSI,"Cons") `SAppV` x) l) (SGlobal (noSI,"Nil")) xs 1997mkList (Namespace ExpLevel _) xs = foldr (\x l -> SBuiltin "Cons" `SAppV` x `SAppV` l) (SBuiltin "Nil") xs
2006mkList _ si xs = error "mkList" 1998mkList _ xs = error "mkList"
2007 1999
2008mkNat (Namespace ExpLevel _) si n = SGlobal (noSI,"fromInt") `SAppV` sLit si (LInt $ fromIntegral n) 2000mkNat (Namespace ExpLevel _) n = SBuiltin "fromInt" `SAppV` sLit (LInt $ fromIntegral n)
2009mkNat _ _ n = toNat n 2001mkNat _ n = toNat n
2010 2002
2011withSI = withRange (,) 2003withSI = withRange (,)
2012 2004
2013mkIf si (b,t,f) = SGlobal (si,"primIfThenElse") `SAppV` b `SAppV` t `SAppV` f 2005mkIf b t f = SBuiltin "primIfThenElse" `SAppV` b `SAppV` t `SAppV` f
2014 2006
2015mkDotDot e f = SGlobal (noSI,"fromTo") `SAppV` e `SAppV` f 2007mkDotDot e f = SBuiltin "fromTo" `SAppV` e `SAppV` f
2016 2008
2017-- n, m >= 1, n < m 2009-- n, m >= 1, n < m
2018manyNM n m p = do 2010manyNM n m p = do
@@ -2029,10 +2021,10 @@ generator ns dbs = do
2029 exp <- dbf' dbs <$> parseTerm ns PrecLam 2021 exp <- dbf' dbs <$> parseTerm ns PrecLam
2030 ge <- ask 2022 ge <- ask
2031 return $ (,) (dbs' <> dbs) $ \e -> application 2023 return $ (,) (dbs' <> dbs) $ \e -> application
2032 [ SGlobal (noSI, "concatMap") 2024 [ SBuiltin "concatMap"
2033 , SLamV $ compileGuardTree id id ge $ Alts 2025 , SLamV $ compileGuardTree id id ge $ Alts
2034 [ compilePatts [(mapP (dbf' dbs) pat, 0)] Nothing $ {-upS $ -} e 2026 [ compilePatts [(mapP (dbf' dbs) pat, 0)] Nothing $ {-upS $ -} e
2035 , GuardLeaf $ SGlobal (noSI, "Nil") 2027 , GuardLeaf $ SBuiltin "Nil"
2036 ] 2028 ]
2037 , exp 2029 , exp
2038 ] 2030 ]
@@ -2041,13 +2033,13 @@ letdecl ns dbs = ask >>= \ge -> reserved "let" *> ((\((dbs', p), e) -> (dbs, \ex
2041 2033
2042boolExpression ns dbs = do 2034boolExpression ns dbs = do
2043 pred <- dbf' dbs <$> parseTerm ns PrecLam 2035 pred <- dbf' dbs <$> parseTerm ns PrecLam
2044 return (dbs, \e -> application [SGlobal (noSI,"primIfThenElse"), pred, e, SGlobal (noSI,"Nil")]) 2036 return (dbs, \e -> application [SBuiltin "primIfThenElse", pred, e, SBuiltin "Nil"])
2045 2037
2046application = foldl1 SAppV 2038application = foldl1 SAppV
2047 2039
2048listCompr :: Namespace -> P SExp 2040listCompr :: Namespace -> P SExp
2049listCompr ns = (\e (dbs', fs) -> foldr ($) (dbff (diffDBNames dbs') e) fs) 2041listCompr ns = (\e (dbs', fs) -> foldr ($) (dbff (diffDBNames dbs') e) fs)
2050 <$> try' "List comprehension" ((SGlobal (noSI, "singleton") `SAppV`) <$ reservedOp "[" <*> parseTerm ns PrecLam <* reservedOp "|") 2042 <$> try' "List comprehension" ((SBuiltin "singleton" `SAppV`) <$ reservedOp "[" <*> parseTerm ns PrecLam <* reservedOp "|")
2051 <*> commaSepUnfold (liftA2 (<|>) (generator ns) $ liftA2 (<|>) (letdecl ns) (boolExpression ns)) mempty <* reservedOp "]" 2043 <*> commaSepUnfold (liftA2 (<|>) (generator ns) $ liftA2 (<|>) (letdecl ns) (boolExpression ns)) mempty <* reservedOp "]"
2052 2044
2053-- todo: make it more efficient 2045-- todo: make it more efficient
@@ -2170,8 +2162,8 @@ parseSomeGuards ns f = do
2170 f <$> ((map (dbfGT e') <$> parseSomeGuards ns (> pos)) <|> (:[]) . GuardLeaf <$ reservedOp "->" <*> (dbf' e' <$> parseETerm ns PrecLam)) 2162 f <$> ((map (dbfGT e') <$> parseSomeGuards ns (> pos)) <|> (:[]) . GuardLeaf <$ reservedOp "->" <*> (dbf' e' <$> parseETerm ns PrecLam))
2171 <*> (parseSomeGuards ns (== pos) <|> pure []) 2163 <*> (parseSomeGuards ns (== pos) <|> pure [])
2172 2164
2173toNat 0 = SGlobal (debugSI "toNat","Zero") 2165toNat 0 = SBuiltin "Zero"
2174toNat n | n > 0 = SAppV (SGlobal (debugSI "toNat","Succ")) $ toNat (n-1) 2166toNat n | n > 0 = SAppV (SBuiltin "Succ") $ toNat (n-1)
2175 2167
2176toNatP si = run where 2168toNatP si = run where
2177 run 0 = PCon (si, "Zero") [] 2169 run 0 = PCon (si, "Zero") []
@@ -2192,22 +2184,45 @@ defined defs = ("'Type":) $ flip foldMap defs $ \case
2192-------------------------------------------------------------------------------- 2184--------------------------------------------------------------------------------
2193 2185
2194class SourceInfo si where 2186class SourceInfo si where
2195 sourceInfo :: si -> SI 2187 sourceInfo :: si -> SI
2196 2188
2197instance SourceInfo SI where 2189instance SourceInfo SI where
2198 sourceInfo = id 2190 sourceInfo = id
2199 2191
2200instance SourceInfo si => SourceInfo [si] where 2192instance SourceInfo si => SourceInfo [si] where
2201 sourceInfo = mconcat . map sourceInfo 2193 sourceInfo = mconcat . map sourceInfo
2202 2194
2203instance SourceInfo ParPat where 2195instance SourceInfo ParPat where
2204 sourceInfo (ParPat ps) = sourceInfo ps 2196 sourceInfo (ParPat ps) = sourceInfo ps
2205 2197
2206instance SourceInfo Pat where 2198instance SourceInfo Pat where
2207 sourceInfo = patSI 2199 sourceInfo = \case
2200 PVar (si,_) -> si
2201 PCon (si,_) ps -> si <> sourceInfo ps
2202 ViewPat e ps -> sourceInfo e <> sourceInfo ps
2203 PatType ps e -> sourceInfo ps <> sourceInfo e
2204
2208 2205
2209instance SourceInfo SExp where 2206instance SourceInfo SExp where
2210 sourceInfo = sexpSI 2207 sourceInfo = \case
2208 SGlobal (si, _) -> si
2209 SBind si _ (si', _) e1 e2 -> si <> si' <> sourceInfo e1 <> sourceInfo e2 -- TODO: just si
2210 SApp si _ e1 e2 -> si
2211 SLet e1 e2 -> sourceInfo e1 <> sourceInfo e2
2212 SVar (si, _) _ -> si
2213 STyped si _ -> si
2214
2215class SetSourceInfo a where
2216 setSI :: SI -> a -> a
2217
2218instance SetSourceInfo SExp where
2219 setSI si = \case
2220 SBind _ a b c d -> SBind si a b c d
2221 SApp _ a b c -> SApp si a b c
2222 SLet a b -> SLet a b
2223 SVar (_, n) i -> SVar (si, n) i
2224 STyped _ t -> STyped si t
2225 SGlobal (_, n) -> SGlobal (si, n)
2211 2226
2212-------------------------------------------------------------------------------- 2227--------------------------------------------------------------------------------
2213 2228
@@ -2222,13 +2237,6 @@ data Pat
2222 | PatType ParPat SExp 2237 | PatType ParPat SExp
2223 deriving Show 2238 deriving Show
2224 2239
2225patSI :: Pat -> SI
2226patSI = \case
2227 PVar (si,_) -> si
2228 PCon (si,_) ps -> si <> sourceInfo ps
2229 ViewPat e ps -> sourceInfo e <> sourceInfo ps
2230 PatType ps e -> sourceInfo ps <> sourceInfo e
2231
2232data GuardTree 2240data GuardTree
2233 = GuardNode SExp SName [ParPat] GuardTree -- _ <- _ 2241 = GuardNode SExp SName [ParPat] GuardTree -- _ <- _
2234 | Alts [GuardTree] -- _ | _ 2242 | Alts [GuardTree] -- _ | _
@@ -2290,7 +2298,7 @@ compileGuardTree unode node adts t = (\x -> traceD (" ! :" ++ showSExp x) x) $
2290 where 2298 where
2291 guardTreeToCases :: GuardTree -> SExp 2299 guardTreeToCases :: GuardTree -> SExp
2292 guardTreeToCases t = case alts t of 2300 guardTreeToCases t = case alts t of
2293 [] -> unode $ SGlobal (debugSI "guardTreeToCases1", "undefined") 2301 [] -> unode $ SBuiltin "undefined"
2294 GuardLeaf e: _ -> node e 2302 GuardLeaf e: _ -> node e
2295 ts@(GuardNode f s _ _: _) -> case Map.lookup s (snd adts) of 2303 ts@(GuardNode f s _ _: _) -> case Map.lookup s (snd adts) of
2296 Nothing -> error $ "Constructor is not defined: " ++ s 2304 Nothing -> error $ "Constructor is not defined: " ++ s
@@ -2454,7 +2462,7 @@ envDoc x m = case x of
2454 EAssign i x ts -> envDoc ts $ shLet i (expDoc x) m 2462 EAssign i x ts -> envDoc ts $ shLet i (expDoc x) m
2455 CheckType t ts -> envDoc ts $ shAnn ":" False <$> m <*> expDoc t 2463 CheckType t ts -> envDoc ts $ shAnn ":" False <$> m <*> expDoc t
2456 CheckSame t ts -> envDoc ts $ shCstr <$> m <*> expDoc t 2464 CheckSame t ts -> envDoc ts $ shCstr <$> m <*> expDoc t
2457 CheckAppType h t te b -> envDoc (EApp1 h (CheckType_ (sexpSI b) t te) b) m 2465 CheckAppType h t te b -> envDoc (EApp1 h (CheckType_ (sourceInfo b) t te) b) m
2458 ELabelEnd ts -> envDoc ts $ shApp Visible (shAtom "labEnd") <$> m 2466 ELabelEnd ts -> envDoc ts $ shApp Visible (shAtom "labEnd") <$> m
2459 2467
2460expDoc :: Exp -> Doc 2468expDoc :: Exp -> Doc