    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE ScopedTypeVariables #-}

    module Test.Validity.Types
      ( CanFail (..),
      )
    where

    -- | A class of types that are the result of functions that can fail
    class CanFail f where
      hasFailed :: f a -> Bool
      resultIfSucceeded :: f a -> Maybe a

    instance CanFail Maybe where
      hasFailed :: forall a. Maybe a -> Bool
    hasFailed Maybe a
    Nothing = Bool
    True
      hasFailed Maybe a
    _ = Bool
    False
      resultIfSucceeded :: forall a. Maybe a -> Maybe a
    resultIfSucceeded Maybe a
    Nothing = Maybe a
    forall a. Maybe a
    Nothing
      resultIfSucceeded (Just a
    r) = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    r

    instance CanFail (Either e) where
      hasFailed :: forall a. Either e a -> Bool
    hasFailed (Left e
    _) = Bool
    True
      hasFailed Either e a
    _ = Bool
    False
      resultIfSucceeded :: forall a. Either e a -> Maybe a
    resultIfSucceeded (Left e
    _) = Maybe a
    forall a. Maybe a
    Nothing
      resultIfSucceeded (Right a
    r) = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    r