Skip to content

Commit 45559e0

Browse files
authored
Merge pull request #9 from input-output-hk/ts/adjustments
Add some debugging features and better explanations
2 parents 35b625e + 0ab52c3 commit 45559e0

File tree

6 files changed

+69
-21
lines changed

6 files changed

+69
-21
lines changed

src/Constrained/GenT.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Constrained.GenT (
4040

4141
-- * So far undocumented
4242
fatalError,
43+
getMessages,
4344
catMessages,
4445
catMessageList,
4546
explain,

src/Constrained/Generation.hs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,10 @@ prepareLinearization p = do
220220
explainNE
221221
( NE.fromList
222222
[ "Linearizing"
223-
, show $ " preds: " <> pretty preds
223+
, show $
224+
" preds: "
225+
<> pretty (take 3 preds)
226+
<> (if length preds > 3 then fromString (" ... " ++ show (length preds - 3) ++ " more.") else "")
224227
, show $ " graph: " <> pretty graph
225228
]
226229
)
@@ -846,7 +849,7 @@ isEmptyPlan (SolverPlan plan _) = null plan
846849

847850
stepPlan :: MonadGenError m => Env -> SolverPlan -> GenT m (Env, SolverPlan)
848851
stepPlan env plan@(SolverPlan [] _) = pure (env, plan)
849-
stepPlan env (SolverPlan (SolverStage x ps spec : pl) gr) = do
852+
stepPlan env (SolverPlan (SolverStage (x :: Var a) ps spec : pl) gr) = do
850853
(spec', specs) <- runGE
851854
$ explain
852855
( show
@@ -864,6 +867,8 @@ stepPlan env (SolverPlan (SolverStage x ps spec : pl) gr) = do
864867
( NE.fromList
865868
( ( "\nStepPlan for variable: "
866869
++ show x
870+
++ "::"
871+
++ showType @a
867872
++ " fails to produce Specification, probably overconstrained."
868873
++ "PS = "
869874
++ unlines (map show ps)
@@ -896,8 +901,10 @@ genFromPreds env0 (optimisePred . optimisePred -> preds) =
896901
go :: Env -> SolverPlan -> GenT m Env
897902
go env plan | isEmptyPlan plan = pure env
898903
go env plan = do
904+
(mess :: String) <- (unlines . map NE.head) <$> getMessages
899905
(env', plan') <-
900-
explain (show $ "Stepping the plan:" /> vsep [pretty plan, pretty env]) $ stepPlan env plan
906+
explain (show (fromString (mess ++ "Stepping the plan:") /> vsep [pretty plan, pretty env])) $
907+
stepPlan env plan
901908
go env' plan'
902909

903910
-- | Push as much information we can backwards through the plan.
@@ -1305,9 +1312,12 @@ data SolverStage where
13051312
} ->
13061313
SolverStage
13071314

1315+
docVar :: Typeable a => Var a -> Doc h
1316+
docVar (v :: Var a) = fromString (show v ++ " :: " ++ showType @a)
1317+
13081318
instance Pretty SolverStage where
13091319
pretty SolverStage {..} =
1310-
viaShow stageVar
1320+
docVar stageVar
13111321
<+> "<-"
13121322
/> vsep'
13131323
( [pretty stageSpec | not $ isTrueSpec stageSpec]

src/Constrained/NumOrd.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ module Constrained.NumOrd (
5151
import Constrained.AbstractSyntax
5252
import Constrained.Base
5353
import Constrained.Conformance
54-
import Constrained.Conformance ()
5554
import Constrained.Core (Value (..), unionWithMaybe)
5655
import Constrained.FunctionSymbol
5756
import Constrained.GenT

src/Constrained/PrettyUtils.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,22 @@ infixl 5 />
7777
showType :: forall t. Typeable t => String
7878
showType = show (typeRep (Proxy @t))
7979

80+
-- | Set to True if you need to see everything while debugging
81+
verboseShort :: Bool
82+
verboseShort = True
83+
8084
-- | Pretty-print a short list in full and truncate longer lists
8185
short :: forall a x. (Show a, Typeable a) => [a] -> Doc x
8286
short [] = "[]"
8387
short [x] =
8488
let raw = show x
85-
refined = if length raw <= 20 then raw else take 20 raw ++ " ... "
89+
refined = if length raw <= 20 || verboseShort then raw else take 20 raw ++ " ... "
8690
in "[" <+> fromString refined <+> "]"
8791
short xs =
88-
let raw = show xs
89-
in if length raw <= 50
90-
then fromString raw
91-
else "([" <+> viaShow (length xs) <+> "elements ...] @" <> prettyType @a <> ")"
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/Map.hs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,39 @@ instance
186186
n <- genFromSpecT size'
187187
let go 0 _ m = pure m
188188
go n' kvs' m = do
189-
mkv <- tryGenT $ genFromSpecT kvs'
189+
mkv <- inspect $ genFromSpecT kvs'
190190
case mkv of
191-
Nothing
192-
| sizeOf m `conformsToSpec` size -> pure m
193-
Just (k, v) ->
191+
Result (k, v) ->
194192
go
195193
(n' - 1)
196194
(kvs' <> typeSpec (Cartesian (notEqualSpec k) mempty))
197195
(Map.insert k v m)
198-
_ ->
196+
GenError msgs ->
197+
if sizeOf m `conformsToSpec` size
198+
then pure m
199+
else
200+
genErrorNE
201+
(pure "Gen error while trying to generate enough elements for a Map." <> catMessageList msgs)
202+
FatalError msgs ->
199203
genErrorNE
200204
( NE.fromList
201-
[ "Failed to generate enough elements for the map:"
202-
, " m = " ++ show m
203-
, " n' = " ++ show n'
204-
, show $ " kvs' = " <> pretty kvs'
205-
, show $ " simplifySpec kvs' = " <> pretty (simplifySpec kvs')
205+
[ "Fatal error while trying to generate enough elements for a map:"
206+
, " The ones we have generated so far = " ++ show m
207+
, " The number we need to still generate: n' = " ++ show n'
208+
, "The original size spec " ++ show size
209+
, "The refined size spec " ++ show size'
210+
, "The computed target size " ++ show n
211+
, "Fatal error messages"
212+
, "<<<---"
213+
-- , "The kvs Spec"
214+
-- , " >>>> "++ show $ " kvs' = " <> pretty kvs'
215+
-- , "The simplified kvs Spec"
216+
-- , " >>>> "++ show(pretty (simplifySpec kvs'))
206217
]
218+
<> catMessageList msgs
219+
<> (pure "--->>>")
207220
)
208-
explain (" n = " ++ show n) $ go n kvs mempty
221+
explain (" The number we are trying for: n = " ++ show n) $ go n kvs mempty
209222
genFromTypeSpec (MapSpec mHint mustKeys mustVals size (simplifySpec -> kvs) foldSpec) = do
210223
!mustMap <- explain "Make the mustMap" $ forM (Set.toList mustKeys) $ \k -> do
211224
let vSpec = constrained $ \v -> satisfies (pair_ (Lit k) v) kvs

src/Constrained/Syntax.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Constrained.Syntax (
3131
explanation,
3232
assertReified,
3333
reify,
34+
reifyWithName,
3435
letBind,
3536
unsafeExists,
3637
forAll,
@@ -210,6 +211,23 @@ reify t f body =
210211
, Explain (pure ("reify " ++ show t ++ " somef $")) $ toPred (body x)
211212
]
212213

214+
-- | Like `reify` but provide a @[`var`| ... |]@-style name explicitly
215+
reifyWithName ::
216+
( HasSpec a
217+
, HasSpec b
218+
, IsPred p
219+
) =>
220+
String ->
221+
Term a ->
222+
(a -> b) ->
223+
(Term b -> p) ->
224+
Pred
225+
reifyWithName nam t f body =
226+
exists (\eval -> pure $ f (eval t)) $ \(name nam -> x) ->
227+
[ reifies x t f
228+
, Explain (pure ("reify " ++ show t ++ " somef $")) $ toPred (body x)
229+
]
230+
213231
-- | Like `suchThat` for constraints
214232
assertReified :: HasSpec a => Term a -> (a -> Bool) -> Pred
215233
-- Note, it is necessary to introduce the extra variable from the `exists` here

0 commit comments

Comments
 (0)