summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-04 11:01:16 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-04 11:01:16 +0200
commit51b37f58afba2c9aaf80bb49267aa7b74f757d52 (patch)
treef6f3cf5c3f079dc8559fefd470effba7b3e0933a /src
parent4fd500adc5a8eee72caca044bac5fd11ef10c6b5 (diff)
improve pretty print
Diffstat (limited to 'src')
-rw-r--r--src/LambdaCube/Compiler/Core.hs74
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
17module LambdaCube.Compiler.Core where 13module LambdaCube.Compiler.Core where
18 14
19import Data.Monoid 15import Data.Monoid
@@ -228,7 +224,7 @@ data ExpType = ET {expr :: Exp, ty :: Type}
228instance Rearrange ExpType where 224instance 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
231instance PShow ExpType where pShow (ET x t) = DAnn (pShow x) (pShow t) 227instance PShow ExpType where pShow = mkDoc False
232 228
233type SExp2 = SExp' ExpType 229type SExp2 = SExp' ExpType
234 230
@@ -246,22 +242,22 @@ isRHS _ = False
246pattern Reverse xs <- (reverse -> xs) 242pattern Reverse xs <- (reverse -> xs)
247 where Reverse = reverse 243 where Reverse = reverse
248 244
249pattern Fun' f vs xs n <- Fun_ _ f vs xs n 245pattern 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
251pattern CaseFun_ a b c <- CaseFun__ _ a b c 247pattern 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
253pattern TyCaseFun_ a b c <- TyCaseFun__ _ a b c 249pattern 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
255pattern App_ a b <- App__ _ a b 251pattern 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
257pattern Con x n y <- Con_ _ x n y 253pattern 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
259pattern TyCon x y <- TyCon_ _ x y 255pattern 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
261pattern Lam y <- Lam_ _ y 257pattern Lam y <- Lam_ _ y
262 where Lam y = Lam_ (lowerDB (maxDB_ y)) y 258 where Lam y = Lam_ (lowerDB (maxDB_ y)) y
263pattern Pi v x y <- Pi_ _ v x y 259pattern 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
266pattern CaseFun a b c = Neut (CaseFun_ a b c) 262pattern CaseFun a b c = Neut (CaseFun_ a b c)
267pattern TyCaseFun a b c = Neut (TyCaseFun_ a b c) 263pattern 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
586instance MkDoc ExpType where 582instance 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
589instance MkDoc Exp where 586instance 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
607instance MkDoc Neutral where 604instance 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
621getTup (hnf -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs 618getTup (hnf -> ConN FHCons [_, _, x, xs]) = (x:) <$> getTup xs
@@ -804,9 +801,6 @@ app_ (Neut f) a = neutApp f a
804 801
805conType (snd . getParams . hnf -> TyCon (TyConName _ _ _ cs _) _) (ConName _ n t) = t --snd $ cs !! n 802conType (snd . getParams . hnf -> TyCon (TyConName _ _ _ cs _) _) (ConName _ n t) = t --snd $ cs !! n
806 803
807mkExpTypes t [] = []
808mkExpTypes t@(Pi _ a _) (x: xs) = ET x t: mkExpTypes (appTy t x) xs
809
810appTy (Pi _ a b) x = subst 0 x b 804appTy (Pi _ a b) x = subst 0 x b
811appTy t x = error $ "appTy: " ++ ppShow t 805appTy t x = error $ "appTy: " ++ ppShow t
812 806
@@ -818,9 +812,9 @@ getParams x = ([], x)
818 812
819class NType a where nType :: a -> Type 813class NType a where nType :: a -> Type
820 814
821instance NType FunName where nType (FunName _ _ t) = t 815instance NType FunName where nType (FunName _ _ t) = t
822instance NType TyConName where nType (TyConName _ _ t _ _) = t 816instance NType TyConName where nType (TyConName _ _ t _ _) = t
823instance NType CaseFunName where nType (CaseFunName _ t _) = t 817instance NType CaseFunName where nType (CaseFunName _ t _) = t
824instance NType TyCaseFunName where nType (TyCaseFunName _ t) = t 818instance NType TyCaseFunName where nType (TyCaseFunName _ t) = t
825 819
826instance NType Lit where 820instance NType Lit where