Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: more compact pretty printing #1094

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions primer/src/Primer/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ data PrettyOptions = PrettyOptions
-- ^ Attempt to print λs and Λs on one line
, inlineForAll :: Bool
-- ^ Attempt to print @for all@ and associated type sig on one line
, inlineCon :: Bool
-- ^ Attempt to print constructors and all arguments on one line
, inlineAnn :: Bool
-- ^ Attempt to print annotations on one line
}

-- | Default PrettyOptions - makes no attempt to group text
Expand All @@ -71,6 +75,8 @@ sparse =
, inlineLet = False
, inlineLambda = False
, inlineForAll = False
, inlineCon = False
, inlineAnn = False
}

-- | Groups whenever possible
Expand All @@ -82,6 +88,8 @@ compact =
, inlineLet = True
, inlineLambda = True
, inlineForAll = True
, inlineCon = True
, inlineAnn = True
}

-- | Pretty prints @Expr'@ using Prettyprinter library
Expand All @@ -91,7 +99,7 @@ prettyExpr opts = \case
EmptyHole _ -> col Red "?"
Con _ n tms ->
let prettyTms = brac Round White . pE <$> tms
in vsep $ col Green (gname opts n) : prettyTms
in (if inlineCon opts then group else identity) $ vsep $ col Green (gname opts n) : prettyTms
Var _ v -> case v of
GlobalVarRef n -> col Blue (gname opts n)
LocalVarRef n -> lname n
Expand Down Expand Up @@ -161,31 +169,31 @@ prettyExpr opts = \case
<> inlineblock opts (pE e)
<> col Yellow "in"
<> line
<> indent' 2 (pE e')
<> indent' 2 (brac Round White $ pE e')
LetType _ v t e ->
col Yellow "let type"
<+> lname v
<+> col Yellow "="
<> inlineblock opts (pT t)
<> col Yellow "in"
<> line
<> indent' 2 (pE e)
<> indent' 2 (brac Round White $ pE e)
Letrec _ v e t e' ->
col Yellow "let rec"
<+> lname v
<+> col Yellow "="
<> inlineblock opts (typeann e t)
<> col Yellow "in"
<> line
<> indent' 2 (pE e')
<> indent' 2 (brac Round White $ pE e')
PrimCon _ p -> prim p
where
pT = prettyType opts
pE = prettyExpr opts
prim = \case
PrimChar c -> "Char" <+> pretty @Text (show c)
PrimInt n -> "Int" <+> pretty @Text (show n)
typeann e t = brac Round Yellow (pE e) <+> col Yellow "::" <> line <> brac Round Yellow (pT t)
typeann e t = brac Round Yellow (pE e) <+> (if inlineAnn opts then group else identity) (col Yellow "::" <> line <> brac Round Yellow (pT t))

-- When grouped: " x "
-- When ungrouped: "\n\tx\n"
Expand Down Expand Up @@ -226,10 +234,15 @@ rBrac Curly = "?}"

-- Adds brackets of type b around "doc" with color c
brac :: BracketType -> Color -> Doc AnsiStyle -> Doc AnsiStyle
brac b c doc = col c (lBrac b) <> line' <> flatAlt (indent 2 doc) doc <> line' <> col c (rBrac b)
--brac b c doc = col c (lBrac b) <> line' <> flatAlt (indent 2 doc) doc <> line' <> col c (rBrac b)
brac b c doc = if 1 == length (T.words $ show doc) -- TODO: hack to avoid superfluous brackets
then doc
else col c (lBrac b) <> line' <> flatAlt (indent 2 doc) doc <> line' <> col c (rBrac b)

col :: Color -> Doc AnsiStyle -> Doc AnsiStyle
col = annotate . color
col = \case
White -> identity -- TODO: hack! we use "White" to mean "terminal default"
c -> annotate $ color c

-- | Pretty prints @Type'@ using Prettyprinter library
prettyType :: PrettyOptions -> Type' b -> Doc AnsiStyle
Expand All @@ -254,7 +267,7 @@ prettyType opts typ = case typ of
<> inlineblock opts (pT t)
<> col Yellow "in"
<> line
<> indent' 2 (pT b)
<> indent' 2 (brac Round White $ pT b)
where
pT = prettyType opts

Expand Down
71 changes: 27 additions & 44 deletions primer/test/outputs/Pretty/comprehensive/Expr (Compact).ansi
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
let x = (True) :: (Bool) in
let rec y =
(
(
{?(Just (?)) :: ((Maybe) (?))?}
)
(
{?unboundName?}
)
) ::
(
{?Maybe?}
)
in
(
λi.
Λβ.
(
(
let type b = Bool in
Left
) @β
)
(
match i with
Zero → False
Succ n → match n with
Zero → (
(
?
)
(
x
)
)
(
y
)
_ → ?
)
) ::
(
Nat -> ∀ α. ((Either) (Bool)) (α)
)
let x = True :: Bool in
(
let rec y = (({?(Just ?) :: (Maybe ?)?}) unboundName) :: Maybe in
(
(
λi.
Λβ.
(
(
let type b = Bool in
Left
) @β
)
(
match i with
Zero → False
Succ n → match n with
Zero → (
?
x
)
y
_ → ?
)
) :: (Nat -> ∀ α. (Either Bool) α)
)
)
130 changes: 52 additions & 78 deletions primer/test/outputs/Pretty/comprehensive/Expr (Sparse).ansi
Original file line number Diff line number Diff line change
@@ -1,83 +1,57 @@
let x =
(
True
) ::
(
Bool
)
True ::
Bool
in
let rec y =
(
(
{?
(
Just
(
(
let rec y =
(
(
{?
(
Just
?
)
) ::
(
(
) ::
(
Maybe
)
(
?
)
)
?}
)
(
{?
unboundName
?}
)
) ::
(
{?
Maybe
?}
)
in
(
λi.
Λβ.
(
(
let type b =
Bool
in
Left
) @β
)
(
match i with
Zero → False
Succ n → match n with
Zero → (
(
?
)
(
x
)
)
(
y
)
_ → ?
)
) ::
(
Nat -> ∀ α.
(
(
Either
)
(
Bool
)
)
(
α
)
)
)
?}
)
unboundName
) ::
Maybe
in
(
(
λi.
Λβ.
(
(
let type b =
Bool
in
Left
) @β
)
(
match i with
Zero → False
Succ n → match n with
Zero → (
?
x
)
y
_ → ?
)
) ::
(
Nat -> ∀ α.
(
Either
Bool
)
α
)
)
)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Nat -> ∀ a. ({?(List) (?)?}) (a)
Nat -> ∀ a. ({?List ??}) a
16 changes: 5 additions & 11 deletions primer/test/outputs/Pretty/comprehensive/Type (Sparse).ansi
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
Nat -> ∀ a.
(
(
{?
(
List
)
(
?
)
List
?
?}
)
(
a
)
)
a