diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-04 11:01:16 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-04 11:01:16 +0200 |
commit | 51b37f58afba2c9aaf80bb49267aa7b74f757d52 (patch) | |
tree | f6f3cf5c3f079dc8559fefd470effba7b3e0933a /src | |
parent | 4fd500adc5a8eee72caca044bac5fd11ef10c6b5 (diff) |
improve pretty print
Diffstat (limited to 'src')
-rw-r--r-- | src/LambdaCube/Compiler/Core.hs | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/src/LambdaCube/Compiler/Core.hs b/src/LambdaCube/Compiler/Core.hs index 818ffedd..7c60cf61 100644 --- a/src/LambdaCube/Compiler/Core.hs +++ b/src/LambdaCube/Compiler/Core.hs | |||
@@ -6,14 +6,10 @@ | |||
6 | {-# LANGUAGE FlexibleInstances #-} | 6 | {-# LANGUAGE FlexibleInstances #-} |
7 | {-# LANGUAGE NoMonomorphismRestriction #-} | 7 | {-# LANGUAGE NoMonomorphismRestriction #-} |
8 | {-# LANGUAGE ScopedTypeVariables #-} | 8 | {-# LANGUAGE ScopedTypeVariables #-} |
9 | {-# LANGUAGE RecursiveDo #-} | ||
10 | {-# LANGUAGE RankNTypes #-} | 9 | {-# LANGUAGE RankNTypes #-} |
11 | {-# LANGUAGE MultiParamTypeClasses #-} | 10 | {-# LANGUAGE MultiParamTypeClasses #-} |
12 | {-# LANGUAGE UndecidableInstances #-} | 11 | --{-# OPTIONS_GHC -fno-warn-overlapping-patterns #-} -- TODO: remove |
13 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 12 | --{-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove |
14 | {-# LANGUAGE DeriveFunctor #-} | ||
15 | {-# OPTIONS_GHC -fno-warn-overlapping-patterns #-} -- TODO: remove | ||
16 | {-# OPTIONS_GHC -fno-warn-unused-binds #-} -- TODO: remove | ||
17 | module LambdaCube.Compiler.Core where | 13 | module LambdaCube.Compiler.Core where |
18 | 14 | ||
19 | import Data.Monoid | 15 | import Data.Monoid |
@@ -228,7 +224,7 @@ data ExpType = ET {expr :: Exp, ty :: Type} | |||
228 | instance Rearrange ExpType where | 224 | instance Rearrange ExpType where |
229 | rearrange l f (ET e t) = ET (rearrange l f e) (rearrange l f t) | 225 | rearrange l f (ET e t) = ET (rearrange l f e) (rearrange l f t) |
230 | 226 | ||
231 | instance PShow ExpType where pShow (ET x t) = DAnn (pShow x) (pShow t) | 227 | instance PShow ExpType where pShow = mkDoc False |
232 | 228 | ||
233 | type SExp2 = SExp' ExpType | 229 | type SExp2 = SExp' ExpType |
234 | 230 | ||
@@ -246,22 +242,22 @@ isRHS _ = False | |||
246 | pattern Reverse xs <- (reverse -> xs) | 242 | pattern Reverse xs <- (reverse -> xs) |
247 | where Reverse = reverse | 243 | where Reverse = reverse |
248 | 244 | ||
249 | pattern Fun' f vs xs n <- Fun_ _ f vs xs n | 245 | pattern Fun' f vs xs n <- Fun_ _ f vs xs n |
250 | where Fun' f vs xs n = Fun_ (foldMap maxDB_ vs <> foldMap maxDB_ xs {- <> maxDB_ n-}) f vs xs n | 246 | where Fun' f vs xs n = Fun_ (foldMap maxDB_ vs <> foldMap maxDB_ xs {- <> maxDB_ n-}) f vs xs n |
251 | pattern CaseFun_ a b c <- CaseFun__ _ a b c | 247 | pattern CaseFun_ a b c <- CaseFun__ _ a b c |
252 | where CaseFun_ a b c = CaseFun__ (maxDB_ c <> foldMap maxDB_ b) a b c | 248 | where CaseFun_ a b c = CaseFun__ (maxDB_ c <> foldMap maxDB_ b) a b c |
253 | pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c | 249 | pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c |
254 | where TyCaseFun_ a b c = TyCaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c | 250 | where TyCaseFun_ a b c = TyCaseFun__ (foldMap maxDB_ b <> maxDB_ c) a b c |
255 | pattern App_ a b <- App__ _ a b | 251 | pattern App_ a b <- App__ _ a b |
256 | where App_ a b = App__ (maxDB_ a <> maxDB_ b) a b | 252 | where App_ a b = App__ (maxDB_ a <> maxDB_ b) a b |
257 | pattern Con x n y <- Con_ _ x n y | 253 | pattern Con x n y <- Con_ _ x n y |
258 | where Con x n y = Con_ (foldMap maxDB_ y) x n y | 254 | where Con x n y = Con_ (foldMap maxDB_ y) x n y |
259 | pattern TyCon x y <- TyCon_ _ x y | 255 | pattern TyCon x y <- TyCon_ _ x y |
260 | where TyCon x y = TyCon_ (foldMap maxDB_ y) x y | 256 | where TyCon x y = TyCon_ (foldMap maxDB_ y) x y |
261 | pattern Lam y <- Lam_ _ y | 257 | pattern Lam y <- Lam_ _ y |
262 | where Lam y = Lam_ (lowerDB (maxDB_ y)) y | 258 | where Lam y = Lam_ (lowerDB (maxDB_ y)) y |
263 | pattern Pi v x y <- Pi_ _ v x y | 259 | pattern Pi v x y <- Pi_ _ v x y |
264 | where Pi v x y = Pi_ (maxDB_ x <> lowerDB (maxDB_ y)) v x y | 260 | where Pi v x y = Pi_ (maxDB_ x <> lowerDB (maxDB_ y)) v x y |
265 | 261 | ||
266 | pattern CaseFun a b c = Neut (CaseFun_ a b c) | 262 | pattern CaseFun a b c = Neut (CaseFun_ a b c) |
267 | pattern TyCaseFun a b c = Neut (TyCaseFun_ a b c) | 263 | pattern TyCaseFun a b c = Neut (TyCaseFun_ a b c) |
@@ -584,13 +580,14 @@ class MkDoc a where | |||
584 | mkDoc :: Bool {-print reduced-} -> a -> Doc | 580 | mkDoc :: Bool {-print reduced-} -> a -> Doc |
585 | 581 | ||
586 | instance MkDoc ExpType where | 582 | instance MkDoc ExpType where |
587 | mkDoc pr (ET e _) = mkDoc pr e | 583 | mkDoc pr (ET e TType) = mkDoc pr e |
584 | mkDoc pr (ET e t) = DAnn (mkDoc pr e) (mkDoc pr t) | ||
588 | 585 | ||
589 | instance MkDoc Exp where | 586 | instance MkDoc Exp where |
590 | mkDoc pr e = green $ f e | 587 | mkDoc pr e = f e |
591 | where | 588 | where |
592 | f = \case | 589 | f = \case |
593 | Lam b -> shLam_ True (BLam Visible) Nothing{-todo!-} (f b) | 590 | Lam b -> shLam_ (usedVar 0 b) (BLam Visible) Nothing (f b) |
594 | Pi h TType b -> shLam_ (usedVar 0 b) (BPi h) Nothing (f b) | 591 | Pi h TType b -> shLam_ (usedVar 0 b) (BPi h) Nothing (f b) |
595 | Pi h a b -> shLam (usedVar 0 b) (BPi h) (f a) (f b) | 592 | Pi h a b -> shLam (usedVar 0 b) (BPi h) (f a) (f b) |
596 | ENat' n -> pShow n | 593 | ENat' n -> pShow n |
@@ -605,17 +602,17 @@ instance MkDoc Exp where | |||
605 | RHS x -> text "_rhs" `DApp` f x | 602 | RHS x -> text "_rhs" `DApp` f x |
606 | 603 | ||
607 | instance MkDoc Neutral where | 604 | instance MkDoc Neutral where |
608 | mkDoc pr e = green $ f e | 605 | mkDoc pr e = f e |
609 | where | 606 | where |
610 | g = mkDoc pr | 607 | g = mkDoc pr |
611 | f = \case | 608 | f = \case |
612 | CstrT' t a b -> shCstr (g (ET a t)) (g (ET b t)) | 609 | CstrT' t a b -> shCstr (g a) (g (ET b t)) |
613 | Reduced' a | pr -> g a | 610 | Reduced' a | pr -> g a |
614 | Fun' s vs (mkExpTypes (nType s) . reverse -> xs) _ -> foldl (shApp Visible) (pShow s) (g <$> xs) | 611 | Fun' s vs xs _ -> foldl DApp (pShow s) (g <$> reverse xs) |
615 | Var_ k -> shVar k | 612 | Var_ k -> shVar k |
616 | App_ a b -> shApp Visible (g a) (g b) | 613 | App_ a b -> f a `DApp` g b |
617 | CaseFun_ s xs n -> foldl (shApp Visible) (pShow s) (map g $ {-mkExpTypes (nType s) $ makeCaseFunPars te n ++ -} xs ++ [Neut n]) | 614 | CaseFun_ s xs n -> foldl DApp (pShow s) (map g $ xs ++ [Neut n]) |
618 | TyCaseFun_ s [m, t, f] n -> foldl (shApp Visible) (pShow s) (g <$> mkExpTypes (nType s) [m, t, Neut n, f]) | 615 | TyCaseFun_ s [m, t, f] n -> foldl DApp (pShow s) (g <$> [m, t, Neut n, f]) |
619 | TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" | 616 | TyCaseFun_ s _ n -> error $ "mkDoc TyCaseFun" |
620 | 617 | ||
621 | getTup (hnf -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs | 618 | getTup (hnf -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs |
@@ -804,9 +801,6 @@ app_ (Neut f) a = neutApp f a | |||
804 | 801 | ||
805 | conType (snd . getParams . hnf -> TyCon (TyConName _ _ _ cs _) _) (ConName _ n t) = t --snd $ cs !! n | 802 | conType (snd . getParams . hnf -> TyCon (TyConName _ _ _ cs _) _) (ConName _ n t) = t --snd $ cs !! n |
806 | 803 | ||
807 | mkExpTypes t [] = [] | ||
808 | mkExpTypes t@(Pi _ a _) (x: xs) = ET x t: mkExpTypes (appTy t x) xs | ||
809 | |||
810 | appTy (Pi _ a b) x = subst 0 x b | 804 | appTy (Pi _ a b) x = subst 0 x b |
811 | appTy t x = error $ "appTy: " ++ ppShow t | 805 | appTy t x = error $ "appTy: " ++ ppShow t |
812 | 806 | ||
@@ -818,9 +812,9 @@ getParams x = ([], x) | |||
818 | 812 | ||
819 | class NType a where nType :: a -> Type | 813 | class NType a where nType :: a -> Type |
820 | 814 | ||
821 | instance NType FunName where nType (FunName _ _ t) = t | 815 | instance NType FunName where nType (FunName _ _ t) = t |
822 | instance NType TyConName where nType (TyConName _ _ t _ _) = t | 816 | instance NType TyConName where nType (TyConName _ _ t _ _) = t |
823 | instance NType CaseFunName where nType (CaseFunName _ t _) = t | 817 | instance NType CaseFunName where nType (CaseFunName _ t _) = t |
824 | instance NType TyCaseFunName where nType (TyCaseFunName _ t) = t | 818 | instance NType TyCaseFunName where nType (TyCaseFunName _ t) = t |
825 | 819 | ||
826 | instance NType Lit where | 820 | instance NType Lit where |