Skip to content

Commit 365ac6a

Browse files
some prettyprinting improvements
1 parent 8a29de5 commit 365ac6a

File tree

6 files changed

+46
-87
lines changed

6 files changed

+46
-87
lines changed

src/Constrained/AbstractSyntax.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ instance (Show a, Typeable a, Show (TypeSpecD deps a)) => Pretty (WithPrec (Spec
377377
ExplainSpec es z -> "ExplainSpec" <+> viaShow es <+> "$" /> pretty z
378378
ErrorSpec es -> "ErrorSpec" /> vsep' (map fromString (NE.toList es))
379379
TrueSpec -> fromString $ "TrueSpec @(" ++ showType @a ++ ")"
380-
MemberSpec xs -> "MemberSpec" <+> short (NE.toList xs)
380+
MemberSpec xs -> "MemberSpec" <+> viaShow (NE.toList xs)
381381
SuspendedSpec x p -> parensIf (d > 10) $ "constrained $ \\" <+> viaShow x <+> "->" /> pretty p
382382
-- TODO: require pretty for `TypeSpec` to make this much nicer
383383
TypeSpecD ts cant ->

src/Constrained/Env.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module Constrained.Env (
2020

2121
import Constrained.Core
2222
import Constrained.GenT
23+
import Constrained.PrettyUtils
2324
import Data.Map (Map)
2425
import Data.Map qualified as Map
2526
import Data.Typeable
@@ -85,6 +86,6 @@ instance Pretty EnvKey where
8586
pretty (EnvKey x) = viaShow x
8687

8788
instance Pretty Env where
88-
pretty (Env m) = vsep ("Env" : (map f (Map.toList m)))
89+
pretty (Env m) = "Env" /> vsep (map f (Map.toList m))
8990
where
9091
f (k, v) = hsep [pretty k, "->", pretty v]

src/Constrained/Generation.hs

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ genFromSpecT (simplifySpec -> spec) = case spec of
121121
[ "genFromSpecT on (TypeSpec tspec cant) at type " ++ showType @a
122122
, "tspec = "
123123
, show s
124-
, "cant = " ++ show (short cant)
124+
, "cant = " ++ show cant
125125
, "with mode " ++ show mode
126126
]
127127
)
@@ -830,21 +830,21 @@ mergeSolverStage :: SolverStage -> [SolverStage] -> [SolverStage]
830830
mergeSolverStage (SolverStage x ps spec relevant) plan =
831831
[ case eqVar x y of
832832
Just Refl ->
833-
SolverStage
834-
y
835-
(ps ++ ps')
836-
( addToErrorSpec
837-
( NE.fromList
838-
( [ "Solving var " ++ show x ++ " fails."
839-
, "Merging the Specs"
840-
, " 1. " ++ show spec
841-
, " 2. " ++ show spec'
842-
]
843-
)
844-
)
845-
(spec <> spec')
846-
)
847-
(relevant <> relevant')
833+
normalizeSolverStage $ SolverStage
834+
y
835+
(ps ++ ps')
836+
( addToErrorSpec
837+
( NE.fromList
838+
( [ "Solving var " ++ show x ++ " fails."
839+
, "Merging the Specs"
840+
, " 1. " ++ show spec
841+
, " 2. " ++ show spec'
842+
]
843+
)
844+
)
845+
(spec <> spec')
846+
)
847+
(relevant <> relevant')
848848
Nothing -> stage
849849
| stage@(SolverStage y ps' spec' relevant') <- plan
850850
]
@@ -856,49 +856,20 @@ stepPlan :: MonadGenError m => SolverPlan -> Env -> SolverPlan -> GenT m (Env, S
856856
stepPlan _ env plan@(SolverPlan []) = pure (env, plan)
857857
stepPlan (SolverPlan origStages) env (SolverPlan (stage@(SolverStage (x :: Var a) ps spec relevant) : pl)) = do
858858
let errorMessage = "Failed to step the plan" />
859-
vsep [ "Relevant parts of original plan: " /> pretty narrowedOrigPlan
860-
, "Relevant parts of the env: " /> pretty narrowedEnv
861-
, "Current stage: " /> pretty stage
859+
vsep [ "Relevant parts of original plan:" //> pretty narrowedOrigPlan
860+
, "Relevant parts of the env:" //> pretty narrowedEnv
861+
, "Current stage:" //> pretty stage
862862
]
863863
-- TODO: tests for this, including tests for transitive behaviour
864864
relevant' = Set.insert (Name x) relevant
865865
narrowedOrigPlan = SolverPlan $ [ st | st@(SolverStage v _ _ _) <- origStages, Name v `Set.member` relevant' ]
866866
narrowedEnv = Env.filterKeys env (\v -> nameOf v `Set.member` (Set.map (\ (Name n) -> nameOf n) relevant'))
867867
explain (show errorMessage) $ do
868-
(spec', specs) <- runGE
869-
$ explain
870-
( show
871-
( "Computing specs for variable "
872-
<> pretty x
873-
/> vsep' (map pretty ps)
874-
)
875-
)
876-
$ do
877-
ispecs <- mapM (computeSpec x) ps
878-
pure $ (fold ispecs, ispecs)
879-
val <-
880-
genFromSpecT
881-
( addToErrorSpec
882-
( NE.fromList
883-
( ( "\nStepPlan for variable: "
884-
++ show x
885-
++ "::"
886-
++ showType @a
887-
++ " fails to produce Specification, probably overconstrained."
888-
++ "PS = "
889-
++ unlines (map show ps)
890-
)
891-
: ("Relevant variables " ++ show relevant)
892-
: ("Original spec " ++ show spec)
893-
: "Predicates"
894-
: zipWith
895-
(\pred specx -> " pred " ++ show pred ++ " -> " ++ show specx)
896-
ps
897-
specs
898-
)
899-
)
900-
(spec <> spec')
901-
)
868+
when (isErrorLike spec) $
869+
genError "The specification in the current stage is unsatisfiable, giving up."
870+
when (not $ null ps) $
871+
fatalError "Something went wrong and not all predicates have been discharged. Report this as a bug in Constrained.Generation"
872+
val <- genFromSpecT spec
902873
let env1 = Env.extend x val env
903874
pure (env1, backPropagation relevant' $ SolverPlan (substStage relevant' x val <$> pl) )
904875

src/Constrained/PrettyUtils.hs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ module Constrained.PrettyUtils (
2323
prettyType,
2424
vsep',
2525
(/>),
26+
(//>),
2627
showType,
27-
short,
2828
) where
2929

3030
import Constrained.List
@@ -73,26 +73,13 @@ h /> cont = hang 2 $ sep [h, align cont]
7373

7474
infixl 5 />
7575

76+
-- | Lay the header (first argument) out above the body
77+
-- and and indent the body by 2
78+
(//>) :: Doc ann -> Doc ann -> Doc ann
79+
h //> cont = hang 2 $ vsep [h, align cont]
80+
81+
infixl 5 //>
82+
7683
-- | Show a `Typeable` thing's type
7784
showType :: forall t. Typeable t => String
7885
showType = show (typeRep (Proxy @t))
79-
80-
-- | Set to True if you need to see everything while debugging
81-
verboseShort :: Bool
82-
verboseShort = True
83-
84-
-- | Pretty-print a short list in full and truncate longer lists
85-
short :: forall a x. (Show a, Typeable a) => [a] -> Doc x
86-
short [] = "[]"
87-
short [x] =
88-
let raw = show x
89-
refined = if length raw <= 20 || verboseShort then raw else take 20 raw ++ " ... "
90-
in "[" <+> fromString refined <+> "]"
91-
short xs =
92-
if verboseShort
93-
then fromString $ unlines (map show xs)
94-
else
95-
let raw = show xs
96-
in if length raw <= 50
97-
then fromString raw
98-
else "([" <+> viaShow (length xs) <+> "elements ...] @" <> prettyType @a <> ")"

src/Constrained/Spec/List.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ instance Semantics ListW where
358358
semantics AppendW = (++)
359359

360360
instance Syntax ListW where
361-
prettySymbol AppendW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "append_" <+> short n <+> prettyPrec 10 y
362-
prettySymbol AppendW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "append_" <+> prettyPrec 10 y <+> short n
361+
prettySymbol AppendW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "append_" <+> viaShow n <+> prettyPrec 10 y
362+
prettySymbol AppendW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "append_" <+> prettyPrec 10 y <+> viaShow n
363363
prettySymbol _ _ _ = Nothing
364364

365365
instance Show (ListW d r) where

src/Constrained/Spec/Set.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ prettySetSpec :: HasSpec a => SetSpec a -> Doc ann
8787
prettySetSpec (SetSpec must elemS size) =
8888
parens
8989
( "SetSpec"
90-
/> sep ["must=" <> short (Set.toList must), "elem=" <> pretty elemS, "size=" <> pretty size]
90+
/> sep ["must=" <> viaShow (Set.toList must), "elem=" <> pretty elemS, "size=" <> pretty size]
9191
)
9292

9393
instance HasSpec a => Show (SetSpec a) where
@@ -163,7 +163,7 @@ instance (Ord a, HasSpec a) => HasSpec (Set a) where
163163
( NE.fromList
164164
[ "Choose size = " ++ show chosenSize
165165
, "szSpec' = " ++ show szSpec'
166-
, "Picking items not in must = " ++ show (short (Set.toList must))
166+
, "Picking items not in must = " ++ show (Set.toList must)
167167
, "that also meet the element test: "
168168
, " " ++ show elemS
169169
]
@@ -251,13 +251,13 @@ instance Semantics SetW where
251251
semantics = setSem
252252

253253
instance Syntax SetW where
254-
prettySymbol SubsetW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "subset_" <+> short (Set.toList n) <+> prettyPrec 10 y
255-
prettySymbol SubsetW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "subset_" <+> prettyPrec 10 y <+> short (Set.toList n)
256-
prettySymbol DisjointW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "disjoint_" <+> short (Set.toList n) <+> prettyPrec 10 y
257-
prettySymbol DisjointW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "disjoint_" <+> prettyPrec 10 y <+> short (Set.toList n)
258-
prettySymbol UnionW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "union_" <+> short (Set.toList n) <+> prettyPrec 10 y
259-
prettySymbol UnionW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "union_" <+> prettyPrec 10 y <+> short (Set.toList n)
260-
prettySymbol MemberW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "member_" <+> prettyPrec 10 y <+> short (Set.toList n)
254+
prettySymbol SubsetW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "subset_" <+> viaShow (Set.toList n) <+> prettyPrec 10 y
255+
prettySymbol SubsetW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "subset_" <+> prettyPrec 10 y <+> viaShow (Set.toList n)
256+
prettySymbol DisjointW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "disjoint_" <+> viaShow (Set.toList n) <+> prettyPrec 10 y
257+
prettySymbol DisjointW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "disjoint_" <+> prettyPrec 10 y <+> viaShow (Set.toList n)
258+
prettySymbol UnionW (Lit n :> y :> Nil) p = Just $ parensIf (p > 10) $ "union_" <+> viaShow (Set.toList n) <+> prettyPrec 10 y
259+
prettySymbol UnionW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "union_" <+> prettyPrec 10 y <+> viaShow (Set.toList n)
260+
prettySymbol MemberW (y :> Lit n :> Nil) p = Just $ parensIf (p > 10) $ "member_" <+> prettyPrec 10 y <+> viaShow (Set.toList n)
261261
prettySymbol _ _ _ = Nothing
262262

263263
instance (Ord a, HasSpec a, HasSpec (Set a)) => Semigroup (Term (Set a)) where

0 commit comments

Comments
 (0)