Skip to content

instance Show Show is imprecise. #633

Description

@phadej

In singletons-3.0.4

infixr 4 :&:

      => Show (SSigma sig) where
  showsPrec p ((sa :: Sing ('WrapSing (sfst :: Sing fst))) :%&: (sb :: Sing snd)) =
    showParen (p >= 5) $
      showsPrec 5 sa . showString " :&: " . showsPrec 5 sb

Technically this is not wrong, but it's a bit imprecise. :&: is right associative, butShow` instance doesn't use it.

Compare to e.g. Show (NP f xs) from sop-core, where :* is also right associative so it's easy to compare:

infixr 5 :*

instance All (Show `Compose` f) xs => Show (NP f xs) where
  showsPrec _ Nil       = showString "Nil"
  showsPrec d (f :* fs) = showParen (d > 5)
    $ showsPrec (5 + 1) f
    . showString " :* "
    . showsPrec 5 fs

Given the above, I'd rewrite

  showsPrec p ((sa :: Sing ('WrapSing (sfst :: Sing fst))) :%&: (sb :: Sing snd)) =
    showParen (p > 4) $
      showsPrec 5 sa . showString " :&: " . showsPrec 4 sb

I.e. use p > 4 to highlight that it's the 4 as in infixr 4 :&:, and change to showsPrec 4 sb: we don't need to bump precendence level for right argument.

IF there is a reason to prefer "more parentheses" i.e. current version, then it probably should be written in the code as comment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions