Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Add Show instances for the Rep types #11

Closed

Conversation

daniel-chambers
Copy link

I've been trying to learn and understand this library, and I found it useful to be able to show the generic rep types that are returned by from, to help visualise and understand the generic structure of a particular value.

For example, using the types in Test.Main in psci:

> from (Cons { head: 1, tail: Nil })
(Inr (Constructor "Cons" (Rec (Product (Field "head" 1) (Field "tail" Nil)))))

> from B
(Inr (Inl (Constructor "B" NoArguments)))

I had to add some missing dependencies and an install script to the package.config so I could build and run this project. I also added psci-support as a dev dependency to the bower.config, for being able to mess around with things in the interactive console. If you aren't cool with these changes, I've done them in separate commits so they can be reverted.

show (Product a b) = "(Product " <> show a <> " " <> show b <> ")"

instance showConstructor :: (IsSymbol name, Show a) => Show (Constructor name a) where
show (Constructor a) = "(Constructor \"" <> reflectSymbol (SProxy :: SProxy name) <> "\" " <> show a <> ")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use show on the string here, instead of wrapping in quotes, since it might need escaping.

show (Rec a) = "(Rec " <> show a <> ")"

instance showField :: (IsSymbol name, Show a) => Show (Field name a) where
show (Field a) = "(Field \"" <> reflectSymbol (SProxy :: SProxy name) <> "\" " <> show a <> ")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here please

@paf31
Copy link
Contributor

paf31 commented Jun 18, 2017

👍 Thanks!

@kritzcreek
Copy link

What do you think about not providing a Show instance, but a monomorphic pretty printer instead. This way it can be DCE'd if it's not used and since it's main use is debugging it wouldn't become part of the interface of the Rep types.

@paf31
Copy link
Contributor

paf31 commented Jun 18, 2017

a monomorphic pretty printer

What type would it have?

@kritzcreek
Copy link

What type would it have?

Oh I see... N monomoprhic pretty printers, but I guess that makes them more tedious to use. It's a little sad... I've been bitten by these random Show instances where I didn't expect them...

@matthewleon
Copy link
Contributor

Would love to see this, or at least something like it, merged. Looks like it needs a rebase.

@JordanMartinez
Copy link
Contributor

I've reimplemented this in purescript/purescript-prelude#250 now that we've ported this repo to prelude.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants