Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This uses <code>GHC.Generics</code> to provide a <code>showsPrecDefault</code>, which can be easily used to define a <code>Show</code> instance.</p> <pre><code>data Person a = Person { name :: a, age :: Int } deriving Generic instance Show a =&gt; Show (Person a) where showsPrec = showsPrecDefault &gt;&gt;&gt; Person "Alihuseyn" 20 Person "Alihuseyn" 20 </code></pre> <p>The definition of <code>showsPrecDefault</code> is below.</p> <pre><code>{-# LANGUAGE DeriveGeneric , FlexibleContexts , FlexibleInstances , KindSignatures , TypeOperators , TypeSynonymInstances #-} import GHC.Generics class GShow f where gshowsPrec :: Int -&gt; f a -&gt; ShowS instance GShow U1 where gshowsPrec _ U1 = id instance Show c =&gt; GShow (Rec0 c) where gshowsPrec p = showsPrec p . unK1 instance GShow f =&gt; GShow (D1 d f) where gshowsPrec p = gshowsPrec p . unM1 instance Constructor c =&gt; GShow (C1 c U1) where gshowsPrec _ c@(M1 U1) = showParen (isInfix c) (showString (conName c)) instance (Constructor c, GShow (M1 i c f)) =&gt; GShow (C1 c (M1 i c f)) where gshowsPrec = gshowsPrec' instance (Constructor c, GShow (f :+: g)) =&gt; GShow (C1 c (f :+: g)) where gshowsPrec = gshowsPrec' instance (Constructor c, GShow (f :*: g)) =&gt; GShow (C1 c (f :*: g)) where gshowsPrec = gshowsPrec' gshowsPrec' :: (Constructor c, GShow f) =&gt; Int -&gt; C1 c f p -&gt; ShowS gshowsPrec' p c@(M1 f) = showParen (p &gt; 10) $ showParen (isInfix c) (showString (conName c)) . showChar ' ' . gshowsPrec 11 f isInfix :: Constructor c =&gt; t c (f :: * -&gt; *) a -&gt; Bool isInfix c = case conFixity c of Infix _ _ -&gt; True _ -&gt; False instance GShow f =&gt; GShow (S1 s f) where gshowsPrec p = gshowsPrec p . unM1 instance (GShow a, GShow b) =&gt; GShow (a :+: b) where gshowsPrec p (L1 a) = gshowsPrec p a gshowsPrec p (R1 b) = gshowsPrec p b instance (GShow a, GShow b) =&gt; GShow (a :*: b) where gshowsPrec p (a :*: b) = gshowsPrec (p + 1) a . showChar ' ' . gshowsPrec (p + 1) b showsPrecDefault :: (Generic a, GShow (Rep a)) =&gt; Int -&gt; a -&gt; ShowS showsPrecDefault p = gshowsPrec p . from </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload