From 8b28015b9a1c0bfb12ac5c7470084f90c10becd9 Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:40:29 +0200 Subject: [PATCH 01/12] fix pretty printer for disjunctions --- src/Syntax.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index 9888ea4..ff562af 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -175,7 +175,7 @@ instance Show VariableName where instance Show Clause where show (Clause name args []) = show (Struct name args) ++ "." - show (Clause name args rhs') = show (Struct name args) ++ " :- " ++ intercalate ", " (map show rhs') ++ "." + show (Clause name args rhs') = show (Struct name args) ++ " :- " ++ intercalate ", " (map (prettyPrint True 1) rhs') ++ "." show (ClauseFn name args _) = show (Struct name args) ++ " :- " ++ "" ++ "." foldrPl :: (Term -> a -> a) -> a -> Term -> a From b497f08fbeb392a6f68c5d47a568832ad4eb2509 Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:41:26 +0200 Subject: [PATCH 02/12] fix pretty printer for conjunctions --- src/Syntax.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index ff562af..f752efe 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -112,8 +112,8 @@ instance Show Term where show = prettyPrint False 0 prettyPrint :: Bool -> Int -> Term -> [Char] -prettyPrint True _ t@(Struct "," [_, _]) = - "(" ++ prettyPrint False 0 t ++ ")" +prettyPrint True _ (Struct "," [l, r]) = + "(" ++ prettyPrint True 0 l ++ ", " ++ prettyPrint True 0 r ++ ")" prettyPrint f n (Struct (flip lookup operatorTable -> Just (p, InfixOp assoc name)) [l, r]) = parensIf (n >= p) $ prettyPrint f n_l l ++ spaced name ++ prettyPrint f n_r r where From 62602412294dd121e3e38dd0621e180a7a648adb Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:00:35 +0200 Subject: [PATCH 03/12] temporarily pin GitHub actions runner to ubuntu-24.04 --- .github/workflows/haskell.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/haskell.yml b/.github/workflows/haskell.yml index b7653dd..8e6b7b9 100644 --- a/.github/workflows/haskell.yml +++ b/.github/workflows/haskell.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: os: - - ubuntu-latest + - ubuntu-24.04 plan: - {build: stack} From 2eecca99b8185b263503b3906d61321674394631 Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:01:54 +0200 Subject: [PATCH 04/12] shorten line --- src/Syntax.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index f752efe..cb35aab 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -175,7 +175,8 @@ instance Show VariableName where instance Show Clause where show (Clause name args []) = show (Struct name args) ++ "." - show (Clause name args rhs') = show (Struct name args) ++ " :- " ++ intercalate ", " (map (prettyPrint True 1) rhs') ++ "." + show (Clause name args rhs') = + show (Struct name args) ++ " :- " ++ intercalate ", " (map (prettyPrint True 1) rhs') ++ "." show (ClauseFn name args _) = show (Struct name args) ++ " :- " ++ "" ++ "." foldrPl :: (Term -> a -> a) -> a -> Term -> a From f6321d6d7f3af47b3814a49e41a5d34cf1610db8 Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:25:13 +0100 Subject: [PATCH 05/12] add unit tests for pretty printing --- package.yaml | 13 +++++++ prolog.cabal | 24 +++++++++++++ test/PrettyPrintingSpec.hs | 72 ++++++++++++++++++++++++++++++++++++++ test/Spec.hs | 1 + 4 files changed, 110 insertions(+) create mode 100644 test/PrettyPrintingSpec.hs create mode 100644 test/Spec.hs diff --git a/package.yaml b/package.yaml index b38ee50..7bd823d 100644 --- a/package.yaml +++ b/package.yaml @@ -63,6 +63,19 @@ tests: - filepath - HUnit - ansi-terminal + unit: + source-dirs: test + main: Spec.hs + ghc-options: + - -Wall + - -Werror + - -threaded + - -rtsopts + - -with-rtsopts=-N + dependencies: + - prolog + - HUnit + - hspec benchmarks: bench: diff --git a/prolog.cabal b/prolog.cabal index a033a2b..76045c0 100644 --- a/prolog.cabal +++ b/prolog.cabal @@ -98,6 +98,30 @@ test-suite spec , transformers >=0.2.2.0 default-language: Haskell2010 +test-suite unit + type: exitcode-stdio-1.0 + main-is: Spec.hs + other-modules: + PrettyPrintingSpec + Paths_prolog + hs-source-dirs: + test + ghc-options: -Wall -Wincomplete-uni-patterns -Werror=incomplete-uni-patterns -Werror=incomplete-patterns -Wall -Werror -threaded -rtsopts -with-rtsopts=-N + build-depends: + HUnit + , base ==4.* + , containers + , hspec + , mtl >=2.0.1.0 + , parsec >=3.1.1 + , pipes + , prolog + , syb >=0.3 + , template-haskell + , th-lift >=0.5.3 + , transformers >=0.2.2.0 + default-language: Haskell2010 + benchmark bench type: exitcode-stdio-1.0 main-is: Main.hs diff --git a/test/PrettyPrintingSpec.hs b/test/PrettyPrintingSpec.hs new file mode 100644 index 0000000..8f81702 --- /dev/null +++ b/test/PrettyPrintingSpec.hs @@ -0,0 +1,72 @@ +module PrettyPrintingSpec where + +import Control.Monad (forM_) +import Language.Prolog (Clause (Clause), Term (Struct), consultString) +import Test.HUnit (assertFailure) +import Test.Hspec (Spec, describe, it, shouldBe) + +dis :: Term -> Term -> Term +dis l r = Struct ";" [l, r] + +con :: Term -> Term -> Term +con l r = Struct "," [l, r] + +a, b, c, d :: Term +a = Struct "a" [] +b = Struct "b" [] +c = Struct "c" [] +d = Struct "d" [] + +preservations :: [(String, String)] +preservations = + [ ("p :- (a;b),c,d.", "p :- (a;b), c, d.") + , ("p :- a;b,c,d.", "p :- a;b, c, d.") + , ("p :- a;(b,c,d).", "p :- a;b, c, d.") + , ("p :- (a;(b,(c,d))).", "p :- a;b, c, d.") + , ("p :- a;b, (c, d).", "p :- a;b, c, d.") + , ("p :- (a, b); c.", "p :- a, b;c.") + , ("p :- a ; b ; c.", "p :- a;b;c.") + , ("p :- a, ! ; b ; c.", "p :- a, !;b;c.") + , ("p :- (a ; b) ; c.", "p :- (a;b);c.") + , ("p :- a, b, c.", "p :- a, b, c.") + , ("p :- \\+ a ; b.", "p :- \\+a;b.") + , ("p :- not(a) ; b.", "p :- not(a);b.") + , ("p :- \\+ (a, b).", "p :- \\+(a, b).") + , ("p :- not(a, b).", "p :- not(a, b).") + , ("p(X) :- X = [a, b, c].", "p(X) :- X = [a,b,c].") + , ("p(X) :- X = [a, b|c].", "p(X) :- X = [a,b|c].") + , ("p(X,Y,Z) :- X is Y + Z.", "p(X, Y, Z) :- X is Y + Z.") + , ("p(X,Y,Z) :- X is Y * Y + Z.", "p(X, Y, Z) :- X is Y * Y + Z.") + , ("p(X,Y,Z) :- X is Y * (Y + Z).", "p(X, Y, Z) :- X is Y * (Y + Z).") + , ("p(X) :- X =.. [Y, Z].", "p(X) :- X =.. [Y,Z].") + , ("p :- q(a, g(b, c)); h.", "p :- q(a, g(b, c));h.") + , ("p(X) :- X = Y, g(Y).", "p(X) :- X = Y, g(Y).") + , ("p(X) :- (X = Y, Y = Z), g(Z).", "p(X) :- X = Y, Y = Z, g(Z).") + , ("p(X, Y) :- X is -Y.", "p(X, Y) :- X is -Y.") + , ("p(X, Y, Z) :- X is -Y - Z.", "p(X, Y, Z) :- X is -Y - Z.") + , ("p(X, Y, Z) :- X is -(Y - Z).", "p(X, Y, Z) :- X is -(Y - Z).") + ] + +expectations :: [(Clause, String)] +expectations = + [ -- a ; (b, (c, d)) + (Clause "p" [] [dis a (con b (con c d))], "p :- a;b, c, d.") + , -- a ; (b, c) + (Clause "p" [] [dis a (con b c)], "p :- a;b, c.") + , -- (a, (b, c)) ; d + (Clause "p" [] [dis (con a (con b c)) d], "p :- a, b, c;d.") + ] + +spec :: Spec +spec = describe "pretty printing" $ do + describe "it should preserve semantics but remove redundant parentheses" $ do + forM_ preservations $ \(input, expected) -> do + it input $ case consultString input of + Left err -> assertFailure $ show err + Right [(n, args, goals)] -> show (Clause n args goals) `shouldBe` expected + _ -> assertFailure "only one clause should be parsed" + + describe "it should correctly print the given value" $ do + forM_ expectations $ \(input, expected) -> do + it (show input) $ + show input `shouldBe` expected diff --git a/test/Spec.hs b/test/Spec.hs new file mode 100644 index 0000000..a824f8c --- /dev/null +++ b/test/Spec.hs @@ -0,0 +1 @@ +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} From 16b09a4e2841c6962c1badb46f1fb0141cdd2f0c Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:28:20 +0100 Subject: [PATCH 06/12] refined implementation by AI --- src/Syntax.hs | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index cb35aab..86e5ed5 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -112,16 +112,19 @@ instance Show Term where show = prettyPrint False 0 prettyPrint :: Bool -> Int -> Term -> [Char] -prettyPrint True _ (Struct "," [l, r]) = - "(" ++ prettyPrint True 0 l ++ ", " ++ prettyPrint True 0 r ++ ")" -prettyPrint f n (Struct (flip lookup operatorTable -> Just (p, InfixOp assoc name)) [l, r]) = - parensIf (n >= p) $ prettyPrint f n_l l ++ spaced name ++ prettyPrint f n_r r +prettyPrint f n (Struct (flip lookup infixOperatorTable -> Just (p, InfixOp assoc name)) [l, r]) = + parensIf addParens $ prettyPrint f' n_l l ++ spaced name ++ prettyPrint f' n_r r where + addParens = f && name == "," || n >= p + f' = not addParens && f (n_l, n_r) = case assoc of AssocLeft -> (p - 1, p) AssocRight -> (p, p - 1) -prettyPrint f n (Struct (flip lookup operatorTable -> Just (p, PrefixOp name)) [r]) = - parensIf (n >= p) $ name ++ prettyPrint f p {- Non-associative -} r +prettyPrint f n (Struct (flip lookup prefixOperatorTable -> Just (p, PrefixOp name)) [r]) = + parensIf addParens $ name ++ prettyPrint f' p {- Non-associative -} r + where + addParens = n >= p + f' = not addParens && f prettyPrint _ _ t@(Struct "." [_, _]) = let (ts, rest) = g [] t in -- case guard (isNil rest) >> sequence (map toChar ts) of @@ -161,11 +164,19 @@ parensIf :: Bool -> String -> String parensIf True s = "(" ++ s ++ ")" parensIf False s = s -operatorTable :: [(String, (Int, Operator))] -operatorTable = concat $ zipWith (map . g) [1 ..] $ hierarchy False - where - g p op@(InfixOp _ name) = (name, (p, op)) - g p op@(PrefixOp name) = (name, (p, op)) +-- Split by arity so that an operator name used as both prefix and infix +-- (e.g. '-') matches the right rule: prefix '-' has precedence 10, infix '-' 5. +infixOperatorTable :: [(Atom, (Int, Operator))] +infixOperatorTable = concat $ zipWith opInfix [1 ..] (hierarchy True) + +prefixOperatorTable :: [(Atom, (Int, Operator))] +prefixOperatorTable = concat $ zipWith opPrefix [1 ..] (hierarchy True) + +opInfix :: Int -> [Operator] -> [(Atom, (Int, Operator))] +opInfix p ops = [(name, (p, op)) | op@(InfixOp _ name) <- ops] + +opPrefix :: Int -> [Operator] -> [(Atom, (Int, Operator))] +opPrefix p ops = [(name, (p, op)) | op@(PrefixOp name) <- ops] instance Show VariableName where show (VariableName 0 v) = v @@ -176,7 +187,7 @@ instance Show VariableName where instance Show Clause where show (Clause name args []) = show (Struct name args) ++ "." show (Clause name args rhs') = - show (Struct name args) ++ " :- " ++ intercalate ", " (map (prettyPrint True 1) rhs') ++ "." + show (Struct name args) ++ " :- " ++ prettyPrint False 0 (foldr1 conjunction (concatMap flattenConjunction rhs')) ++ "." show (ClauseFn name args _) = show (Struct name args) ++ " :- " ++ "" ++ "." foldrPl :: (Term -> a -> a) -> a -> Term -> a @@ -192,6 +203,13 @@ cons t1 t2 = Struct "." [t1, t2] nil :: Term nil = Struct "[]" [] +conjunction :: Term -> Term -> Term +conjunction l r = Struct "," [l, r] + +flattenConjunction :: Term -> [Term] +flattenConjunction (Struct "," [l, r]) = flattenConjunction l ++ flattenConjunction r +flattenConjunction t = [t] + data Operator = PrefixOp String | InfixOp Assoc String From 437be12b9fc834673d393d2b3a963dfafa271950 Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:36:44 +0100 Subject: [PATCH 07/12] remove Show instance and export prettify functions --- src/Prolog.hs | 6 ++++++ src/Syntax.hs | 34 ++++++++++++++++++++-------------- test/PrettyPrintingSpec.hs | 10 +++++----- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/Prolog.hs b/src/Prolog.hs index 80012a7..4f3c89e 100644 --- a/src/Prolog.hs +++ b/src/Prolog.hs @@ -41,6 +41,9 @@ module Prolog ( runQueryN', Signature (..), termSignature, + prettifyVariableName, + prettifyTerm, + prettifyClause, ) where @@ -76,6 +79,9 @@ import Syntax ( Term (..), VariableName (..), cut, + prettifyClause, + prettifyTerm, + prettifyVariableName, rhs, var, ) diff --git a/src/Syntax.hs b/src/Syntax.hs index 86e5ed5..8e3a828 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -27,6 +27,9 @@ module Syntax ( hierarchy, Operator (..), Assoc (..), + prettifyVariableName, + prettifyTerm, + prettifyClause, ) where @@ -108,8 +111,8 @@ compoundTerms :: Term -> Maybe (Int, Atom, [Term]) compoundTerms (Struct a ts) = Just (length ts, a, ts) compoundTerms _ = Nothing -instance Show Term where - show = prettyPrint False 0 +prettifyTerm :: Term -> String +prettifyTerm = prettyPrint False 0 prettyPrint :: Bool -> Int -> Term -> [Char] prettyPrint f n (Struct (flip lookup infixOperatorTable -> Just (p, InfixOp assoc name)) [l, r]) = @@ -141,7 +144,7 @@ prettyPrint _ _ t@(Struct "." [_, _]) = isNil _ = False prettyPrint _ _ (Struct a []) = a prettyPrint _ _ (Struct a ts) = a ++ "(" ++ intercalate ", " (map (prettyPrint True 0) ts) ++ ")" -prettyPrint _ _ (Var v) = show v +prettyPrint _ _ (Var v) = prettifyVariableName v -- prettyPrint _ _ Wildcard = "_" prettyPrint _ _ (Cut _) = "!" @@ -178,17 +181,20 @@ opInfix p ops = [(name, (p, op)) | op@(InfixOp _ name) <- ops] opPrefix :: Int -> [Operator] -> [(Atom, (Int, Operator))] opPrefix p ops = [(name, (p, op)) | op@(PrefixOp name) <- ops] -instance Show VariableName where - show (VariableName 0 v) = v - show (VariableName i v) = v ++ subscriptDigits i - show (Wildcard (Just i)) = "_" ++ show i - show (Wildcard Nothing) = "_" - -instance Show Clause where - show (Clause name args []) = show (Struct name args) ++ "." - show (Clause name args rhs') = - show (Struct name args) ++ " :- " ++ prettyPrint False 0 (foldr1 conjunction (concatMap flattenConjunction rhs')) ++ "." - show (ClauseFn name args _) = show (Struct name args) ++ " :- " ++ "" ++ "." +prettifyVariableName :: VariableName -> String +prettifyVariableName (VariableName 0 v) = v +prettifyVariableName (VariableName i v) = v ++ subscriptDigits i +prettifyVariableName (Wildcard (Just i)) = "_" ++ show i +prettifyVariableName (Wildcard Nothing) = "_" + +prettifyClause :: Clause -> String +prettifyClause (Clause name args []) = prettifyTerm (Struct name args) ++ "." +prettifyClause (Clause name args rhs') = + prettifyTerm (Struct name args) + ++ " :- " + ++ prettyPrint False 0 (foldr1 conjunction (concatMap flattenConjunction rhs')) + ++ "." +prettifyClause (ClauseFn name args _) = prettifyTerm (Struct name args) ++ " :- " ++ "" ++ "." foldrPl :: (Term -> a -> a) -> a -> Term -> a foldrPl f k (Struct "." [h, t]) = f h (foldrPl f k t) diff --git a/test/PrettyPrintingSpec.hs b/test/PrettyPrintingSpec.hs index 8f81702..d0e61ce 100644 --- a/test/PrettyPrintingSpec.hs +++ b/test/PrettyPrintingSpec.hs @@ -1,7 +1,7 @@ module PrettyPrintingSpec where import Control.Monad (forM_) -import Language.Prolog (Clause (Clause), Term (Struct), consultString) +import Language.Prolog (Clause (Clause), Term (Struct), consultString, prettifyClause) import Test.HUnit (assertFailure) import Test.Hspec (Spec, describe, it, shouldBe) @@ -63,10 +63,10 @@ spec = describe "pretty printing" $ do forM_ preservations $ \(input, expected) -> do it input $ case consultString input of Left err -> assertFailure $ show err - Right [(n, args, goals)] -> show (Clause n args goals) `shouldBe` expected + Right [(n, args, goals)] -> prettifyClause (Clause n args goals) `shouldBe` expected _ -> assertFailure "only one clause should be parsed" describe "it should correctly print the given value" $ do - forM_ expectations $ \(input, expected) -> do - it (show input) $ - show input `shouldBe` expected + forM_ (zip [1 :: Int ..] expectations) $ \(i, (input, expected)) -> do + it ("case " ++ show i) $ + prettifyClause input `shouldBe` expected From 326a8c15d20b3bf3e3b982570ac38c2d17609c1e Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Sun, 25 Oct 2026 16:57:22 +0100 Subject: [PATCH 08/12] add (default) Show instances --- src/Syntax.hs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index 8e3a828..f211b2a 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -8,6 +8,7 @@ #if !MIN_VERSION_base(4,18,0) {-# LANGUAGE DerivingStrategies #-} #endif +{-# LANGUAGE RecordWildCards #-} module Syntax ( Term (..), @@ -47,7 +48,7 @@ data Term = Struct Atom [Term] | Var VariableName | Cut Int - deriving (Data, Eq, Generic) + deriving (Data, Eq, Generic, Show) #if !MIN_VERSION_base(4,18,0) deriving Typeable #endif @@ -66,6 +67,21 @@ data Clause deriving Typeable #endif +instance Show Clause where + show c = + name + ++ " {predicateName = " + ++ show (predicateName c) + ++ ", predicateArgs = " + ++ show (predicateArgs c) + ++ ", " + ++ lastArg + ++ "}" + where + (name, lastArg) = case c of + Clause {} -> ("Clause", "rhs_ = " ++ show (rhs_ c)) + ClauseFn {} -> ("ClauseFn", "fn = ") + rhs :: Clause -> [Term] -> [Goal] rhs (Clause _ _ rhs') = const rhs' rhs (ClauseFn _ _ fn') = fn' @@ -73,7 +89,7 @@ rhs (ClauseFn _ _ fn') = fn' data VariableName = VariableName Int String | Wildcard (Maybe Int) - deriving (Data, Eq, Generic, Ord) + deriving (Data, Eq, Generic, Ord, Show) #if !MIN_VERSION_base(4,18,0) deriving Typeable #endif From 1bae9e91df046ff5001bf7fae7eae0b3d9fc5e15 Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:08:39 +0100 Subject: [PATCH 09/12] allow Hspec --- .github/actions/spelling/allow.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/spelling/allow.txt b/.github/actions/spelling/allow.txt index 6fc5936..7b3a819 100644 --- a/.github/actions/spelling/allow.txt +++ b/.github/actions/spelling/allow.txt @@ -11,6 +11,7 @@ fst GHC Haskeline hds +Hspec HUnit lexer mempty From f6de34a0032530f293c49ca2e942c8b98414803c Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Tue, 27 Oct 2026 11:54:33 +0100 Subject: [PATCH 10/12] put space after disjunction --- src/Syntax.hs | 9 +++------ test/PrettyPrintingSpec.hs | 30 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index f211b2a..f8ee8b9 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -36,7 +36,7 @@ where import Data.Char (digitToInt) import Data.Generics (Data (..)) -import Data.List (intercalate, uncons, unsnoc) +import Data.List (intercalate, uncons) #if !MIN_VERSION_base(4,18,0) import Data.Typeable (Typeable) #endif @@ -169,11 +169,8 @@ prettyPrint _ _ (Cut _) = "!" spaced :: [Char] -> [Char] spaced s = - let - condPrefix = maybe True ((`notElem` ",;") . fst) $ uncons s - condPostfix = maybe False ((/= ';') . snd) $ unsnoc s - in - spaceIf condPrefix ++ s ++ spaceIf condPostfix + let condPrefix = maybe True ((`notElem` ",;") . fst) $ uncons s + in spaceIf condPrefix ++ s ++ " " spaceIf :: Bool -> String spaceIf True = " " diff --git a/test/PrettyPrintingSpec.hs b/test/PrettyPrintingSpec.hs index d0e61ce..8f3cee6 100644 --- a/test/PrettyPrintingSpec.hs +++ b/test/PrettyPrintingSpec.hs @@ -19,18 +19,18 @@ d = Struct "d" [] preservations :: [(String, String)] preservations = - [ ("p :- (a;b),c,d.", "p :- (a;b), c, d.") - , ("p :- a;b,c,d.", "p :- a;b, c, d.") - , ("p :- a;(b,c,d).", "p :- a;b, c, d.") - , ("p :- (a;(b,(c,d))).", "p :- a;b, c, d.") - , ("p :- a;b, (c, d).", "p :- a;b, c, d.") - , ("p :- (a, b); c.", "p :- a, b;c.") - , ("p :- a ; b ; c.", "p :- a;b;c.") - , ("p :- a, ! ; b ; c.", "p :- a, !;b;c.") - , ("p :- (a ; b) ; c.", "p :- (a;b);c.") + [ ("p :- (a;b),c,d.", "p :- (a; b), c, d.") + , ("p :- a;b,c,d.", "p :- a; b, c, d.") + , ("p :- a;(b,c,d).", "p :- a; b, c, d.") + , ("p :- (a;(b,(c,d))).", "p :- a; b, c, d.") + , ("p :- a;b, (c, d).", "p :- a; b, c, d.") + , ("p :- (a, b); c.", "p :- a, b; c.") + , ("p :- a ; b ; c.", "p :- a; b; c.") + , ("p :- a, ! ; b ; c.", "p :- a, !; b; c.") + , ("p :- (a ; b) ; c.", "p :- (a; b); c.") , ("p :- a, b, c.", "p :- a, b, c.") - , ("p :- \\+ a ; b.", "p :- \\+a;b.") - , ("p :- not(a) ; b.", "p :- not(a);b.") + , ("p :- \\+ a ; b.", "p :- \\+a; b.") + , ("p :- not(a) ; b.", "p :- not(a); b.") , ("p :- \\+ (a, b).", "p :- \\+(a, b).") , ("p :- not(a, b).", "p :- not(a, b).") , ("p(X) :- X = [a, b, c].", "p(X) :- X = [a,b,c].") @@ -39,7 +39,7 @@ preservations = , ("p(X,Y,Z) :- X is Y * Y + Z.", "p(X, Y, Z) :- X is Y * Y + Z.") , ("p(X,Y,Z) :- X is Y * (Y + Z).", "p(X, Y, Z) :- X is Y * (Y + Z).") , ("p(X) :- X =.. [Y, Z].", "p(X) :- X =.. [Y,Z].") - , ("p :- q(a, g(b, c)); h.", "p :- q(a, g(b, c));h.") + , ("p :- q(a, g(b, c)); h.", "p :- q(a, g(b, c)); h.") , ("p(X) :- X = Y, g(Y).", "p(X) :- X = Y, g(Y).") , ("p(X) :- (X = Y, Y = Z), g(Z).", "p(X) :- X = Y, Y = Z, g(Z).") , ("p(X, Y) :- X is -Y.", "p(X, Y) :- X is -Y.") @@ -50,11 +50,11 @@ preservations = expectations :: [(Clause, String)] expectations = [ -- a ; (b, (c, d)) - (Clause "p" [] [dis a (con b (con c d))], "p :- a;b, c, d.") + (Clause "p" [] [dis a (con b (con c d))], "p :- a; b, c, d.") , -- a ; (b, c) - (Clause "p" [] [dis a (con b c)], "p :- a;b, c.") + (Clause "p" [] [dis a (con b c)], "p :- a; b, c.") , -- (a, (b, c)) ; d - (Clause "p" [] [dis (con a (con b c)) d], "p :- a, b, c;d.") + (Clause "p" [] [dis (con a (con b c)) d], "p :- a, b, c; d.") ] spec :: Spec From b9da824fb965f4ee0069bdefe7c05b0e9dc05b7a Mon Sep 17 00:00:00 2001 From: nimec01 <24428341+nimec01@users.noreply.github.com> Date: Tue, 27 Oct 2026 12:30:50 +0100 Subject: [PATCH 11/12] don't prepend space for empty input --- src/Syntax.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index f8ee8b9..f9a5fac 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -170,7 +170,7 @@ prettyPrint _ _ (Cut _) = "!" spaced :: [Char] -> [Char] spaced s = let condPrefix = maybe True ((`notElem` ",;") . fst) $ uncons s - in spaceIf condPrefix ++ s ++ " " + in spaceIf condPrefix ++ s ++ spaceIf (not $ null s) spaceIf :: Bool -> String spaceIf True = " " From 44b7e89a19b4c41808febc70a185f93f67059bee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janis=20Voigtl=C3=A4nder?= Date: Tue, 27 Oct 2026 13:42:55 +0100 Subject: [PATCH 12/12] refactor, semantics-preservingly --- src/Syntax.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Syntax.hs b/src/Syntax.hs index f9a5fac..6e58eea 100644 --- a/src/Syntax.hs +++ b/src/Syntax.hs @@ -169,8 +169,8 @@ prettyPrint _ _ (Cut _) = "!" spaced :: [Char] -> [Char] spaced s = - let condPrefix = maybe True ((`notElem` ",;") . fst) $ uncons s - in spaceIf condPrefix ++ s ++ spaceIf (not $ null s) + let condPrefix = maybe False ((`notElem` ",;") . fst) $ uncons s + in spaceIf condPrefix ++ s ++ " " spaceIf :: Bool -> String spaceIf True = " "