Created
September 11, 2018 20:38
-
-
Save harpocrates/d6866c5d61542bf04434bb1349d5ea64 to your computer and use it in GitHub Desktop.
Newtype for deriving `Show1`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE ScopedTypeVariables #-} | |
| {-# LANGUAGE TypeApplications #-} | |
| {-# LANGUAGE StandaloneDeriving #-} | |
| {-# LANGUAGE InstanceSigs #-} | |
| {-# LANGUAGE QuantifiedConstraints #-} | |
| {-# LANGUAGE UndecidableInstances #-} | |
| {-# LANGUAGE DerivingVia #-} | |
| {-# LANGUAGE DerivingStrategies #-} | |
| {-# LANGUAGE DeriveFunctor #-} | |
| module Show1Stuff where | |
| import Data.Functor.Classes | |
| -- | Copy of `Maybe` where `Show1` is derived | |
| data Maybe' a = Nothing' | Just' a | |
| deriving stock (Functor, Show) | |
| deriving (Show1) via (Identity1 Maybe') | |
| ---------- | |
| newtype Identity1 f a = Identity1 { runIdentity1 :: f a } | |
| instance (Functor f, forall a. Show a => Show (f a)) => Show1 (Identity1 f) where | |
| liftShowsPrec :: forall a. (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> Identity1 f a -> ShowS | |
| liftShowsPrec sp _ p = showsPrec p . fmap (WSD sp) . runIdentity1 | |
| -- | Pass `showsPrec` explicitly | |
| data WithShowDict a = WSD (Int -> a -> ShowS) a | |
| instance Show (WithShowDict a) where | |
| showsPrec p (WSD showsPrecF x) = showsPrecF p x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment