diff options
author | Tom Smeding <tom@tomsmeding.com> | 2024-09-11 20:58:18 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2024-09-11 20:58:18 +0200 |
commit | 252753f898bb67b3f42f83248a85638b1bc11fb4 (patch) | |
tree | b8d37f3772e14beb8f1f0ba2936bd90cd810d291 /src | |
parent | 826f3b55be0e864b34e2b2b979b5a02239c44252 (diff) |
Show instance
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/Dependent/EnumMap/Strict/Internal.hs | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/src/Data/Dependent/EnumMap/Strict/Internal.hs b/src/Data/Dependent/EnumMap/Strict/Internal.hs index 17de2fd..1a26f1a 100644 --- a/src/Data/Dependent/EnumMap/Strict/Internal.hs +++ b/src/Data/Dependent/EnumMap/Strict/Internal.hs @@ -1,6 +1,7 @@ {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE RankNTypes #-} +{-# LANGUAGE QuantifiedConstraints #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} module Data.Dependent.EnumMap.Strict.Internal where @@ -8,7 +9,9 @@ module Data.Dependent.EnumMap.Strict.Internal where import Data.Bifunctor (bimap) import Data.Dependent.Sum import qualified Data.IntMap.Strict as IM +import Data.Kind (Type) import Data.Some +import Text.Show (showListWith) import Unsafe.Coerce (unsafeCoerce) @@ -16,6 +19,11 @@ data KV k v = forall a. KV !(Enum1Info k) !(v a) newtype DEnumMap k v = DEnumMap (IM.IntMap (KV k v)) +instance (Enum1 k, forall a. Show (k a), forall a. Show (v a)) + => Show (DEnumMap (k :: kind -> Type) (v :: kind -> Type)) where + showsPrec d mp = showParen (d > 10) $ + showString "DEnumMap " . showListWith (\(k :=> v) -> showsPrec 2 k . showString " :=> " . showsPrec 1 v) (toList mp) + class Enum1 f where type Enum1Info f fromEnum1 :: f a -> (Int, Enum1Info f) @@ -213,11 +221,15 @@ keys (DEnumMap m) = map (\(k, KV inf _) -> toEnum1 k inf) (IM.assocs m) -- ** Lists --- toList +toList :: Enum1 k => DEnumMap k v -> [DSum k v] +toList = toAscList -- ** Ordered lists --- toAscList +toAscList :: Enum1 k => DEnumMap k v -> [DSum k v] +toAscList (DEnumMap m) = + map (\(i, KV inf v) -> case toEnum1 i inf of Some k -> k :=> coe1 v) + (IM.toAscList m) toDescList :: Enum1 k => DEnumMap k v -> [DSum k v] toDescList (DEnumMap m) = |