Skip to content

Fix pretty printing problems - #28

Merged
jvoigtlaender merged 12 commits into
fmidue:masterfrom
nimec01:pretty-printing
Oct 27, 2026
Merged

jvoigtlaender merged 12 commits into
fmidue:masterfrom
nimec01:pretty-printing

Conversation

@nimec01

@nimec01 nimec01 commented Oct 20, 2026

Copy link
Copy Markdown

Closes #26
Closes #27

@jvoigtlaender

Copy link
Copy Markdown
Member

@patritzenfeld, can you take a look and estimate whether this may have detrimental effects somewhere?

@patritzenfeld

patritzenfeld commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Using this example:

consultString "p :- a;b, X = z, g(X,Y).

would previously result in

Right [("p",[],[a;,(b, (X = z, g(X, Y)))])]

which already looks weird.

Now it parses to

Right [("p",[],[a;,(b, (,(X = z, g(X, Y))))])]

with another comma.

Also, this term seems to be interpreted incorrectly: a;,(b, (,(X = z, g(X, Y)))) is interpreted as one single predicate/condition. This would normally be split up, e.g. with

map (\(a,b,c) -> map show c) $ fromRight undefined $ consultString "p :- (a;b), X = z, g(X,Y)."

the conditions are correctly separated

[["a;b","X = z","g(X, Y)"]]

but with

map (\(a,b,c) -> map show c) $ fromRight undefined $ consultString "p :- a;b, X = z, g(X,Y)."

you get

[["a;,(b, (,(X = z, g(X, Y))))"]] instead.

That's problematic as this output is used for prolog-graph-lib, so students could see something like that when using ;.

This seems to be somewhat unrelated to the changes here, except for that extra comma, so maybe we should handle this in another pull request?

I couldn't find any other problematic effects for now.

@nimec01

nimec01 commented Oct 21, 2026

Copy link
Copy Markdown
Author

But we would normally use the pretty printed output for a Clause, right? That would result in

ghci> map (\(n,a,g) -> Clause n a g) <$> consultString "p :- a;b, X = z, g(X,Y)."
Right [p :- (a;(b, (X = z, g(X, Y)))).]

ghci> map (\(n,a,g) -> Clause n a g) <$> consultString "p :- (a;b), X = z, g(X,Y)."
Right [p :- (a;b), X = z, g(X, Y).]

@jvoigtlaender

Copy link
Copy Markdown
Member

Still, maybe this mixture is bad.

One might ask: Why do we ever use the Show instance at all (except possibly for debugging)? If there is a dedicated pretty-printer, why is not all client code using that pretty-printer?

The point being that then Show could simply be a derived instance that produces the standard "Haskell reading" of the values, and hence wouldn't have the mixtures like Right [("p",[],[a;,(b, (,(X = z, g(X, Y))))])].

@jvoigtlaender

Copy link
Copy Markdown
Member

(Radically, we could drop the Show instance for Clause here, then recompile all dependent packages to spot places wher it was previously used, and decide whether to call the pretty-printer there instead or whether there is a genuinely meaningful use of show there and what the resulting String actually should be there.)

@nimec01

nimec01 commented Oct 21, 2026

Copy link
Copy Markdown
Author

Yeah, I also really don't like that the pretty printer is incorporated into the Show instance. We should provide the pretty printer for clauses (do we also need them for terms separately?) as an explicit function and drop the Show instance to make this switch apparent. We can then add the derived instances at a later date.

@nimec01

nimec01 commented Oct 25, 2026

Copy link
Copy Markdown
Author

I let an agent update the implementation such the outputs are simplified now. I also added some test cases to see if things break. It looks good for now, but you might want to expand the test suite with some more cases I didn't cover yet.

Concerning the "remove the Show instance and see where it breaks" approach: removing the Show instances makes the Interpeter module fail to build, so this approach doesn't really work for detecting where the Show instances were used (outside this package)

Comment thread test/PrettyPrintingSpec.hs Resolved
@jvoigtlaender

Copy link
Copy Markdown
Member

I haven't checked the pretty-printing logic in detail, but the unit tests look fine, so let's hope for the best, 😄

@jvoigtlaender
jvoigtlaender merged commit 4f84d64 into fmidue:master Oct 27, 2026
7 checks passed
@nimec01
nimec01 deleted the pretty-printing branch October 27, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pretty print produces wrong output for conjunction Pretty printer produces wrong results for disjuctions

3 participants