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

Showf type synonym #109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Data/Parameterized/All.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
------------------------------------------------------------------------

{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}

module Data.Parameterized.All
Expand All @@ -59,9 +60,6 @@ instance FunctorF All where
instance FoldableF All where
foldMapF toMonoid (All x) = toMonoid x

instance ShowF f => Show (All f) where
show (All fa) = showF fa

instance EqF f => Eq (All f) where
(All x) == (All y) = eqF x y

Expand Down
2 changes: 0 additions & 2 deletions src/Data/Parameterized/BoolRepr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ instance Show (BoolRepr m) where
show FalseRepr = "FalseRepr"
show TrueRepr = "TrueRepr"

instance ShowF BoolRepr

instance HashableF BoolRepr where
hashWithSaltF = hashWithSalt

Expand Down
34 changes: 6 additions & 28 deletions src/Data/Parameterized/Classes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ This module declares classes for working with types with the kind
not restricted to '*'.
-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand All @@ -38,8 +40,7 @@ module Data.Parameterized.Classes
, fromOrdering
, ordFCompose
-- * Typeclass generalizations
, ShowF(..)
, showsF
, ShowF
, HashableF(..)
, CoercibleF(..)
-- * Type function application constructor
Expand All @@ -62,7 +63,6 @@ import Data.Functor.Compose (Compose(..))
import Data.Kind
import Data.Hashable
import Data.Maybe (isJust)
import Data.Proxy
import Data.Type.Equality as Equality

import Data.Parameterized.Compose ()
Expand Down Expand Up @@ -247,30 +247,8 @@ instance OrdF f => OrdF (Compose f g) where
------------------------------------------------------------------------
-- ShowF

-- | A parameterized type that can be shown on all instances.
--
-- To implement @'ShowF' g@, one should implement an instance @'Show'
-- (g tp)@ for all argument types @tp@, then write an empty instance
-- @instance 'ShowF' g@.
class ShowF (f :: k -> Type) where
-- | Provides a show instance for each type.
withShow :: p f -> q tp -> (Show (f tp) => a) -> a

default withShow :: Show (f tp) => p f -> q tp -> (Show (f tp) => a) -> a
withShow _ _ x = x

showF :: forall tp . f tp -> String
showF x = withShow (Proxy :: Proxy f) (Proxy :: Proxy tp) (show x)

-- | Like 'showsPrec', the precedence argument is /one more/ than the
-- precedence of the enclosing context.
showsPrecF :: forall tp. Int -> f tp -> String -> String
showsPrecF p x = withShow (Proxy :: Proxy f) (Proxy :: Proxy tp) (showsPrec p x)

showsF :: ShowF f => f tp -> String -> String
showsF x = showsPrecF 0 x

instance Show x => ShowF (Const x)
-- | A type synonym indicating that a type can be shown on all instances.
type ShowF f = forall x . Show (f x)

------------------------------------------------------------------------
-- IxedF
Expand Down Expand Up @@ -343,7 +321,7 @@ instance OrdF f => Ord (TypeAp f tp) where
compare (TypeAp x) (TypeAp y) = toOrdering (compareF x y)

instance ShowF f => Show (TypeAp f tp) where
showsPrec p (TypeAp x) = showsPrecF p x
showsPrec p (TypeAp x) = showsPrec p x

instance HashableF f => Hashable (TypeAp f tp) where
hashWithSalt s (TypeAp x) = hashWithSaltF s x
Expand Down
10 changes: 2 additions & 8 deletions src/Data/Parameterized/Context/Safe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
Expand Down Expand Up @@ -137,8 +138,6 @@ data Size (ctx :: Ctx k) where
instance Show (Size ctx) where
show = show . sizeInt

instance ShowF Size

-- | Convert a context size to an 'Int'.
sizeInt :: Size ctx -> Int
sizeInt SizeZero = 0
Expand Down Expand Up @@ -414,16 +413,13 @@ intIndex n sz = listToMaybe $ drop n $ indexList sz
instance Show (Index ctx tp) where
show = show . indexVal

instance ShowF (Index ctx)

