{-# LANGUAGE
FlexibleInstances,
GADTs,
TemplateHaskell,
TypeFamilies,
NoImplicitPrelude
  #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}

module DataFamilies.Instances where

import BasePrelude hiding (Product)

import Json
import DataFamilies.Types
import Test.QuickCheck (Arbitrary(..), elements, oneof)

instance (Arbitrary a) => Arbitrary (Approx a) where
    arbitrary = Approx <$> arbitrary

instance Arbitrary (Nullary Int) where
    arbitrary = elements [C1, C2, C3]

instance Arbitrary a => Arbitrary (SomeType c () a) where
    arbitrary = oneof [ pure Nullary
                      , Unary   <$> arbitrary
                      , Product <$> arbitrary <*> arbitrary <*> arbitrary
                      , Record  <$> arbitrary <*> arbitrary <*> arbitrary
                      ]

instance Arbitrary (GADT String) where
    arbitrary = GADT <$> arbitrary

deriveJson defaultOptions 'C1
deriveJson defaultOptions 'Nullary
deriveJson defaultOptions 'Approx

deriveToJson defaultOptions 'GADT
-- We must write the FromJson instance head ourselves
-- due to the refined GADT return type
instance FromJson (GADT String) where
    parseJson = $(mkParseJson defaultOptions 'GADT)
