Skip to content

Commit 847a1a4

Browse files
JordanMartinezturlando
authored andcommitted
Complete daniel-chambers' PR adding Show instances to generics rep types (purescript#250)
* Add Show instance to generic rep types (complete daniel-chambers PR) Helps one see the generic representation for a given value * Add `@` in front of constructor name in Show instance for generic type * Add tests for Inl, NoArguments, and Product
1 parent 85fa966 commit 847a1a4

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Data/Generic/Rep.purs

+19
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module Data.Generic.Rep
1111
, Argument(..)
1212
) where
1313

14+
import Data.Semigroup ((<>))
15+
import Data.Show (class Show, show)
16+
import Data.Symbol (class IsSymbol, reflectSymbol)
1417
import Type.Proxy (Proxy(..))
1518

1619
-- | A representation for types with no constructors.
@@ -19,19 +22,35 @@ data NoConstructors
1922
-- | A representation for constructors with no arguments.
2023
data NoArguments = NoArguments
2124

25+
instance showNoArguments :: Show NoArguments where
26+
show _ = "NoArguments"
27+
2228
-- | A representation for types with multiple constructors.
2329
data Sum a b = Inl a | Inr b
2430

31+
instance showSum :: (Show a, Show b) => Show (Sum a b) where
32+
show (Inl a) = "(Inl " <> show a <> ")"
33+
show (Inr b) = "(Inr " <> show b <> ")"
34+
2535
-- | A representation for constructors with multiple fields.
2636
data Product a b = Product a b
2737

38+
instance showProduct :: (Show a, Show b) => Show (Product a b) where
39+
show (Product a b) = "(Product " <> show a <> " " <> show b <> ")"
40+
2841
-- | A representation for constructors which includes the data constructor name
2942
-- | as a type-level string.
3043
newtype Constructor (name :: Symbol) a = Constructor a
3144

45+
instance showConstructor :: (IsSymbol name, Show a) => Show (Constructor name a) where
46+
show (Constructor a) = "(Constructor @" <> show (reflectSymbol (Proxy :: Proxy name)) <> " " <> show a <> ")"
47+
3248
-- | A representation for an argument in a data constructor.
3349
newtype Argument a = Argument a
3450

51+
instance showArgument :: Show a => Show (Argument a) where
52+
show (Argument a) = "(Argument " <> show a <> ")"
53+
3554
-- | The `Generic` class asserts the existence of a type function from types
3655
-- | to their representations using the type constructors defined in this module.
3756
class Generic a rep | a -> rep where

test/Data/Generic/Rep.purs

+9
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ testGenericRep = do
123123
assert "Checking show" $
124124
show (cons 1 (cons 2 Nil)) == "(Cons { head: 1, tail: (Cons { head: 2, tail: Nil }) })"
125125

126+
assert "Checking show for generic types: Inl, NoArguments" $
127+
show (G.from (Nil :: List Int)) == "(Inl (Constructor @\"Nil\" NoArguments))"
128+
129+
assert "Checking show for generic types: Inr, Constructor, and Single Argument" $
130+
show (G.from $ cons 1 Nil) == "(Inr (Constructor @\"Cons\" (Argument { head: 1, tail: Nil })))"
131+
132+
assert "Checking show for generic types: Product" $
133+
show (G.from $ Pair 1 2) == "(Constructor @\"Pair\" (Product (Argument 1) (Argument 2)))"
134+
126135
assert "Checking equality" $
127136
cons 1 (cons 2 Nil) == cons 1 (cons 2 Nil)
128137

0 commit comments

Comments
 (0)