diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-04 06:59:57 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-04 06:59:57 +0200 |
commit | 8003edf130fdcf784cdd8d689dbbcf563ef69062 (patch) | |
tree | 81a5a1f8747f2aa613914555cf3bf788f0a1d431 /src | |
parent | a4c615aeeed83787049e33eca12c7615692a1394 (diff) |
refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Core.hs | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs index 8bb6b80c..c11e8140 100644 --- a/src/LambdaCube/Compiler/Core.hs +++ b/src/LambdaCube/Compiler/Core.hs | |||
@@ -203,8 +203,9 @@ data Exp | |||
203 | | Con_ !MaxDB ConName !Int{-number of ereased arguments applied-} [Exp] | 203 | | Con_ !MaxDB ConName !Int{-number of ereased arguments applied-} [Exp] |
204 | | TyCon_ !MaxDB TyConName [Exp] | 204 | | TyCon_ !MaxDB TyConName [Exp] |
205 | | Pi_ !MaxDB Visibility Exp Exp | 205 | | Pi_ !MaxDB Visibility Exp Exp |
206 | | Delta | ||
207 | | Neut Neutral | 206 | | Neut Neutral |
207 | | Delta | ||
208 | | RHS Exp | ||
208 | 209 | ||
209 | data Neutral | 210 | data Neutral |
210 | = Var_ !Int{-De Bruijn index-} | 211 | = Var_ !Int{-De Bruijn index-} |
@@ -212,7 +213,6 @@ data Neutral | |||
212 | | CaseFun__ !MaxDB CaseFunName [Exp] Neutral | 213 | | CaseFun__ !MaxDB CaseFunName [Exp] Neutral |
213 | | TyCaseFun__ !MaxDB TyCaseFunName [Exp] Neutral | 214 | | TyCaseFun__ !MaxDB TyCaseFunName [Exp] Neutral |
214 | | Fun_ !MaxDB FunName [Exp]{-local vars-} [Exp]{-given parameters, reversed-} Exp{-unfolded expression-} | 215 | | Fun_ !MaxDB FunName [Exp]{-local vars-} [Exp]{-given parameters, reversed-} Exp{-unfolded expression-} |
215 | | RHS_ Exp -- not neut? | ||
216 | 216 | ||
217 | -------------------------------------------------------------------------------- auxiliary functions and patterns | 217 | -------------------------------------------------------------------------------- auxiliary functions and patterns |
218 | 218 | ||
@@ -229,7 +229,7 @@ infixr 1 :~> | |||
229 | 229 | ||
230 | pattern NoLE <- (isNoRHS -> True) | 230 | pattern NoLE <- (isNoRHS -> True) |
231 | 231 | ||
232 | isNoRHS (Neut (RHS_ _)) = False | 232 | isNoRHS RHS{} = False |
233 | isNoRHS _ = True | 233 | isNoRHS _ = True |
234 | 234 | ||
235 | getLams' (Lam e) = first (+1) $ getLams' e | 235 | getLams' (Lam e) = first (+1) $ getLams' e |
@@ -246,12 +246,6 @@ pattern TFun' a t b <- DFun_ (FunName a _ t) b | |||
246 | where TFun' a t b = DFun_ (getFunDef' a t) b | 246 | where TFun' a t b = DFun_ (getFunDef' a t) b |
247 | pattern TFun a t b = Neut (TFun' a t b) | 247 | pattern TFun a t b = Neut (TFun' a t b) |
248 | 248 | ||
249 | getFunDef' a t = fn | ||
250 | where | ||
251 | fn = FunName a (DeltaDef d) t | ||
252 | d = getFunDef a $ \xs -> Neut $ Fun fn (reverse xs) Delta | ||
253 | |||
254 | |||
255 | pattern CaseFun_ a b c <- CaseFun__ _ a b c where CaseFun_ a b c = CaseFun__ (maxDB_ c <> foldMap maxDB_ b) a b c | 249 | pattern CaseFun_ a b c <- CaseFun__ _ a b c where CaseFun_ a b c = CaseFun__ (maxDB_ c <> foldMap maxDB_ b) a b c |
256 | pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c where TyCaseFun_ a b c = TyCaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c | 250 | pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c where TyCaseFun_ a b c = TyCaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c |
257 | pattern App_ a b <- App__ _ a b where App_ a b = App__ (maxDB_ a <> maxDB_ b) a b | 251 | pattern App_ a b <- App__ _ a b where App_ a b = App__ (maxDB_ a <> maxDB_ b) a b |
@@ -348,8 +342,6 @@ trueExp = EBool True | |||
348 | 342 | ||
349 | -------------------------------------------------------------------------------- label handling | 343 | -------------------------------------------------------------------------------- label handling |
350 | 344 | ||
351 | pattern RHS x = Neut (RHS_ x) | ||
352 | |||
353 | --pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp | 345 | --pmLabel' :: FunName -> [Exp] -> Int -> [Exp] -> Exp -> Exp |
354 | pmLabel' _ (FunName _ (DeltaDef f) _) _ as Delta = f $ reverse as | 346 | pmLabel' _ (FunName _ (DeltaDef f) _) _ as Delta = f $ reverse as |
355 | pmLabel' md f vs xs (unfixlabel -> y) = Neut $ Fun_ md f vs xs y | 347 | pmLabel' md f vs xs (unfixlabel -> y) = Neut $ Fun_ md f vs xs y |
@@ -363,12 +355,12 @@ dropLams (unfixlabel -> Neut x) = x | |||
363 | numLams (unfixlabel -> Lam x) = 1 + numLams x | 355 | numLams (unfixlabel -> Lam x) = 1 + numLams x |
364 | numLams x = 0 | 356 | numLams x = 0 |
365 | 357 | ||
366 | pattern FL' y <- Fun' f _ xs (Neut (RHS_ y)) | 358 | pattern FL' y <- Fun' f _ xs (RHS y) |
367 | pattern FL y <- Neut (FL' y) | 359 | pattern FL y <- Neut (FL' y) |
368 | 360 | ||
369 | pattern Func n def ty xs <- (mkFunc -> Just (n, def, ty, xs)) | 361 | pattern Func n def ty xs <- (mkFunc -> Just (n, def, ty, xs)) |
370 | 362 | ||
371 | mkFunc (Neut (Fun (FunName n (ExpDef def) ty) xs (Neut RHS_{}))) | Just def' <- removeLams (length xs) def = Just (n, def', ty, xs) | 363 | mkFunc (Neut (Fun (FunName n (ExpDef def) ty) xs RHS{})) | Just def' <- removeLams (length xs) def = Just (n, def', ty, xs) |
372 | mkFunc _ = Nothing | 364 | mkFunc _ = Nothing |
373 | 365 | ||
374 | removeLams 0 (RHS x) = Just x | 366 | removeLams 0 (RHS x) = Just x |
@@ -416,13 +408,13 @@ instance Eq Exp where | |||
416 | TType_ f == TType_ f' = f == f' | 408 | TType_ f == TType_ f' = f == f' |
417 | ELit l == ELit l' = l == l' | 409 | ELit l == ELit l' = l == l' |
418 | Neut a == Neut a' = a == a' | 410 | Neut a == Neut a' = a == a' |
411 | RHS a == RHS a' = a == a' | ||
419 | _ == _ = False | 412 | _ == _ = False |
420 | 413 | ||
421 | instance Eq Neutral where | 414 | instance Eq Neutral where |
422 | Fun' f vs a _ == Fun' f' vs' a' _ = (f, vs, a) == (f', vs', a') | 415 | Fun' f vs a _ == Fun' f' vs' a' _ = (f, vs, a) == (f', vs', a') |
423 | FL' a == a' = a == Neut a' | 416 | FL' a == a' = a == Neut a' |
424 | a == FL' a' = Neut a == a' | 417 | a == FL' a' = Neut a == a' |
425 | RHS_ a == RHS_ a' = a == a' | ||
426 | CaseFun_ a b c == CaseFun_ a' b' c' = (a, b, c) == (a', b', c') | 418 | CaseFun_ a b c == CaseFun_ a' b' c' = (a, b, c) == (a', b', c') |
427 | TyCaseFun_ a b c == TyCaseFun_ a' b' c' = (a, b, c) == (a', b', c') | 419 | TyCaseFun_ a b c == TyCaseFun_ a' b' c' = (a, b, c) == (a', b', c') |
428 | App_ a b == App_ a' b' = (a, b) == (a', b') | 420 | App_ a b == App_ a' b' = (a, b) == (a', b') |
@@ -444,7 +436,6 @@ instance Subst Exp Exp where | |||
444 | TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n) | 436 | TyCaseFun_ s as n -> evalTyCaseFun s (f i <$> as) (substNeut n) |
445 | App_ a b -> app_ (substNeut a) (f i b) | 437 | App_ a b -> app_ (substNeut a) (f i b) |
446 | Fun_ md fn vs xs v -> pmLabel' (md <> upDB i dx) fn (f i <$> vs) (f i <$> xs) $ f i v | 438 | Fun_ md fn vs xs v -> pmLabel' (md <> upDB i dx) fn (f i <$> vs) (f i <$> xs) $ f i v |
447 | RHS_ a -> RHS $ f i a | ||
448 | f i e | cmpDB i e = e | 439 | f i e | cmpDB i e = e |
449 | f i e = case e of | 440 | f i e = case e of |
450 | Lam_ md b -> Lam_ (md <> upDB i dx) (f (i+1) b) | 441 | Lam_ md b -> Lam_ (md <> upDB i dx) (f (i+1) b) |
@@ -452,6 +443,7 @@ instance Subst Exp Exp where | |||
452 | Pi_ md h a b -> Pi_ (md <> upDB i dx) h (f i a) (f (i+1) b) | 443 | Pi_ md h a b -> Pi_ (md <> upDB i dx) h (f i a) (f (i+1) b) |
453 | TyCon_ md s as -> TyCon_ (md <> upDB i dx) s $ f i <$> as | 444 | TyCon_ md s as -> TyCon_ (md <> upDB i dx) s $ f i <$> as |
454 | Delta -> Delta | 445 | Delta -> Delta |
446 | RHS a -> RHS $ f i a | ||
455 | 447 | ||
456 | instance Rearrange Exp where | 448 | instance Rearrange Exp where |
457 | rearrange i g = f i where | 449 | rearrange i g = f i where |
@@ -463,6 +455,7 @@ instance Rearrange Exp where | |||
463 | TyCon_ md s as -> TyCon_ (upDB_ g i md) s $ map (f i) as | 455 | TyCon_ md s as -> TyCon_ (upDB_ g i md) s $ map (f i) as |
464 | Neut x -> Neut $ rearrange i g x | 456 | Neut x -> Neut $ rearrange i g x |
465 | Delta -> Delta | 457 | Delta -> Delta |
458 | RHS x -> RHS $ rearrange i g x | ||
466 | 459 | ||
467 | instance Rearrange Neutral where | 460 | instance Rearrange Neutral where |
468 | rearrange i g = f i where | 461 | rearrange i g = f i where |
@@ -473,7 +466,6 @@ instance Rearrange Neutral where | |||
473 | TyCaseFun__ md s as ne -> TyCaseFun__ (upDB_ g i md) s (rearrange i g <$> as) (rearrange i g ne) | 466 | TyCaseFun__ md s as ne -> TyCaseFun__ (upDB_ g i md) s (rearrange i g <$> as) (rearrange i g ne) |
474 | App__ md a b -> App__ (upDB_ g i md) (rearrange i g a) (rearrange i g b) | 467 | App__ md a b -> App__ (upDB_ g i md) (rearrange i g a) (rearrange i g b) |
475 | Fun_ md fn vs x y -> Fun_ (upDB_ g i md) fn (rearrange i g <$> vs) (rearrange i g <$> x) $ rearrange i g y | 468 | Fun_ md fn vs x y -> Fun_ (upDB_ g i md) fn (rearrange i g <$> vs) (rearrange i g <$> x) $ rearrange i g y |
476 | RHS_ x -> RHS_ $ rearrange i g x | ||
477 | 469 | ||
478 | instance Up Exp where | 470 | instance Up Exp where |
479 | usedVar i e | 471 | usedVar i e |
@@ -489,6 +481,7 @@ instance Up Exp where | |||
489 | ELit{} -> mempty | 481 | ELit{} -> mempty |
490 | Neut x -> foldVar f i x | 482 | Neut x -> foldVar f i x |
491 | Delta -> mempty | 483 | Delta -> mempty |
484 | RHS x -> foldVar f i x | ||
492 | 485 | ||
493 | instance Up Neutral where | 486 | instance Up Neutral where |
494 | usedVar i e | 487 | usedVar i e |
@@ -501,7 +494,6 @@ instance Up Neutral where | |||
501 | TyCaseFun_ _ as n -> foldMap (foldVar f i) as <> foldVar f i n | 494 | TyCaseFun_ _ as n -> foldMap (foldVar f i) as <> foldVar f i n |
502 | App_ a b -> foldVar f i a <> foldVar f i b | 495 | App_ a b -> foldVar f i a <> foldVar f i b |
503 | Fun' _ vs x d -> foldMap (foldVar f i) vs <> foldMap (foldVar f i) x -- <> foldVar f i d | 496 | Fun' _ vs x d -> foldMap (foldVar f i) vs <> foldMap (foldVar f i) x -- <> foldVar f i d |
504 | RHS_ x -> foldVar f i x | ||
505 | 497 | ||
506 | instance HasMaxDB Exp where | 498 | instance HasMaxDB Exp where |
507 | maxDB_ = \case | 499 | maxDB_ = \case |
@@ -514,6 +506,7 @@ instance HasMaxDB Exp where | |||
514 | ELit{} -> mempty | 506 | ELit{} -> mempty |
515 | Neut x -> maxDB_ x | 507 | Neut x -> maxDB_ x |
516 | Delta -> mempty | 508 | Delta -> mempty |
509 | RHS x -> maxDB_ x | ||
517 | 510 | ||
518 | instance HasMaxDB Neutral where | 511 | instance HasMaxDB Neutral where |
519 | maxDB_ = \case | 512 | maxDB_ = \case |
@@ -522,7 +515,6 @@ instance HasMaxDB Neutral where | |||
522 | TyCaseFun__ c _ _ _ -> c | 515 | TyCaseFun__ c _ _ _ -> c |
523 | App__ c a b -> c | 516 | App__ c a b -> c |
524 | Fun_ c _ _ _ _ -> c | 517 | Fun_ c _ _ _ _ -> c |
525 | RHS_ x -> maxDB_ x | ||
526 | 518 | ||
527 | instance (Subst x a, Subst x b) => Subst x (a, b) where | 519 | instance (Subst x a, Subst x b) => Subst x (a, b) where |
528 | subst_ i dx x (a, b) = (subst_ i dx x a, subst_ i dx x b) | 520 | subst_ i dx x (a, b) = (subst_ i dx x a, subst_ i dx x b) |
@@ -550,6 +542,7 @@ instance ClosedExp Exp where | |||
550 | e@ELit{} -> e | 542 | e@ELit{} -> e |
551 | Neut a -> Neut $ closedExp a | 543 | Neut a -> Neut $ closedExp a |
552 | Delta -> Delta | 544 | Delta -> Delta |
545 | RHS a -> RHS (closedExp a) | ||
553 | 546 | ||
554 | instance ClosedExp Neutral where | 547 | instance ClosedExp Neutral where |
555 | closedExp = \case | 548 | closedExp = \case |
@@ -558,7 +551,6 @@ instance ClosedExp Neutral where | |||
558 | TyCaseFun__ _ a as n -> TyCaseFun__ mempty a (closedExp <$> as) (closedExp n) | 551 | TyCaseFun__ _ a as n -> TyCaseFun__ mempty a (closedExp <$> as) (closedExp n) |
559 | App__ _ a b -> App__ mempty (closedExp a) (closedExp b) | 552 | App__ _ a b -> App__ mempty (closedExp a) (closedExp b) |
560 | Fun_ _ f l x y -> Fun_ mempty f l (closedExp <$> x) y | 553 | Fun_ _ f l x y -> Fun_ mempty f l (closedExp <$> x) y |
561 | RHS_ a -> RHS_ (closedExp a) | ||
562 | 554 | ||
563 | -------------------------------------------------------------------------------- pretty print | 555 | -------------------------------------------------------------------------------- pretty print |
564 | -- todo: do this via conversion to SExp? | 556 | -- todo: do this via conversion to SExp? |
@@ -590,6 +582,7 @@ instance MkDoc Exp where | |||
590 | ELit l -> pShow l | 582 | ELit l -> pShow l |
591 | Neut x -> mkDoc pr x | 583 | Neut x -> mkDoc pr x |
592 | Delta -> text "^delta" | 584 | Delta -> text "^delta" |
585 | RHS x -> shApp Visible (text "_rhs") (f x) | ||
593 | 586 | ||
594 | instance MkDoc Neutral where | 587 | instance MkDoc Neutral where |
595 | mkDoc pr e = green $ f e | 588 | mkDoc pr e = green $ f e |
@@ -604,7 +597,6 @@ instance MkDoc Neutral where | |||
604 | CaseFun_ s xs n -> foldl (shApp Visible) (pShow s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) | 597 | CaseFun_ s xs n -> foldl (shApp Visible) (pShow s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) |
605 | TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (pShow s) (g <$> mkExpTypes (nType s) [m, t, Neut n, f]) | 598 | TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (pShow s) (g <$> mkExpTypes (nType s) [m, t, Neut n, f]) |
606 | TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" | 599 | TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" |
607 | RHS_ x -> shApp Visible (text "labend") (g x) | ||
608 | 600 | ||
609 | getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs | 601 | getTup (unfixlabel -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs |
610 | getTup (unfixlabel -> ConN FHNil []) = Just [] | 602 | getTup (unfixlabel -> ConN FHNil []) = Just [] |
@@ -649,6 +641,10 @@ evalCoe a b t d = Coe a b t d | |||
649 | MT "finElim" [m, z, s, n, ConN "FZero" [i]] -> z `app_` i | 641 | MT "finElim" [m, z, s, n, ConN "FZero" [i]] -> z `app_` i |
650 | -} | 642 | -} |
651 | 643 | ||
644 | getFunDef' a t = fn | ||
645 | where | ||
646 | fn = FunName a (DeltaDef $ getFunDef a $ \xs -> Neut $ Fun fn (reverse xs) Delta) t | ||
647 | |||
652 | getFunDef s f = case s of | 648 | getFunDef s f = case s of |
653 | FEqCT -> \case (t: a: b: _) -> cstr t a b | 649 | FEqCT -> \case (t: a: b: _) -> cstr t a b |
654 | FT2 -> \case (a: b: _) -> t2 a b | 650 | FT2 -> \case (a: b: _) -> t2 a b |