aeson / Data / Aeson / Types / Generic.hs

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
{-# LANGUAGE DefaultSignatures, EmptyDataDecls, FlexibleInstances,
    FunctionalDependencies, KindSignatures, OverlappingInstances,
    ScopedTypeVariables, TypeOperators, UndecidableInstances,
    ViewPatterns, NamedFieldPuns, FlexibleContexts, PatternGuards #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

-- |
-- Module:      Data.Aeson.Types.Generic
-- Copyright:   (c) 2012 Bryan O'Sullivan
--              (c) 2011, 2012 Bas Van Dijk
--              (c) 2011 MailRank, Inc.
-- License:     Apache
-- Maintainer:  Bryan O'Sullivan <bos@serpentine.com>
-- Stability:   experimental
-- Portability: portable
--
-- Types for working with JSON data.

module Data.Aeson.Types.Generic ( ) where

import Control.Applicative ((<*>), (<$>), (<|>), pure)
import Control.Monad.ST (ST)
import Data.Aeson.Types.Class
import Data.Aeson.Types.Internal
import Data.Bits (shiftR)
import Data.DList (DList, toList, empty)
import Data.Maybe (fromMaybe)
import Data.Monoid (mappend)
import Data.Text (Text, pack, unpack)
import GHC.Generics
import qualified Data.HashMap.Strict as H
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM

--------------------------------------------------------------------------------
-- Generic toJSON

instance (GToJSON a) => GToJSON (M1 i c a) where
    gToJSON opts = gToJSON opts . unM1
    {-# INLINE gToJSON #-}

instance (ToJSON a) => GToJSON (K1 i a) where
    gToJSON _opts = toJSON . unK1
    {-# INLINE gToJSON #-}

instance GToJSON U1 where
    gToJSON _opts _ = emptyArray
    {-# INLINE gToJSON #-}

instance (ConsToJSON a) => GToJSON (C1 c a) where
    gToJSON opts = consToJSON opts . unM1
    {-# INLINE gToJSON #-}

instance ( GProductToValues a, GProductToValues b
         , ProductSize      a, ProductSize      b) => GToJSON (a :*: b) where
    gToJSON opts p =
        Array $ V.create $ do
          mv <- VM.unsafeNew lenProduct
          gProductToValues opts mv 0 lenProduct p
          return mv
        where
          lenProduct = unTagged2 (productSize :: Tagged2 (a :*: b) Int)
    {-# INLINE gToJSON #-}

instance (AllNullary (a :+: b) c, GSumToJSON' c (a :+: b)) => GToJSON (a :+: b) where
    gToJSON = unTagged (gSumToJSON' :: Tagged c (Options -> (a :+: b) d -> Value))
    {-# INLINE gToJSON #-}

class GSumToJSON' b f where
    gSumToJSON' :: Tagged b (Options -> f a -> Value)

instance ( GSumToString           f
         , GExtractSum            f
         , GObjectWithType        f
         , GObjectWithSingleField f
         ) => GSumToJSON' True f where
    gSumToJSON' = Tagged $ \opts x ->
                    if nullaryToString opts
                    then gSumToString  opts x
                    else sumToJSON     opts x
    {-# INLINE gSumToJSON' #-}

instance (GExtractSum f, GObjectWithType f, GObjectWithSingleField f) => GSumToJSON' False f where
    gSumToJSON' = Tagged sumToJSON
    {-# INLINE gSumToJSON' #-}

sumToJSON :: (GExtractSum f, GObjectWithType f, GObjectWithSingleField f) => Options -> f a -> Value
sumToJSON opts x =
    case sumEncoding opts of
      TwoElemArray ->
          Array $ V.create $ do
            let (typ, val) = gExtractSum x opts
            mv <- VM.unsafeNew 2
            VM.unsafeWrite mv 0 typ
            VM.unsafeWrite mv 1 val
            return mv
      ObjectWithType{typeFieldName,valueFieldName} ->
          object $ gObjectWithType opts typeFieldName valueFieldName x
      ObjectWithSingleField ->
          Object $ gObjectWithSingleField opts x
{-# INLINE sumToJSON #-}

--------------------------------------------------------------------------------

class GObjectWithType f where
    gObjectWithType :: Options -> String -> String -> f a -> [Pair]

instance (GObjectWithType a, GObjectWithType b) => GObjectWithType (a :+: b) where
    gObjectWithType     opts typeFieldName valueFieldName (L1 x) =
        gObjectWithType opts typeFieldName valueFieldName     x
    gObjectWithType     opts typeFieldName valueFieldName (R1 x) =
        gObjectWithType opts typeFieldName valueFieldName     x
    {-# INLINE gObjectWithType #-}

instance (IsRecord a b, Constructor c, GObjectWithType' b a) =>
    GObjectWithType (C1 c a) where
    gObjectWithType opts typeFieldName valueFieldName x =
        (pack typeFieldName .= constructorNameModifier opts
                                 (conName (undefined :: t c a p))) :
        unTagged (gObjectWithType' opts valueFieldName (unM1 x) :: Tagged b [Pair])
    {-# INLINE gObjectWithType #-}

class GObjectWithType' b f where
    gObjectWithType' :: Options -> String -> f a -> Tagged b [Pair]

instance (GRecordToPairs f) => GObjectWithType' True f where
    gObjectWithType' opts _ x = Tagged $ toList $ gRecordToPairs opts x
    {-# INLINE gObjectWithType' #-}

instance (GToJSON f) => GObjectWithType' False f where
    gObjectWithType' opts valueFieldName x =
        Tagged [pack valueFieldName .= gToJSON opts x]
    {-# INLINE gObjectWithType' #-}

--------------------------------------------------------------------------------

class GSumToString f where
    gSumToString :: Options -> f a -> Value

instance (GSumToString a, GSumToString b) => GSumToString (a :+: b) where
    gSumToString opts (L1 x) = gSumToString opts x
    gSumToString opts (R1 x) = gSumToString opts x
    {-# INLINE gSumToString #-}

instance (Constructor c, GToJSON a, ConsToJSON a) => GSumToString (C1 c a) where
    gSumToString opts _ = String $ pack $ constructorNameModifier opts
                                        $ conName (undefined :: t c a p)
    {-# INLINE gSumToString #-}

--------------------------------------------------------------------------------

class GExtractSum f where
    gExtractSum :: f a -> Options -> (Value, Value)

instance (GExtractSum a, GExtractSum b) => GExtractSum (a :+: b) where
    gExtractSum (L1 x) = gExtractSum x
    gExtractSum (R1 x) = gExtractSum x
    {-# INLINE gExtractSum #-}

instance (Constructor c, GToJSON a, ConsToJSON a) => GExtractSum (C1 c a) where
    gExtractSum x opts = ( String $ pack $ constructorNameModifier opts
                                         $ conName (undefined :: t c a p)
                         , gToJSON opts x
                         )
    {-# INLINE gExtractSum #-}

--------------------------------------------------------------------------------

class ConsToJSON    f where consToJSON  ::           Options -> f a -> Value
class ConsToJSON' b f where consToJSON' :: Tagged b (Options -> f a -> Value)

newtype Tagged s b = Tagged {unTagged :: b}

instance (IsRecord f b, ConsToJSON' b f) => ConsToJSON f where
    consToJSON = unTagged (consToJSON' :: Tagged b (Options -> f a -> Value))
    {-# INLINE consToJSON #-}

instance (GRecordToPairs f) => ConsToJSON' True f where
    consToJSON' = Tagged (\opts -> object . toList . gRecordToPairs opts)
    {-# INLINE consToJSON' #-}

instance GToJSON f => ConsToJSON' False f where
    consToJSON' = Tagged gToJSON
    {-# INLINE consToJSON' #-}

--------------------------------------------------------------------------------

class GRecordToPairs f where
    gRecordToPairs :: Options -> f a -> DList Pair

instance (GRecordToPairs a, GRecordToPairs b) => GRecordToPairs (a :*: b) where
    gRecordToPairs opts (a :*: b) = gRecordToPairs opts a `mappend`
                                    gRecordToPairs opts b
    {-# INLINE gRecordToPairs #-}

instance (Selector s, GToJSON a) => GRecordToPairs (S1 s a) where
    gRecordToPairs opts m1 = pure ( pack $ fieldNameModifier opts $ selName m1
                                  , gToJSON opts (unM1 m1)
                                  )
    {-# INLINE gRecordToPairs #-}

instance (Selector s, ToJSON a) => GRecordToPairs (S1 s (K1 i (Maybe a))) where
    gRecordToPairs opts (M1 k1) | omitNothingFields opts
                                , K1 Nothing <- k1 = empty
    gRecordToPairs opts m1 = pure ( pack $ fieldNameModifier opts $ selName m1
                                  , gToJSON opts (unM1 m1)
                                  )
    {-# INLINE gRecordToPairs #-}

--------------------------------------------------------------------------------

class GProductToValues f where
    gProductToValues :: Options
                     -> VM.MVector s Value
                     -> Int -- ^ index
                     -> Int -- ^ length
                     -> f a
                     -> ST s ()

instance (GProductToValues a, GProductToValues b) => GProductToValues (a :*: b) where
    gProductToValues opts mv ix len (a :*: b) = do
      gProductToValues opts mv ix  lenL a
      gProductToValues opts mv ixR lenR b
        where
          lenL = len `shiftR` 1
          ixR  = ix + lenL
          lenR = len - lenL
    {-# INLINE gProductToValues #-}

instance (GToJSON a) => GProductToValues a where
    gProductToValues opts mv ix _ = VM.unsafeWrite mv ix . gToJSON opts
    {-# INLINE gProductToValues #-}

--------------------------------------------------------------------------------

class GObjectWithSingleField f where
    gObjectWithSingleField :: Options -> f a -> Object

instance (GObjectWithSingleField a, GObjectWithSingleField b) =>
    GObjectWithSingleField (a :+: b) where
    gObjectWithSingleField opts (L1 x) = gObjectWithSingleField opts x
    gObjectWithSingleField opts (R1 x) = gObjectWithSingleField opts x
    {-# INLINE gObjectWithSingleField #-}

instance (Constructor c, GToJSON a, ConsToJSON a) =>
    GObjectWithSingleField (C1 c a) where
    gObjectWithSingleField opts x =
        H.singleton ( pack $ constructorNameModifier opts
                    $ conName (undefined :: t c a p)
                    ) $ gToJSON opts x
    {-# INLINE gObjectWithSingleField #-}

--------------------------------------------------------------------------------
-- Generic parseJSON

instance (GFromJSON a) => GFromJSON (M1 i c a) where
    gParseJSON opts = fmap M1 . gParseJSON opts
    {-# INLINE gParseJSON #-}

instance (FromJSON a) => GFromJSON (K1 i a) where
    gParseJSON _opts = fmap K1 . parseJSON
    {-# INLINE gParseJSON #-}

instance GFromJSON U1 where
    gParseJSON _opts v
        | isEmptyArray v = pure U1
        | otherwise      = typeMismatch "unit constructor (U1)" v
    {-# INLINE gParseJSON #-}

instance (ConsFromJSON a) => GFromJSON (C1 c a) where
    gParseJSON opts = fmap M1 . consParseJSON opts
    {-# INLINE gParseJSON #-}

instance ( GFromProduct a, GFromProduct b
         , ProductSize a, ProductSize b) => GFromJSON (a :*: b) where
    gParseJSON opts = withArray "product (:*:)" $ \arr ->
      let lenArray = V.length arr
          lenProduct = unTagged2 (productSize :: Tagged2 (a :*: b) Int) in
      if lenArray == lenProduct
      then gParseProduct opts arr 0 lenProduct
      else fail $ "When expecting a product of " ++ show lenProduct ++
                  " values, encountered an Array of " ++ show lenArray ++
                  " elements instead"
    {-# INLINE gParseJSON #-}

instance (AllNullary (a :+: b) c, GParseSum' c (a :+: b)) => GFromJSON (a :+: b) where
    gParseJSON = unTagged (gParseSum' :: Tagged c (Options -> Value -> Parser ((a :+: b) d)))
    {-# INLINE gParseJSON #-}

class GParseSum' b f where
    gParseSum' :: Tagged b (Options -> Value -> Parser (f a))

instance (GSumFromString (a :+: b), GFromPair (a :+: b), GFromObjectWithType (a :+: b)) =>
    GParseSum' True (a :+: b) where
    gParseSum' = Tagged $ \opts v ->
                   if nullaryToString      opts
                   then parseSumFromString opts v
                   else parseSum           opts v
    {-# INLINE gParseSum' #-}

instance (GFromPair (a :+: b), GFromObjectWithType (a :+: b)) => GParseSum' False (a :+: b) where
    gParseSum' = Tagged parseSum
    {-# INLINE gParseSum' #-}

--------------------------------------------------------------------------------

parseSumFromString :: GSumFromString f => Options -> Value -> Parser (f a)
parseSumFromString opts = withText "Text" $ \key ->
                            maybe (notFound $ unpack key) return $
                              gParseSumFromString opts key
{-# INLINE parseSumFromString #-}

class GSumFromString f where
    gParseSumFromString :: Options -> Text -> Maybe (f a)

instance (GSumFromString a, GSumFromString b) => GSumFromString (a :+: b) where
    gParseSumFromString opts key = (L1 <$> gParseSumFromString opts key) <|>
                                   (R1 <$> gParseSumFromString opts key)
    {-# INLINE gParseSumFromString #-}

instance (Constructor c) => GSumFromString (C1 c U1) where
    gParseSumFromString opts key | key == name = Just $ M1 U1
                                 | otherwise   = Nothing
        where
          name = pack $ constructorNameModifier opts $ conName (undefined :: t c U1 p)
    {-# INLINE gParseSumFromString #-}

--------------------------------------------------------------------------------

parseSum :: (GFromPair (a :+: b), GFromObjectWithType (a :+: b)) =>
            Options -> Value -> Parser ((a :+: b) c)
parseSum opts v =
    case sumEncoding opts of
      TwoElemArray -> parseTwoElemArray opts v
      ObjectWithType{typeFieldName,valueFieldName} ->
          parseFromObjectWithType opts typeFieldName valueFieldName v
      ObjectWithSingleField -> parseFromObjectWithSingleField opts v
{-# INLINE parseSum #-}

parseFromObjectWithSingleField :: (GFromPair (a :+: b)) => Options -> Value -> Parser ((a :+: b) c)
parseFromObjectWithSingleField opts = withObject "Object" $ \obj ->
  case H.toList obj of
    [keyVal@(key, _)] -> fromMaybe (notFound $ unpack key) $ gParsePair opts keyVal
    _ -> fail "Object doesn't have a single field"
{-# INLINE parseFromObjectWithSingleField #-}

notFound :: String -> Parser a
notFound key = fail $ "The key \"" ++ key ++ "\" was not found"
{-# INLINE notFound #-}

--------------------------------------------------------------------------------

parseTwoElemArray :: GFromPair f => Options -> Value -> Parser (f a)
parseTwoElemArray opts = withArray "Array" $ \arr ->
  if V.length arr == 2
  then case V.unsafeIndex arr 0 of
         String key -> fromMaybe (notFound $ unpack key) $
                         gParsePair opts (key, V.unsafeIndex arr 1)
         _ -> fail "First element is not a String"
  else fail "Array doesn't have 2 elements"
{-# INLINE parseTwoElemArray #-}

--------------------------------------------------------------------------------

parseFromObjectWithType :: (GFromObjectWithType f)
                        => Options -> String -> String -> Value -> Parser (f a)
parseFromObjectWithType opts typeFieldName valueFieldName = withObject "Object" $ \obj -> do
  key <- obj .: pack typeFieldName
  fromMaybe (notFound $ unpack key) $
    gParseFromObjectWithType opts valueFieldName obj key
{-# INLINE parseFromObjectWithType #-}

class GFromObjectWithType f where
    gParseFromObjectWithType :: Options -> String -> Object -> Text -> Maybe (Parser (f a))

instance (GFromObjectWithType a, GFromObjectWithType b) =>
    GFromObjectWithType (a :+: b) where
        gParseFromObjectWithType opts valueFieldName obj key =
            (fmap L1 <$> gParseFromObjectWithType opts valueFieldName obj key) <|>
            (fmap R1 <$> gParseFromObjectWithType opts valueFieldName obj key)
        {-# INLINE gParseFromObjectWithType #-}

instance (GFromObjectWithType' f, Constructor c) => GFromObjectWithType (C1 c f) where
    gParseFromObjectWithType opts valueFieldName obj key
        | key == name = Just $ M1 <$> gParseFromObjectWithType' opts valueFieldName obj
        | otherwise = Nothing
        where
          name = pack $ constructorNameModifier opts $ conName (undefined :: t c f p)
    {-# INLINE gParseFromObjectWithType #-}

class GFromObjectWithType' f where
    gParseFromObjectWithType' :: Options -> String -> Object -> Parser (f a)

instance (IsRecord f b, GFromObjectWithType'' b f) => GFromObjectWithType' f where
    gParseFromObjectWithType' =
        unTagged (gParseFromObjectWithType'' ::
                      Tagged b (Options -> String -> Object -> Parser (f a)))
    {-# INLINE gParseFromObjectWithType' #-}

class GFromObjectWithType'' b f where
    gParseFromObjectWithType'' :: Tagged b (Options -> String -> Object -> Parser (f a))

instance (GFromRecord f) => GFromObjectWithType'' True f where
    gParseFromObjectWithType'' = Tagged $ \opts _ obj -> gParseRecord opts obj
    {-# INLINE gParseFromObjectWithType'' #-}

instance (GFromJSON f) => GFromObjectWithType'' False f where
    gParseFromObjectWithType'' = Tagged $ \opts valueFieldName obj ->
      gParseJSON opts =<< (obj .: pack valueFieldName)
    {-# INLINE gParseFromObjectWithType'' #-}

--------------------------------------------------------------------------------

class ConsFromJSON    f where
    consParseJSON  ::           Options -> Value -> Parser (f a)
class ConsFromJSON' b f where
    consParseJSON' :: Tagged b (Options -> Value -> Parser (f a))

instance (IsRecord f b, ConsFromJSON' b f) => ConsFromJSON f where
    consParseJSON =
        unTagged (consParseJSON' :: Tagged b (Options -> Value -> Parser (f a)))
    {-# INLINE consParseJSON #-}

instance (GFromRecord f) => ConsFromJSON' True f where
    consParseJSON' = Tagged $ withObject "record (:*:)" . gParseRecord
    {-# INLINE consParseJSON' #-}

instance (GFromJSON f) => ConsFromJSON' False f where
    consParseJSON' = Tagged gParseJSON
    {-# INLINE consParseJSON' #-}

--------------------------------------------------------------------------------

class GFromRecord f where
    gParseRecord :: Options -> Object -> Parser (f a)

instance (GFromRecord a, GFromRecord b) => GFromRecord (a :*: b) where
    gParseRecord opts obj = (:*:) <$> gParseRecord opts obj
                                  <*> gParseRecord opts obj
    {-# INLINE gParseRecord #-}

instance (Selector s, GFromJSON a) => GFromRecord (S1 s a) where
    gParseRecord opts = maybe (notFound key) (gParseJSON opts)
                      . H.lookup (pack key)
        where
          key = fieldNameModifier opts $ selName (undefined :: t s a p)
    {-# INLINE gParseRecord #-}

instance (Selector s, FromJSON a) => GFromRecord (S1 s (K1 i (Maybe a))) where
    gParseRecord opts obj = (M1 . K1) <$> obj .:? pack key
        where
          key = fieldNameModifier opts $
                  selName (undefined :: t s (K1 i (Maybe a)) p)
    {-# INLINE gParseRecord #-}

--------------------------------------------------------------------------------

class ProductSize f where
    productSize :: Tagged2 f Int

newtype Tagged2 (s :: * -> *) b = Tagged2 {unTagged2 :: b}

instance (ProductSize a, ProductSize b) => ProductSize (a :*: b) where
    productSize = Tagged2 $ unTagged2 (productSize :: Tagged2 a Int) +
                            unTagged2 (productSize :: Tagged2 b Int)
    {-# INLINE productSize #-}

instance ProductSize (S1 s a) where
    productSize = Tagged2 1
    {-# INLINE productSize #-}

--------------------------------------------------------------------------------

class GFromProduct f where
    gParseProduct :: Options -> Array -> Int -> Int -> Parser (f a)

instance (GFromProduct a, GFromProduct b) => GFromProduct (a :*: b) where
    gParseProduct opts arr ix len =
        (:*:) <$> gParseProduct opts arr ix  lenL
              <*> gParseProduct opts arr ixR lenR
        where
          lenL = len `shiftR` 1
          ixR  = ix + lenL
          lenR = len - lenL
    {-# INLINE gParseProduct #-}

instance (GFromJSON a) => GFromProduct (S1 s a) where
    gParseProduct opts arr ix _ = gParseJSON opts $ V.unsafeIndex arr ix
    {-# INLINE gParseProduct #-}

--------------------------------------------------------------------------------

class GFromPair f where
    gParsePair :: Options -> Pair -> Maybe (Parser (f a))

instance (GFromPair a, GFromPair b) => GFromPair (a :+: b) where
    gParsePair opts keyVal = (fmap L1 <$> gParsePair opts keyVal) <|>
                             (fmap R1 <$> gParsePair opts keyVal)
    {-# INLINE gParsePair #-}

instance (Constructor c, GFromJSON a, ConsFromJSON a) => GFromPair (C1 c a) where
    gParsePair opts (key, value)
        | key == name = Just $ gParseJSON opts value
        | otherwise   = Nothing
        where
          name = pack $ constructorNameModifier opts $ conName (undefined :: t c a p)
    {-# INLINE gParsePair #-}

--------------------------------------------------------------------------------

class IsRecord (f :: * -> *) b | f -> b

data True
data False

instance (IsRecord f b) => IsRecord (f :*: g) b
instance IsRecord (M1 S NoSelector f) False
instance (IsRecord f b) => IsRecord (M1 S c f) b
instance IsRecord (K1 i c) True
instance IsRecord U1 False

--------------------------------------------------------------------------------

class AllNullary (f :: * -> *) b | f -> b

instance (AllNullary a b1, AllNullary c b2, And b1 b2 b3) =>
    AllNullary (a :+: c) b3
instance AllNullary a b => AllNullary (M1 i c a) b
instance AllNullary (a :*: c) False
instance AllNullary (K1 i c) False
instance AllNullary U1 True

class And b1 b2 b3 | b1 b2 -> b3

instance And True True    True

instance And False False  False
instance And False True   False
instance And True  False  False

--------------------------------------------------------------------------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.