deriving-show-simple (empty) → 0
raw patch · 6 files changed
+157/−0 lines, 6 filesdep +HUnitdep +basedep +deriving-show-simplesetup-changed
Dependencies added: HUnit, base, deriving-show-simple
Files
- CHANGELOG.md +5/−0
- Deriving/Show/Simple.hs +56/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- deriving-show-simple.cabal +40/−0
- tests/spec.hs +24/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for simple-show++## 0 - 2020/10/28++* First version. Released on an unsuspecting world.
+ Deriving/Show/Simple.hs view
@@ -0,0 +1,56 @@+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE TypeOperators #-}+{-# LANGUAGE DataKinds #-}+{-# LANGUAGE PolyKinds #-}+{-# LANGUAGE UndecidableInstances #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeFamilies #-}+module Deriving.Show.Simple (showsPrecSimple, WrapSimple(..)) where++import Data.Proxy+import Data.List (intersperse)+import GHC.Generics+import GHC.TypeLits++-- | Like 'showsPrec', but shows it as if their record fields are stripped+showsPrecSimple :: (Generic a, GShowSimple (Rep a)) => Int -> a -> ShowS+showsPrecSimple p a = case shows' (from a) of+ [x] -> x+ xs -> showParen (p > 10) $ foldr (.) id+ $ intersperse (showChar ' ') xs++class GShowSimple f where+ shows' :: f x -> [ShowS]++instance GShowSimple V1 where+ shows' _ = error "impossible"++instance GShowSimple U1 where+ shows' _ = []++instance Show a => GShowSimple (K1 c a) where+ shows' (K1 a) = [showsPrec 11 a]++instance (GShowSimple f, GShowSimple g) => GShowSimple (f :*: g) where+ shows' (f :*: g) = shows' f ++ shows' g++instance (GShowSimple f, GShowSimple g) => GShowSimple (f :+: g) where+ shows' (L1 a) = shows' a+ shows' (R1 a) = shows' a++instance (KnownSymbol name, GShowSimple f, c ~ 'MetaCons name fix rec) => GShowSimple (C1 c f) where+ shows' (M1 a) = showString (symbolVal (Proxy :: Proxy name)) : shows' a++instance (GShowSimple f) => GShowSimple (D1 meta f) where+ shows' (M1 a) = shows' a++instance (GShowSimple f) => GShowSimple (S1 meta f) where+ shows' (M1 a) = shows' a++-- | The 'Show' instance uses 'showsPrecSimple'. Useful in combination with DerivingVia+newtype WrapSimple a = WrapSimple { unwrapSimple :: a }++instance (Generic a, GShowSimple (Rep a)) => Show (WrapSimple a) where+ showsPrec d = showsPrecSimple d . unwrapSimple+
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Fumiaki Kinoshita (c) 2020++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Fumiaki Kinoshita nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ deriving-show-simple.cabal view
@@ -0,0 +1,40 @@+cabal-version: 2.4+-- Initial package description 'simple-show.cabal' generated by 'cabal+-- init'. For further documentation, see+-- http://haskell.org/cabal/users-guide/++name: deriving-show-simple+version: 0+synopsis: Derive a Show instance without field selector names+description:+ GHC's stock deriving includes field names to a Show representation, but sometimes it's not desiable.+ This package provides a wrapper which has a Show instance as if the type were not a record.+bug-reports: https://github.com/fumieval/deriving-show-simple+license: BSD-3-Clause+license-file: LICENSE+author: Fumiaki Kinoshita+maintainer: fumiexcel@gmail.com+copyright: Copyright (c) 2020 Fumiaki Kinoshita+category: Generics+extra-source-files: CHANGELOG.md++source-repository head+ type: git+ location: https://github.com/fumieval/deriving-show-simple++library+ exposed-modules: Deriving.Show.Simple+ -- other-modules:+ -- other-extensions:+ build-depends: base >=4.9 && <5+ -- hs-source-dirs:+ default-language: Haskell2010+ ghc-options: -Wall -Wcompat++test-suite spec+ type: exitcode-stdio-1.0+ main-is: spec.hs+ ghc-options: -Wall -Wcompat+ hs-source-dirs: tests+ build-depends: base, HUnit, deriving-show-simple+ default-language: Haskell2010
+ tests/spec.hs view
@@ -0,0 +1,24 @@+import Control.Monad (unless)+import Data.Proxy+import Data.Functor.Identity+import Deriving.Show.Simple+import System.Exit (exitFailure)+import Test.HUnit++identity :: Identity Int+identity = Identity 42++tuple :: (Int, Identity Int)+tuple = (0, identity)++main :: IO ()+main = do+ count <- runTestTT $ TestList+ [ showsPrecSimple 11 Proxy "" ~?= "Proxy"+ , showsPrecSimple 1 Proxy "" ~?= "Proxy"+ , showsPrecSimple 11 identity "" ~?= "(Identity 42)"+ , showsPrecSimple 1 identity "" ~?= "Identity 42"+ , showsPrecSimple 11 tuple "" ~?= "((,) 0 (Identity 42))"+ , showsPrecSimple 1 tuple "" ~?= "(,) 0 (Identity 42)"+ ]+ unless (failures count == 0 && errors count == 0) exitFailure