packages feed

morley-1.14.0: src/Util/Instances.hs

-- SPDX-FileCopyrightText: 2020 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-TQ

{-# OPTIONS_GHC -Wno-orphans #-}

-- | Missing instances from libraries.
module Util.Instances () where

import Data.Default (Default(..))
import Data.Vinyl (Dict(..), Rec(..), ReifyConstraint(reifyConstraint))
import Data.Vinyl.Functor as Vinyl (Compose(..), (:.))
import Fmt (Buildable(..))

instance Default Natural where
  def = 0

instance Buildable Natural where
  build = build @Integer . fromIntegral

instance Buildable a => (Buildable (Identity a)) where
  build (Identity x) = build x

-- I've added this orphan instance to @vinyl@,
-- so we'll be able to delete it in the future.
-- https://github.com/VinylRecords/Vinyl/pull/149
instance ReifyConstraint NFData f xs => NFData (Rec f xs) where
  rnf = go . reifyConstraint @NFData
    where
      go :: forall elems. Rec (Dict NFData :. f) elems -> ()
      go RNil = ()
      go (Vinyl.Compose (Dict x) :& xs) = rnf x `seq` go xs