Skip to content

Complete daniel-chambers' PR adding Show instances to generics rep types #250

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

Merged
merged 4 commits into from
Dec 26, 2020
Merged
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
19 changes: 19 additions & 0 deletions src/Data/Generic/Rep.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module Data.Generic.Rep
, Argument(..)
) where

import Data.Semigroup ((<>))
import Data.Show (class Show, show)
import Data.Symbol (class IsSymbol, reflectSymbol)
import Type.Proxy (Proxy(..))

-- | A representation for types with no constructors.
Expand All @@ -19,19 +22,35 @@ data NoConstructors
-- | A representation for constructors with no arguments.
data NoArguments = NoArguments

instance showNoArguments :: Show NoArguments where
show _ = "NoArguments"

-- | A representation for types with multiple constructors.
data Sum a b = Inl a | Inr b

instance showSum :: (Show a, Show b) => Show (Sum a b) where
show (Inl a) = "(Inl " <> show a <> ")"
show (Inr b) = "(Inr " <> show b <> ")"

-- | A representation for constructors with multiple fields.
data Product a b = Product a b

instance showProduct :: (Show a, Show b) => Show (Product a b) where
show (Product a b) = "(Product " <> show a <> " " <> show b <> ")"

-- | A representation for constructors which includes the data constructor name
-- | as a type-level string.
newtype Constructor (name :: Symbol) a = Constructor a

instance showConstructor :: (IsSymbol name, Show a) => Show (Constructor name a) where
show (Constructor a) = "(Constructor @" <> show (reflectSymbol (Proxy :: Proxy name)) <> " " <> show a <> ")"

-- | A representation for an argument in a data constructor.
newtype Argument a = Argument a

instance showArgument :: Show a => Show (Argument a) where
show (Argument a) = "(Argument " <> show a <> ")"

-- | The `Generic` class asserts the existence of a type function from types
-- | to their representations using the type constructors defined in this module.
class Generic a rep | a -> rep where
Expand Down
9 changes: 9 additions & 0 deletions test/Data/Generic/Rep.purs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ testGenericRep = do
assert "Checking show" $
show (cons 1 (cons 2 Nil)) == "(Cons { head: 1, tail: (Cons { head: 2, tail: Nil }) })"

assert "Checking show for generic types: Inl, NoArguments" $
show (G.from (Nil :: List Int)) == "(Inl (Constructor @\"Nil\" NoArguments))"

assert "Checking show for generic types: Inr, Constructor, and Single Argument" $
show (G.from $ cons 1 Nil) == "(Inr (Constructor @\"Cons\" (Argument { head: 1, tail: Nil })))"

assert "Checking show for generic types: Product" $
show (G.from $ Pair 1 2) == "(Constructor @\"Pair\" (Product (Argument 1) (Argument 2)))"

assert "Checking equality" $
cons 1 (cons 2 Nil) == cons 1 (cons 2 Nil)

Expand Down