summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-11 20:57:07 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-12 00:50:34 +0200
commite4725c07ee3e7e3fc010df418d16f37c39b0af0f (patch)
treecb10e1d1203eed875955097311ccbe0943564226 /src
parent95e006bf5afa8d3473e3fe4401f4c9316186a428 (diff)
mutual function definitions
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Core.hs57
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs19
-rw-r--r--src/LambdaCube/Compiler/DesugaredSource.hs3
-rw-r--r--src/LambdaCube/Compiler/Infer.hs44
-rw-r--r--src/LambdaCube/Compiler/Parser.hs14
-rw-r--r--src/LambdaCube/Compiler/Pretty.hs1
-rw-r--r--src/LambdaCube/Compiler/Statements.hs41
7 files changed, 113 insertions, 66 deletions
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs
index 27bd6372..487cc4cc 100644
--- a/src/LambdaCube/Compiler/Core.hs
+++ b/src/LambdaCube/Compiler/Core.hs
@@ -35,7 +35,7 @@ data CaseFunName = CaseFunName FName Type Int{-num of parameters-}
35data TyCaseFunName = TyCaseFunName FName Type 35data TyCaseFunName = TyCaseFunName FName Type
36 36
37data FunDef 37data FunDef
38 = DeltaDef !Int{-arity-} ([Exp] -> Exp) 38 = DeltaDef !Int{-arity-} (FreeVars -> [Exp] -> Exp)
39 | NoDef 39 | NoDef
40 | ExpDef Exp 40 | ExpDef Exp
41 41
@@ -245,7 +245,7 @@ mkOrdering x = case x of
245conTypeName :: ConName -> TyConName 245conTypeName :: ConName -> TyConName
246conTypeName (ConName _ _ t) = case snd $ getParams t of TyCon n _ -> n 246conTypeName (ConName _ _ t) = case snd $ getParams t of TyCon n _ -> n
247 247
248mkFun_ _ (FunName _ _ (DeltaDef ar f) _) as _ | length as == ar = f $ reverse as 248mkFun_ md (FunName _ _ (DeltaDef ar f) _) as _ | length as == ar = f md $ reverse as
249mkFun_ md f xs y = Neut $ Fun_ md f xs $ hnf y 249mkFun_ md f xs y = Neut $ Fun_ md f xs $ hnf y
250 250
251mkFun :: FunName -> [Exp] -> Exp -> Exp 251mkFun :: FunName -> [Exp] -> Exp -> Exp
@@ -293,6 +293,7 @@ down t x | usedVar t x = Nothing
293 | otherwise = Just $ subst_ t mempty (error $ "impossible: down" ++ ppShow (t,x, getFreeVars x) :: Exp) x 293 | otherwise = Just $ subst_ t mempty (error $ "impossible: down" ++ ppShow (t,x, getFreeVars x) :: Exp) x
294 294
295instance Eq Exp where 295instance Eq Exp where
296 Neut a == Neut a' = a == a' -- try to compare by structure before reduction
296 Reduced a == a' = a == a' 297 Reduced a == a' = a == a'
297 a == Reduced a' = a == a' 298 a == Reduced a' = a == a'
298 Lam a == Lam a' = a == a' 299 Lam a == Lam a' = a == a'
@@ -301,12 +302,11 @@ instance Eq Exp where
301 TyCon a b == TyCon a' b' = (a, b) == (a', b') 302 TyCon a b == TyCon a' b' = (a, b) == (a', b')
302 TType_ f == TType_ f' = f == f' 303 TType_ f == TType_ f' = f == f'
303 ELit l == ELit l' = l == l' 304 ELit l == ELit l' = l == l'
304 Neut a == Neut a' = a == a'
305 RHS a == RHS a' = a == a' 305 RHS a == RHS a' = a == a'
306 _ == _ = False 306 _ == _ = False
307 307
308instance Eq Neutral where 308instance Eq Neutral where
309 Fun f a _ == Fun f' a' _ = (f, a) == (f', a') 309 Fun f a _ == Fun f' a' _ = (f, a) == (f', a') -- try to compare by structure before reduction
310 ReducedN a == a' = a == Neut a' 310 ReducedN a == a' = a == Neut a'
311 a == ReducedN a' = Neut a == a' 311 a == ReducedN a' = Neut a == a'
312 CaseFun_ a b c == CaseFun_ a' b' c' = (a, b, c) == (a', b', c') 312 CaseFun_ a b c == CaseFun_ a' b' c' = (a, b, c) == (a', b', c')
@@ -323,7 +323,7 @@ instance Subst Exp Exp where
323 substNeut e | dbGE i e = Neut e 323 substNeut e | dbGE i e = Neut e
324 substNeut e = case e of 324 substNeut e = case e of
325 Var_ k -> case compare k i of GT -> Var $ k - 1; LT -> Var k; EQ -> up (i - i0) x 325 Var_ k -> case compare k i of GT -> Var $ k - 1; LT -> Var k; EQ -> up (i - i0) x
326 CaseFun_ s as n -> evalCaseFun s (f i <$> as) (substNeut n) 326 CaseFun__ fs s as n -> evalCaseFun (adjustDB i fs) s (f i <$> as) (substNeut n)
327 TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n) 327 TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n)
328 App_ a b -> app_ (substNeut a) (f i b) 328 App_ a b -> app_ (substNeut a) (f i b)
329 Fun_ md fn xs v -> mkFun_ (adjustDB i md) fn (f i <$> xs) $ f i v 329 Fun_ md fn xs v -> mkFun_ (adjustDB i md) fn (f i <$> xs) $ f i v
@@ -426,14 +426,26 @@ showNth n = show n ++ f (n `div` 10 `mod` 10) (n `mod` 10)
426 f _ 3 = "rd" 426 f _ 3 = "rd"
427 f _ _ = "th" 427 f _ _ = "th"
428 428
429pattern FFix f <- Fun (FunName FprimFix _ _ _) [f, _] _
430
431getFixLam (Lam (Neut (Fun s@(FunName _ loc _ _) xs _)))
432 | loc > 0
433 , (h, v) <- splitAt loc $ reverse xs
434 , Neut (Var_ 0) <- last h
435 = Just (s, v)
436getFixLam _ = Nothing
437
429instance MkDoc Neutral where 438instance MkDoc Neutral where
430 mkDoc pr@(reduce, body) = \case 439 mkDoc pr@(reduce, body) = \case
431 CstrT' t a b -> shCstr (mkDoc pr a) (mkDoc pr (ET b t)) 440 CstrT' t a b -> shCstr (mkDoc pr a) (mkDoc pr (ET b t))
432 Fun (FunName _ _ (ExpDef d) _) xs _ | body -> mkDoc (reduce, False) (foldl app_ d xs) 441 Fun (FunName _ _ (ExpDef d) _) xs _ | body -> mkDoc (reduce, False) (foldl app_ d $ reverse xs)
433 Fun (FunName _ _ (DeltaDef _ d) _) xs _ | body -> text "<<builtin>>" 442 FFix (getFixLam -> Just (s, xs)) | not body -> foldl DApp (pShow s) $ mkDoc pr <$> xs
434 Fun (FunName _ _ NoDef _) xs _ | body -> text "<<builtin>>" 443 FFix f {- | body -} -> foldl DApp "primFix" [{-pShow t -}"_", mkDoc pr f]
444 Fun (FunName _ _ (DeltaDef n _) _) _ _ | body -> text $ "<<delta function with arity " ++ show n ++ ">>"
445 Fun (FunName _ _ NoDef _) _ _ | body -> "<<builtin>>"
435 ReducedN a | reduce -> mkDoc pr a 446 ReducedN a | reduce -> mkDoc pr a
436 Fun s xs _ -> foldl DApp (pShow s) (mkDoc pr <$> reverse xs) 447 Fun s@(FunName _ loc _ _) xs _ -> foldl DApp ({-foldl DHApp (-}pShow s{-) h-}) v
448 where (h, v) = splitAt loc $ mkDoc pr <$> reverse xs
437 Var_ k -> shVar k 449 Var_ k -> shVar k
438 App_ a b -> mkDoc pr a `DApp` mkDoc pr b 450 App_ a b -> mkDoc pr a `DApp` mkDoc pr b
439 CaseFun_ s@(CaseFunName _ _ p) xs n | body -> text $ "<<case function of a type with " ++ show p ++ " parameters>>" 451 CaseFun_ s@(CaseFunName _ _ p) xs n | body -> text $ "<<case function of a type with " ++ show p ++ " parameters>>"
@@ -441,6 +453,7 @@ instance MkDoc Neutral where
441 TyCaseFun_ _ _ _ | body -> text "<<type case function>>" 453 TyCaseFun_ _ _ _ | body -> text "<<type case function>>"
442 TyCaseFun_ s [m, t, f] n -> foldl DApp (pShow s) (mkDoc pr <$> [m, t, Neut n, f]) 454 TyCaseFun_ s [m, t, f] n -> foldl DApp (pShow s) (mkDoc pr <$> [m, t, Neut n, f])
443 TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" 455 TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun"
456 _ -> "()"
444 457
445-------------------------------------------------------------------------------- reduction 458-------------------------------------------------------------------------------- reduction
446 459
@@ -455,9 +468,19 @@ instance MkDoc Neutral where
455-} 468-}
456 469
457pattern FunName' a t <- FunName a _ _ t 470pattern FunName' a t <- FunName a _ _ t
458 where FunName' a t = fn 471 where FunName' a t = mkFunDef a t
459 where 472
460 fn = FunName a 0 (maybe NoDef (DeltaDef (length $ fst $ getParams t)) $ getFunDef t a $ \xs -> Neut $ Fun fn (reverse xs) delta) t 473
474mkFunDef a@(show -> "primFix") t = fn
475 where
476 fn = FunName a 0 (DeltaDef (length $ fst $ getParams t) fx) t
477 fx s xs = Neut $ Fun_ s fn (reverse xs) $ case xs of
478 _: f: _ -> RHS x where x = f `app_` Neut (Fun_ s fn (reverse xs) $ RHS x)
479 _ -> delta
480
481mkFunDef a t = fn
482 where
483 fn = FunName a 0 (maybe NoDef (DeltaDef (length $ fst $ getParams t) . const) $ getFunDef t a $ \xs -> Neut $ Fun fn (reverse xs) delta) t
461 484
462getFunDef t s f = case show s of 485getFunDef t s f = case show s of
463 "'EqCT" -> Just $ \case (t: a: b: _) -> cstr t a b 486 "'EqCT" -> Just $ \case (t: a: b: _) -> cstr t a b
@@ -541,15 +564,17 @@ getFunDef t s f = case show s of
541 564
542 modF x y = x - fromIntegral (floor (x / y)) * y 565 modF x y = x - fromIntegral (floor (x / y)) * y
543 566
544evalCaseFun a ps (Con n@(ConName _ i _) _ vs) 567evalCaseFun _ a ps (Con n@(ConName _ i _) _ vs)
545 | i /= (-1) = foldl app_ (ps !!! (i + 1)) vs 568 | i /= (-1) = foldl app_ (ps !!! (i + 1)) vs
546 | otherwise = error "evcf" 569 | otherwise = error "evcf"
547 where 570 where
548 xs !!! i | i >= length xs = error $ "!!! " ++ ppShow a ++ " " ++ show i ++ " " ++ ppShow n ++ "\n" ++ ppShow ps 571 xs !!! i | i >= length xs = error $ "!!! " ++ ppShow a ++ " " ++ show i ++ " " ++ ppShow n ++ "\n" ++ ppShow ps
549 xs !!! i = xs !! i 572 xs !!! i = xs !! i
550evalCaseFun a b (Reduced c) = evalCaseFun a b c 573evalCaseFun fs a b (Reduced c) = evalCaseFun fs a b c
551evalCaseFun a b (Neut c) = CaseFun a b c 574evalCaseFun fs a b (Neut c) = Neut $ CaseFun__ fs a b c
552evalCaseFun a b x = error $ "evalCaseFun: " ++ ppShow (a, x) 575evalCaseFun _ a b x = error $ "evalCaseFun: " ++ ppShow (a, x)
576
577evalCaseFun' a b c = evalCaseFun (getFreeVars c <> foldMap getFreeVars b) a b c
553 578
554evalTyCaseFun a b (Reduced c) = evalTyCaseFun a b c 579evalTyCaseFun a b (Reduced c) = evalTyCaseFun a b c
555evalTyCaseFun a b (Neut c) = TyCaseFun a b c 580evalTyCaseFun a b (Neut c) = TyCaseFun a b c
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index d24a7b11..353560cd 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -865,7 +865,7 @@ mkLam _ = Nothing
865 865
866mkCon (ExpTV (I.Con s n xs) et vs) = Just (untick $ show s, chain vs (I.conType et s) $ I.mkConPars n et ++ xs) 866mkCon (ExpTV (I.Con s n xs) et vs) = Just (untick $ show s, chain vs (I.conType et s) $ I.mkConPars n et ++ xs)
867mkCon (ExpTV (I.TyCon s xs) et vs) = Just (untick $ show s, chain vs (nType s) xs) 867mkCon (ExpTV (I.TyCon s xs) et vs) = Just (untick $ show s, chain vs (nType s) xs)
868mkCon (ExpTV (I.Neut (I.Fun s@(I.FunName _ 0 _{-I.DeltaDef{}-} _) (reverse -> xs) def)) et vs) = Just (untick $ show s, chain vs (nType s) xs) 868mkCon (ExpTV (I.Neut (I.Fun s@(I.FunName _ loc _{-I.DeltaDef{}-} _) (reverse -> xs) def)) et vs) = Just (untick $ show s, drop loc $ chain vs (nType s) xs)
869mkCon (ExpTV (I.CaseFun s xs n) et vs) = Just (untick $ show s, chain vs (nType s) $ makeCaseFunPars' (mkEnv vs) n ++ xs ++ [I.Neut n]) 869mkCon (ExpTV (I.CaseFun s xs n) et vs) = Just (untick $ show s, chain vs (nType s) $ makeCaseFunPars' (mkEnv vs) n ++ xs ++ [I.Neut n])
870mkCon (ExpTV (I.TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, I.Neut n, f]) 870mkCon (ExpTV (I.TyCaseFun s [m, t, f] n) et vs) = Just (untick $ show s, chain vs (nType s) [m, t, I.Neut n, f])
871mkCon _ = Nothing 871mkCon _ = Nothing
@@ -878,15 +878,28 @@ removeRHS 0 (I.RHS x) = Just x
878removeRHS n (I.Lam x) | n > 0 = I.Lam <$> removeRHS (n-1) x 878removeRHS n (I.Lam x) | n > 0 = I.Lam <$> removeRHS (n-1) x
879removeRHS _ _ = Nothing 879removeRHS _ _ = Nothing
880 880
881mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) 0 (I.ExpDef def_) nt) xs I.RHS{})) ty vs) 881mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs I.RHS{})) ty vs)
882 | Just def <- removeRHS (length xs) def_ 882 | Just def <- removeRHS (length xs) def_
883 , all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n 883 , all (supType . tyOf) (r: xs') && n `notElem` ["typeAnn"] && all validChar n
884 = Just (untick n +++ intercalate "_" (filter (/="TT") $ map (filter isAlphaNum . plainShow) hs), toExp $ I.ET (foldl I.app_ def hs) (foldl I.appTy nt hs), tyOf r, xs') 884 = Just (untick n +++ intercalate "_" (filter (/="TT") $ map (filter isAlphaNum . plainShow) hs), toExp $ I.ET (foldl I.app_ def hs) (foldl I.appTy nt hs), tyOf r, xs')
885 where 885 where
886 a +++ [] = a 886 a +++ [] = a
887 a +++ b = a ++ "_" ++ b 887 a +++ b = a ++ "_" ++ b
888 (map (expOf . snd) -> hs, map snd -> xs') = span ((==Hidden) . fst) $ chain' vs nt $ reverse xs 888 (map (expOf . snd) -> hs, map snd -> xs') = splitAt loc $ chain' vs nt $ reverse xs
889 validChar = isAlphaNum 889 validChar = isAlphaNum
890{-
891mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs I.RHS{})) ty vs)
892 | Just def <- removeRHS (length xs) def_
893 , all validChar n
894 = tracePShow (text n, reverse xs, supType . tyOf <$> (r: xs')) Nothing
895 where
896 a +++ [] = a
897 a +++ b = a ++ "_" ++ b
898 (map (expOf . snd) -> hs, map snd -> xs') = splitAt loc $ chain' vs nt $ reverse xs
899 validChar = isAlphaNum
900mkFunc r@(ExpTV (I.Neut (I.Fun (I.FunName (show -> n) loc (I.ExpDef def_) nt) xs I.RHS{})) ty vs)
901 = tracePShow (text n, take loc $ reverse xs) Nothing
902-}
890mkFunc _ = Nothing 903mkFunc _ = Nothing
891 904
892chain vs t@(I.Pi Hidden at y) (a: as) = chain vs (I.appTy t a) as 905chain vs t@(I.Pi Hidden at y) (a: as) = chain vs (I.appTy t a) as
diff --git a/src/LambdaCube/Compiler/DesugaredSource.hs b/src/LambdaCube/Compiler/DesugaredSource.hs
index e83eb0ea..06849849 100644
--- a/src/LambdaCube/Compiler/DesugaredSource.hs
+++ b/src/LambdaCube/Compiler/DesugaredSource.hs
@@ -269,6 +269,9 @@ data SExp' a
269 | STyped a 269 | STyped a
270 deriving (Eq) 270 deriving (Eq)
271 271
272sLHS _ (SRHS x) = x
273sLHS n x = SLHS n x
274
272type SExp = SExp' Void 275type SExp = SExp' Void
273 276
274data Binder 277data Binder
diff --git a/src/LambdaCube/Compiler/Infer.hs b/src/LambdaCube/Compiler/Infer.hs
index 444dd1b9..089392d1 100644
--- a/src/LambdaCube/Compiler/Infer.hs
+++ b/src/LambdaCube/Compiler/Infer.hs
@@ -36,7 +36,6 @@ import LambdaCube.Compiler.Utils
36import LambdaCube.Compiler.DeBruijn 36import LambdaCube.Compiler.DeBruijn
37import LambdaCube.Compiler.Pretty hiding (braces, parens) 37import LambdaCube.Compiler.Pretty hiding (braces, parens)
38import LambdaCube.Compiler.DesugaredSource hiding (getList) 38import LambdaCube.Compiler.DesugaredSource hiding (getList)
39import LambdaCube.Compiler.Statements (addFix)
40import LambdaCube.Compiler.Core 39import LambdaCube.Compiler.Core
41import LambdaCube.Compiler.InferMonad 40import LambdaCube.Compiler.InferMonad
42 41
@@ -49,20 +48,15 @@ varType err n_ env = f n_ env where
49 f n (ELet2 _ (ET x t) es) = if n == 0 then (BLam Visible{-??-}, up 1 t) else second (up 1) $ f (n-1) es 48 f n (ELet2 _ (ET x t) es) = if n == 0 then (BLam Visible{-??-}, up 1 t) else second (up 1) $ f (n-1) es
50 f n e = either (error $ "varType: " ++ err ++ "\n" ++ show n_ ++ "\n" ++ ppShow env) (f n) $ parent e 49 f n e = either (error $ "varType: " ++ err ++ "\n" ++ show n_ ++ "\n" ++ ppShow env) (f n) $ parent e
51 50
52mkELet n x xt env = term 51mkELet n x xt env = mkFun fn (Var <$> reverse vs) x
53 where 52 where
54 vs = nub . concat $ grow [] mempty $ free x <> free xt
55 fn = FunName (mkFName n) (length vs) (ExpDef $ foldr addLam x vs) (foldr addPi xt vs) 53 fn = FunName (mkFName n) (length vs) (ExpDef $ foldr addLam x vs) (foldr addPi xt vs)
56 54
57 term = mkFun fn (Var <$> reverse vs) $ getFix x 0
58
59 getFix (Lam_ db z) i = Lam_ db $ getFix z (i+1)
60 getFix (DFun FprimFix _ [_, Lam f]) i = subst 0 (foldl app_ term (downTo 0 i)) f
61 getFix x _ = x
62
63 addLam v x = Lam $ rMove v 0 x 55 addLam v x = Lam $ rMove v 0 x
64 addPi v x = Pi Visible (snd $ varType "mkELet" v env) $ rMove v 0 x 56 addPi v x = Pi Visible (snd $ varType "mkELet" v env) $ rMove v 0 x
65 57
58 vs = nub . concat $ grow [] mempty $ free x <> free xt
59
66 -- TODO: avoid infinite loop if variable types refer each-other mutually 60 -- TODO: avoid infinite loop if variable types refer each-other mutually
67 grow accu acc s 61 grow accu acc s
68 | Set.null s = accu 62 | Set.null s = accu
@@ -101,7 +95,8 @@ envDoc x m = case x of
101 CheckIType t ts -> envDoc ts $ shAnn m (text "??") -- mkDoc ts' t 95 CheckIType t ts -> envDoc ts $ shAnn m (text "??") -- mkDoc ts' t
102-- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t 96-- CheckSame t ts -> envDoc ts $ shCstr <$> m <*> mkDoc ts' t
103 CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m 97 CheckAppType si h t te b -> envDoc (EApp1 si h (CheckType_ (sourceInfo b) t te) b) m
104 ERHS ts -> envDoc ts $ shApp Visible (text "rhs") m 98 ERHS ts -> envDoc ts $ shApp Visible "rhs" m
99 ELHS n ts -> envDoc ts $ shApp Visible ("lhs" `DApp` pShow n) m
105 x -> error $ "envDoc: " ++ ppShow x 100 x -> error $ "envDoc: " ++ ppShow x
106 101
107instance MkDoc a => MkDoc (CEnv a) where 102instance MkDoc a => MkDoc (CEnv a) where
@@ -162,6 +157,7 @@ data Env
162 | ELet2 SIName ExpType Env 157 | ELet2 SIName ExpType Env
163 | EGlobal 158 | EGlobal
164 | ERHS Env 159 | ERHS Env
160 | ELHS SIName Env
165 161
166 | EAssign Int ExpType Env 162 | EAssign Int ExpType Env
167 | CheckType_ SI Type Env 163 | CheckType_ SI Type Env
@@ -185,6 +181,7 @@ parent = \case
185-- CheckSame _ x -> Right x 181-- CheckSame _ x -> Right x
186 CheckAppType _ _ _ x _ -> Right x 182 CheckAppType _ _ _ x _ -> Right x
187 ERHS x -> Right x 183 ERHS x -> Right x
184 ELHS _ x -> Right x
188 EGlobal -> Left () 185 EGlobal -> Left ()
189 186
190-------------------------------------------------------------------------------- simple typing 187-------------------------------------------------------------------------------- simple typing
@@ -230,6 +227,8 @@ substTo i x = subst i x . up1_ (i+1)
230mkLet x xt y = subst 0 x y 227mkLet x xt y = subst 0 x y
231--mkLet x xt (ET y yt) = ET (Let (ET x xt) y) yt 228--mkLet x xt (ET y yt) = ET (Let (ET x xt) y) yt
232 229
230ET a at `etApp` e = ET (app_ a e) (appTy at e)
231
233inferN_ :: forall m . Monad m => (forall a . String -> String -> IM m a -> IM m a) -> Env -> SExp2 -> IM m ExpType' 232inferN_ :: forall m . Monad m => (forall a . String -> String -> IM m a -> IM m a) -> Env -> SExp2 -> IM m ExpType'
234inferN_ tellTrace = infer where 233inferN_ tellTrace = infer where
235 234
@@ -238,7 +237,7 @@ inferN_ tellTrace = infer where
238 Parens x -> infer te x 237 Parens x -> infer te x
239 SAnn x t -> checkN (CheckIType x te) t TType 238 SAnn x t -> checkN (CheckIType x te) t TType
240 SRHS x -> infer (ERHS te) x 239 SRHS x -> infer (ERHS te) x
241 SLHS n x -> infer te x 240 SLHS n x -> infer (ELHS n te) x
242 SVar sn i -> focusTell te exp $ ET (Var i) $ snd $ varType "C2" i te 241 SVar sn i -> focusTell te exp $ ET (Var i) $ snd $ varType "C2" i te
243 SLit si l -> focusTell te exp $ ET (ELit l) (nType l) 242 SLit si l -> focusTell te exp $ ET (ELit l) (nType l)
244 STyped et -> focusTell' te exp et 243 STyped et -> focusTell' te exp et
@@ -252,6 +251,9 @@ inferN_ tellTrace = infer where
252 251
253 checkN_ te (Parens e) t = checkN_ te e t 252 checkN_ te (Parens e) t = checkN_ te e t
254 checkN_ te e t 253 checkN_ te e t
254 | SBuiltin "primFix" `SAppV` (SLam Visible _ f) <- e = do
255 pf <- getDef te mempty "primFix"
256 checkN (EBind2 (BLam Visible) t $ EApp2 mempty Visible (pf `etApp` t) te) f $ up 1 t
255 | x@(SGlobal (sName -> MatchName n)) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` SVar siv v `SAppV` b <- e 257 | x@(SGlobal (sName -> MatchName n)) `SAppV` SLamV (Wildcard _) `SAppV` a `SAppV` SVar siv v `SAppV` b <- e
256 = infer te $ x `SAppV` SLam Visible SType (STyped (ET (subst (v+1) (Var 0) $ up 1 t) TType)) `SAppV` a `SAppV` SVar siv v `SAppV` b 258 = infer te $ x `SAppV` SLam Visible SType (STyped (ET (subst (v+1) (Var 0) $ up 1 t) TType)) `SAppV` a `SAppV` SVar siv v `SAppV` b
257 -- temporal hack 259 -- temporal hack
@@ -269,9 +271,9 @@ inferN_ tellTrace = infer where
269 = infer te $ x `SAppV` SLamV (SLamV (STyped (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v 271 = infer te $ x `SAppV` SLamV (SLamV (STyped (subst (n'+2) (Var 1) $ up1_ (n'+3) $ up 2 t, TType))) `SAppV` a `SAppV` b `SAppV` SVar siv v
270-} 272-}
271 | SRHS x <- e = checkN (ERHS te) x t 273 | SRHS x <- e = checkN (ERHS te) x t
272 | SLHS n x <- e = checkN te x t 274 | SLHS n x <- e = checkN (ELHS n te) x t
273 | SApp_ si h a b <- e = infer (CheckAppType si h t te b) a 275 | SApp_ si h a b <- e = infer (CheckAppType si h t te b) a
274 | SLam h a b <- e, Pi h' x y <- t, h == h' = do 276 | SLam h a b <- e, Pi h' x y <- t, h == h' = do
275-- tellType e t 277-- tellType e t
276 let same = checkSame te a x 278 let same = checkSame te a x
277 if same then checkN (EBind2 (BLam h) x te) b $ hnf y else error $ "checkSame:\n" ++ ppShow a ++ "\nwith\n" ++ showEnvExp te (ET x TType) 279 if same then checkN (EBind2 (BLam h) x te) b $ hnf y else error $ "checkSame:\n" ++ ppShow a ++ "\nwith\n" ++ showEnvExp te (ET x TType)
@@ -311,6 +313,7 @@ inferN_ tellTrace = infer where
311 focus_ :: Env -> ExpType -> IM m ExpType' 313 focus_ :: Env -> ExpType -> IM m ExpType'
312 focus_ env eet@(ET e et) = tellTrace "focus" (showEnvExp env eet) $ case env of 314 focus_ env eet@(ET e et) = tellTrace "focus" (showEnvExp env eet) $ case env of
313 ERHS te -> focus_ te (ET (RHS $ hnf e) et) 315 ERHS te -> focus_ te (ET (RHS $ hnf e) et)
316 ELHS n te -> focus_ te (ET (mkELet n e et te) et)
314-- CheckSame x te -> focus_ (EBind2_ (debugSI "focus_ CheckSame") BMeta (cstr x e) te) $ up 1 eet 317-- CheckSame x te -> focus_ (EBind2_ (debugSI "focus_ CheckSame") BMeta (cstr x e) te) $ up 1 eet
315 CheckAppType si h t te b -- App1 h (CheckType t te) b 318 CheckAppType si h t te b -- App1 h (CheckType t te) b
316 | Pi h' x (down 0 -> Just y) <- et, h == h' -> case t of 319 | Pi h' x (down 0 -> Just y) <- et, h == h' -> case t of
@@ -325,7 +328,7 @@ inferN_ tellTrace = infer where
325 | otherwise -> infer (CheckType_ (sourceInfo b) (Var 2) $ cstr' h (up 2 et) (Pi Visible (Var 1) (Var 1)) (up 2 e) $ EBind2_ (sourceInfo b) BMeta TType $ EBind2_ (sourceInfo b) BMeta TType te) (up 3 b) 328 | otherwise -> infer (CheckType_ (sourceInfo b) (Var 2) $ cstr' h (up 2 et) (Pi Visible (Var 1) (Var 1)) (up 2 e) $ EBind2_ (sourceInfo b) BMeta TType $ EBind2_ (sourceInfo b) BMeta TType te) (up 3 b)
326 where 329 where
327 cstr' h x y e = EApp2 mempty h (ET (evalCoe (up 1 x) (up 1 y) (Var 0) (up 1 e)) (up 1 y)) . EBind2_ (sourceInfo b) BMeta (cstr_ TType x y) 330 cstr' h x y e = EApp2 mempty h (ET (evalCoe (up 1 x) (up 1 y) (Var 0) (up 1 e)) (up 1 y)) . EBind2_ (sourceInfo b) BMeta (cstr_ TType x y)
328 ELet2 ln (ET x{-let-} xt) te -> focus_ te $ mkLet (mkELet ln x xt te){-let-} xt eet{-in-} 331 ELet2 ln (ET x{-let-} xt) te -> focus_ te $ mkLet x{-let-} xt eet{-in-}
329 CheckIType x te -> checkN te x $ hnf e 332 CheckIType x te -> checkN te x $ hnf e
330 CheckType_ si t te 333 CheckType_ si t te
331 | hArgs et > hArgs t 334 | hArgs et > hArgs t
@@ -333,7 +336,7 @@ inferN_ tellTrace = infer where
333 | hArgs et < hArgs t, Pi Hidden t1 t2 <- t 336 | hArgs et < hArgs t, Pi Hidden t1 t2 <- t
334 -> focus_ (CheckType_ si t2 $ EBind2 (BLam Hidden) t1 te) eet 337 -> focus_ (CheckType_ si t2 $ EBind2 (BLam Hidden) t1 te) eet
335 | otherwise -> focus_ (EBind2_ si BMeta (cstr_ TType t et) te) $ up 1 eet 338 | otherwise -> focus_ (EBind2_ si BMeta (cstr_ TType t et) te) $ up 1 eet
336 EApp2 si h (ET a at) te -> focusTell te si $ ET (app_ a e) (appTy at e) -- h?? 339 EApp2 si h a te -> focusTell te si $ a `etApp` e -- h??
337 EBind1 si h te b -> infer (EBind2_ (sourceInfo b) h e te) b 340 EBind1 si h te b -> infer (EBind2_ (sourceInfo b) h e te) b
338 EBind2_ si (BLam h) a te -> focus_ te $ lamPi h a eet 341 EBind2_ si (BLam h) a te -> focus_ te $ lamPi h a eet
339 EBind2_ si (BPi h) a te -> focusTell te si $ ET (Pi h a e) TType 342 EBind2_ si (BPi h) a te -> focusTell te si $ ET (Pi h a e) TType
@@ -344,6 +347,7 @@ inferN_ tellTrace = infer where
344 ELet1 le te b{-in-} -> infer (ELet2 le (replaceMetas' eet{-let-}) te) b{-in-} 347 ELet1 le te b{-in-} -> infer (ELet2 le (replaceMetas' eet{-let-}) te) b{-in-}
345 EBind2_ si BMeta tt_ te 348 EBind2_ si BMeta tt_ te
346 | ERHS te' <- te -> refocus (ERHS $ EBind2_ si BMeta tt_ te') eet 349 | ERHS te' <- te -> refocus (ERHS $ EBind2_ si BMeta tt_ te') eet
350 | ELHS n te' <- te -> refocus (ELHS n $ EBind2_ si BMeta tt_ te') eet
347 | Unit <- tt -> refocus te $ subst 0 TT eet 351 | Unit <- tt -> refocus te $ subst 0 TT eet
348 | Empty msg <- tt -> throwError' $ ETypeError (text msg) si 352 | Empty msg <- tt -> throwError' $ ETypeError (text msg) si
349 | CW (hnf -> T2 x y) <- tt, let te' = EBind2_ si BMeta (up 1 $ cw y) $ EBind2_ si BMeta (cw x) te 353 | CW (hnf -> T2 x y) <- tt, let te' = EBind2_ si BMeta (up 1 $ cw y) $ EBind2_ si BMeta (cw x) te
@@ -377,6 +381,7 @@ inferN_ tellTrace = infer where
377 381
378 EAssign i b te -> case te of 382 EAssign i b te -> case te of
379 ERHS te' -> refocus' (ERHS $ EAssign i b te') eet 383 ERHS te' -> refocus' (ERHS $ EAssign i b te') eet
384 ELHS n te' -> refocus' (ELHS n $ EAssign i b te') eet
380 EBind2_ si h x te' | i > 0, Just b' <- down 0 b 385 EBind2_ si h x te' | i > 0, Just b' <- down 0 b
381 -> refocus' (EBind2_ si h (subst (i-1) (expr b') x) (EAssign (i-1) b' te')) eet 386 -> refocus' (EBind2_ si h (subst (i-1) (expr b') x) (EAssign (i-1) b' te')) eet
382 ELet2 le x te' | i > 0, Just b' <- down 0 b 387 ELet2 le x te' | i > 0, Just b' <- down 0 b
@@ -413,7 +418,6 @@ inferN_ tellTrace = infer where
413 _ -> case eet of 418 _ -> case eet of
414 MEnd x -> throwError' $ ErrorMsg $ "focus todo:" <+> pShow x 419 MEnd x -> throwError' $ ErrorMsg $ "focus todo:" <+> pShow x
415 _ -> case env of 420 _ -> case env of
416-- EBind2 h _ _ -> throwError' $ ErrorMsg $ "focus checkMetas" <+> pShow h
417 _ -> throwError' $ ErrorMsg $ "focus checkMetas:" <$$> pShow env <$$> "---" <$$> pShow eet 421 _ -> throwError' $ ErrorMsg $ "focus checkMetas:" <$$> pShow env <$$> "---" <$$> pShow eet
418 where 422 where
419 refocus_ :: (Env -> CEnv ExpType -> IM m ExpType') -> Env -> CEnv ExpType -> IM m ExpType' 423 refocus_ :: (Env -> CEnv ExpType -> IM m ExpType') -> Env -> CEnv ExpType -> IM m ExpType'
@@ -463,7 +467,7 @@ recheck' sn e (ET x xt) = ET (recheck_ "main" (checkEnv e) (ET x xt)) xt
463 -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b] 467 -> checkApps "a" [] zt (Neut . App__ md a' . head) te at [b]
464 ET (Con_ md s n as) zt -> checkApps (ppShow s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as 468 ET (Con_ md s n as) zt -> checkApps (ppShow s) [] zt (Con_ md s n . drop (conParams s)) te (conType zt s) $ mkConPars n zt ++ as
465 ET (TyCon_ md s as) zt -> checkApps (ppShow s) [] zt (TyCon_ md s) te (nType s) as 469 ET (TyCon_ md s as) zt -> checkApps (ppShow s) [] zt (TyCon_ md s) te (nType s) as
466 ET (CaseFun s@(CaseFunName _ t pars) as n) zt -> checkApps (ppShow s) [] zt (\xs -> evalCaseFun s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n]) 470 ET (Neut (CaseFun__ fs s@(CaseFunName _ t pars) as n)) zt -> checkApps (ppShow s) [] zt (\xs -> evalCaseFun fs s (init $ drop pars xs) (last xs)) te (nType s) (makeCaseFunPars te n ++ as ++ [Neut n])
467 ET (TyCaseFun s [m, t, f] n) zt -> checkApps (ppShow s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f] 471 ET (TyCaseFun s [m, t, f] n) zt -> checkApps (ppShow s) [] zt (\[m, t, n, f] -> evalTyCaseFun s [m, t, f] n) te (nType s) [m, t, Neut n, f]
468 ET (Neut (Fun_ md f a x)) zt -> checkApps ("lab-" ++ show f ++ ppShow a ++ "\n" ++ ppShow (nType f)) [] zt (\xs -> Neut $ Fun_ md f (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x 472 ET (Neut (Fun_ md f a x)) zt -> checkApps ("lab-" ++ show f ++ ppShow a ++ "\n" ++ ppShow (nType f)) [] zt (\xs -> Neut $ Fun_ md f (reverse xs) x) te (nType f) $ reverse a -- TODO: recheck x
469 ET (Let (ET x xt) y) zt -> Let (recheck'' "let" te $ ET x xt) $ recheck_ "let_in" te $ ET y (Pi Visible xt $ up1 zt) -- TODO 473 ET (Let (ET x xt) y) zt -> Let (recheck'' "let" te $ ET x xt) $ recheck_ "let_in" te $ ET y (Pi Visible xt $ up1 zt) -- TODO
@@ -493,9 +497,9 @@ handleStmt = \case
493 addToEnv n $ flip ET t $ lamify t $ DFun (mkFName n) t 497 addToEnv n $ flip ET t $ lamify t $ DFun (mkFName n) t
494 StLet n mt t_ -> do 498 StLet n mt t_ -> do
495 let t__ = maybe id (flip SAnn) mt t_ 499 let t__ = maybe id (flip SAnn) mt t_
496 ET x t <- inferTerm n $ trSExp' $ addFix n t__ 500 ET x t <- inferTerm n $ trSExp' t__
497 tellType (sourceInfo n) t 501 tellType (sourceInfo n) t
498 addToEnv n (ET (mkELet n x t EGlobal) t) 502 addToEnv n (ET x t)
499{- -- hack 503{- -- hack
500 when (snd (getParams t) == TType) $ do 504 when (snd (getParams t) == TType) $ do
501 let ps' = fst $ getParams t 505 let ps' = fst $ getParams t
@@ -551,7 +555,7 @@ handleStmt = \case
551 ) 555 )
552 return (e1, es, tcn, cfn, ct, cons) 556 return (e1, es, tcn, cfn, ct, cons)
553 557
554 e2 <- addToEnv (SIName (sourceInfo s) $ CaseName (sName s)) $ ET (lamify ct $ \xs -> evalCaseFun cfn (init $ drop (length ps) xs) (last xs)) ct 558 e2 <- addToEnv (SIName (sourceInfo s) $ CaseName (sName s)) $ ET (lamify ct $ \xs -> evalCaseFun' cfn (init $ drop (length ps) xs) (last xs)) ct
555 let ps' = fst $ getParams vty 559 let ps' = fst $ getParams vty
556 t = (TType :~> TType) 560 t = (TType :~> TType)
557 :~> addParams ps' (Var (length ps') `app_` TyCon tcn (downTo 0 $ length ps')) 561 :~> addParams ps' (Var (length ps') `app_` TyCon tcn (downTo 0 $ length ps'))
diff --git a/src/LambdaCube/Compiler/Parser.hs b/src/LambdaCube/Compiler/Parser.hs
index 79975251..7ca3d5cb 100644
--- a/src/LambdaCube/Compiler/Parser.hs
+++ b/src/LambdaCube/Compiler/Parser.hs
@@ -249,7 +249,7 @@ parseTermProj = level parseTermAtom $ \t ->
249parseTermAtom = 249parseTermAtom =
250 mkLit <$> try_ "literal" parseLit 250 mkLit <$> try_ "literal" parseLit
251 <|> Wildcard (Wildcard SType) <$ reserved "_" 251 <|> Wildcard (Wildcard SType) <$ reserved "_"
252 <|> mkLets <$ reserved "let" <*> parseDefs <* reserved "in" <*> setR parseTermLam 252 <|> mkLets <$ reserved "let" <*> parseDefs sLHS <* reserved "in" <*> setR parseTermLam
253 <|> SGlobal <$> lowerCase 253 <|> SGlobal <$> lowerCase
254 <|> SGlobal <$> upperCase_ 254 <|> SGlobal <$> upperCase_
255 <|> braces (mkRecord <$> commaSep ((,) <$> lowerCase <* symbol ":" <*> setR parseTermLam)) 255 <|> braces (mkRecord <$> commaSep ((,) <$> lowerCase <* symbol ":" <*> setR parseTermLam))
@@ -434,12 +434,12 @@ parseDef =
434 option {-open type family-}[TypeFamily x $ UncurryS ts t] $ do 434 option {-open type family-}[TypeFamily x $ UncurryS ts t] $ do
435 cs <- (reserved "where" >>) $ identation True $ funAltDef Nothing $ mfilter (== x) upperCase 435 cs <- (reserved "where" >>) $ identation True $ funAltDef Nothing $ mfilter (== x) upperCase
436 -- closed type family desugared here 436 -- closed type family desugared here
437 runCheck $ fmap Stmt <$> compileStmt (compileGuardTrees id . const Nothing) [TypeAnn x $ UncurryS ts t] cs 437 runCheck $ fmap Stmt <$> compileStmt SLHS (compileGuardTrees id . const Nothing) [TypeAnn x $ UncurryS ts t] cs
438 <|> pure <$ reserved "instance" <*> funAltDef Nothing upperCase 438 <|> pure <$ reserved "instance" <*> funAltDef Nothing upperCase
439 <|> do x <- upperCase 439 <|> do x <- upperCase
440 (nps, ts) <- telescope $ Just (Wildcard SType) 440 (nps, ts) <- telescope $ Just (Wildcard SType)
441 rhs <- deBruijnify nps <$ reservedOp "=" <*> setR parseTermLam 441 rhs <- deBruijnify nps <$ reservedOp "=" <*> setR parseTermLam
442 runCheck $ fmap Stmt <$> compileStmt (compileGuardTrees id . const Nothing) 442 runCheck $ fmap Stmt <$> compileStmt SLHS (compileGuardTrees id . const Nothing)
443 [{-TypeAnn x $ UncurryS ts $ SType-}{-todo-}] 443 [{-TypeAnn x $ UncurryS ts $ SType-}{-todo-}]
444 [funAlt' x ts (map PVarSimp $ reverse nps) $ noGuards rhs] 444 [funAlt' x ts (map PVarSimp $ reverse nps) $ noGuards rhs]
445 <|> do try_ "typed ident" $ map (uncurry TypeAnn) <$> typedIds addForalls' [] 445 <|> do try_ "typed ident" $ map (uncurry TypeAnn) <$> typedIds addForalls' []
@@ -465,10 +465,10 @@ parseDef =
465 465
466parseRHS :: String -> BodyParser GuardTrees 466parseRHS :: String -> BodyParser GuardTrees
467parseRHS tok = do 467parseRHS tok = do
468 mkGuards <$> some (reservedOp "|" *> guard) <*> option [] (reserved "where" *> parseDefs) 468 mkGuards <$> some (reservedOp "|" *> guard) <*> option [] (reserved "where" *> parseDefs sLHS)
469 <|> do 469 <|> do
470 rhs <- reservedOp tok *> setR parseTermLam 470 rhs <- reservedOp tok *> setR parseTermLam
471 f <- option id $ mkLets <$ reserved "where" <*> parseDefs 471 f <- option id $ mkLets <$ reserved "where" <*> parseDefs sLHS
472 noGuards <$> trackSI (pure $ f rhs) 472 noGuards <$> trackSI (pure $ f rhs)
473 where 473 where
474 guard = do 474 guard = do
@@ -482,7 +482,7 @@ parseRHS tok = do
482 482
483 mkGuards gs wh = mkLets_ lLet wh $ mconcat [foldr (uncurry guardNode') (noGuards e) ge | (ge, e) <- gs] 483 mkGuards gs wh = mkLets_ lLet wh $ mconcat [foldr (uncurry guardNode') (noGuards e) ge | (ge, e) <- gs]
484 484
485parseDefs = identation True parseDef >>= runCheck . compileStmt' . concat 485parseDefs lhs = identation True parseDef >>= runCheck . compileStmt'_ lhs SRHS SRHS . concat
486 486
487funAltDef parseOpName parseName = do 487funAltDef parseOpName parseName = do
488 (n, (fee, tss)) <- 488 (n, (fee, tss)) <-
@@ -555,7 +555,7 @@ parseModule = do
555 { extensions = exts 555 { extensions = exts
556 , moduleImports = [(SIName mempty "Prelude", ImportAllBut []) | NoImplicitPrelude `notElem` exts] ++ idefs 556 , moduleImports = [(SIName mempty "Prelude", ImportAllBut []) | NoImplicitPrelude `notElem` exts] ++ idefs
557 , moduleExports = join $ snd <$> header 557 , moduleExports = join $ snd <$> header
558 , definitions = \ge -> runParse (parseDefs <* eof) (env { desugarInfo = ge }, st) 558 , definitions = \ge -> runParse (parseDefs SLHS <* eof) (env { desugarInfo = ge }, st)
559 } 559 }
560 560
561parseLC :: FileInfo -> Either ParseError Module 561parseLC :: FileInfo -> Either ParseError Module
diff --git a/src/LambdaCube/Compiler/Pretty.hs b/src/LambdaCube/Compiler/Pretty.hs
index e2ebb5e4..204e4893 100644
--- a/src/LambdaCube/Compiler/Pretty.hs
+++ b/src/LambdaCube/Compiler/Pretty.hs
@@ -320,6 +320,7 @@ infixl 4 `DApp`
320 320
321pattern DAt x = DGlue (InfixR 20) (DText "@") x 321pattern DAt x = DGlue (InfixR 20) (DText "@") x
322pattern DApp x y = DSep (InfixL 10) x y 322pattern DApp x y = DSep (InfixL 10) x y
323pattern DHApp x y = DSep (InfixL 10) x (DAt y)
323pattern DSemi x y = DOp ";" (InfixR (-19)) x y 324pattern DSemi x y = DOp ";" (InfixR (-19)) x y
324pattern DArr_ s x y = DOp s (InfixR (-1)) x y -- -> => . 325pattern DArr_ s x y = DOp s (InfixR (-1)) x y -- -> => .
325pattern DCstr x y = DOp "~" (Infix (-2)) x y 326pattern DCstr x y = DOp "~" (Infix (-2)) x y
diff --git a/src/LambdaCube/Compiler/Statements.hs b/src/LambdaCube/Compiler/Statements.hs
index 178e5505..9d48ffc2 100644
--- a/src/LambdaCube/Compiler/Statements.hs
+++ b/src/LambdaCube/Compiler/Statements.hs
@@ -49,17 +49,10 @@ mkLets_ mkLet = mkLets' mkLet . concatMap desugarMutual . sortDefs
49 49
50mkLets' mkLet = f where 50mkLets' mkLet = f where
51 f [] e = e 51 f [] e = e
52 f (StLet n mt x: ds) e = mkLet n (maybe id (flip SAnn) mt (addFix n x)) (deBruijnify [n] $ f ds e) 52 f (StLet n mt x: ds) e = mkLet n (maybe id (flip SAnn) mt x) (deBruijnify [n] $ f ds e)
53 f (PrecDef{}: ds) e = f ds e 53 f (PrecDef{}: ds) e = f ds e
54 f (x: ds) e = error $ "mkLets: " ++ ppShow x 54 f (x: ds) e = error $ "mkLets: " ++ ppShow x
55 55
56addFix n x
57 | usedS n x = SBuiltin "primFix" `SAppV` SLamV (deBruijnify [n] x)
58 | otherwise = x
59
60addFix' (StLet n nt nd) = StLet n nt $ addFix n nd
61addFix' x = x
62
63type DefinedSet = Set.Set SName 56type DefinedSet = Set.Set SName
64 57
65addForalls :: DefinedSet -> SExp -> SExp 58addForalls :: DefinedSet -> SExp -> SExp
@@ -75,14 +68,14 @@ addForalls defined x = foldl f x [v | v@(sName -> vh:_) <- reverse $ names x, sN
75 68
76------------------------------------------------------------------------ 69------------------------------------------------------------------------
77 70
78compileStmt' = compileStmt'_ SRHS SRHS 71compileStmt' = compileStmt'_ SLHS SRHS SRHS
79 72
80compileStmt'_ ulend lend ds = fmap concat . sequence $ map (compileStmt (\si vt -> compileGuardTree ulend lend (Just si) vt . mconcat) ds) $ groupBy h ds where 73compileStmt'_ lhs ulend lend ds = fmap concat . sequence $ map (compileStmt lhs (\si vt -> compileGuardTree ulend lend (Just si) vt . mconcat) ds) $ groupBy h ds where
81 h (FunAlt n _ _) (FunAlt m _ _) = m == n 74 h (FunAlt n _ _) (FunAlt m _ _) = m == n
82 h _ _ = False 75 h _ _ = False
83 76
84compileStmt :: MonadWriter [ParseCheck] m => (SIName -> [(Visibility, SExp)] -> [GuardTrees] -> m SExp) -> [PreStmt] -> [PreStmt] -> m [Stmt] 77--compileStmt :: MonadWriter [ParseCheck] m => (SIName -> [(Visibility, SExp)] -> [GuardTrees] -> m SExp) -> [PreStmt] -> [PreStmt] -> m [Stmt]
85compileStmt compilegt ds = \case 78compileStmt lhs compilegt ds = \case
86 [Instance{}] -> return [] 79 [Instance{}] -> return []
87 [Class n ps ms] -> do 80 [Class n ps ms] -> do
88 cd <- compileStmt' $ 81 cd <- compileStmt' $
@@ -90,7 +83,7 @@ compileStmt compilegt ds = \case
90 ++ [ funAlt n (map noTA ps) $ noGuards $ foldr (SAppV2 $ SBuiltin "'T2") (SBuiltin "'CUnit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ] 83 ++ [ funAlt n (map noTA ps) $ noGuards $ foldr (SAppV2 $ SBuiltin "'T2") (SBuiltin "'CUnit") cstrs | Instance n' ps cstrs _ <- ds, n == n' ]
91 ++ [ funAlt n (replicate (length ps) (noTA $ PVarSimp $ dummyName "cst0")) $ noGuards $ SBuiltin "'CEmpty" `SAppV` sLit (LString $ "no instance of " ++ sName n ++ " on ???"{-TODO-})] 84 ++ [ funAlt n (replicate (length ps) (noTA $ PVarSimp $ dummyName "cst0")) $ noGuards $ SBuiltin "'CEmpty" `SAppV` sLit (LString $ "no instance of " ++ sName n ++ " on ???"{-TODO-})]
92 cds <- sequence 85 cds <- sequence
93 [ compileStmt'_ SRHS SRHS{-id-} 86 [ compileStmt'_ SLHS SRHS SRHS{-id-}
94 $ TypeAnn m (UncurryS (map ((,) Hidden) ps) $ SPi Hidden (SCW $ foldl SAppV (SGlobal n) $ downToS "a2" 0 $ length ps) $ up1 t) 87 $ TypeAnn m (UncurryS (map ((,) Hidden) ps) $ SPi Hidden (SCW $ foldl SAppV (SGlobal n) $ downToS "a2" 0 $ length ps) $ up1 t)
95 : as 88 : as
96 | (m, t) <- ms 89 | (m, t) <- ms
@@ -106,14 +99,14 @@ compileStmt compilegt ds = \case
106 [TypeAnn n t] -> return [Primitive n t | n `notElem` [n' | FunAlt n' _ _ <- ds]] 99 [TypeAnn n t] -> return [Primitive n t | n `notElem` [n' | FunAlt n' _ _ <- ds]]
107 tf@[TypeFamily n t] -> case [d | d@(FunAlt n' _ _) <- ds, n' == n] of 100 tf@[TypeFamily n t] -> case [d | d@(FunAlt n' _ _) <- ds, n' == n] of
108 [] -> return [Primitive n t] 101 [] -> return [Primitive n t]
109 alts -> compileStmt compileGuardTrees' [TypeAnn n t] alts 102 alts -> compileStmt lhs compileGuardTrees' [TypeAnn n t] alts
110 fs@(FunAlt n vs _: _) -> case groupBy ((==) `on` fst) [(length vs, n) | FunAlt n vs _ <- fs] of 103 fs@(FunAlt n vs _: _) -> case groupBy ((==) `on` fst) [(length vs, n) | FunAlt n vs _ <- fs] of
111 [gs@((num, _): _)] 104 [gs@((num, _): _)]
112 | num == 0 && length gs > 1 -> fail $ "redefined " ++ sName n ++ ":\n" ++ show (vcat $ pShow . sourceInfo . snd <$> gs) 105 | num == 0 && length gs > 1 -> fail $ "redefined " ++ sName n ++ ":\n" ++ show (vcat $ pShow . sourceInfo . snd <$> gs)
113 | n `elem` [n' | TypeFamily n' _ <- ds] -> return [] 106 | n `elem` [n' | TypeFamily n' _ <- ds] -> return []
114 | otherwise -> do 107 | otherwise -> do
115 cf <- compilegt (SIName_ (mconcat [sourceInfo n | FunAlt n _ _ <- fs]) (nameFixity n) $ sName n) vs [gt | FunAlt _ _ gt <- fs] 108 cf <- compilegt (SIName_ (mconcat [sourceInfo n | FunAlt n _ _ <- fs]) (nameFixity n) $ sName n) vs [gt | FunAlt _ _ gt <- fs]
116 return [StLet n (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) $ SLHS n cf] 109 return [StLet n (listToMaybe [t | TypeAnn n' t <- ds, n' == n]) $ lhs n cf]
117 fs -> fail $ "different number of arguments of " ++ sName n ++ ":\n" ++ show (vcat $ pShow . sourceInfo . snd . head <$> fs) 110 fs -> fail $ "different number of arguments of " ++ sName n ++ ":\n" ++ show (vcat $ pShow . sourceInfo . snd . head <$> fs)
118 [Stmt x] -> return [x] 111 [Stmt x] -> return [x]
119 where 112 where
@@ -134,21 +127,29 @@ desugarValueDef p e = sequence
134 dns = reverse $ getPVars p 127 dns = reverse $ getPVars p
135 n = mangleNames dns 128 n = mangleNames dns
136 129
137getLet (StLet x Nothing (SLHS _ (SRHS dx))) = Just (x, dx) 130getLet (StLet x mt dx) = Just (x, mt, dx)
138getLet _ = Nothing 131getLet _ = Nothing
139 132
140fst' (x, _) = x -- TODO 133fst' (x, _) = x -- TODO
141 134
142desugarMutual :: {-MonadWriter [ParseCheck] m => -} [Stmt] -> [Stmt] 135desugarMutual :: {-MonadWriter [ParseCheck] m => -} [Stmt] -> [Stmt]
143desugarMutual [x] = [x] 136desugarMutual [x@Primitive{}] = [x]
144desugarMutual (traverse getLet -> Just (unzip -> (ns, ds))) = fst' $ runWriter $ do 137desugarMutual [x@Data{}] = [x]
145 ss <- compileStmt' =<< desugarValueDef (foldr cHCons cHNil $ PVarSimp <$> ns) (SGlobal xy) 138desugarMutual [x@PrecDef{}] = [x]
139desugarMutual [StLet n nt nd] = [StLet n nt $ addFix n nt nd]
140desugarMutual (traverse getLet -> Just (unzip3 -> (ns, ts, ds))) = fst' $ runWriter $ do
141 ss <- compileStmt'_ sLHS SRHS SRHS =<< desugarValueDef (foldr cHCons cHNil $ PVarSimp <$> ns) (SGlobal xy)
146 return $ 142 return $
147 StLet xy Nothing (addFix xy $ SLHS xy $ SRHS $ mkLets' SLet ss $ foldr HCons HNil ds) : ss 143 StLet xy ty (addFix xy ty $ mkLets' SLet ss $ foldr HCons HNil ds) : ss
148 where 144 where
145 ty = Nothing -- TODO: Just $ HList $ foldr BCons BNil $ const (Wildcard SType) <$> ts
149 xy = mangleNames ns 146 xy = mangleNames ns
150desugarMutual xs = error "desugarMutual" 147desugarMutual xs = error "desugarMutual"
151 148
149addFix n nt x
150 | usedS n x = SBuiltin "primFix" `SAppV` SLam Visible (maybe (Wildcard SType) id nt) (deBruijnify [n] x)
151 | otherwise = x
152
152mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs) 153mangleNames xs = SIName (foldMap sourceInfo xs) $ "_" ++ intercalate "_" (sName <$> xs)
153 154
154 155