summaryrefslogtreecommitdiff
path: root/src/LambdaCube/Compiler/Core.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/LambdaCube/Compiler/Core.hs')
-rw-r--r--src/LambdaCube/Compiler/Core.hs57
1 files changed, 41 insertions, 16 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