on master help sign in
fix display of nested constructed atoms with parentheses
8c3934aAgentM
ProjectM36/Base.hs+4−2
@@ -70,8 +70,10 @@ instance Eq Atom where
70 _ == _ = False
70 _ == _ = False
71
71
72instance Show Atom where
72instance Show Atom where
73− show (Atom atom) = "Atom " ++ show atom
73+ showsPrec _ (Atom atom) = showString "Atom " . shows atom
74− show (ConstructedAtom dConsName _ atomList) = unpack dConsName ++ " " ++ L.concat (L.intersperse " " (map show atomList))
74+ showsPrec v (ConstructedAtom dConsName _ atomList) = showParen (v >= 4) showIt
75+ where showIt = showString (unpack dConsName) . showString " " . compose (map (showsPrec 4) atomList)
76+ compose = foldr (.) id
75
77
76instance Binary Atom where
78instance Binary Atom where
77 put atom@(Atom val) = put (atomTypeForAtom atom) >> put val
79 put atom@(Atom val) = put (atomTypeForAtom atom) >> put val
ProjectM36/Relation/Show/Term.hs+12−6
@@ -104,14 +104,20 @@ relationAsTable rel@(Relation _ tupleSet) = (header, body)
104 body = L.foldl' tupleFolder [] (asList tupleSet)
104 body = L.foldl' tupleFolder [] (asList tupleSet)
105 tupleFolder acc tuple = acc ++ [map (\attrName -> case atomForAttributeName attrName tuple of
105 tupleFolder acc tuple = acc ++ [map (\attrName -> case atomForAttributeName attrName tuple of
106 Left _ -> "?"
106 Left _ -> "?"
107− Right atom -> showAtom atom
107+ Right atom -> showAtom 0 atom
108 ) oAttrNames]
108 ) oAttrNames]
109
109
110+showParens :: Bool -> StringType -> StringType
111+showParens predicate f = if predicate then
112+ "(" `T.append` f `T.append` ")"
113+ else
114+ f
115+
110−showAtom :: Atom -> StringType
116+showAtom :: Int -> Atom -> StringType
111−showAtom (Atom atom) = case cast atom of
117+showAtom _ (Atom atom) = case cast atom of
112− Just rel -> renderTable $ relationAsTable rel
118+ Just rel -> renderTable $ relationAsTable rel
113− Nothing -> toText atom
119+ Nothing -> toText atom
114−showAtom (ConstructedAtom dConsName _ atoms) = dConsName `T.append` " " `T.append` T.concat (L.intersperse " " (map showAtom atoms))
120+showAtom level (ConstructedAtom dConsName _ atoms) = showParens (level >= 1 && length atoms >= 1) $ T.concat (L.intersperse " " (dConsName : (map (showAtom 1) atoms)))
115
121
116renderTable :: Table -> StringType
122renderTable :: Table -> StringType
117renderTable table = renderHeader table (fst cellLocs) `T.append` renderBody (snd table) cellLocs
123renderTable table = renderHeader table (fst cellLocs) `T.append` renderBody (snd table) cellLocs