Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ShowsPrec? #25

Open
Zemyla opened this issue Apr 18, 2016 · 0 comments
Open

Add ShowsPrec? #25

Zemyla opened this issue Apr 18, 2016 · 0 comments

Comments

@Zemyla
Copy link

Zemyla commented Apr 18, 2016

The relevant type for Show is certainly a good Contravariant, Divisible, and Decidable:

newtype ShowsPrec a = ShowsPrec { runShowsPrec :: Int -> a -> ShowS }

instance Contravariant ShowsPrec where
    contramap f (ShowsPrec s) = ShowsPrec $ \p -> s p . f

instance Divisible ShowsPrec where
    conquer = ShowsPrec $ \_ _ -> id
    divide f (ShowsPrec sb) (ShowsPrec sc) = ShowsPrec $ \p -> uncurry (\b c -> sb p b . sc p c) . f

instance Decidable ShowsPrec where
    lose f = ShowsPrec $ \_ -> absurd . f
    choose f (ShowsPrec sb) (ShowsPrec sb) = ShowsPrec $ \p -> either (sb p) (sc p) . f

It also admits a number of useful combinators:

showCharP :: Char -> ShowsPrec a
showCharP c = ShowsPrec $ \_ _ -> showChar c

showStringP :: String -> ShowsPrec a
showStringP str = ShowsPrec $ \_ _ -> showString str

(>*) :: (Divisible f) => f () -> f a -> f a
(>*) = divide ((,) ())

(*<) :: (Divisible f) => f a -> f () -> f a
(*<) = divide (\a -> (a, ()))

parenP :: Int -> ShowsPrec a -> ShowsPrec a
parenP p0 (ShowsPrec sh) = ShowsPrec $ \p a -> showParen (p > p0) $ sh (p0 + 1) a

These are just examples, but it'd allow building up a ShowsPrec from smaller pieces, and fit with Equivalence and Comparison as "Contravariants arising from Prelude typeclasses".

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

No branches or pull requests

1 participant