-- | View of indexes as pointing to the last element in the
-- index range or pointing to an earlier element in a smaller
-- range.
data IndexView ctx tp where
IndexViewLast :: Size ctx -> IndexView (ctx '::> t) t
IndexViewInit :: Index ctx t -> IndexView (ctx '::> u) t

instance ShowF (IndexView ctx)
deriving instance Show (IndexView ctx tp)

-- | Project an index
Expand Down Expand Up @@ -602,9 +598,7 @@ instance HashableF f => Hashable (Assignment f ctx) where
hashWithSalt s (AssignmentExtend asgn x) = (s `hashWithSalt` asgn) `hashWithSaltF` x

instance ShowF f => Show (Assignment f ctx) where
show a = "[" ++ intercalate ", " (toList showF a) ++ "]"

instance ShowF f => ShowF (Assignment f)
show a = "[" ++ intercalate ", " (toList show a) ++ "]"

instance FunctorFC Assignment where
fmapFC = fmapFCDefault
Expand Down
14 changes: 3 additions & 11 deletions src/Data/Parameterized/Context/Unsafe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -146,8 +147,6 @@ sizeToNatRepr (Size n) = NatRepr (fromIntegral n)
instance Show (Size ctx) where
show (Size i) = show i

instance ShowF Size

-- | A context that can be determined statically at compiler time.
class KnownContext (ctx :: Ctx k) where
knownSize :: Size ctx
Expand Down Expand Up @@ -335,8 +334,6 @@ intIndex i n | 0 <= i && i < sizeInt n = Just (Some (Index i))
instance Show (Index ctx tp) where
show = show . indexVal

instance ShowF (Index ctx)

-- | View of indexes as pointing to the last element in the
-- index range or pointing to an earlier element in a smaller
-- range.
Expand All @@ -345,7 +342,6 @@ data IndexView ctx tp where
IndexViewInit :: !(Index ctx t) -> IndexView (ctx '::> u) t

deriving instance Show (IndexView ctx tp)
instance ShowF (IndexView ctx)

-- | Project an index
viewIndex :: Size ctx -> Index ctx tp -> IndexView ctx tp
Expand Down Expand Up @@ -454,11 +450,9 @@ traverse_bal = go
{-# INLINABLE traverse_bal #-}

instance ShowF f => Show (BalancedTree h f tp) where
show (BalLeaf x) = showF x
show (BalLeaf x) = show x
show (BalPair x y) = "BalPair " Prelude.++ show x Prelude.++ " " Prelude.++ show y

instance ShowF f => ShowF (BalancedTree h f)

unsafe_bal_generate :: forall ctx h f t
. Int -- ^ Height of tree to generate
-> Int -- ^ Starting offset for entries.
Expand Down Expand Up @@ -855,9 +849,7 @@ instance HashableF f => HashableF (Assignment f) where
hashWithSaltF = hashWithSalt

instance ShowF f => Show (Assignment f ctx) where
show a = "[" Prelude.++ intercalate ", " (toListFC showF a) Prelude.++ "]"

instance ShowF f => ShowF (Assignment f)
show a = "[" Prelude.++ intercalate ", " (toListFC show a) Prelude.++ "]"

{-# DEPRECATED adjust "Replace 'adjust f i asgn' with 'Lens.over (ixF i) f asgn' instead." #-}
adjust :: (f tp -> f tp) -> Index ctx tp -> Assignment f ctx -> Assignment f ctx
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Parameterized/DataKind.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
Expand Down Expand Up @@ -33,8 +34,7 @@ snd (PairRepr _ b) = b
$(return [])

instance ( ShowF f, ShowF g ) => Show (PairRepr f g p) where
show (PairRepr a b) = showChar '(' . showsF a . showChar ',' . showsF b $ ")"
instance ( ShowF f, ShowF g ) => ShowF (PairRepr f g)
show (PairRepr a b) = showChar '(' . shows a . showChar ',' . shows b $ ")"

deriving instance ( Eq (f a), Eq (g b) ) => Eq (PairRepr f g '(a, b))
instance ( TestEquality f, TestEquality g ) => TestEquality (PairRepr f g) where
Expand Down
7 changes: 2 additions & 5 deletions src/Data/Parameterized/List.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ use the 'Data.Parameterized.List.List' type for this purpose.
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
Expand Down Expand Up @@ -174,12 +175,10 @@ instance ShowF f => Show (List f sh) where
showsPrec p (elt :< rest) = showParen (p > precCons) $
-- Unlike a derived 'Show' instance, we don't print parens implied
-- by right associativity.
showsPrecF (precCons+1) elt . showString " :< " . showsPrec 0 rest
showsPrec (precCons+1) elt . showString " :< " . showsPrec 0 rest
where
precCons = 5

instance ShowF f => ShowF (List f)

instance FunctorFC List where
fmapFC _ Nil = Nil
fmapFC f (x :< xs) = f x :< fmapFC f xs
Expand Down Expand Up @@ -251,8 +250,6 @@ data Index :: [k] -> k -> Type where
deriving instance Eq (Index l x)
deriving instance Show (Index l x)

instance ShowF (Index l)

instance TestEquality (Index l) where
testEquality IndexHere IndexHere = Just Refl
testEquality (IndexThere x) (IndexThere y) = testEquality x y
Expand Down
3 changes: 2 additions & 1 deletion src/Data/Parameterized/Map.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Some code was adapted from containers.
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
Expand Down Expand Up @@ -309,7 +310,7 @@ instance TraversableF (MapF ktp) where
traverseF = traverse

instance (ShowF ktp, ShowF rtp) => Show (MapF ktp rtp) where
show m = showMap showF showF m
show m = showMap show show m

-- | Return all keys of the map in ascending order.
keys :: MapF k a -> [Some k]
Expand Down
2 changes: 0 additions & 2 deletions src/Data/Parameterized/NatRepr/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ instance PolyEq (NatRepr m) (NatRepr n) where
instance Show (NatRepr n) where
show (NatRepr n) = show n

instance ShowF NatRepr

instance HashableF NatRepr where
hashWithSaltF = hashWithSalt

Expand Down
2 changes: 0 additions & 2 deletions src/Data/Parameterized/Nonce.hs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ instance OrdF (Nonce s) where
instance HashableF (Nonce s) where
hashWithSaltF s (Nonce x) = hashWithSalt s x

instance ShowF (Nonce s)

------------------------------------------------------------------------
-- * GlobalNonceGenerator

Expand Down
2 changes: 0 additions & 2 deletions src/Data/Parameterized/Nonce/Unsafe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ instance OrdF Nonce where
instance HashableF Nonce where
hashWithSaltF s (Nonce x) = hashWithSalt s x

instance ShowF Nonce

{-# INLINE freshNonce #-}
-- | Get a fresh index and increment the counter.
freshNonce :: NonceGenerator s -> ST s (Nonce tp)
Expand Down
2 changes: 0 additions & 2 deletions src/Data/Parameterized/Peano.hs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ instance PolyEq (PeanoRepr m) (PeanoRepr n) where
instance Show (PeanoRepr p) where
show p = show (peanoValue p)

instance ShowF PeanoRepr

instance HashableF PeanoRepr where
hashWithSaltF = hashWithSalt

Expand Down
3 changes: 2 additions & 1 deletion src/Data/Parameterized/Some.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
module Data.Parameterized.Some
( Some(..)
Expand Down Expand Up @@ -38,7 +39,7 @@ instance HashableF f => Hashable (Some f) where
hash (Some x) = hashF x

instance ShowF f => Show (Some f) where
show (Some x) = showF x
show (Some x) = show x

-- | Project out of Some.
viewSome :: (forall tp . f tp -> r) -> Some f -> r
Expand Down
2 changes: 0 additions & 2 deletions src/Data/Parameterized/SymbolRepr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ instance Hashable (SymbolRepr nm) where
instance Show (SymbolRepr nm) where
show (SymbolRepr nm) = Text.unpack nm

instance ShowF SymbolRepr


-- | The SomeSym hides a Symbol parameter but preserves a
-- KnownSymbol constraint on the hidden parameter.
Expand Down
3 changes: 0 additions & 3 deletions test/Test/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ instance Show (Payload tp) where
show (StringPayload x) = show x <> " :: String"
show (BoolPayload x) = show x <> " :: Bool"

instance ShowF Payload


twiddle :: Payload a -> Payload a
twiddle (IntPayload n) = IntPayload (n+1)
Expand All @@ -76,7 +74,6 @@ twiddle (BoolPayload b) = BoolPayload (not b)
-- constraint.

data MyMaybe t = (Show t) => MyJust t | MyNothing
instance ShowF MyMaybe
instance Show (MyMaybe t) where
show (MyJust x) = "MyJust " <> show x
show MyNothing = "MyNothing"
Expand Down